]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/complex_number.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2010 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
9 // A complex number micro parser.
11 // [ JDG May 10, 2002 ] spirit1
12 // [ JDG May 9, 2007 ] spirit2
14 ///////////////////////////////////////////////////////////////////////////////
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
25 ///////////////////////////////////////////////////////////////////////////////
26 // Our complex number parser/compiler
27 ///////////////////////////////////////////////////////////////////////////////
28 //[tutorial_complex_number
31 template <typename Iterator
>
32 bool parse_complex(Iterator first
, Iterator last
, std::complex<double>& c
)
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
;
42 bool r
= phrase_parse(first
, last
,
46 '(' >> double_
[ref(rN
) = _1
]
47 >> -(',' >> double_
[ref(iN
) = _1
]) >> ')'
48 | double_
[ref(rN
) = _1
]
54 if (!r
|| first
!= last
) // fail if we did not get a full match
56 c
= std::complex<double>(rN
, iN
);
62 ////////////////////////////////////////////////////////////////////////////
64 ////////////////////////////////////////////////////////////////////////////
68 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
69 std::cout
<< "\t\tA complex number micro parser for Spirit...\n\n";
70 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
72 std::cout
<< "Give me a complex number of the form r or (r) or (r,i) \n";
73 std::cout
<< "Type [q or Q] to quit\n\n";
76 while (getline(std::cin
, str
))
78 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
81 std::complex<double> c
;
82 if (client::parse_complex(str
.begin(), str
.end(), c
))
84 std::cout
<< "-------------------------\n";
85 std::cout
<< "Parsing succeeded\n";
86 std::cout
<< "got: " << c
<< std::endl
;
87 std::cout
<< "\n-------------------------\n";
91 std::cout
<< "-------------------------\n";
92 std::cout
<< "Parsing failed\n";
93 std::cout
<< "-------------------------\n";
97 std::cout
<< "Bye... :-) \n\n";