1 // Copyright (c) 2001-2010 Hartmut Kaiser
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/spirit/include/lex_lexertl.hpp>
8 #include <boost/spirit/include/qi_parse.hpp>
9 #include <boost/spirit/include/qi_operator.hpp>
10 #include <boost/spirit/include/qi_action.hpp>
11 #include <boost/spirit/include/qi_grammar.hpp>
13 #include <boost/core/lightweight_test.hpp>
14 #include <boost/phoenix/operator.hpp>
19 namespace qi
= boost::spirit::qi
;
20 namespace lex
= boost::spirit::lex
;
24 IDWORD
= lex::min_token_id
,
29 template <typename Lexer
>
30 struct word_count_tokens
: lex::lexer
<Lexer
>
34 this->self
.add_pattern
39 lex::string("{TEST}", IDWORD
)
40 | lex::char_('a', IDCHAR
)
41 | lex::string(".", IDANY
)
46 template <typename Iterator
>
47 struct word_count_grammar
: qi::grammar
<Iterator
>
49 template <typename TokenDef
>
50 word_count_grammar(TokenDef
const&)
51 : word_count_grammar::base_type(start
)
54 using boost::phoenix::ref
;
57 start
= *( token(IDWORD
) [++ref(w
)]
58 | token(IDCHAR
) [++ref(c
)]
59 | token(IDANY
) [++ref(a
)]
64 qi::rule
<Iterator
> start
;
70 typedef lex::lexertl::token
<
71 const char*, boost::mpl::vector
<std::string
>
74 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
75 typedef word_count_tokens
<lexer_type
>::iterator_type iterator_type
;
76 word_count_tokens
<lexer_type
> word_count
; // Our lexer
77 word_count_grammar
<iterator_type
> g (word_count
); // Our parser
79 std::string
str ("AaBCD");
80 char const* first
= str
.c_str();
81 char const* last
= &first
[str
.size()];
83 BOOST_TEST(lex::tokenize_and_parse(first
, last
, word_count
, g
));
84 BOOST_TEST(g
.w
== 1 && g
.c
== 1 && g
.a
== 3);
86 return boost::report_errors();