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

source code of boost/libs/spirit/test/lex/bol_reset.cpp