]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/complex_number.cpp
update ceph source to reef 18.1.2
[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/spirit/home/x3.hpp>
18
19 #include <iostream>
20 #include <string>
21 #include <complex>
22
23 ///////////////////////////////////////////////////////////////////////////////
24 // Our complex number parser/compiler
25 ///////////////////////////////////////////////////////////////////////////////
26 namespace client
27 {
28 template <typename Iterator>
29 bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
30 {
31 using boost::spirit::x3::double_;
32 using boost::spirit::x3::_attr;
33 using boost::spirit::x3::phrase_parse;
34 using boost::spirit::x3::ascii::space;
35
36 double rN = 0.0;
37 double iN = 0.0;
38 auto fr = [&](auto& ctx){ rN = _attr(ctx); };
39 auto fi = [&](auto& ctx){ iN = _attr(ctx); };
40
41 bool r = phrase_parse(first, last,
42
43 // Begin grammar
44 (
45 '(' >> double_[fr]
46 >> -(',' >> double_[fi]) >> ')'
47 | double_[fr]
48 ),
49 // End grammar
50
51 space);
52
53 if (!r || first != last) // fail if we did not get a full match
54 return false;
55 c = std::complex<double>(rN, iN);
56 return r;
57 }
58 }
59
60 ////////////////////////////////////////////////////////////////////////////
61 // Main program
62 ////////////////////////////////////////////////////////////////////////////
63 int
64 main()
65 {
66 std::cout << "/////////////////////////////////////////////////////////\n\n";
67 std::cout << "\t\tA complex number micro parser for Spirit...\n\n";
68 std::cout << "/////////////////////////////////////////////////////////\n\n";
69
70 std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
71 std::cout << "Type [q or Q] to quit\n\n";
72
73 std::string str;
74 while (getline(std::cin, str))
75 {
76 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
77 break;
78
79 std::complex<double> c;
80 if (client::parse_complex(str.begin(), str.end(), c))
81 {
82 std::cout << "-------------------------\n";
83 std::cout << "Parsing succeeded\n";
84 std::cout << "got: " << c << std::endl;
85 std::cout << "\n-------------------------\n";
86 }
87 else
88 {
89 std::cout << "-------------------------\n";
90 std::cout << "Parsing failed\n";
91 std::cout << "-------------------------\n";
92 }
93 }
94
95 std::cout << "Bye... :-) \n\n";
96 return 0;
97 }