]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2010 Hartmut Kaiser |
2 | // Copyright (c) 2010 Mathias Gaunard | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <boost/config/warning_disable.hpp> | |
8 | #include <boost/detail/lightweight_test.hpp> | |
9 | ||
10 | #include <boost/spirit/include/support_multi_pass.hpp> | |
11 | #include <boost/spirit/include/classic_position_iterator.hpp> | |
12 | #include <boost/spirit/include/lex_lexertl.hpp> | |
13 | ||
7c673cae FG |
14 | namespace spirit = boost::spirit; |
15 | namespace lex = spirit::lex; | |
7c673cae FG |
16 | |
17 | typedef spirit::classic::position_iterator2< | |
18 | spirit::multi_pass<std::istreambuf_iterator<char> > | |
19 | > file_iterator; | |
20 | ||
21 | inline file_iterator | |
22 | make_file_iterator(std::istream& input, const std::string& filename) | |
23 | { | |
24 | return file_iterator( | |
25 | spirit::make_default_multi_pass( | |
26 | std::istreambuf_iterator<char>(input)), | |
27 | spirit::multi_pass<std::istreambuf_iterator<char> >(), | |
28 | filename); | |
29 | } | |
30 | ||
31 | enum token_id | |
32 | { | |
33 | ID_WORD = lex::min_token_id + 1, | |
34 | ID_EOL | |
35 | }; | |
36 | ||
37 | typedef lex::lexertl::token< | |
38 | file_iterator, boost::mpl::vector<>, boost::mpl::true_, token_id | |
39 | > token_type; | |
40 | ||
41 | struct lexer | |
42 | : lex::lexer<lex::lexertl::actor_lexer<token_type> > | |
43 | { | |
44 | lexer() : word("^[a-zA-Z0-9]+$", ID_WORD) | |
45 | { | |
46 | typedef lex::token_def<lex::unused_type, char, token_id> toked_def; | |
47 | ||
48 | self("INITIAL", "O") = | |
49 | word | |
50 | | toked_def("!.*$") [ | |
51 | lex::_pass = lex::pass_flags::pass_ignore | |
52 | ] | |
53 | | toked_def('\n', ID_EOL) | |
54 | ; | |
55 | ||
56 | self("O", "INITIAL") = | |
57 | toked_def(".") [ | |
58 | lex::_pass = lex::pass_flags::pass_fail | |
59 | ] | |
60 | ; | |
61 | } | |
62 | ||
63 | lex::token_def<lex::unused_type, char, token_id> word; | |
64 | }; | |
65 | ||
66 | typedef lexer::iterator_type token_iterator; | |
67 | ||
68 | int main() | |
69 | { | |
70 | std::stringstream ss; | |
71 | ss << "!foo\nbar\n!baz"; | |
72 | ||
73 | file_iterator begin = make_file_iterator(ss, "SS"); | |
74 | file_iterator end; | |
75 | ||
76 | lexer l; | |
77 | token_iterator begin2 = l.begin(begin, end); | |
78 | token_iterator end2 = l.end(); | |
79 | ||
80 | token_id test_data[] = { ID_EOL, ID_WORD, ID_EOL }; | |
81 | std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]); | |
82 | ||
83 | token_iterator it = begin2; | |
84 | std::size_t i = 0; | |
85 | for (/**/; it != end2 && i < test_data_size; ++it, ++i) | |
86 | { | |
87 | BOOST_TEST(it->id() == test_data[i]); | |
88 | } | |
89 | BOOST_TEST(it == end2); | |
90 | BOOST_TEST(i == test_data_size); | |
91 | ||
92 | return boost::report_errors(); | |
93 | } |