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 <boost/detail/lightweight_test.hpp>
14 # include <iostream>
15 # include <string>
16
17 # include <boost/spirit/include/classic_core.hpp>
18 # include <boost/spirit/include/classic_loops.hpp>
19 # include <boost/spirit/include/classic_ast.hpp>
20 # include <boost/spirit/include/classic_tree_to_xml.hpp>
21
22 using namespace BOOST_SPIRIT_CLASSIC_NS;
23
24 static const int numID = 1;
25 static const int funcID = 2;
26 static const int expressionID = 3;
27
28 struct grammar_fail : public grammar<grammar_fail>
29 {
30 template <typename ScannerT>
31 struct definition
32 {
definitiongrammar_fail::definition33 definition(grammar_fail const& /*self */)
34 {
35 num = leaf_node_d[real_p];
36 func = root_node_d[ch_p('+') | '-']
37 >> repeat_p(2)[expression];
38 expression = func | num;
39 }
40 rule<ScannerT, parser_context<>, parser_tag<numID> > num;
41 rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
42 typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
43 expr_t expression;
startgrammar_fail::definition44 expr_t const& start() const { return expression; }
45 };
46 };
47 struct grammar_success : public grammar<grammar_success>
48 {
49 template <typename ScannerT>
50 struct definition
51 {
definitiongrammar_success::definition52 definition(grammar_success const& /*self */)
53 {
54 num = leaf_node_d[real_p];
55 func = root_node_d[ch_p('+') | '-']
56 >> expression >> expression; // line differing from grammar_fail
57 expression = func | num;
58 }
59 rule<ScannerT, parser_context<>, parser_tag<numID> > num;
60 rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
61 typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
62 expr_t expression;
startgrammar_success::definition63 expr_t const& start() const { return expression; }
64 };
65 };
66
main()67 int main() {
68
69 std::map<parser_id, std::string> rule_names;
70 rule_names[expressionID] = "expression";
71 rule_names[funcID] = "func";
72 rule_names[numID] = "num";
73
74 std::string test("+ 1 - 2 3");
75
76 // case 1
77 grammar_fail g_fail;
78 tree_parse_info<> info1 = ast_parse(test.c_str(), g_fail, space_p);
79 BOOST_TEST(info1.full);
80
81 //std::cout << "...Case 1: Using repeat_p\n";
82 //tree_to_xml(std::cerr, info1.trees, test, rule_names);
83
84 // case 2
85 grammar_success g_success;
86 tree_parse_info<> info2 = ast_parse(test.c_str(), g_success, space_p);
87 BOOST_TEST(info2.full);
88
89 //std::cout << "...Case 2: No repeat_p\n";
90 //tree_to_xml(std::cerr, info2.trees, test, rule_names);
91 // could be used for test case instead of printing the xml stuff:
92 BOOST_TEST(info2.trees.begin()->children.size() == 2);
93 BOOST_TEST(info1.trees.begin()->children.size() == 2);
94 return boost::report_errors();
95 }
96