1 // Copyright (c) 2002-2010 Joel de Guzman
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/config/warning_disable.hpp>
7 #include <boost/spirit/include/qi.hpp>
8 #include <boost/spirit/include/phoenix_core.hpp>
9 #include <boost/spirit/include/phoenix_operator.hpp>
10 #include <boost/spirit/include/phoenix_object.hpp>
11 #include <boost/spirit/include/phoenix_stl.hpp>
12 #include <boost/fusion/include/adapt_struct.hpp>
13 #include <boost/fusion/include/io.hpp>
19 using boost::spirit::qi::grammar
;
20 using boost::spirit::qi::rule
;
21 using boost::spirit::qi::char_
;
22 using boost::spirit::qi::parse
;
23 using boost::spirit::qi::_val
;
24 using boost::spirit::qi::_1
;
25 using boost::phoenix::push_back
;
27 #define ATTR_PROPAGATE
33 std::cout
<< "default construct" << std::endl
;
38 std::cout
<< "construct from char" << std::endl
;
41 test_attr(test_attr
const&)
43 std::cout
<< "copy construct" << std::endl
;
46 test_attr
& operator=(test_attr
const&)
48 std::cout
<< "assign" << std::endl
;
53 template <typename Iterator
>
54 struct test_parser
: grammar
<Iterator
, std::vector
<test_attr
>() >
56 test_parser() : test_parser::base_type(start
)
59 start
= char_
>> *(',' >> char_
);
61 start
= char_
[push_back(_val
, _1
)] >> *(',' >> char_
[push_back(_val
, _1
)]);
65 rule
<Iterator
, std::vector
<test_attr
>()> start
;
70 typedef std::string::const_iterator iterator_type
;
71 typedef test_parser
<iterator_type
> test_parser
;
74 std::string str
= "a,b,c,d,e";
76 std::vector
<test_attr
> result
;
78 std::string::const_iterator iter
= str
.begin();
79 std::string::const_iterator end
= str
.end();
80 bool r
= parse(iter
, end
, g
, result
);
84 std::cout
<< "-------------------------\n";
85 std::cout
<< "Parsing succeeded\n";
86 std::cout
<< "\n-------------------------\n";
90 std::cout
<< "-------------------------\n";
91 std::cout
<< "Parsing failed\n";
92 std::cout
<< "-------------------------\n";
95 std::cout
<< "Bye... :-) \n\n";