]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/complex_number.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2015 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
13 // [ JDG May 12, 2015 ] spirit X3
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <boost/config/warning_disable.hpp>
18 #include <boost/spirit/home/x3.hpp>
24 ///////////////////////////////////////////////////////////////////////////////
25 // Our complex number parser/compiler
26 ///////////////////////////////////////////////////////////////////////////////
29 template <typename Iterator
>
30 bool parse_complex(Iterator first
, Iterator last
, std::complex<double>& c
)
32 using boost::spirit::x3::double_
;
33 using boost::spirit::x3::_attr
;
34 using boost::spirit::x3::phrase_parse
;
35 using boost::spirit::x3::ascii::space
;
39 auto fr
= [&](auto& ctx
){ rN
= _attr(ctx
); };
40 auto fi
= [&](auto& ctx
){ iN
= _attr(ctx
); };
42 bool r
= phrase_parse(first
, last
,
47 >> -(',' >> double_
[fi
]) >> ')'
54 if (!r
|| first
!= last
) // fail if we did not get a full match
56 c
= std::complex<double>(rN
, iN
);
61 ////////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////////
67 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
68 std::cout
<< "\t\tA complex number micro parser for Spirit...\n\n";
69 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
71 std::cout
<< "Give me a complex number of the form r or (r) or (r,i) \n";
72 std::cout
<< "Type [q or Q] to quit\n\n";
75 while (getline(std::cin
, str
))
77 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
80 std::complex<double> c
;
81 if (client::parse_complex(str
.begin(), str
.end(), c
))
83 std::cout
<< "-------------------------\n";
84 std::cout
<< "Parsing succeeded\n";
85 std::cout
<< "got: " << c
<< std::endl
;
86 std::cout
<< "\n-------------------------\n";
90 std::cout
<< "-------------------------\n";
91 std::cout
<< "Parsing failed\n";
92 std::cout
<< "-------------------------\n";
96 std::cout
<< "Bye... :-) \n\n";