]>
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/config/warning_disable.hpp> |
12 | #include <boost/detail/lightweight_test.hpp> | |
13 | ||
14 | #include <boost/spirit/include/support_multi_pass.hpp> | |
15 | #include <boost/spirit/include/classic_position_iterator.hpp> | |
16 | #include <boost/spirit/include/lex_lexertl.hpp> | |
17 | ||
7c673cae FG |
18 | namespace spirit = boost::spirit; |
19 | namespace lex = spirit::lex; | |
7c673cae FG |
20 | |
21 | typedef spirit::classic::position_iterator2< | |
22 | spirit::multi_pass<std::istreambuf_iterator<char> > | |
23 | > file_iterator; | |
24 | ||
25 | inline file_iterator | |
26 | make_file_iterator(std::istream& input, const std::string& filename) | |
27 | { | |
28 | return file_iterator( | |
29 | spirit::make_default_multi_pass( | |
30 | std::istreambuf_iterator<char>(input)), | |
31 | spirit::multi_pass<std::istreambuf_iterator<char> >(), | |
32 | filename); | |
33 | } | |
34 | ||
35 | typedef lex::lexertl::token<file_iterator> token_type; | |
36 | ||
37 | struct lexer | |
38 | : lex::lexer<lex::lexertl::actor_lexer<token_type> > | |
39 | { | |
40 | lexer() : word("^[a-zA-Z0-9]+$", 1) | |
41 | { | |
42 | self = word [ | |
43 | lex::_state = "O" | |
44 | ] | |
45 | | lex::token_def<>("!.*$") [ | |
46 | lex::_state = "O" | |
47 | , lex::_pass = lex::pass_flags::pass_ignore | |
48 | ] | |
49 | | lex::token_def<>('\n', 2) [ | |
50 | lex::_state = "O" | |
51 | ] | |
52 | ; | |
53 | ||
54 | self("O") = | |
55 | lex::token_def<>(".") [ | |
56 | lex::_state = "INITIAL" | |
57 | , lex::_pass = lex::pass_flags::pass_fail | |
58 | ] | |
59 | ; | |
60 | } | |
61 | ||
62 | lex::token_def<> word; | |
63 | }; | |
64 | ||
65 | typedef lexer::iterator_type token_iterator; | |
66 | ||
67 | int main() | |
68 | { | |
69 | std::stringstream ss; | |
70 | ss << "!foo\nbar\n!baz"; | |
71 | ||
72 | file_iterator begin = make_file_iterator(ss, "SS"); | |
73 | file_iterator end; | |
74 | ||
75 | lexer l; | |
76 | token_iterator begin2 = l.begin(begin, end); | |
77 | token_iterator end2 = l.end(); | |
78 | ||
79 | std::size_t test_data[] = { 2, 1, 2 }; | |
80 | std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]); | |
81 | ||
82 | token_iterator it = begin2; | |
83 | std::size_t i = 0; | |
84 | for (/**/; it != end2 && i < test_data_size; ++it, ++i) | |
85 | { | |
86 | BOOST_TEST(it->id() == test_data[i]); | |
87 | } | |
88 | BOOST_TEST(it == end2); | |
89 | BOOST_TEST(i == test_data_size); | |
90 | ||
91 | return boost::report_errors(); | |
92 | } |