1 | /*============================================================================= |
---|
2 | Copyright (c) 2003 Hartmut Kaiser |
---|
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 | #include <iostream> |
---|
10 | #include <boost/detail/lightweight_test.hpp> |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6 |
---|
15 | #define BOOST_SPIRIT_SELECT_LIMIT 6 |
---|
16 | #define PHOENIX_LIMIT 6 |
---|
17 | |
---|
18 | //#define BOOST_SPIRIT_DEBUG |
---|
19 | #include <boost/mpl/list.hpp> |
---|
20 | #include <boost/mpl/for_each.hpp> |
---|
21 | |
---|
22 | #include <boost/spirit/core/primitives/primitives.hpp> |
---|
23 | #include <boost/spirit/core/primitives/numerics.hpp> |
---|
24 | #include <boost/spirit/core/composite/actions.hpp> |
---|
25 | #include <boost/spirit/core/composite/operators.hpp> |
---|
26 | #include <boost/spirit/core/non_terminal/rule.hpp> |
---|
27 | #include <boost/spirit/core/non_terminal/grammar.hpp> |
---|
28 | #include <boost/spirit/dynamic/switch.hpp> |
---|
29 | #include <boost/spirit/dynamic/select.hpp> |
---|
30 | #include <boost/spirit/attribute/closure.hpp> |
---|
31 | |
---|
32 | using namespace boost::spirit; |
---|
33 | |
---|
34 | namespace test_grammars { |
---|
35 | |
---|
36 | /////////////////////////////////////////////////////////////////////////////// |
---|
37 | // Test the direct switch_p usage |
---|
38 | struct switch_grammar_direct : public grammar<switch_grammar_direct> |
---|
39 | { |
---|
40 | template <typename ScannerT> |
---|
41 | struct definition |
---|
42 | { |
---|
43 | definition(switch_grammar_direct const& /*self*/) |
---|
44 | { |
---|
45 | r = switch_p [ |
---|
46 | case_p<'a'>(int_p), |
---|
47 | case_p<'b'>(ch_p(',')), |
---|
48 | case_p<'c'>(str_p("bcd")), |
---|
49 | case_p<'d'>(eps_p) |
---|
50 | ]; |
---|
51 | } |
---|
52 | |
---|
53 | rule<ScannerT> r; |
---|
54 | rule<ScannerT> const& start() const { return r; } |
---|
55 | }; |
---|
56 | }; |
---|
57 | |
---|
58 | /////////////////////////////////////////////////////////////////////////////// |
---|
59 | // Test the switch_p usage given a parser as the switch condition |
---|
60 | struct switch_grammar_parser : public grammar<switch_grammar_parser> |
---|
61 | { |
---|
62 | template <typename ScannerT> |
---|
63 | struct definition |
---|
64 | { |
---|
65 | definition(switch_grammar_parser const& /*self*/) |
---|
66 | { |
---|
67 | r = switch_p(anychar_p) [ |
---|
68 | case_p<'a'>(int_p), |
---|
69 | case_p<'b'>(ch_p(',')), |
---|
70 | case_p<'c'>(str_p("bcd")), |
---|
71 | case_p<'d'>(eps_p) |
---|
72 | ]; |
---|
73 | } |
---|
74 | |
---|
75 | rule<ScannerT> r; |
---|
76 | rule<ScannerT> const& start() const { return r; } |
---|
77 | }; |
---|
78 | }; |
---|
79 | |
---|
80 | /////////////////////////////////////////////////////////////////////////////// |
---|
81 | // Test the switch_p usage given an actor as the switch condition |
---|
82 | struct select_result : public boost::spirit::closure<select_result, int> |
---|
83 | { |
---|
84 | member1 val; |
---|
85 | }; |
---|
86 | |
---|
87 | struct switch_grammar_actor : public grammar<switch_grammar_actor> |
---|
88 | { |
---|
89 | template <typename ScannerT> |
---|
90 | struct definition |
---|
91 | { |
---|
92 | definition(switch_grammar_actor const& /*self*/) |
---|
93 | { |
---|
94 | using phoenix::arg1; |
---|
95 | r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >> |
---|
96 | switch_p(r.val) [ |
---|
97 | case_p<0>(int_p), |
---|
98 | case_p<1>(ch_p(',')), |
---|
99 | case_p<2>(str_p("bcd")), |
---|
100 | case_p<3>(eps_p) |
---|
101 | ]; |
---|
102 | } |
---|
103 | |
---|
104 | rule<ScannerT, select_result::context_t> r; |
---|
105 | rule<ScannerT, select_result::context_t> const& |
---|
106 | start() const { return r; } |
---|
107 | }; |
---|
108 | }; |
---|
109 | |
---|
110 | } // namespace test_grammars |
---|
111 | |
---|
112 | /////////////////////////////////////////////////////////////////////////////// |
---|
113 | namespace tests { |
---|
114 | |
---|
115 | // Tests for known (to the grammars) sequences |
---|
116 | struct check_grammar_known { |
---|
117 | |
---|
118 | template <typename GrammarT> |
---|
119 | void operator()(GrammarT) |
---|
120 | { |
---|
121 | GrammarT g; |
---|
122 | |
---|
123 | BOOST_TEST(parse("a1", g).full); |
---|
124 | BOOST_TEST(!parse("a,", g).hit); |
---|
125 | BOOST_TEST(!parse("abcd", g).hit); |
---|
126 | BOOST_TEST(!parse("a", g).hit); |
---|
127 | |
---|
128 | BOOST_TEST(parse("a 1", g, space_p).full); |
---|
129 | BOOST_TEST(!parse("a ,", g, space_p).hit); |
---|
130 | BOOST_TEST(!parse("a bcd", g, space_p).hit); |
---|
131 | BOOST_TEST(!parse("a ", g, space_p).hit); |
---|
132 | |
---|
133 | BOOST_TEST(!parse("b1", g).hit); |
---|
134 | BOOST_TEST(parse("b,", g).full); |
---|
135 | BOOST_TEST(!parse("bbcd", g).hit); |
---|
136 | BOOST_TEST(!parse("b", g).hit); |
---|
137 | |
---|
138 | BOOST_TEST(!parse("b 1", g, space_p).hit); |
---|
139 | BOOST_TEST(parse("b ,", g, space_p).full); |
---|
140 | BOOST_TEST(!parse("b bcd", g, space_p).hit); |
---|
141 | BOOST_TEST(!parse("b ", g, space_p).hit); |
---|
142 | |
---|
143 | BOOST_TEST(!parse("c1", g).hit); |
---|
144 | BOOST_TEST(!parse("c,", g).hit); |
---|
145 | BOOST_TEST(parse("cbcd", g).full); |
---|
146 | BOOST_TEST(!parse("c", g).hit); |
---|
147 | |
---|
148 | BOOST_TEST(!parse("c 1", g, space_p).hit); |
---|
149 | BOOST_TEST(!parse("c ,", g, space_p).hit); |
---|
150 | BOOST_TEST(parse("c bcd", g, space_p).full); |
---|
151 | BOOST_TEST(!parse("c ", g, space_p).hit); |
---|
152 | |
---|
153 | BOOST_TEST(parse("d1", g).hit); |
---|
154 | BOOST_TEST(parse("d,", g).hit); |
---|
155 | BOOST_TEST(parse("dbcd", g).hit); |
---|
156 | BOOST_TEST(parse("d", g).full); |
---|
157 | |
---|
158 | BOOST_TEST(parse("d 1", g, space_p).hit); |
---|
159 | BOOST_TEST(parse("d ,", g, space_p).hit); |
---|
160 | BOOST_TEST(parse("d bcd", g, space_p).hit); |
---|
161 | BOOST_TEST(parse(" d", g, space_p).full); // JDG 10-18-2005 removed trailing ' ' to |
---|
162 | // avoid post skip problems |
---|
163 | |
---|
164 | BOOST_TEST(parse(" a 1 b , c bcd d", *g, space_p).full); |
---|
165 | // JDG 10-18-2005 removed trailing ' ' to avoid post skip problems |
---|
166 | } |
---|
167 | }; |
---|
168 | |
---|
169 | // Tests for unknown (to the grammar) sequences |
---|
170 | struct check_grammar_unknown_default { |
---|
171 | |
---|
172 | template <typename GrammarT> |
---|
173 | void operator()(GrammarT) |
---|
174 | { |
---|
175 | GrammarT g; |
---|
176 | |
---|
177 | BOOST_TEST(!parse("x1", g).hit); |
---|
178 | BOOST_TEST(!parse("x,", g).hit); |
---|
179 | BOOST_TEST(!parse("xbcd", g).hit); |
---|
180 | |
---|
181 | BOOST_TEST(!parse("x 1", g, space_p).hit); |
---|
182 | BOOST_TEST(!parse("x ,", g, space_p).hit); |
---|
183 | BOOST_TEST(!parse("x bcd", g, space_p).hit); |
---|
184 | } |
---|
185 | }; |
---|
186 | |
---|
187 | } // namespace tests |
---|
188 | |
---|
189 | int |
---|
190 | main() |
---|
191 | { |
---|
192 | // Test switch_p without any default_p case branches |
---|
193 | typedef boost::mpl::list< |
---|
194 | test_grammars::switch_grammar_direct, |
---|
195 | test_grammars::switch_grammar_parser, |
---|
196 | test_grammars::switch_grammar_actor |
---|
197 | > grammar_list_t; |
---|
198 | |
---|
199 | boost::mpl::for_each<grammar_list_t>(tests::check_grammar_known()); |
---|
200 | boost::mpl::for_each<grammar_list_t>(tests::check_grammar_unknown_default()); |
---|
201 | |
---|
202 | return boost::report_errors(); |
---|
203 | } |
---|