1 /*=============================================================================
2 Copyright (c) 2004 Chris Hoeppler
3 http://spirit.sourceforge.net/
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 =============================================================================*/
10 // This test verifies, that reapeat_p et.al. work correctly while using AST's
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>
21 #include <boost/core/lightweight_test.hpp>
23 using namespace BOOST_SPIRIT_CLASSIC_NS
;
25 static const int numID
= 1;
26 static const int funcID
= 2;
27 static const int expressionID
= 3;
29 struct grammar_fail
: public grammar
<grammar_fail
>
31 template <typename ScannerT
>
34 definition(grammar_fail
const& /*self */)
36 num
= leaf_node_d
[real_p
];
37 func
= root_node_d
[ch_p('+') | '-']
38 >> repeat_p(2)[expression
];
39 expression
= func
| num
;
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
;
45 expr_t
const& start() const { return expression
; }
48 struct grammar_success
: public grammar
<grammar_success
>
50 template <typename ScannerT
>
53 definition(grammar_success
const& /*self */)
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
;
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
;
64 expr_t
const& start() const { return expression
; }
70 std::map
<parser_id
, std::string
> rule_names
;
71 rule_names
[expressionID
] = "expression";
72 rule_names
[funcID
] = "func";
73 rule_names
[numID
] = "num";
75 std::string
test("+ 1 - 2 3");
79 tree_parse_info
<> info1
= ast_parse(test
.c_str(), g_fail
, space_p
);
80 BOOST_TEST(info1
.full
);
82 //std::cout << "...Case 1: Using repeat_p\n";
83 //tree_to_xml(std::cerr, info1.trees, test, rule_names);
86 grammar_success g_success
;
87 tree_parse_info
<> info2
= ast_parse(test
.c_str(), g_success
, space_p
);
88 BOOST_TEST(info2
.full
);
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();