1 /*=============================================================================
2 Copyright (c) 2003 Hartmut Kaiser
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 #include <boost/detail/lightweight_test.hpp>
13 #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6
14 #define BOOST_SPIRIT_SELECT_LIMIT 6
15 #define PHOENIX_LIMIT 6
17 //#define BOOST_SPIRIT_DEBUG
18 #include <boost/mpl/list.hpp>
19 #include <boost/mpl/for_each.hpp>
21 #include <boost/spirit/include/classic_primitives.hpp>
22 #include <boost/spirit/include/classic_numerics.hpp>
23 #include <boost/spirit/include/classic_actions.hpp>
24 #include <boost/spirit/include/classic_operators.hpp>
25 #include <boost/spirit/include/classic_rule.hpp>
26 #include <boost/spirit/include/classic_grammar.hpp>
27 #include <boost/spirit/include/classic_switch.hpp>
28 #include <boost/spirit/include/classic_select.hpp>
29 #include <boost/spirit/include/classic_closure.hpp>
31 using namespace BOOST_SPIRIT_CLASSIC_NS
;
33 namespace test_grammars
{
35 ///////////////////////////////////////////////////////////////////////////////
36 // Test the direct switch_p usage (with default_p)
37 struct switch_grammar_direct_default1
38 : public grammar
<switch_grammar_direct_default1
>
40 template <typename ScannerT
>
43 definition(switch_grammar_direct_default1
const& /*self*/)
47 case_p
<'b'>(ch_p(',')),
48 case_p
<'c'>(str_p("bcd")),
49 default_p(str_p("default"))
54 rule
<ScannerT
> const& start() const { return r
; }
58 struct switch_grammar_direct_default2
59 : public grammar
<switch_grammar_direct_default2
>
61 template <typename ScannerT
>
64 definition(switch_grammar_direct_default2
const& /*self*/)
68 case_p
<'b'>(ch_p(',')),
69 default_p(str_p("default")),
70 case_p
<'c'>(str_p("bcd"))
75 rule
<ScannerT
> const& start() const { return r
; }
79 struct switch_grammar_direct_default3
80 : public grammar
<switch_grammar_direct_default3
>
82 template <typename ScannerT
>
85 definition(switch_grammar_direct_default3
const& /*self*/)
88 default_p(str_p("default")),
90 case_p
<'b'>(ch_p(',')),
91 case_p
<'c'>(str_p("bcd"))
96 rule
<ScannerT
> const& start() const { return r
; }
100 ///////////////////////////////////////////////////////////////////////////////
101 // Test the switch_p usage given a parser as the switch condition
102 struct switch_grammar_parser_default1
103 : public grammar
<switch_grammar_parser_default1
>
105 template <typename ScannerT
>
108 definition(switch_grammar_parser_default1
const& /*self*/)
110 r
= switch_p(anychar_p
) [
112 case_p
<'b'>(ch_p(',')),
113 case_p
<'c'>(str_p("bcd")),
114 default_p(str_p("default"))
119 rule
<ScannerT
> const& start() const { return r
; }
123 struct switch_grammar_parser_default2
124 : public grammar
<switch_grammar_parser_default2
>
126 template <typename ScannerT
>
129 definition(switch_grammar_parser_default2
const& /*self*/)
131 r
= switch_p(anychar_p
) [
133 case_p
<'b'>(ch_p(',')),
134 default_p(str_p("default")),
135 case_p
<'c'>(str_p("bcd"))
140 rule
<ScannerT
> const& start() const { return r
; }
144 struct switch_grammar_parser_default3
145 : public grammar
<switch_grammar_parser_default3
>
147 template <typename ScannerT
>
150 definition(switch_grammar_parser_default3
const& /*self*/)
152 r
= switch_p(anychar_p
) [
153 default_p(str_p("default")),
155 case_p
<'b'>(ch_p(',')),
156 case_p
<'c'>(str_p("bcd"))
161 rule
<ScannerT
> const& start() const { return r
; }
165 ///////////////////////////////////////////////////////////////////////////////
166 // Test the switch_p usage given an actor as the switch condition
167 struct select_result
: public BOOST_SPIRIT_CLASSIC_NS::closure
<select_result
, int>
172 ///////////////////////////////////////////////////////////////////////////
173 struct switch_grammar_actor_default1
174 : public grammar
<switch_grammar_actor_default1
>
176 template <typename ScannerT
>
179 definition(switch_grammar_actor_default1
const& /*self*/)
182 r
= select_p('a', 'b', 'c', 'd')[r
.val
= arg1
] >>
185 case_p
<1>(ch_p(',')),
186 case_p
<2>(str_p("bcd")),
187 default_p(str_p("default"))
191 rule
<ScannerT
, select_result::context_t
> r
;
192 rule
<ScannerT
, select_result::context_t
> const&
193 start() const { return r
; }
197 struct switch_grammar_actor_default2
198 : public grammar
<switch_grammar_actor_default2
>
200 template <typename ScannerT
>
203 definition(switch_grammar_actor_default2
const& /*self*/)
206 r
= select_p('a', 'b', 'c', 'd')[r
.val
= arg1
] >>
209 case_p
<1>(ch_p(',')),
210 default_p(str_p("default")),
211 case_p
<2>(str_p("bcd"))
215 rule
<ScannerT
, select_result::context_t
> r
;
216 rule
<ScannerT
, select_result::context_t
> const&
217 start() const { return r
; }
221 struct switch_grammar_actor_default3
222 : public grammar
<switch_grammar_actor_default3
>
224 template <typename ScannerT
>
227 definition(switch_grammar_actor_default3
const& /*self*/)
230 r
= select_p('a', 'b', 'c', 'd')[r
.val
= arg1
] >>
232 default_p(str_p("default")),
234 case_p
<1>(ch_p(',')),
235 case_p
<2>(str_p("bcd"))
239 rule
<ScannerT
, select_result::context_t
> r
;
240 rule
<ScannerT
, select_result::context_t
> const&
241 start() const { return r
; }
245 } // namespace test_grammars
247 ///////////////////////////////////////////////////////////////////////////////
250 // Tests for known (to the grammars) sequences
251 struct check_grammar_known
{
253 template <typename GrammarT
>
254 void operator()(GrammarT
)
258 BOOST_TEST(parse("a1", g
).full
);
259 BOOST_TEST(!parse("a,", g
).hit
);
260 BOOST_TEST(!parse("abcd", g
).hit
);
262 BOOST_TEST(parse("a 1", g
, space_p
).full
);
263 BOOST_TEST(!parse("a ,", g
, space_p
).hit
);
264 BOOST_TEST(!parse("a bcd", g
, space_p
).hit
);
266 BOOST_TEST(!parse("b1", g
).hit
);
267 BOOST_TEST(parse("b,", g
).full
);
268 BOOST_TEST(!parse("bbcd", g
).hit
);
270 BOOST_TEST(!parse("b 1", g
, space_p
).hit
);
271 BOOST_TEST(parse("b ,", g
, space_p
).full
);
272 BOOST_TEST(!parse("b bcd", g
, space_p
).hit
);
274 BOOST_TEST(!parse("c1", g
).hit
);
275 BOOST_TEST(!parse("c,", g
).hit
);
276 BOOST_TEST(parse("cbcd", g
).full
);
278 BOOST_TEST(!parse("c 1", g
, space_p
).hit
);
279 BOOST_TEST(!parse("c ,", g
, space_p
).hit
);
280 BOOST_TEST(parse("c bcd", g
, space_p
).full
);
284 // Tests for unknown (to the grammar) sequences
285 struct check_grammar_unknown_default
{
287 template <typename GrammarT
>
288 void operator()(GrammarT
)
292 BOOST_TEST(!parse("d1", g
).hit
);
293 BOOST_TEST(!parse("d,", g
).hit
);
294 BOOST_TEST(!parse("dbcd", g
).hit
);
296 BOOST_TEST(!parse("d 1", g
, space_p
).hit
);
297 BOOST_TEST(!parse("d ,", g
, space_p
).hit
);
298 BOOST_TEST(!parse("d bcd", g
, space_p
).hit
);
302 // Tests for the default branches (with parsers) of the grammars
303 struct check_grammar_default
{
305 template <typename GrammarT
>
306 void operator()(GrammarT
)
310 BOOST_TEST(parse("ddefault", g
).full
);
311 BOOST_TEST(parse("d default", g
, space_p
).full
);
320 // Test switch_p parsers containing general default_p(...) case branches
321 typedef boost::mpl::list
<
323 test_grammars::switch_grammar_direct_default1
,
324 test_grammars::switch_grammar_direct_default2
,
325 test_grammars::switch_grammar_direct_default3
,
327 // switch_p(parser) syntax
328 test_grammars::switch_grammar_parser_default1
,
329 test_grammars::switch_grammar_parser_default2
,
330 test_grammars::switch_grammar_parser_default3
,
332 // switch_p(actor) syntax
333 test_grammars::switch_grammar_actor_default1
,
334 test_grammars::switch_grammar_actor_default2
,
335 test_grammars::switch_grammar_actor_default3
338 boost::mpl::for_each
<default_list_t
>(tests::check_grammar_known());
339 boost::mpl::for_each
<default_list_t
>(tests::check_grammar_unknown_default());
340 boost::mpl::for_each
<default_list_t
>(tests::check_grammar_default());
342 return boost::report_errors();