• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <sstream>
22 
23 namespace spirit = boost::spirit;
24 namespace lex = spirit::lex;
25 namespace phoenix = boost::phoenix;
26 
27 typedef spirit::classic::position_iterator2<
28     spirit::multi_pass<std::istreambuf_iterator<char> >
29 > file_iterator;
30 
31 typedef boost::iterator_range<file_iterator> file_range;
32 
33 inline file_iterator
make_file_iterator(std::istream & input,const std::string & filename)34 make_file_iterator(std::istream& input, const std::string& filename)
35 {
36     return file_iterator(
37         spirit::make_default_multi_pass(
38             std::istreambuf_iterator<char>(input)),
39         spirit::multi_pass<std::istreambuf_iterator<char> >(),
40         filename);
41 }
42 
43 struct string_literal
44 {
string_literalstring_literal45     string_literal(file_iterator, file_iterator)
46     {
47     }
48 };
49 
50 typedef lex::lexertl::token<
51     file_iterator, boost::mpl::vector<string_literal>
52 > token_type;
53 
54 struct lexer
55   : lex::lexer<lex::lexertl::actor_lexer<token_type> >
56 {
lexerlexer57     lexer() : st("'[^'\\n]*'", 1)
58     {
59         lex::token_def<> string_lookahead('\'');
60         self("LA") = string_lookahead;
61 
62         // make sure lookahead is implicitly evaluated using the lexer state
63         // the token_def has been associated with
64         self = st [
65                 phoenix::if_(lex::lookahead(string_lookahead)) [ lex::more() ]
66             ]
67             ;
68     }
69 
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);
85     token_iterator end2 = l.end();
86 
87     char const* test_data[] = { "1,'foo'", "1,'foo''bar'" };
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         std::stringstream ss;
95         ss << it->id() << "," << *it;
96         BOOST_TEST(ss.str() == test_data[i]);
97     }
98     BOOST_TEST(it == end2);
99     BOOST_TEST(i == test_data_size);
100 
101     return boost::report_errors();
102 }
103