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