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