1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #include <boost/mpl/print.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/include/phoenix_object.hpp>
9 #include <boost/spirit/include/phoenix_operator.hpp>
10 #include <boost/spirit/include/lex_lexertl.hpp>
12 ///////////////////////////////////////////////////////////////////////////////
14 ///////////////////////////////////////////////////////////////////////////////
15 template <typename Lexer
>
16 struct switch_state_tokens
: boost::spirit::lex::lexer
<Lexer
>
18 // define tokens and associate them with the lexer
21 namespace phoenix
= boost::phoenix
;
22 using boost::spirit::lex::_state
;
24 identifier
= "[a-zA-Z_][a-zA-Z0-9_]*";
25 this->self
= identifier
[ phoenix::ref(state_
) = _state
];
28 this->self("INT") = integer
[ _state
= "INITIAL" ];
32 boost::spirit::lex::token_def
<> identifier
, integer
;
35 ///////////////////////////////////////////////////////////////////////////////
38 using namespace boost::spirit
;
39 using namespace boost::spirit::lex
;
41 typedef std::string::iterator base_iterator_type
;
42 typedef boost::spirit::lex::lexertl::token
<base_iterator_type
> token_type
;
43 typedef boost::spirit::lex::lexertl::actor_lexer
<token_type
> lexer_type
;
46 switch_state_tokens
<lexer_type
> lex
;
49 // verify whether using _state as an rvalue works
50 std::string
input("abc123");
51 base_iterator_type first
= input
.begin();
52 BOOST_TEST(boost::spirit::lex::tokenize(first
, input
.end(), lex
) &&
53 lex
.state_
== "INITIAL");
56 // verify whether using _state as an lvalue works
57 std::string
input("123abc123");
58 base_iterator_type first
= input
.begin();
59 BOOST_TEST(boost::spirit::lex::tokenize(first
, input
.end(), lex
, "INT") &&
60 lex
.state_
== "INITIAL");
64 return boost::report_errors();