Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/test/repeat_ast_tests.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.1 KB
Line 
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/core.hpp>
18# include <boost/spirit/utility/loops.hpp>
19# include <boost/spirit/tree/ast.hpp>
20# include <boost/spirit/tree/tree_to_xml.hpp>
21
22using namespace boost::spirit;
23
24static const int numID = 1;
25static const int funcID = 2;
26static const int expressionID = 3;
27
28struct grammar_fail : public grammar<grammar_fail>
29{
30    template <typename ScannerT>
31    struct definition
32    {
33        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;
44        expr_t const& start() const { return expression; }
45    };
46};
47struct grammar_success : public grammar<grammar_success>
48{
49    template <typename ScannerT>
50    struct definition
51    {
52        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;
63        expr_t const& start() const { return expression; }
64    };
65};
66
67int 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}
Note: See TracBrowser for help on using the repository browser.