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