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 parser for arbitrary tuples. This example presents a parser
10 // for an employee structure.
12 // [ JDG May 9, 2007 ]
13 // [ JDG May 13, 2015 ] spirit X3
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <boost/config/warning_disable.hpp>
18 #include <boost/spirit/home/x3.hpp>
19 #include <boost/fusion/include/adapt_struct.hpp>
20 #include <boost/fusion/include/io.hpp>
25 namespace client
{ namespace ast
27 ///////////////////////////////////////////////////////////////////////////
28 // Our employee struct
29 ///////////////////////////////////////////////////////////////////////////
38 using boost::fusion::operator<<;
41 // We need to tell fusion about our employee struct
42 // to make it a first-class fusion citizen. This has to
43 // be in global scope.
45 BOOST_FUSION_ADAPT_STRUCT(client::ast::employee
,
46 age
, forename
, surname
, salary
51 ///////////////////////////////////////////////////////////////////////////////
52 // Our employee parser
53 ///////////////////////////////////////////////////////////////////////////////
56 namespace x3
= boost::spirit::x3
;
57 namespace ascii
= boost::spirit::x3::ascii
;
65 x3::rule
<class employee
, ast::employee
> const employee
= "employee";
67 auto const quoted_string
= lexeme
['"' >> +(char_
- '"') >> '"'];
69 auto const employee_def
=
73 >> quoted_string
>> ','
74 >> quoted_string
>> ','
79 BOOST_SPIRIT_DEFINE(employee
);
83 ////////////////////////////////////////////////////////////////////////////
85 ////////////////////////////////////////////////////////////////////////////
89 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
90 std::cout
<< "\t\tAn employee parser for Spirit...\n\n";
91 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
94 << "Give me an employee of the form :"
95 << "employee{age, \"forename\", \"surname\", salary } \n";
96 std::cout
<< "Type [q or Q] to quit\n\n";
98 using boost::spirit::x3::ascii::space
;
99 typedef std::string::const_iterator iterator_type
;
100 using client::parser::employee
;
103 while (getline(std::cin
, str
))
105 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
108 client::ast::employee emp
;
109 iterator_type iter
= str
.begin();
110 iterator_type
const end
= str
.end();
111 bool r
= phrase_parse(iter
, end
, employee
, space
, emp
);
113 if (r
&& iter
== end
)
115 std::cout
<< boost::fusion::tuple_open('[');
116 std::cout
<< boost::fusion::tuple_close(']');
117 std::cout
<< boost::fusion::tuple_delimiter(", ");
119 std::cout
<< "-------------------------\n";
120 std::cout
<< "Parsing succeeded\n";
121 std::cout
<< "got: " << emp
<< std::endl
;
122 std::cout
<< "\n-------------------------\n";
126 std::cout
<< "-------------------------\n";
127 std::cout
<< "Parsing failed\n";
128 std::cout
<< "-------------------------\n";
132 std::cout
<< "Bye... :-) \n\n";