]>
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 | ||
7c673cae FG |
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 | ||
7c673cae FG |
14 | namespace spirit = boost::spirit; |
15 | namespace lex = spirit::lex; | |
7c673cae FG |
16 | |
17 | typedef spirit::classic::position_iterator2< | |
18 | spirit::multi_pass<std::istreambuf_iterator<char> > | |
19 | > file_iterator; | |
20 | ||
21 | typedef boost::iterator_range<file_iterator> file_range; | |
22 | ||
23 | inline file_iterator | |
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 | { | |
35 | identifier(file_iterator, file_iterator) | |
36 | { | |
37 | } | |
38 | }; | |
39 | ||
40 | struct string_literal | |
41 | { | |
42 | 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 | { | |
54 | lexer() | |
55 | : id("[a-zA-Z0-9]+", 1) | |
56 | , st("'[^'\\n]*'", 2) | |
57 | { | |
58 | self("ST") = | |
59 | st [ lex::_state = "INITIAL" ] | |
60 | ; | |
61 | ||
62 | self("*") = | |
63 | id [ lex::_state = "ST" ] | |
64 | | lex::token_def<>(".", 3) [ lex::_state = "ST" ] | |
65 | ; | |
66 | } | |
67 | ||
68 | lex::token_def<identifier> id; | |
69 | lex::token_def<string_literal> st; | |
70 | }; | |
71 | ||
72 | typedef lexer::iterator_type token_iterator; | |
73 | ||
74 | int main() | |
75 | { | |
76 | std::stringstream ss; | |
77 | ss << "foo 'bar'"; | |
78 | ||
79 | file_iterator begin = make_file_iterator(ss, "SS"); | |
80 | file_iterator end; | |
81 | ||
82 | lexer l; | |
83 | token_iterator begin2 = l.begin(begin, end, "ST"); | |
84 | token_iterator end2 = l.end(); | |
85 | ||
86 | std::size_t test_data[] = { 1, 3, 2 }; | |
87 | std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]); | |
88 | ||
89 | token_iterator it = begin2; | |
90 | std::size_t i = 0; | |
91 | for (/**/; it != end2 && i < test_data_size; ++it, ++i) | |
92 | { | |
93 | BOOST_TEST(it->id() == test_data[i]); | |
94 | } | |
95 | BOOST_TEST(it == end2); | |
96 | BOOST_TEST(i == test_data_size); | |
97 | ||
98 | return boost::report_errors(); | |
99 | } |