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 | // Additionally this test makes sure the syntax 'self("state", "targetstate")' |
11 | // works properly. |
12 | |
13 | #include <boost/spirit/include/lex_lexertl.hpp> |
14 | |
15 | #include <boost/core/lightweight_test.hpp> |
16 | #include <boost/phoenix/operator/self.hpp> |
17 | |
18 | #include <sstream> |
19 | |
20 | namespace spirit = boost::spirit; |
21 | namespace lex = spirit::lex; |
22 | |
23 | typedef char const* content_iterator; |
24 | typedef lex::lexertl::token<content_iterator> token_type; |
25 | |
26 | struct lexer |
27 | : lex::lexer<lex::lexertl::actor_lexer<token_type> > |
28 | { |
29 | lexer() : word("^[a-zA-Z0-9]+$" , 1) |
30 | { |
31 | self("INITIAL" , "O" ) = |
32 | word |
33 | | lex::string("!.*$" ) [ |
34 | lex::_pass = lex::pass_flags::pass_ignore |
35 | ] |
36 | | lex::token_def<>('\n', 2) |
37 | ; |
38 | |
39 | self("O" , "INITIAL" ) = |
40 | lex::string("." ) [ |
41 | lex::_pass = lex::pass_flags::pass_fail |
42 | ] |
43 | ; |
44 | } |
45 | |
46 | lex::token_def<> word; |
47 | }; |
48 | |
49 | typedef lexer::iterator_type token_iterator; |
50 | |
51 | int main() |
52 | { |
53 | std::string const s = "!foo\nbar\n!baz" ; |
54 | |
55 | content_iterator begin = s.data(); |
56 | content_iterator end = s.data() + s.size(); |
57 | |
58 | lexer l; |
59 | token_iterator begin2 = l.begin(first&: begin, last: end); |
60 | token_iterator end2 = l.end(); |
61 | |
62 | std::size_t test_data[] = { 2, 1, 2 }; |
63 | std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]); |
64 | |
65 | token_iterator it = begin2; |
66 | std::size_t i = 0; |
67 | for (/**/; it != end2 && i < test_data_size; ++it, ++i) |
68 | { |
69 | BOOST_TEST(it->id() == test_data[i]); |
70 | } |
71 | BOOST_TEST(it == end2); |
72 | BOOST_TEST(i == test_data_size); |
73 | |
74 | return boost::report_errors(); |
75 | } |
76 | |