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/spirit/include/lex_lexertl.hpp>
11
12#include <boost/core/lightweight_test.hpp>
13#include <boost/phoenix/core.hpp>
14#include <boost/phoenix/operator.hpp>
15#include <boost/phoenix/statement.hpp>
16
17#include <sstream>
18
19namespace spirit = boost::spirit;
20namespace lex = spirit::lex;
21namespace phoenix = boost::phoenix;
22
23typedef char const* content_iterator;
24
25struct string_literal
26{
27 string_literal(content_iterator, content_iterator)
28 {
29 }
30};
31
32typedef lex::lexertl::token<
33 content_iterator, boost::mpl::vector<string_literal>
34> token_type;
35
36struct lexer
37 : lex::lexer<lex::lexertl::actor_lexer<token_type> >
38{
39 lexer() : st("'[^'\\n]*'", 1)
40 {
41 lex::token_def<> string_lookahead('\'');
42 self("LA") = string_lookahead;
43
44 // make sure lookahead is implicitly evaluated using the lexer state
45 // the token_def has been associated with
46 self = st [
47 phoenix::if_(cond: lex::lookahead(tok: string_lookahead)) [ lex::more() ]
48 ]
49 ;
50 }
51
52 lex::token_def<string_literal> st;
53};
54
55typedef lexer::iterator_type token_iterator;
56
57int main()
58{
59 std::string const s = "'foo''bar'";
60
61 content_iterator begin = s.data();
62 content_iterator end = s.data() + s.size();
63
64 lexer l;
65 token_iterator begin2 = l.begin(first&: begin, last: end);
66 token_iterator end2 = l.end();
67
68 char const* test_data[] = { "1,'foo'", "1,'foo''bar'" };
69 std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
70
71 token_iterator it = begin2;
72 std::size_t i = 0;
73 for (/**/; it != end2 && i < test_data_size; ++it, ++i)
74 {
75 std::stringstream ss2;
76 ss2 << it->id() << "," << *it;
77 BOOST_TEST(ss2.str() == test_data[i]);
78 }
79 BOOST_TEST(it == end2);
80 BOOST_TEST(i == test_data_size);
81
82 return boost::report_errors();
83}
84

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