]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/lex/regression_syntax_error.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / regression_syntax_error.cpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2009 Jean-Francois Ostiguy
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 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/config/warning_disable.hpp>
9
10 #include <boost/spirit/include/lex_lexertl.hpp>
11 #include <boost/spirit/include/qi.hpp>
12
13 #include <string>
14 #include <iostream>
15 #include <sstream>
16
17 namespace lex = boost::spirit::lex;
18 namespace qi = boost::spirit::qi;
19 namespace mpl = boost::mpl;
20
21 template <typename Lexer>
22 struct my_lexer : lex::lexer<Lexer>
23 {
24 my_lexer()
25 {
26 delimiter = "BEGIN|END";
27 identifier = "[a-zA-Z][_\\.a-zA-Z0-9]*";
28 ws = "[ \\t\\n]+";
29 real = "([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([-+]?[1-9]+\\.?([eE][-+]?[0-9]+))";
30 integer = "[0-9]+";
31
32 this->self += ws[lex::_pass = lex::pass_flags::pass_ignore];
33 this->self += delimiter;
34 this->self += identifier;
35 this->self += real;
36 this->self += integer;
37 this->self += '=';
38 this->self += ';';
39 }
40
41 lex::token_def<> ws;
42 lex::token_def<std::string> identifier;
43 lex::token_def<int> integer;
44 lex::token_def<double> real;
45 lex::token_def<> delimiter;
46 };
47
48 template <typename Iterator>
49 struct my_grammar : qi::grammar<Iterator>
50 {
51 template <typename TokenDef>
52 my_grammar( TokenDef const& tok )
53 : my_grammar::base_type(statement)
54 {
55 statement
56 = qi::eoi
57 | *(delimiter | declaration)
58 ;
59
60 delimiter = tok.delimiter >> tok.identifier;
61 declaration = tok.identifier >> option >> ';';
62 option = *(tok.identifier >> '=' >> (tok.real | tok.integer));
63 }
64
65 qi::rule<Iterator> statement, delimiter, declaration, option;
66 };
67
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;
72
73 int main()
74 {
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" ;
79
80 char const* first = &test_string[0];
81 char const* last = &first[test_string.size()];
82
83 my_lexer<lexer_type> lexer;
84 my_grammar<iterator_type> grammar(lexer);
85
86 BOOST_TEST(lex::tokenize_and_parse(first, last, lexer, grammar));
87 BOOST_TEST(first != last);
88
89 return boost::report_errors();
90 }
91
92