]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/test/switch_tests_eps_default.cpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / libs / spirit / classic / test / switch_tests_eps_default.cpp
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
11
12 #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6
13 #define BOOST_SPIRIT_SELECT_LIMIT 6
14 #define PHOENIX_LIMIT 6
15
16 //#define BOOST_SPIRIT_DEBUG
17 #include <boost/mpl/list.hpp>
18 #include <boost/mpl/for_each.hpp>
19
20 #include <boost/spirit/include/classic_primitives.hpp>
21 #include <boost/spirit/include/classic_numerics.hpp>
22 #include <boost/spirit/include/classic_actions.hpp>
23 #include <boost/spirit/include/classic_operators.hpp>
24 #include <boost/spirit/include/classic_rule.hpp>
25 #include <boost/spirit/include/classic_grammar.hpp>
26 #include <boost/spirit/include/classic_switch.hpp>
27 #include <boost/spirit/include/classic_select.hpp>
28 #include <boost/spirit/include/classic_closure.hpp>
29
30 #include <boost/core/lightweight_test.hpp>
31
32 using namespace BOOST_SPIRIT_CLASSIC_NS;
33
34 namespace test_grammars {
35
36 ///////////////////////////////////////////////////////////////////////////////
37 // Test the direct switch_p usage (with default_p)
38 struct switch_grammar_direct_default4
39 : public grammar<switch_grammar_direct_default4>
40 {
41 template <typename ScannerT>
42 struct definition
43 {
44 definition(switch_grammar_direct_default4 const& /*self*/)
45 {
46 r = switch_p [(
47 case_p<'a'>(int_p),
48 case_p<'b'>(ch_p(',')),
49 case_p<'c'>(str_p("bcd")),
50 default_p
51 )];
52 }
53
54 rule<ScannerT> r;
55 rule<ScannerT> const& start() const { return r; }
56 };
57 };
58
59 struct switch_grammar_direct_default5
60 : public grammar<switch_grammar_direct_default5>
61 {
62 template <typename ScannerT>
63 struct definition
64 {
65 definition(switch_grammar_direct_default5 const& /*self*/)
66 {
67 r = switch_p [(
68 case_p<'a'>(int_p),
69 case_p<'b'>(ch_p(',')),
70 default_p,
71 case_p<'c'>(str_p("bcd"))
72 )];
73 }
74
75 rule<ScannerT> r;
76 rule<ScannerT> const& start() const { return r; }
77 };
78 };
79
80 struct switch_grammar_direct_default6
81 : public grammar<switch_grammar_direct_default6>
82 {
83 template <typename ScannerT>
84 struct definition
85 {
86 definition(switch_grammar_direct_default6 const& /*self*/)
87 {
88 r = switch_p [(
89 default_p,
90 case_p<'a'>(int_p),
91 case_p<'b'>(ch_p(',')),
92 case_p<'c'>(str_p("bcd"))
93 )];
94 }
95
96 rule<ScannerT> r;
97 rule<ScannerT> const& start() const { return r; }
98 };
99 };
100
101 ///////////////////////////////////////////////////////////////////////////////
102 // Test the switch_p usage given a parser as the switch condition
103 struct switch_grammar_parser_default4
104 : public grammar<switch_grammar_parser_default4>
105 {
106 template <typename ScannerT>
107 struct definition
108 {
109 definition(switch_grammar_parser_default4 const& /*self*/)
110 {
111 r = switch_p(anychar_p) [(
112 case_p<'a'>(int_p),
113 case_p<'b'>(ch_p(',')),
114 case_p<'c'>(str_p("bcd")),
115 default_p
116 )];
117 }
118
119 rule<ScannerT> r;
120 rule<ScannerT> const& start() const { return r; }
121 };
122 };
123
124 struct switch_grammar_parser_default5
125 : public grammar<switch_grammar_parser_default5>
126 {
127 template <typename ScannerT>
128 struct definition
129 {
130 definition(switch_grammar_parser_default5 const& /*self*/)
131 {
132 r = switch_p(anychar_p) [(
133 case_p<'a'>(int_p),
134 case_p<'b'>(ch_p(',')),
135 default_p,
136 case_p<'c'>(str_p("bcd"))
137 )];
138 }
139
140 rule<ScannerT> r;
141 rule<ScannerT> const& start() const { return r; }
142 };
143 };
144
145 struct switch_grammar_parser_default6
146 : public grammar<switch_grammar_parser_default6>
147 {
148 template <typename ScannerT>
149 struct definition
150 {
151 definition(switch_grammar_parser_default6 const& /*self*/)
152 {
153 r = switch_p(anychar_p) [(
154 default_p,
155 case_p<'a'>(int_p),
156 case_p<'b'>(ch_p(',')),
157 case_p<'c'>(str_p("bcd"))
158 )];
159 }
160
161 rule<ScannerT> r;
162 rule<ScannerT> const& start() const { return r; }
163 };
164 };
165
166 ///////////////////////////////////////////////////////////////////////////////
167 // Test the switch_p usage given an actor as the switch condition
168 struct select_result : public BOOST_SPIRIT_CLASSIC_NS::closure<select_result, int>
169 {
170 member1 val;
171 };
172
173 struct switch_grammar_actor_default4
174 : public grammar<switch_grammar_actor_default4>
175 {
176 template <typename ScannerT>
177 struct definition
178 {
179 definition(switch_grammar_actor_default4 const& /*self*/)
180 {
181 using phoenix::arg1;
182 r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
183 switch_p(r.val) [(
184 case_p<0>(int_p),
185 case_p<1>(ch_p(',')),
186 case_p<2>(str_p("bcd")),
187 default_p
188 )];
189 }
190
191 rule<ScannerT, select_result::context_t> r;
192 rule<ScannerT, select_result::context_t> const&
193 start() const { return r; }
194 };
195 };
196
197 struct switch_grammar_actor_default5
198 : public grammar<switch_grammar_actor_default5>
199 {
200 template <typename ScannerT>
201 struct definition
202 {
203 definition(switch_grammar_actor_default5 const& /*self*/)
204 {
205 using phoenix::arg1;
206 r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
207 switch_p(r.val) [(
208 case_p<0>(int_p),
209 case_p<1>(ch_p(',')),
210 default_p,
211 case_p<2>(str_p("bcd"))
212 )];
213 }
214
215 rule<ScannerT, select_result::context_t> r;
216 rule<ScannerT, select_result::context_t> const&
217 start() const { return r; }
218 };
219 };
220
221 struct switch_grammar_actor_default6
222 : public grammar<switch_grammar_actor_default6>
223 {
224 template <typename ScannerT>
225 struct definition
226 {
227 definition(switch_grammar_actor_default6 const& /*self*/)
228 {
229 using phoenix::arg1;
230 r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
231 switch_p(r.val) [(
232 default_p,
233 case_p<0>(int_p),
234 case_p<1>(ch_p(',')),
235 case_p<2>(str_p("bcd"))
236 )];
237 }
238
239 rule<ScannerT, select_result::context_t> r;
240 rule<ScannerT, select_result::context_t> const&
241 start() const { return r; }
242 };
243 };
244
245 } // namespace test_grammars
246
247 ///////////////////////////////////////////////////////////////////////////////
248 namespace tests {
249
250 // Tests for known (to the grammars) sequences
251 struct check_grammar_known {
252
253 template <typename GrammarT>
254 void operator()(GrammarT)
255 {
256 GrammarT g;
257
258 BOOST_TEST(parse("a1", g).full);
259 BOOST_TEST(!parse("a,", g).hit);
260 BOOST_TEST(!parse("abcd", g).hit);
261
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);
265
266 BOOST_TEST(!parse("b1", g).hit);
267 BOOST_TEST(parse("b,", g).full);
268 BOOST_TEST(!parse("bbcd", g).hit);
269
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);
273
274 BOOST_TEST(!parse("c1", g).hit);
275 BOOST_TEST(!parse("c,", g).hit);
276 BOOST_TEST(parse("cbcd", g).full);
277
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);
281 }
282 };
283
284 // Tests for known (to the grammars) sequences
285 // Tests for the default branches (without parsers) of the grammars
286 struct check_grammar_default_plain {
287
288 template <typename GrammarT>
289 void operator()(GrammarT)
290 {
291 GrammarT g;
292
293 BOOST_TEST(parse("d", g).full);
294 BOOST_TEST(parse(" d", g, space_p).full); // JDG 10-18-2005 removed trailing ' ' to
295 // avoid post skip problems
296 }
297 };
298
299 } // namespace tests
300
301 int
302 main()
303 {
304 // Test switch_p parsers containing special (epsilon) default_p case
305 // branches
306 typedef boost::mpl::list<
307 // switch_p syntax
308 test_grammars::switch_grammar_direct_default4,
309 test_grammars::switch_grammar_direct_default5,
310 test_grammars::switch_grammar_direct_default6,
311
312 // switch_p(parser) syntax
313 test_grammars::switch_grammar_parser_default4,
314 test_grammars::switch_grammar_parser_default5,
315 test_grammars::switch_grammar_parser_default6,
316
317 // switch_p(actor) syntax
318 test_grammars::switch_grammar_actor_default4,
319 test_grammars::switch_grammar_actor_default5,
320 test_grammars::switch_grammar_actor_default6
321 > default_epsilon_list_t;
322
323 boost::mpl::for_each<default_epsilon_list_t>(tests::check_grammar_known());
324 boost::mpl::for_each<default_epsilon_list_t>(
325 tests::check_grammar_default_plain());
326
327 return boost::report_errors();
328 }