1 | // Copyright (c) 2009 Carl Barron |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #include <boost/spirit/include/lex.hpp> |
7 | #include <boost/spirit/include/lex_lexertl.hpp> |
8 | |
9 | #include <boost/core/lightweight_test.hpp> |
10 | #include <boost/phoenix/core.hpp> |
11 | #include <boost/phoenix/function.hpp> |
12 | #include <boost/phoenix/operator/self.hpp> |
13 | #include <string> |
14 | #include <iostream> |
15 | |
16 | namespace lex = boost::spirit::lex; |
17 | namespace phoenix = boost::phoenix; |
18 | |
19 | /////////////////////////////////////////////////////////////////////////////// |
20 | struct square_impl |
21 | { |
22 | template <class> |
23 | struct result { typedef int type; }; |
24 | |
25 | template <class A> |
26 | int operator () (const A &x) const |
27 | { return (x) * (x); } |
28 | }; |
29 | |
30 | phoenix::function<square_impl> const square = square_impl(); |
31 | |
32 | /////////////////////////////////////////////////////////////////////////////// |
33 | template <class Lexer> |
34 | struct test_tokens : lex::lexer<Lexer> |
35 | { |
36 | test_tokens() |
37 | { |
38 | a = "a"; |
39 | this->self = a [lex::_val = square(*lex::_start)]; |
40 | } |
41 | |
42 | lex::token_def<int> a; |
43 | }; |
44 | |
45 | struct catch_result |
46 | { |
47 | template <class Token> |
48 | bool operator() (Token const& x) const |
49 | { |
50 | BOOST_TEST(x.value().which() == 1); |
51 | BOOST_TEST(boost::get<int>(x.value()) == 9409); // 9409 == 'a' * 'a' |
52 | return true; |
53 | } |
54 | }; |
55 | |
56 | /////////////////////////////////////////////////////////////////////////////// |
57 | int main() |
58 | { |
59 | typedef lex::lexertl::token<std::string::iterator |
60 | , boost::mpl::vector<int> > token_type; |
61 | |
62 | std::string in = "a"; |
63 | std::string::iterator first(in.begin()); |
64 | |
65 | test_tokens<lex::lexertl::actor_lexer<token_type> > the_lexer; |
66 | BOOST_TEST(lex::tokenize(first, in.end(), the_lexer, catch_result())); |
67 | |
68 | return boost::report_errors(); |
69 | } |
70 |