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