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 #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
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 typedef boost::iterator_range<file_iterator> file_range;
23
24 inline file_iterator
make_file_iterator(std::istream & input,const std::string & filename)25 make_file_iterator(std::istream& input, const std::string& filename)
26 {
27 return file_iterator(
28 spirit::make_default_multi_pass(
29 std::istreambuf_iterator<char>(input)),
30 spirit::multi_pass<std::istreambuf_iterator<char> >(),
31 filename);
32 }
33
34 struct identifier
35 {
identifieridentifier36 identifier(file_iterator, file_iterator)
37 {
38 }
39 };
40
41 struct string_literal
42 {
string_literalstring_literal43 string_literal(file_iterator, file_iterator)
44 {
45 }
46 };
47
48 typedef lex::lexertl::token<
49 file_iterator, boost::mpl::vector<identifier, string_literal>
50 > token_type;
51
52 struct lexer
53 : lex::lexer<lex::lexertl::actor_lexer<token_type> >
54 {
lexerlexer55 lexer()
56 : id("[a-zA-Z0-9]+", 1)
57 , st("'[^'\\n]*'", 2)
58 {
59 self("ST") =
60 st [ lex::_state = "INITIAL" ]
61 ;
62
63 self("*") =
64 id [ lex::_state = "ST" ]
65 | lex::token_def<>(".", 3) [ lex::_state = "ST" ]
66 ;
67 }
68
69 lex::token_def<identifier> id;
70 lex::token_def<string_literal> st;
71 };
72
73 typedef lexer::iterator_type token_iterator;
74
main()75 int main()
76 {
77 std::stringstream ss;
78 ss << "foo 'bar'";
79
80 file_iterator begin = make_file_iterator(ss, "SS");
81 file_iterator end;
82
83 lexer l;
84 token_iterator begin2 = l.begin(begin, end, "ST");
85 token_iterator end2 = l.end();
86
87 std::size_t test_data[] = { 1, 3, 2 };
88 std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
89
90 token_iterator it = begin2;
91 std::size_t i = 0;
92 for (/**/; it != end2 && i < test_data_size; ++it, ++i)
93 {
94 BOOST_TEST(it->id() == test_data[i]);
95 }
96 BOOST_TEST(it == end2);
97 BOOST_TEST(i == test_data_size);
98
99 return boost::report_errors();
100 }
101