| 1 | // Copyright (c) 2001-2010 Hartmut Kaiser |
| 2 | // Copyright (c) 2001-2010 Joel de Guzman |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | /////////////////////////////////////////////////////////////////////////////// |
| 8 | // |
| 9 | // A complex number micro generator - take 2. Look'ma no semantic actions! |
| 10 | // |
| 11 | // [ HK July 26, 2009 ] spirit2 |
| 12 | // |
| 13 | /////////////////////////////////////////////////////////////////////////////// |
| 14 | |
| 15 | #include <boost/spirit/include/qi.hpp> |
| 16 | #include <boost/spirit/include/karma.hpp> |
| 17 | #include <boost/phoenix/core.hpp> |
| 18 | #include <boost/phoenix/operator.hpp> |
| 19 | #include <boost/fusion/include/std_pair.hpp> |
| 20 | |
| 21 | #include <iostream> |
| 22 | #include <string> |
| 23 | #include <complex> |
| 24 | |
| 25 | namespace client |
| 26 | { |
| 27 | /////////////////////////////////////////////////////////////////////////// |
| 28 | // Our complex number parser/compiler (that's just a copy of the complex |
| 29 | // number example from Qi (see examples/qi/complex_number.cpp) |
| 30 | /////////////////////////////////////////////////////////////////////////// |
| 31 | template <typename Iterator> |
| 32 | bool parse_complex(Iterator first, Iterator last, std::complex<double>& c) |
| 33 | { |
| 34 | using boost::spirit::qi::double_; |
| 35 | using boost::spirit::qi::_1; |
| 36 | using boost::spirit::qi::phrase_parse; |
| 37 | using boost::spirit::ascii::space; |
| 38 | using boost::phoenix::ref; |
| 39 | |
| 40 | double rN = 0.0; |
| 41 | double iN = 0.0; |
| 42 | bool r = phrase_parse(first, last, |
| 43 | ( |
| 44 | '(' >> double_[ref(rN) = _1] |
| 45 | >> -(',' >> double_[ref(iN) = _1]) >> ')' |
| 46 | | double_[ref(rN) = _1] |
| 47 | ), |
| 48 | space); |
| 49 | |
| 50 | if (!r || first != last) // fail if we did not get a full match |
| 51 | return false; |
| 52 | c = std::complex<double>(rN, iN); |
| 53 | return r; |
| 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////// |
| 57 | // Our complex number generator |
| 58 | /////////////////////////////////////////////////////////////////////////// |
| 59 | //[tutorial_karma_complex_number_easier |
| 60 | template <typename OutputIterator> |
| 61 | bool generate_complex(OutputIterator sink, std::complex<double> const& c) |
| 62 | { |
| 63 | using boost::spirit::karma::double_; |
| 64 | using boost::spirit::karma::omit; |
| 65 | using boost::spirit::karma::generate; |
| 66 | |
| 67 | return generate(sink, |
| 68 | |
| 69 | // Begin grammar |
| 70 | ( |
| 71 | !double_(0.0) << '(' << double_ << ", " << double_ << ')' |
| 72 | | omit[double_] << double_ << omit[double_] |
| 73 | ), |
| 74 | // End grammar |
| 75 | |
| 76 | c.imag(), c.real(), c.imag() // Data to output |
| 77 | ); |
| 78 | } |
| 79 | //] |
| 80 | } |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | // Main program |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | int main() |
| 86 | { |
| 87 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 88 | std::cout << "\t\tA complex number micro generator for Spirit...\n\n" ; |
| 89 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 90 | |
| 91 | std::cout << "Give me a complex number of the form r or (r) or (r,i) \n" ; |
| 92 | std::cout << "Type [q or Q] to quit\n\n" ; |
| 93 | |
| 94 | std::string str; |
| 95 | while (getline(is&: std::cin, str&: str)) |
| 96 | { |
| 97 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 98 | break; |
| 99 | |
| 100 | std::complex<double> c; |
| 101 | if (client::parse_complex(first: str.begin(), last: str.end(), c)) |
| 102 | { |
| 103 | std::cout << "-------------------------\n" ; |
| 104 | |
| 105 | std::string generated; |
| 106 | std::back_insert_iterator<std::string> sink(generated); |
| 107 | if (!client::generate_complex(sink, c)) |
| 108 | { |
| 109 | std::cout << "-------------------------\n" ; |
| 110 | std::cout << "Generating failed\n" ; |
| 111 | std::cout << "-------------------------\n" ; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | std::cout << "-------------------------\n" ; |
| 116 | std::cout << "Generated: " << generated << "\n" ; |
| 117 | std::cout << "-------------------------\n" ; |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | std::cout << "-------------------------\n" ; |
| 123 | std::cout << "Parsing failed\n" ; |
| 124 | std::cout << "-------------------------\n" ; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | std::cout << "Bye... :-) \n\n" ; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |