]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/lex/regression_file_iterator2.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / regression_file_iterator2.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 #include <boost/spirit/include/support_multi_pass.hpp>
8 #include <boost/spirit/include/classic_position_iterator.hpp>
9 #include <boost/spirit/include/lex_lexertl.hpp>
10
11 #include <boost/core/lightweight_test.hpp>
12 #include <boost/phoenix/operator/self.hpp>
13 #include <boost/phoenix/statement/sequence.hpp>
14
15 namespace spirit = boost::spirit;
16 namespace lex = spirit::lex;
17
18 typedef spirit::classic::position_iterator2<
19 spirit::multi_pass<std::istreambuf_iterator<char> >
20 > file_iterator;
21
22 inline file_iterator
23 make_file_iterator(std::istream& input, const std::string& filename)
24 {
25 return file_iterator(
26 spirit::make_default_multi_pass(
27 std::istreambuf_iterator<char>(input)),
28 spirit::multi_pass<std::istreambuf_iterator<char> >(),
29 filename);
30 }
31
32 struct identifier
33 {
34 identifier(file_iterator, file_iterator)
35 {
36 }
37 };
38
39 struct string_literal
40 {
41 string_literal(file_iterator, file_iterator)
42 {
43 }
44 };
45
46 typedef lex::lexertl::token<
47 file_iterator, boost::mpl::vector<identifier, string_literal>
48 > token_type;
49
50 struct lexer
51 : lex::lexer<lex::lexertl::actor_lexer<token_type> >
52 {
53 lexer()
54 : id("[a-zA-Z0-9]+", 1)
55 , st("'[^'\\n]*'", 2)
56 {
57 self = id [
58 lex::_state = "ST"
59 ]
60 | lex::token_def<>(".", 3) [
61 lex::_state = "ST"
62 ]
63 ;
64
65 self("ST") =
66 st [
67 lex::_state = "INITIAL"
68 ]
69 | lex::token_def<>(".", 4) [(
70 lex::_state = "INITIAL"
71 , lex::_pass = lex::pass_flags::pass_fail
72 )]
73 ;
74 }
75
76 lex::token_def<identifier> id;
77 lex::token_def<string_literal> st;
78 };
79
80 typedef lexer::iterator_type token_iterator;
81
82 int main()
83 {
84 std::stringstream ss;
85 ss << "foo 'bar'";
86
87 file_iterator begin = make_file_iterator(ss, "SS");
88 file_iterator end;
89
90 lexer l;
91 token_iterator begin2 = l.begin(begin, end, "ST");
92 token_iterator end2 = l.end();
93
94 std::size_t test_data[] = { 1, 3, 2 };
95 std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
96
97 token_iterator it = begin2;
98 std::size_t i = 0;
99 for (/**/; it != end2 && i < test_data_size; ++it, ++i)
100 {
101 BOOST_TEST(it->id() == test_data[i]);
102 }
103 BOOST_TEST(it == end2);
104 BOOST_TEST(i == test_data_size);
105
106 return boost::report_errors();
107 }