]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/lex/state_switcher.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / state_switcher.cpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
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/detail/lightweight_test.hpp>
7 #include <boost/spirit/include/qi.hpp>
8 #include <boost/spirit/include/lex_lexertl.hpp>
9 #include "test_parser.hpp"
10
11 ///////////////////////////////////////////////////////////////////////////////
12 // Token definition
13 ///////////////////////////////////////////////////////////////////////////////
14 template <typename Lexer>
15 struct switch_state_tokens : boost::spirit::lex::lexer<Lexer>
16 {
17 switch_state_tokens()
18 {
19 // define tokens and associate them with the lexer
20 identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
21 this->self = identifier;
22
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;
27
28 separators = "[,;]";
29 this->self("SEP") = separators;
30 }
31
32 boost::spirit::lex::token_def<> identifier, white_space, separators;
33 };
34
35 ///////////////////////////////////////////////////////////////////////////////
36 int main()
37 {
38 using namespace boost::spirit;
39 using namespace boost::spirit::qi;
40 using namespace spirit_test;
41
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;
45
46 {
47 // the tokens class will be initialized inside the test_parser function
48 switch_state_tokens<lexer_type> lex;
49
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));
53
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));
57 }
58
59 {
60 // the tokens class will be initialized inside the test_parser function
61 switch_state_tokens<lexer_type> lex;
62
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]));
69
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]));
76 }
77
78 {
79 // the tokens class will be initialized inside the test_parser function
80 switch_state_tokens<lexer_type> lex;
81
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));
86 }
87
88 return boost::report_errors();
89 }
90