1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2009 Jean-Francois Ostiguy
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)
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/config/warning_disable.hpp>
10 #include <boost/spirit/include/lex_lexertl.hpp>
11 #include <boost/spirit/include/qi.hpp>
17 namespace lex
= boost::spirit::lex
;
18 namespace qi
= boost::spirit::qi
;
19 namespace mpl
= boost::mpl
;
21 template <typename Lexer
>
22 struct my_lexer
: lex::lexer
<Lexer
>
26 delimiter
= "BEGIN|END";
27 identifier
= "[a-zA-Z][_\\.a-zA-Z0-9]*";
29 real
= "([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([-+]?[1-9]+\\.?([eE][-+]?[0-9]+))";
32 this->self
+= ws
[lex::_pass
= lex::pass_flags::pass_ignore
];
33 this->self
+= delimiter
;
34 this->self
+= identifier
;
36 this->self
+= integer
;
42 lex::token_def
<std::string
> identifier
;
43 lex::token_def
<int> integer
;
44 lex::token_def
<double> real
;
45 lex::token_def
<> delimiter
;
48 template <typename Iterator
>
49 struct my_grammar
: qi::grammar
<Iterator
>
51 template <typename TokenDef
>
52 my_grammar( TokenDef
const& tok
)
53 : my_grammar::base_type(statement
)
57 | *(delimiter
| declaration
)
60 delimiter
= tok
.delimiter
>> tok
.identifier
;
61 declaration
= tok
.identifier
>> option
>> ';';
62 option
= *(tok
.identifier
>> '=' >> (tok
.real
| tok
.integer
));
65 qi::rule
<Iterator
> statement
, delimiter
, declaration
, option
;
68 typedef lex::lexertl::token
<char const*
69 , mpl::vector
<std::string
, double, int> > token_type
;
70 typedef lex::lexertl::actor_lexer
<token_type
> lexer_type
;
71 typedef my_lexer
<lexer_type
>::iterator_type iterator_type
;
75 std::string
test_string ("BEGIN section\n");
76 // we introduce a syntax error: ";;" instead of ";" as a terminator.
77 test_string
+= "Identity;;\n"; // this will make the parser fail
78 test_string
+= "END section\n" ;
80 char const* first
= &test_string
[0];
81 char const* last
= &first
[test_string
.size()];
83 my_lexer
<lexer_type
> lexer
;
84 my_grammar
<iterator_type
> grammar(lexer
);
86 BOOST_TEST(lex::tokenize_and_parse(first
, last
, lexer
, grammar
));
87 BOOST_TEST(first
!= last
);
89 return boost::report_errors();