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/detail/lightweight_test.hpp>
7 #include <boost/spirit/include/qi.hpp>
8 #include <boost/spirit/include/lex_lexertl.hpp>
9 #include "test_parser.hpp"
11 ///////////////////////////////////////////////////////////////////////////////
13 ///////////////////////////////////////////////////////////////////////////////
14 template <typename Lexer
>
15 struct switch_state_tokens
: boost::spirit::lex::lexer
<Lexer
>
19 // define tokens and associate them with the lexer
20 identifier
= "[a-zA-Z_][a-zA-Z0-9_]*";
21 this->self
= identifier
;
23 // any token definition to be used as the skip parser during parsing
24 // has to be associated with a separate lexer state (here 'WS')
25 white_space
= "[ \\t\\n]+";
26 this->self("WS") = white_space
;
29 this->self("SEP") = separators
;
32 boost::spirit::lex::token_def
<> identifier
, white_space
, separators
;
35 ///////////////////////////////////////////////////////////////////////////////
38 using namespace boost::spirit
;
39 using namespace boost::spirit::qi
;
40 using namespace spirit_test
;
42 typedef std::string::iterator base_iterator_type
;
43 typedef boost::spirit::lex::lexertl::token
<base_iterator_type
> token_type
;
44 typedef boost::spirit::lex::lexertl::lexer
<token_type
> lexer_type
;
47 // the tokens class will be initialized inside the test_parser function
48 switch_state_tokens
<lexer_type
> lex
;
50 BOOST_TEST(test_parser("ident", lex
.identifier
, lex
));
51 BOOST_TEST(!test_parser("ident", set_state("WS") >> lex
.identifier
, lex
));
52 BOOST_TEST(!test_parser("ident", in_state("WS")[lex
.identifier
], lex
));
54 BOOST_TEST(test_parser("\t \n", set_state("WS") >> lex
.white_space
, lex
));
55 BOOST_TEST(test_parser("\t \n", in_state("WS")[lex
.white_space
], lex
));
56 BOOST_TEST(!test_parser("\t \n", lex
.white_space
, lex
));
60 // the tokens class will be initialized inside the test_parser function
61 switch_state_tokens
<lexer_type
> lex
;
63 BOOST_TEST(test_parser(",ident", lex
.identifier
, lex
,
64 in_state("SEP")[lex
.separators
]));
65 BOOST_TEST(!test_parser(";ident", set_state("WS") >> lex
.identifier
,
66 lex
, in_state("SEP")[lex
.separators
]));
67 BOOST_TEST(!test_parser(",ident", in_state("WS")[lex
.identifier
],
68 lex
, in_state("SEP")[lex
.separators
]));
70 BOOST_TEST(test_parser(",\t \n", set_state("WS") >> lex
.white_space
,
71 lex
, in_state("SEP")[lex
.separators
]));
72 BOOST_TEST(test_parser(";\t \n", in_state("WS")[lex
.white_space
],
73 lex
, in_state("SEP")[lex
.separators
]));
74 BOOST_TEST(!test_parser(",\t \n", lex
.white_space
, lex
,
75 in_state("SEP")[lex
.separators
]));
79 // the tokens class will be initialized inside the test_parser function
80 switch_state_tokens
<lexer_type
> lex
;
82 BOOST_TEST(test_parser("ident\t \n",
83 lex
.identifier
>> set_state("WS") >> lex
.white_space
, lex
));
84 BOOST_TEST(test_parser("\t \nident",
85 in_state("WS")[lex
.white_space
] >> lex
.identifier
, lex
));
88 return boost::report_errors();