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.
10 //
11 // [ HK July 7, 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
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::eps;
65 using boost::spirit::karma::double_;
66 using boost::spirit::karma::_1;
67 using boost::spirit::karma::generate;
68
69 return generate(sink,
70 // Begin grammar
71 (
72 eps(c.imag() != 0) <<
73 '(' << double_[_1 = c.real()] << ", " << double_[_1 = c.imag()] << ')'
74 | double_[_1 = c.real()]
75 )
76 // End grammar
77 );
78 }
79 //]
80 }
81
82 ///////////////////////////////////////////////////////////////////////////////
83 // Main program
84 ///////////////////////////////////////////////////////////////////////////////
main()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(std::cin, 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(str.begin(), 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