]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/complex_number.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / complex_number.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2015 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 parser.
10 //
11 // [ JDG May 10, 2002 ] spirit1
12 // [ JDG May 9, 2007 ] spirit2
13 // [ JDG May 12, 2015 ] spirit X3
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <boost/config/warning_disable.hpp>
18 #include <boost/spirit/home/x3.hpp>
19
20 #include <iostream>
21 #include <string>
22 #include <complex>
23
24 ///////////////////////////////////////////////////////////////////////////////
25 // Our complex number parser/compiler
26 ///////////////////////////////////////////////////////////////////////////////
27 namespace client
28 {
29 template <typename Iterator>
30 bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
31 {
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;
36
37 double rN = 0.0;
38 double iN = 0.0;
39 auto fr = [&](auto& ctx){ rN = _attr(ctx); };
40 auto fi = [&](auto& ctx){ iN = _attr(ctx); };
41
42 bool r = phrase_parse(first, last,
43
44 // Begin grammar
45 (
46 '(' >> double_[fr]
47 >> -(',' >> double_[fi]) >> ')'
48 | double_[fr]
49 ),
50 // End grammar
51
52 space);
53
54 if (!r || first != last) // fail if we did not get a full match
55 return false;
56 c = std::complex<double>(rN, iN);
57 return r;
58 }
59 }
60
61 ////////////////////////////////////////////////////////////////////////////
62 // Main program
63 ////////////////////////////////////////////////////////////////////////////
64 int
65 main()
66 {
67 std::cout << "/////////////////////////////////////////////////////////\n\n";
68 std::cout << "\t\tA complex number micro parser for Spirit...\n\n";
69 std::cout << "/////////////////////////////////////////////////////////\n\n";
70
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";
73
74 std::string str;
75 while (getline(std::cin, str))
76 {
77 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
78 break;
79
80 std::complex<double> c;
81 if (client::parse_complex(str.begin(), str.end(), c))
82 {
83 std::cout << "-------------------------\n";
84 std::cout << "Parsing succeeded\n";
85 std::cout << "got: " << c << std::endl;
86 std::cout << "\n-------------------------\n";
87 }
88 else
89 {
90 std::cout << "-------------------------\n";
91 std::cout << "Parsing failed\n";
92 std::cout << "-------------------------\n";
93 }
94 }
95
96 std::cout << "Bye... :-) \n\n";
97 return 0;
98 }