]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/employee.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / employee.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 parser for arbitrary tuples. This example presents a parser
10 // for an employee structure.
11 //
12 // [ JDG May 9, 2007 ]
13 // [ JDG May 13, 2015 ] spirit X3
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <boost/spirit/home/x3.hpp>
18 #include <boost/fusion/include/adapt_struct.hpp>
19 #include <boost/fusion/include/io.hpp>
20
21 #include <iostream>
22 #include <string>
23
24 namespace client { namespace ast
25 {
26 ///////////////////////////////////////////////////////////////////////////
27 // Our employee struct
28 ///////////////////////////////////////////////////////////////////////////
29 struct employee
30 {
31 int age;
32 std::string forename;
33 std::string surname;
34 double salary;
35 };
36
37 using boost::fusion::operator<<;
38 }}
39
40 // We need to tell fusion about our employee struct
41 // to make it a first-class fusion citizen. This has to
42 // be in global scope.
43
44 BOOST_FUSION_ADAPT_STRUCT(client::ast::employee,
45 age, forename, surname, salary
46 )
47
48 namespace client
49 {
50 ///////////////////////////////////////////////////////////////////////////////
51 // Our employee parser
52 ///////////////////////////////////////////////////////////////////////////////
53 namespace parser
54 {
55 namespace x3 = boost::spirit::x3;
56 namespace ascii = boost::spirit::x3::ascii;
57
58 using x3::int_;
59 using x3::lit;
60 using x3::double_;
61 using x3::lexeme;
62 using ascii::char_;
63
64 x3::rule<class employee, ast::employee> const employee = "employee";
65
66 auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"'];
67
68 auto const employee_def =
69 lit("employee")
70 >> '{'
71 >> int_ >> ','
72 >> quoted_string >> ','
73 >> quoted_string >> ','
74 >> double_
75 >> '}'
76 ;
77
78 BOOST_SPIRIT_DEFINE(employee);
79 }
80 }
81
82 ////////////////////////////////////////////////////////////////////////////
83 // Main program
84 ////////////////////////////////////////////////////////////////////////////
85 int
86 main()
87 {
88 std::cout << "/////////////////////////////////////////////////////////\n\n";
89 std::cout << "\t\tAn employee parser for Spirit...\n\n";
90 std::cout << "/////////////////////////////////////////////////////////\n\n";
91
92 std::cout
93 << "Give me an employee of the form :"
94 << "employee{age, \"forename\", \"surname\", salary } \n";
95 std::cout << "Type [q or Q] to quit\n\n";
96
97 using boost::spirit::x3::ascii::space;
98 typedef std::string::const_iterator iterator_type;
99 using client::parser::employee;
100
101 std::string str;
102 while (getline(std::cin, str))
103 {
104 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
105 break;
106
107 client::ast::employee emp;
108 iterator_type iter = str.begin();
109 iterator_type const end = str.end();
110 bool r = phrase_parse(iter, end, employee, space, emp);
111
112 if (r && iter == end)
113 {
114 std::cout << boost::fusion::tuple_open('[');
115 std::cout << boost::fusion::tuple_close(']');
116 std::cout << boost::fusion::tuple_delimiter(", ");
117
118 std::cout << "-------------------------\n";
119 std::cout << "Parsing succeeded\n";
120 std::cout << "got: " << emp << std::endl;
121 std::cout << "\n-------------------------\n";
122 }
123 else
124 {
125 std::cout << "-------------------------\n";
126 std::cout << "Parsing failed\n";
127 std::cout << "-------------------------\n";
128 }
129 }
130
131 std::cout << "Bye... :-) \n\n";
132 return 0;
133 }