]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/test/repeat_ast_tests.cpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / libs / spirit / classic / test / repeat_ast_tests.cpp
1 /*=============================================================================
2 Copyright (c) 2004 Chris Hoeppler
3 http://spirit.sourceforge.net/
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9
10 // This test verifies, that reapeat_p et.al. work correctly while using AST's
11
12 # include <map>
13 # include <iostream>
14 # include <string>
15
16 # include <boost/spirit/include/classic_core.hpp>
17 # include <boost/spirit/include/classic_loops.hpp>
18 # include <boost/spirit/include/classic_ast.hpp>
19 # include <boost/spirit/include/classic_tree_to_xml.hpp>
20
21 #include <boost/core/lightweight_test.hpp>
22
23 using namespace BOOST_SPIRIT_CLASSIC_NS;
24
25 static const int numID = 1;
26 static const int funcID = 2;
27 static const int expressionID = 3;
28
29 struct grammar_fail : public grammar<grammar_fail>
30 {
31 template <typename ScannerT>
32 struct definition
33 {
34 definition(grammar_fail const& /*self */)
35 {
36 num = leaf_node_d[real_p];
37 func = root_node_d[ch_p('+') | '-']
38 >> repeat_p(2)[expression];
39 expression = func | num;
40 }
41 rule<ScannerT, parser_context<>, parser_tag<numID> > num;
42 rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
43 typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
44 expr_t expression;
45 expr_t const& start() const { return expression; }
46 };
47 };
48 struct grammar_success : public grammar<grammar_success>
49 {
50 template <typename ScannerT>
51 struct definition
52 {
53 definition(grammar_success const& /*self */)
54 {
55 num = leaf_node_d[real_p];
56 func = root_node_d[ch_p('+') | '-']
57 >> expression >> expression; // line differing from grammar_fail
58 expression = func | num;
59 }
60 rule<ScannerT, parser_context<>, parser_tag<numID> > num;
61 rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
62 typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
63 expr_t expression;
64 expr_t const& start() const { return expression; }
65 };
66 };
67
68 int main() {
69
70 std::map<parser_id, std::string> rule_names;
71 rule_names[expressionID] = "expression";
72 rule_names[funcID] = "func";
73 rule_names[numID] = "num";
74
75 std::string test("+ 1 - 2 3");
76
77 // case 1
78 grammar_fail g_fail;
79 tree_parse_info<> info1 = ast_parse(test.c_str(), g_fail, space_p);
80 BOOST_TEST(info1.full);
81
82 //std::cout << "...Case 1: Using repeat_p\n";
83 //tree_to_xml(std::cerr, info1.trees, test, rule_names);
84
85 // case 2
86 grammar_success g_success;
87 tree_parse_info<> info2 = ast_parse(test.c_str(), g_success, space_p);
88 BOOST_TEST(info2.full);
89
90 //std::cout << "...Case 2: No repeat_p\n";
91 //tree_to_xml(std::cerr, info2.trees, test, rule_names);
92 // could be used for test case instead of printing the xml stuff:
93 BOOST_TEST(info2.trees.begin()->children.size() == 2);
94 BOOST_TEST(info1.trees.begin()->children.size() == 2);
95 return boost::report_errors();
96 }