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/spirit/home/x3.hpp>
18 #include <boost/fusion/include/adapt_struct.hpp>
19 #include <boost/fusion/include/io.hpp>
24 namespace client
{ namespace ast
26 ///////////////////////////////////////////////////////////////////////////
27 // Our employee struct
28 ///////////////////////////////////////////////////////////////////////////
37 using boost::fusion::operator<<;
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.
44 BOOST_FUSION_ADAPT_STRUCT(client::ast::employee
,
45 age
, forename
, surname
, salary
50 ///////////////////////////////////////////////////////////////////////////////
51 // Our employee parser
52 ///////////////////////////////////////////////////////////////////////////////
55 namespace x3
= boost::spirit::x3
;
56 namespace ascii
= boost::spirit::x3::ascii
;
64 x3::rule
<class employee
, ast::employee
> const employee
= "employee";
66 auto const quoted_string
= lexeme
['"' >> +(char_
- '"') >> '"'];
68 auto const employee_def
=
72 >> quoted_string
>> ','
73 >> quoted_string
>> ','
78 BOOST_SPIRIT_DEFINE(employee
);
82 ////////////////////////////////////////////////////////////////////////////
84 ////////////////////////////////////////////////////////////////////////////
88 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
89 std::cout
<< "\t\tAn employee parser for Spirit...\n\n";
90 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
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";
97 using boost::spirit::x3::ascii::space
;
98 typedef std::string::const_iterator iterator_type
;
99 using client::parser::employee
;
102 while (getline(std::cin
, str
))
104 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
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
);
112 if (r
&& iter
== end
)
114 std::cout
<< boost::fusion::tuple_open('[');
115 std::cout
<< boost::fusion::tuple_close(']');
116 std::cout
<< boost::fusion::tuple_delimiter(", ");
118 std::cout
<< "-------------------------\n";
119 std::cout
<< "Parsing succeeded\n";
120 std::cout
<< "got: " << emp
<< std::endl
;
121 std::cout
<< "\n-------------------------\n";
125 std::cout
<< "-------------------------\n";
126 std::cout
<< "Parsing failed\n";
127 std::cout
<< "-------------------------\n";
131 std::cout
<< "Bye... :-) \n\n";