1 // Copyright (c) 2001-2010 Hartmut Kaiser
2 // Copyright (c) 2010 Mathias Gaunard
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)
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
10 // Additionally this test makes sure the syntax 'self("state", "targetstate")'
13 #include <boost/config/warning_disable.hpp>
14 #include <boost/detail/lightweight_test.hpp>
16 #include <boost/spirit/include/support_multi_pass.hpp>
17 #include <boost/spirit/include/classic_position_iterator.hpp>
18 #include <boost/spirit/include/lex_lexertl.hpp>
19 #include <boost/phoenix/operator/self.hpp>
21 namespace spirit
= boost::spirit
;
22 namespace lex
= spirit::lex
;
24 typedef spirit::classic::position_iterator2
<
25 spirit::multi_pass
<std::istreambuf_iterator
<char> >
29 make_file_iterator(std::istream
& input
, const std::string
& filename
)
32 spirit::make_default_multi_pass(
33 std::istreambuf_iterator
<char>(input
)),
34 spirit::multi_pass
<std::istreambuf_iterator
<char> >(),
38 typedef lex::lexertl::token
<file_iterator
> token_type
;
41 : lex::lexer
<lex::lexertl::actor_lexer
<token_type
> >
43 lexer() : word("^[a-zA-Z0-9]+$", 1)
45 self("INITIAL", "O") =
47 | lex::string("!.*$") [
48 lex::_pass
= lex::pass_flags::pass_ignore
50 | lex::token_def
<>('\n', 2)
53 self("O", "INITIAL") =
55 lex::_pass
= lex::pass_flags::pass_fail
60 lex::token_def
<> word
;
63 typedef lexer::iterator_type token_iterator
;
68 ss
<< "!foo\nbar\n!baz";
70 file_iterator begin
= make_file_iterator(ss
, "SS");
74 token_iterator begin2
= l
.begin(begin
, end
);
75 token_iterator end2
= l
.end();
77 std::size_t test_data
[] = { 2, 1, 2 };
78 std::size_t const test_data_size
= sizeof(test_data
)/sizeof(test_data
[0]);
80 token_iterator it
= begin2
;
82 for (/**/; it
!= end2
&& i
< test_data_size
; ++it
, ++i
)
84 BOOST_TEST(it
->id() == test_data
[i
]);
86 BOOST_TEST(it
== end2
);
87 BOOST_TEST(i
== test_data_size
);
89 return boost::report_errors();