// Copyright (c) 2001-2010 Hartmut Kaiser // Copyright (c) 2009 Tor Brede Vekterli // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include #include #include #include namespace qi = boost::spirit::qi; namespace lex = boost::spirit::lex; enum tokenids { IDANY = lex::min_token_id + 10 // Lower 8 bits is 0x0a, same as '\n' }; template struct word_count_tokens : lex::lexer { word_count_tokens() { this->self.add_pattern ("TEST", "A") ; word = "{TEST}"; this->self.add (word) ('\n') (".", IDANY) ; } lex::token_def word; }; template struct word_count_grammar : qi::grammar { template word_count_grammar(TokenDef const& tok) : word_count_grammar::base_type(start) , c(0), w(0), l(0) { using boost::phoenix::ref; using qi::lit; using qi::token; start = *( tok.word [++ref(w)] | lit('\n') [++ref(l)] | token(IDANY) [++ref(c)] ) ; } std::size_t c, w, l; qi::rule start; }; int main() { typedef lex::lexertl::token< const char*, boost::mpl::vector > token_type; typedef lex::lexertl::lexer lexer_type; typedef word_count_tokens::iterator_type iterator_type; word_count_tokens word_count; // Our lexer word_count_grammar g (word_count); // Our parser std::string str ("A\nBCDEFGHI"); char const* first = str.c_str(); char const* last = &first[str.size()]; BOOST_TEST(lex::tokenize_and_parse(first, last, word_count, g)); BOOST_TEST(g.l == 1 && g.w == 1 && g.c == 8); return boost::report_errors(); }