• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2003 Joel de Guzman
3     Copyright (c) 2003 Hartmut Kaiser
4     http://spirit.sourceforge.net/
5 
6     Use, modification and distribution is subject to the Boost Software
7     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8     http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #include <iostream>
11 #include <boost/detail/lightweight_test.hpp>
12 
13 
14 //#define BOOST_SPIRIT_DEBUG
15 #define BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE
16 #include <boost/spirit/include/classic_core.hpp>
17 #include <boost/spirit/include/classic_grammar_def.hpp>
18 using namespace BOOST_SPIRIT_CLASSIC_NS;
19 
20 //////////////////////////////////////////////////////////////////////////////
21 //
22 //  Grammar tests
23 //
24 ///////////////////////////////////////////////////////////////////////////////
25 struct num_list : public grammar<num_list>
26 {
27     enum {
28         default_rule = 0,
29         num_rule = 1
30     };
31 
32     template <typename ScannerT>
33     struct definition
34     :   public grammar_def<rule<ScannerT>, same>
35     {
definitionnum_list::definition36         definition(num_list const& /*self*/)
37         {
38             num = int_p;
39             r = num >> *(',' >> num);
40 
41             this->start_parsers(r, num);
42 
43             BOOST_SPIRIT_DEBUG_RULE(num);
44             BOOST_SPIRIT_DEBUG_RULE(r);
45         }
46 
47         rule<ScannerT> r, num;
48 
startnum_list::definition49         rule<ScannerT> const& start() const { return r; }
50     };
51 };
52 
53 struct num_list_ex : public grammar<num_list_ex>
54 {
55     enum {
56         default_rule = 0,
57         num_rule = 1,
58         integer_rule = 2
59     };
60 
61     template <typename ScannerT>
62     struct definition
63     :   public grammar_def<rule<ScannerT>, same, int_parser<int, 10, 1, -1> >
64     {
definitionnum_list_ex::definition65         definition(num_list_ex const& /*self*/)
66         {
67             num = integer;
68             r = num >> *(',' >> num);
69 
70             this->start_parsers(r, num, integer);
71 
72             BOOST_SPIRIT_DEBUG_RULE(num);
73             BOOST_SPIRIT_DEBUG_RULE(r);
74         }
75 
76         rule<ScannerT> r, num;
77         int_parser<int, 10, 1, -1> integer;
78 
startnum_list_ex::definition79         rule<ScannerT> const& start() const { return r; }
80     };
81 };
82 
83 void
grammar_tests()84 grammar_tests()
85 {
86     num_list nlist;
87     BOOST_SPIRIT_DEBUG_GRAMMAR(nlist);
88 
89     parse_info<char const*> pi;
90     pi = parse("123, 456, 789", nlist, space_p);
91     BOOST_TEST(pi.hit);
92     BOOST_TEST(pi.full);
93 
94     num_list_ex nlistex;
95     BOOST_SPIRIT_DEBUG_GRAMMAR(nlistex);
96 
97     pi = parse("123, 456, 789", nlist.use_parser<num_list::default_rule>(),
98         space_p);
99     BOOST_TEST(pi.hit);
100     BOOST_TEST(pi.full);
101 
102     pi = parse("123", nlist.use_parser<num_list::num_rule>(), space_p);
103     BOOST_TEST(pi.hit);
104     BOOST_TEST(pi.full);
105 
106     pi = parse("123, 456, 789", nlistex, space_p);
107     BOOST_TEST(pi.hit);
108     BOOST_TEST(pi.full);
109 
110     pi = parse("123, 456, 789",
111         nlistex.use_parser<num_list_ex::default_rule>(), space_p);
112     BOOST_TEST(pi.hit);
113     BOOST_TEST(pi.full);
114 
115     pi = parse("123", nlistex.use_parser<num_list_ex::num_rule>(), space_p);
116     BOOST_TEST(pi.hit);
117     BOOST_TEST(pi.full);
118 
119     pi = parse("123", nlistex.use_parser<num_list_ex::integer_rule>(),
120         space_p);
121     BOOST_TEST(pi.hit);
122     BOOST_TEST(pi.full);
123 }
124 
125 ///////////////////////////////////////////////////////////////////////////////
126 //
127 //  Main
128 //
129 ///////////////////////////////////////////////////////////////////////////////
130 int
main()131 main()
132 {
133     grammar_tests();
134     return boost::report_errors();
135 }
136 
137