]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/lex/regression_file_iterator3.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / regression_file_iterator3.cpp
1 // Copyright (c) 2001-2011 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_DEBUG 1 // required for token streaming
8 // #define BOOST_SPIRIT_LEXERTL_DEBUG 1
9
10 #include <boost/config/warning_disable.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12
13 #include <boost/spirit/include/support_multi_pass.hpp>
14 #include <boost/spirit/include/classic_position_iterator.hpp>
15 #include <boost/spirit/include/lex_lexertl.hpp>
16
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/include/phoenix_operator.hpp>
19 #include <boost/spirit/include/phoenix_statement.hpp>
20 #include <boost/spirit/include/phoenix_object.hpp>
21 #include <boost/spirit/include/phoenix_stl.hpp>
22
23 #include <sstream>
24
25 namespace spirit = boost::spirit;
26 namespace lex = spirit::lex;
27 namespace phoenix = boost::phoenix;
28
29 typedef spirit::classic::position_iterator2<
30 spirit::multi_pass<std::istreambuf_iterator<char> >
31 > file_iterator;
32
33 typedef boost::iterator_range<file_iterator> file_range;
34
35 inline file_iterator
36 make_file_iterator(std::istream& input, const std::string& filename)
37 {
38 return file_iterator(
39 spirit::make_default_multi_pass(
40 std::istreambuf_iterator<char>(input)),
41 spirit::multi_pass<std::istreambuf_iterator<char> >(),
42 filename);
43 }
44
45 struct string_literal
46 {
47 string_literal(file_iterator, file_iterator)
48 {
49 }
50 };
51
52 typedef lex::lexertl::token<
53 file_iterator, boost::mpl::vector<string_literal>
54 > token_type;
55
56 struct lexer
57 : lex::lexer<lex::lexertl::actor_lexer<token_type> >
58 {
59 lexer() : st("'[^'\\n]*'", 1)
60 {
61 lex::token_def<> string_lookahead('\'');
62 self("LA") = string_lookahead;
63
64 // make sure lookahead is implicitly evaluated using the lexer state
65 // the token_def has been associated with
66 self = st [
67 phoenix::if_(lex::lookahead(string_lookahead)) [ lex::more() ]
68 ]
69 ;
70 }
71
72 lex::token_def<string_literal> st;
73 };
74
75 typedef lexer::iterator_type token_iterator;
76
77 int main()
78 {
79 std::stringstream ss;
80 ss << "'foo''bar'";
81
82 file_iterator begin = make_file_iterator(ss, "SS");
83 file_iterator end;
84
85 lexer l;
86 token_iterator begin2 = l.begin(begin, end);
87 token_iterator end2 = l.end();
88
89 char const* test_data[] = { "1,'foo'", "1,'foo''bar'" };
90 std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
91
92 token_iterator it = begin2;
93 std::size_t i = 0;
94 for (/**/; it != end2 && i < test_data_size; ++it, ++i)
95 {
96 std::stringstream ss;
97 ss << it->id() << "," << *it;
98 BOOST_TEST(ss.str() == test_data[i]);
99 }
100 BOOST_TEST(it == end2);
101 BOOST_TEST(i == test_data_size);
102
103 return boost::report_errors();
104 }