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/spirit/include/lex_lexertl.hpp>
8
9#include <boost/core/lightweight_test.hpp>
10#include <boost/phoenix/operator/self.hpp>
11
12#include <sstream>
13
14namespace spirit = boost::spirit;
15namespace lex = spirit::lex;
16
17typedef char const* content_iterator;
18
19enum token_id
20{
21 ID_WORD = lex::min_token_id + 1,
22 ID_EOL
23};
24
25typedef lex::lexertl::token<
26 content_iterator, boost::mpl::vector<>, boost::mpl::true_, token_id
27> token_type;
28
29struct lexer
30 : lex::lexer<lex::lexertl::actor_lexer<token_type> >
31{
32 lexer() : word("^[a-zA-Z0-9]+$", ID_WORD)
33 {
34 typedef lex::token_def<lex::unused_type, char, token_id> toked_def;
35
36 self("INITIAL", "O") =
37 word
38 | toked_def("!.*$") [
39 lex::_pass = lex::pass_flags::pass_ignore
40 ]
41 | toked_def('\n', ID_EOL)
42 ;
43
44 self("O", "INITIAL") =
45 toked_def(".") [
46 lex::_pass = lex::pass_flags::pass_fail
47 ]
48 ;
49 }
50
51 lex::token_def<lex::unused_type, char, token_id> word;
52};
53
54typedef lexer::iterator_type token_iterator;
55
56int main()
57{
58 std::string const s = "!foo\nbar\n!baz";
59
60 content_iterator begin = s.data();
61 content_iterator end = s.data() + s.size();
62
63 lexer l;
64 token_iterator begin2 = l.begin(first&: begin, last: end);
65 token_iterator end2 = l.end();
66
67 token_id test_data[] = { ID_EOL, ID_WORD, ID_EOL };
68 std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
69
70 token_iterator it = begin2;
71 std::size_t i = 0;
72 for (/**/; it != end2 && i < test_data_size; ++it, ++i)
73 {
74 BOOST_TEST(it->id() == test_data[i]);
75 }
76 BOOST_TEST(it == end2);
77 BOOST_TEST(i == test_data_size);
78
79 return boost::report_errors();
80}
81

source code of boost/libs/spirit/test/lex/id_type_enum.cpp