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 // #define BOOST_SPIRIT_LEXERTL_DEBUG
7 #define BOOST_VARIANT_MINIMIZE_SIZE
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/config/warning_disable.hpp>
12 #include <boost/spirit/include/qi.hpp>
13 #include <boost/spirit/include/lex_lexertl.hpp>
14 #include <boost/spirit/include/phoenix_operator.hpp>
15 #include <boost/spirit/include/phoenix_statement.hpp>
16 #include <boost/spirit/include/phoenix_container.hpp>
21 namespace qi
= boost::spirit::qi
;
22 namespace lex
= boost::spirit::lex
;
26 IDWORD
= lex::min_token_id
,
31 template <typename Lexer
>
32 struct word_count_tokens
: lex::lexer
<Lexer
>
36 this->self
.add_pattern
41 lex::string("{TEST}", IDWORD
)
42 | lex::char_('a', IDCHAR
)
43 | lex::string(".", IDANY
)
48 template <typename Iterator
>
49 struct word_count_grammar
: qi::grammar
<Iterator
>
51 template <typename TokenDef
>
52 word_count_grammar(TokenDef
const&)
53 : word_count_grammar::base_type(start
)
56 using boost::phoenix::ref
;
59 start
= *( token(IDWORD
) [++ref(w
)]
60 | token(IDCHAR
) [++ref(c
)]
61 | token(IDANY
) [++ref(a
)]
66 qi::rule
<Iterator
> start
;
72 typedef lex::lexertl::token
<
73 const char*, boost::mpl::vector
<std::string
>
76 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
77 typedef word_count_tokens
<lexer_type
>::iterator_type iterator_type
;
78 word_count_tokens
<lexer_type
> word_count
; // Our lexer
79 word_count_grammar
<iterator_type
> g (word_count
); // Our parser
81 std::string
str ("AaBCD");
82 char const* first
= str
.c_str();
83 char const* last
= &first
[str
.size()];
85 BOOST_TEST(lex::tokenize_and_parse(first
, last
, word_count
, g
));
86 BOOST_TEST(g
.w
== 1 && g
.c
== 1 && g
.a
== 3);
88 return boost::report_errors();