| 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 3. |
| 10 | // |
| 11 | // Look'ma, still no semantic actions! And no explicit access to member |
| 12 | // functions any more. |
| 13 | // |
| 14 | // [ HK April 6, 2010 ] spirit2 |
| 15 | // |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | |
| 18 | #include <boost/spirit/include/qi.hpp> |
| 19 | #include <boost/spirit/include/karma.hpp> |
| 20 | #include <boost/phoenix/core.hpp> |
| 21 | #include <boost/phoenix/operator.hpp> |
| 22 | #include <boost/fusion/include/std_pair.hpp> |
| 23 | #include <boost/fusion/include/adapt_adt.hpp> |
| 24 | #include <boost/spirit/include/support_adapt_adt_attributes.hpp> |
| 25 | |
| 26 | #include <iostream> |
| 27 | #include <string> |
| 28 | #include <complex> |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // The following macro adapts the type std::complex<double> as a fusion |
| 32 | // sequence. |
| 33 | //[tutorial_karma_complex_number_adapt_class |
| 34 | // We can leave off the setters as Karma does not need them. |
| 35 | BOOST_FUSION_ADAPT_ADT( |
| 36 | std::complex<double>, |
| 37 | (bool, bool, obj.imag() != 0, /**/) |
| 38 | (double, double, obj.real(), /**/) |
| 39 | (double, double, obj.imag(), /**/) |
| 40 | ) |
| 41 | //] |
| 42 | |
| 43 | namespace client |
| 44 | { |
| 45 | /////////////////////////////////////////////////////////////////////////// |
| 46 | // Our complex number parser/compiler (that's just a copy of the complex |
| 47 | // number example from Qi (see examples/qi/complex_number.cpp) |
| 48 | /////////////////////////////////////////////////////////////////////////// |
| 49 | template <typename Iterator> |
| 50 | bool parse_complex(Iterator first, Iterator last, std::complex<double>& c) |
| 51 | { |
| 52 | using boost::spirit::qi::double_; |
| 53 | using boost::spirit::qi::_1; |
| 54 | using boost::spirit::qi::phrase_parse; |
| 55 | using boost::spirit::ascii::space; |
| 56 | using boost::phoenix::ref; |
| 57 | |
| 58 | double rN = 0.0; |
| 59 | double iN = 0.0; |
| 60 | bool r = phrase_parse(first, last, |
| 61 | ( |
| 62 | '(' >> double_[ref(rN) = _1] |
| 63 | >> -(',' >> double_[ref(iN) = _1]) >> ')' |
| 64 | | double_[ref(rN) = _1] |
| 65 | ), |
| 66 | space); |
| 67 | |
| 68 | if (!r || first != last) // fail if we did not get a full match |
| 69 | return false; |
| 70 | c = std::complex<double>(rN, iN); |
| 71 | return r; |
| 72 | } |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////// |
| 75 | // Our complex number generator |
| 76 | /////////////////////////////////////////////////////////////////////////// |
| 77 | //[tutorial_karma_complex_number_adapt |
| 78 | template <typename OutputIterator> |
| 79 | bool generate_complex(OutputIterator sink, std::complex<double> const& c) |
| 80 | { |
| 81 | using boost::spirit::karma::double_; |
| 82 | using boost::spirit::karma::bool_; |
| 83 | using boost::spirit::karma::true_; |
| 84 | using boost::spirit::karma::omit; |
| 85 | using boost::spirit::karma::generate; |
| 86 | |
| 87 | return generate(sink, |
| 88 | |
| 89 | // Begin grammar |
| 90 | ( |
| 91 | &true_ << '(' << double_ << ", " << double_ << ')' |
| 92 | | omit[bool_] << double_ << omit[double_] |
| 93 | ), |
| 94 | // End grammar |
| 95 | |
| 96 | c // Data to output |
| 97 | ); |
| 98 | } |
| 99 | //] |
| 100 | } |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | // Main program |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | int main() |
| 106 | { |
| 107 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 108 | std::cout << "\t\tA complex number micro generator for Spirit...\n\n" ; |
| 109 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 110 | |
| 111 | std::cout << "Give me a complex number of the form r or (r) or (r,i) \n" ; |
| 112 | std::cout << "Type [q or Q] to quit\n\n" ; |
| 113 | |
| 114 | std::string str; |
| 115 | while (getline(is&: std::cin, str&: str)) |
| 116 | { |
| 117 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 118 | break; |
| 119 | |
| 120 | std::complex<double> c; |
| 121 | if (client::parse_complex(first: str.begin(), last: str.end(), c)) |
| 122 | { |
| 123 | std::cout << "-------------------------\n" ; |
| 124 | |
| 125 | std::string generated; |
| 126 | std::back_insert_iterator<std::string> sink(generated); |
| 127 | if (!client::generate_complex(sink, c)) |
| 128 | { |
| 129 | std::cout << "-------------------------\n" ; |
| 130 | std::cout << "Generating failed\n" ; |
| 131 | std::cout << "-------------------------\n" ; |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | std::cout << "-------------------------\n" ; |
| 136 | std::cout << "Generated: " << generated << "\n" ; |
| 137 | std::cout << "-------------------------\n" ; |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | std::cout << "-------------------------\n" ; |
| 143 | std::cout << "Parsing failed\n" ; |
| 144 | std::cout << "-------------------------\n" ; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | std::cout << "Bye... :-) \n\n" ; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |