]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/example/qi/employee.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / employee.cpp
CommitLineData
7c673cae
FG
1/*=============================================================================
2 Copyright (c) 2002-2010 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//
14///////////////////////////////////////////////////////////////////////////////
15
7c673cae 16#include <boost/spirit/include/qi.hpp>
1e59de90
TL
17#include <boost/phoenix/core.hpp>
18#include <boost/phoenix/operator.hpp>
19#include <boost/phoenix/object.hpp>
7c673cae
FG
20#include <boost/fusion/include/adapt_struct.hpp>
21#include <boost/fusion/include/io.hpp>
22
23#include <iostream>
24#include <string>
25#include <complex>
26
27namespace client
28{
29 namespace qi = boost::spirit::qi;
30 namespace ascii = boost::spirit::ascii;
31
32 ///////////////////////////////////////////////////////////////////////////
33 // Our employee struct
34 ///////////////////////////////////////////////////////////////////////////
35 //[tutorial_employee_struct
36 struct employee
37 {
38 int age;
39 std::string surname;
40 std::string forename;
41 double salary;
42 };
43 //]
44}
45
46// We need to tell fusion about our employee struct
47// to make it a first-class fusion citizen. This has to
48// be in global scope.
49
50//[tutorial_employee_adapt_struct
51BOOST_FUSION_ADAPT_STRUCT(
52 client::employee,
53 (int, age)
54 (std::string, surname)
55 (std::string, forename)
56 (double, salary)
57)
58//]
59
60namespace client
61{
62 ///////////////////////////////////////////////////////////////////////////////
63 // Our employee parser
64 ///////////////////////////////////////////////////////////////////////////////
65 //[tutorial_employee_parser
66 template <typename Iterator>
67 struct employee_parser : qi::grammar<Iterator, employee(), ascii::space_type>
68 {
69 employee_parser() : employee_parser::base_type(start)
70 {
71 using qi::int_;
72 using qi::lit;
73 using qi::double_;
74 using qi::lexeme;
75 using ascii::char_;
76
77 quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
78
79 start %=
80 lit("employee")
81 >> '{'
82 >> int_ >> ','
83 >> quoted_string >> ','
84 >> quoted_string >> ','
85 >> double_
86 >> '}'
87 ;
88 }
89
90 qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
91 qi::rule<Iterator, employee(), ascii::space_type> start;
92 };
93 //]
94}
95
96////////////////////////////////////////////////////////////////////////////
97// Main program
98////////////////////////////////////////////////////////////////////////////
99int
100main()
101{
102 std::cout << "/////////////////////////////////////////////////////////\n\n";
103 std::cout << "\t\tAn employee parser for Spirit...\n\n";
104 std::cout << "/////////////////////////////////////////////////////////\n\n";
105
106 std::cout
107 << "Give me an employee of the form :"
108 << "employee{age, \"surname\", \"forename\", salary } \n";
109 std::cout << "Type [q or Q] to quit\n\n";
110
111 using boost::spirit::ascii::space;
112 typedef std::string::const_iterator iterator_type;
113 typedef client::employee_parser<iterator_type> employee_parser;
114
115 employee_parser g; // Our grammar
116 std::string str;
117 while (getline(std::cin, str))
118 {
119 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
120 break;
121
122 client::employee emp;
123 std::string::const_iterator iter = str.begin();
124 std::string::const_iterator end = str.end();
125 bool r = phrase_parse(iter, end, g, space, emp);
126
127 if (r && iter == end)
128 {
129 std::cout << boost::fusion::tuple_open('[');
130 std::cout << boost::fusion::tuple_close(']');
131 std::cout << boost::fusion::tuple_delimiter(", ");
132
133 std::cout << "-------------------------\n";
134 std::cout << "Parsing succeeded\n";
135 std::cout << "got: " << boost::fusion::as_vector(emp) << std::endl;
136 std::cout << "\n-------------------------\n";
137 }
138 else
139 {
140 std::cout << "-------------------------\n";
141 std::cout << "Parsing failed\n";
142 std::cout << "-------------------------\n";
143 }
144 }
145
146 std::cout << "Bye... :-) \n\n";
147 return 0;
148}
149
150