]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2010 Hartmut Kaiser |
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 | ||
7c673cae | 6 | #include <boost/spirit/include/lex_lexertl.hpp> |
1e59de90 | 7 | |
92f5a8d4 TL |
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> | |
1e59de90 TL |
12 | |
13 | #include <boost/core/lightweight_test.hpp> | |
14 | #include <boost/phoenix/operator.hpp> | |
7c673cae FG |
15 | |
16 | #include <iostream> | |
17 | #include <string> | |
18 | ||
19 | namespace qi = boost::spirit::qi; | |
20 | namespace lex = boost::spirit::lex; | |
21 | ||
22 | enum tokenids | |
23 | { | |
24 | IDWORD = lex::min_token_id, | |
25 | IDCHAR, | |
26 | IDANY | |
27 | }; | |
28 | ||
29 | template <typename Lexer> | |
30 | struct word_count_tokens : lex::lexer<Lexer> | |
31 | { | |
32 | word_count_tokens() | |
33 | { | |
34 | this->self.add_pattern | |
35 | ("TEST", "A") | |
36 | ; | |
37 | ||
38 | this->self = | |
39 | lex::string("{TEST}", IDWORD) | |
40 | | lex::char_('a', IDCHAR) | |
41 | | lex::string(".", IDANY) | |
42 | ; | |
43 | } | |
44 | }; | |
45 | ||
46 | template <typename Iterator> | |
47 | struct word_count_grammar : qi::grammar<Iterator> | |
48 | { | |
49 | template <typename TokenDef> | |
50 | word_count_grammar(TokenDef const&) | |
51 | : word_count_grammar::base_type(start) | |
52 | , w(0), c(0), a(0) | |
53 | { | |
54 | using boost::phoenix::ref; | |
55 | using qi::token; | |
56 | ||
57 | start = *( token(IDWORD) [++ref(w)] | |
58 | | token(IDCHAR) [++ref(c)] | |
59 | | token(IDANY) [++ref(a)] | |
60 | ) | |
61 | ; | |
62 | } | |
63 | std::size_t w, c, a; | |
64 | qi::rule<Iterator> start; | |
65 | }; | |
66 | ||
67 | ||
68 | int main() | |
69 | { | |
70 | typedef lex::lexertl::token< | |
71 | const char*, boost::mpl::vector<std::string> | |
72 | > token_type; | |
73 | ||
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 | |
78 | ||
79 | std::string str ("AaBCD"); | |
80 | char const* first = str.c_str(); | |
81 | char const* last = &first[str.size()]; | |
82 | ||
83 | BOOST_TEST(lex::tokenize_and_parse(first, last, word_count, g)); | |
84 | BOOST_TEST(g.w == 1 && g.c == 1 && g.a == 3); | |
85 | ||
86 | return boost::report_errors(); | |
87 | } |