• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //   Copyright (c) 2002-2010 Joel de Guzman
2 //
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)
5 
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>
14 
15 #include <iostream>
16 #include <string>
17 #include <complex>
18 
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;
26 
27 #define ATTR_PROPAGATE
28 
29 struct test_attr
30 {
test_attrtest_attr31     test_attr()
32     {
33         std::cout << "default construct" << std::endl;
34     }
35 
test_attrtest_attr36     test_attr(char)
37     {
38         std::cout << "construct from char" << std::endl;
39     }
40 
test_attrtest_attr41     test_attr(test_attr const&)
42     {
43         std::cout << "copy construct" << std::endl;
44     }
45 
operator =test_attr46     test_attr& operator=(test_attr const&)
47     {
48         std::cout << "assign" << std::endl;
49         return *this;
50     }
51 };
52 
53 template <typename Iterator>
54 struct test_parser : grammar<Iterator, std::vector<test_attr>() >
55 {
test_parsertest_parser56     test_parser() : test_parser::base_type(start)
57     {
58 #ifdef ATTR_PROPAGATE
59         start = char_ >> *(',' >> char_);
60 #else
61         start = char_[push_back(_val, _1)] >> *(',' >> char_[push_back(_val, _1)]);
62 #endif
63     }
64 
65     rule<Iterator, std::vector<test_attr>()> start;
66 };
67 
main()68 int main()
69 {
70     typedef std::string::const_iterator iterator_type;
71     typedef test_parser<iterator_type> test_parser;
72 
73     test_parser g;
74     std::string str = "a,b,c,d,e";
75 
76     std::vector<test_attr> result;
77     result.reserve(20);
78     std::string::const_iterator iter = str.begin();
79     std::string::const_iterator end = str.end();
80     bool r = parse(iter, end, g, result);
81 
82     if (r && iter == end)
83     {
84         std::cout << "-------------------------\n";
85         std::cout << "Parsing succeeded\n";
86         std::cout << "\n-------------------------\n";
87     }
88     else
89     {
90         std::cout << "-------------------------\n";
91         std::cout << "Parsing failed\n";
92         std::cout << "-------------------------\n";
93     }
94 
95     std::cout << "Bye... :-) \n\n";
96     return 0;
97 }
98 
99