]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae FG |
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 | ||
1e59de90 TL |
14 | #include <boost/core/lightweight_test.hpp> |
15 | #include <boost/phoenix/core.hpp> | |
16 | #include <boost/phoenix/operator.hpp> | |
17 | #include <boost/phoenix/statement.hpp> | |
7c673cae FG |
18 | |
19 | #include <sstream> | |
20 | ||
21 | namespace spirit = boost::spirit; | |
22 | namespace lex = spirit::lex; | |
23 | namespace phoenix = boost::phoenix; | |
24 | ||
25 | typedef spirit::classic::position_iterator2< | |
26 | spirit::multi_pass<std::istreambuf_iterator<char> > | |
27 | > file_iterator; | |
28 | ||
29 | typedef boost::iterator_range<file_iterator> file_range; | |
30 | ||
31 | inline file_iterator | |
32 | make_file_iterator(std::istream& input, const std::string& filename) | |
33 | { | |
34 | return file_iterator( | |
35 | spirit::make_default_multi_pass( | |
36 | std::istreambuf_iterator<char>(input)), | |
37 | spirit::multi_pass<std::istreambuf_iterator<char> >(), | |
38 | filename); | |
39 | } | |
40 | ||
41 | struct string_literal | |
42 | { | |
43 | string_literal(file_iterator, file_iterator) | |
44 | { | |
45 | } | |
46 | }; | |
47 | ||
48 | typedef lex::lexertl::token< | |
49 | file_iterator, boost::mpl::vector<string_literal> | |
50 | > token_type; | |
51 | ||
52 | struct lexer | |
53 | : lex::lexer<lex::lexertl::actor_lexer<token_type> > | |
54 | { | |
55 | lexer() : st("'[^'\\n]*'", 1) | |
56 | { | |
57 | lex::token_def<> string_lookahead('\''); | |
58 | self("LA") = string_lookahead; | |
59 | ||
60 | // make sure lookahead is implicitly evaluated using the lexer state | |
61 | // the token_def has been associated with | |
62 | self = st [ | |
63 | phoenix::if_(lex::lookahead(string_lookahead)) [ lex::more() ] | |
64 | ] | |
65 | ; | |
66 | } | |
67 | ||
68 | lex::token_def<string_literal> st; | |
69 | }; | |
70 | ||
71 | typedef lexer::iterator_type token_iterator; | |
72 | ||
73 | int main() | |
74 | { | |
75 | std::stringstream ss; | |
76 | ss << "'foo''bar'"; | |
77 | ||
78 | file_iterator begin = make_file_iterator(ss, "SS"); | |
79 | file_iterator end; | |
80 | ||
81 | lexer l; | |
82 | token_iterator begin2 = l.begin(begin, end); | |
83 | token_iterator end2 = l.end(); | |
84 | ||
85 | char const* test_data[] = { "1,'foo'", "1,'foo''bar'" }; | |
86 | std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]); | |
87 | ||
88 | token_iterator it = begin2; | |
89 | std::size_t i = 0; | |
90 | for (/**/; it != end2 && i < test_data_size; ++it, ++i) | |
91 | { | |
1e59de90 TL |
92 | std::stringstream ss2; |
93 | ss2 << it->id() << "," << *it; | |
94 | BOOST_TEST(ss2.str() == test_data[i]); | |
7c673cae FG |
95 | } |
96 | BOOST_TEST(it == end2); | |
97 | BOOST_TEST(i == test_data_size); | |
98 | ||
99 | return boost::report_errors(); | |
100 | } |