- #include <boost/spirit/include/qi.hpp>
- #include <boost/spirit/include/phoenix_core.hpp>
- #include <boost/spirit/include/phoenix_operator.hpp>
- #include <boost/spirit/include/phoenix_object.hpp>
- #include <boost/fusion/include/adapt_struct.hpp>
- #include <boost/fusion/include/io.hpp>
- #include <iostream>
- #include <string>
- namespace qi = boost::spirit::qi;
- namespace ascii = boost::spirit::ascii;
- namespace phx = boost::phoenix;
- namespace client
- {
- struct print_action
- {
- void operator()(int const& i, qi::unused_type, qi::unused_type) const
- {
- std::cout << i << std::endl;
- }
- };
- template <typename Iterator, typename Skipper>
- struct grammer : qi::grammar<Iterator, std::vector<int>(), Skipper>
- {
- grammer() : grammer::base_type(start), comment_char( '|' )
- {
- using ascii::no_case;
- using qi::_val;
- using qi::_1;
- using qi::lexeme;
- using qi::omit;
- using qi::char_;
- using qi::no_skip;
- using qi::char_;
- using qi::int_;
- using qi::eol;
- comment =
- char_( phx::ref( comment_char ) )
- >> *( char_ - eol ) >> eol
- ;
- data %=
- int_ >> *( ',' >> int_ )
- ;
- start = comment | data
- ;
- BOOST_SPIRIT_DEBUG_NODE( comment );
- BOOST_SPIRIT_DEBUG_NODE( data );
- BOOST_SPIRIT_DEBUG_NODE( start );
- }
- char comment_char;
- qi::rule<Iterator, Skipper> comment;
- qi::rule<Iterator, std::vector<int>(), Skipper> data;
- qi::rule<Iterator, std::vector<int>(), Skipper> start;
- };
- }
- int main(int argc, char * argv[])
- {
- typedef const char* Iterator;
- Iterator iter =
- "| default comment\n"
- "|4,"
- "| default comment\n"
- "2"
- ;
- Iterator end = iter + std::strlen(iter);
- typedef const char* Iterator;
- typedef client::grammer<Iterator, ascii::space_type> grammar_type;
- grammar_type g;
- std::vector<int> v;
- bool r = phrase_parse(iter, end, g, ascii::space, v);
- if (r && iter == end)
- {
- std::cout << "-------------------------\n";
- std::cout << "Parsing succeeded\n";
- std::cout << v[0] << "," << v[1] << std::endl;
- std::cout << "-------------------------\n";
- }
- else
- {
- std::string rest(iter, end);
- std::cout << "-------------------------\n";
- std::cout << "Parsing failed\n";
- std::cout << "stopped at: \"" << rest << "\"\n";
- std::cout << "-------------------------\n";
- }
- return 0;
- }
- $ g++ -DBOOST_SPIRIT_DEBUG -I /home/olaf/Projects/programming/cpp/boost/trunk/ support.cpp -o support && ./support
- <start>
- <try>| default comment\n|4</try>
- <comment>
- <try>| default comment\n|4</try>
- <fail/>
- </comment>
- <data>
- <try>| default comment\n|4</try>
- <fail/>
- </data>
- <fail/>
- </start>
- -------------------------
- Parsing failed
- stopped at: "| default comment
- |4,| default comment
- 2"
- -------------------------
