]>
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 | // This test makes sure that the BOL state (begin of line) is properly reset | |
8 | // if a token matched at the beginning of a line is discarded using | |
9 | // lex::pass_fail. | |
10 | ||
7c673cae FG |
11 | #include <boost/spirit/include/support_multi_pass.hpp> |
12 | #include <boost/spirit/include/classic_position_iterator.hpp> | |
13 | #include <boost/spirit/include/lex_lexertl.hpp> | |
1e59de90 TL |
14 | |
15 | #include <boost/core/lightweight_test.hpp> | |
f67539c2 TL |
16 | #include <boost/phoenix/operator/self.hpp> |
17 | #include <boost/phoenix/statement/sequence.hpp> | |
7c673cae | 18 | |
7c673cae FG |
19 | namespace spirit = boost::spirit; |
20 | namespace lex = spirit::lex; | |
7c673cae FG |
21 | |
22 | typedef spirit::classic::position_iterator2< | |
23 | spirit::multi_pass<std::istreambuf_iterator<char> > | |
24 | > file_iterator; | |
25 | ||
26 | inline file_iterator | |
27 | make_file_iterator(std::istream& input, const std::string& filename) | |
28 | { | |
29 | return file_iterator( | |
30 | spirit::make_default_multi_pass( | |
31 | std::istreambuf_iterator<char>(input)), | |
32 | spirit::multi_pass<std::istreambuf_iterator<char> >(), | |
33 | filename); | |
34 | } | |
35 | ||
36 | typedef lex::lexertl::token<file_iterator> token_type; | |
37 | ||
38 | struct lexer | |
39 | : lex::lexer<lex::lexertl::actor_lexer<token_type> > | |
40 | { | |
41 | lexer() : word("^[a-zA-Z0-9]+$", 1) | |
42 | { | |
43 | self = word [ | |
44 | lex::_state = "O" | |
45 | ] | |
20effc67 | 46 | | lex::token_def<>("!.*$") [( |
7c673cae FG |
47 | lex::_state = "O" |
48 | , lex::_pass = lex::pass_flags::pass_ignore | |
20effc67 | 49 | )] |
7c673cae FG |
50 | | lex::token_def<>('\n', 2) [ |
51 | lex::_state = "O" | |
52 | ] | |
53 | ; | |
54 | ||
55 | self("O") = | |
20effc67 | 56 | lex::token_def<>(".") [( |
7c673cae FG |
57 | lex::_state = "INITIAL" |
58 | , lex::_pass = lex::pass_flags::pass_fail | |
20effc67 | 59 | )] |
7c673cae FG |
60 | ; |
61 | } | |
62 | ||
63 | lex::token_def<> 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 | std::size_t test_data[] = { 2, 1, 2 }; | |
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 | } |