1 // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/metaparse/grammar.hpp>
7 #include <boost/metaparse/lit_c.hpp>
8 #include <boost/metaparse/get_result.hpp>
9 #include <boost/metaparse/start.hpp>
10 #include <boost/metaparse/is_error.hpp>
11 #include <boost/metaparse/string.hpp>
12
13 #include "test_case.hpp"
14
15 #include <boost/mpl/apply_wrap.hpp>
16 #include <boost/mpl/equal_to.hpp>
17 #include <boost/mpl/equal.hpp>
18 #include <boost/mpl/vector.hpp>
19 #include <boost/mpl/char.hpp>
20 #include <boost/mpl/assert.hpp>
21
22 using boost::mpl::char_;
23
24 namespace
25 {
26 struct next_char
27 {
28 typedef next_char type;
29
30 template <class C>
31 struct apply
32 {
33 typedef char_<C::type::value + 1> type;
34 };
35 };
36 }
37
BOOST_METAPARSE_TEST_CASE(grammar)38 BOOST_METAPARSE_TEST_CASE(grammar)
39 {
40 using boost::metaparse::grammar;
41 using boost::metaparse::lit_c;
42 using boost::metaparse::get_result;
43 using boost::metaparse::start;
44 using boost::metaparse::is_error;
45 using boost::metaparse::string;
46
47 using boost::mpl::apply_wrap2;
48 using boost::mpl::equal_to;
49 using boost::mpl::equal;
50 using boost::mpl::vector;
51
52 // import
53 BOOST_MPL_ASSERT((
54 equal_to<
55 char_<'x'>,
56 get_result<
57 apply_wrap2<
58 grammar<>
59 ::import<string<'S'>, lit_c<'x'> >::type,
60 string<'x'>,
61 start
62 >
63 >::type
64 >
65 ));
66
67 // rename_import
68 BOOST_MPL_ASSERT((
69 equal_to<
70 char_<'x'>,
71 get_result<
72 apply_wrap2<
73 grammar<>
74 ::import<string<'I'>, lit_c<'x'> >::type
75 ::rule<string<'S',' ',':',':','=',' ','I'> >::type,
76 string<'x'>,
77 start
78 >
79 >::type
80 >
81 ));
82
83 // char
84 BOOST_MPL_ASSERT((
85 equal_to<
86 char_<'x'>,
87 get_result<
88 apply_wrap2<
89 grammar<>
90 ::rule<string<'S',' ',':',':','=',' ','\'','x','\''> >::type,
91 string<'x'>,
92 start
93 >
94 >::type
95 >
96 ));
97
98 // char_failure
99 BOOST_MPL_ASSERT((
100 is_error<
101 apply_wrap2<
102 grammar<>
103 ::rule<string<'S',' ',':',':','=',' ','\'','x','\''> >::type,
104 string<'y'>,
105 start
106 >
107 >
108 ));
109
110 // char_n
111 BOOST_MPL_ASSERT((
112 equal_to<
113 char_<'\n'>,
114 get_result<
115 apply_wrap2<
116 grammar<>
117 ::rule<string<'S',' ',':',':','=',' ','\'','\\','n','\''> >::type,
118 string<'\n'>,
119 start
120 >
121 >::type
122 >
123 ));
124
125 // char_r
126 BOOST_MPL_ASSERT((
127 equal_to<
128 char_<'\r'>,
129 get_result<
130 apply_wrap2<
131 grammar<>
132 ::rule<string<'S',' ',':',':','=',' ','\'','\\','r','\''> >::type,
133 string<'\r'>,
134 start
135 >
136 >::type
137 >
138 ));
139
140 // char_t
141 BOOST_MPL_ASSERT((
142 equal_to<
143 char_<'\t'>,
144 get_result<
145 apply_wrap2<
146 grammar<>
147 ::rule<string<'S',' ',':',':','=',' ','\'','\\','t','\''> >::type,
148 string<'\t'>,
149 start
150 >
151 >::type
152 >
153 ));
154
155 // backslash
156 BOOST_MPL_ASSERT((
157 equal_to<
158 char_<'\\'>,
159 get_result<
160 apply_wrap2<
161 grammar<>
162 ::rule<string<'S',' ',':',':','=',' ','\'','\\','\\','\''> >::type,
163 string<'\\'>,
164 start
165 >
166 >::type
167 >
168 ));
169
170 // char_\'
171 BOOST_MPL_ASSERT((
172 equal_to<
173 char_<'\''>,
174 get_result<
175 apply_wrap2<
176 grammar<>
177 ::rule<string<'S',' ',':',':','=',' ','\'','\\','\'','\''> >::type,
178 string<'\''>,
179 start
180 >
181 >::type
182 >
183 ));
184
185 // rename_rule
186 BOOST_MPL_ASSERT((
187 equal_to<
188 char_<'x'>,
189 get_result<
190 apply_wrap2<
191 grammar<>
192 ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
193 ::rule<string<'S',' ',':',':','=',' ','R'> >::type,
194 string<'x'>,
195 start
196 >
197 >::type
198 >
199 ));
200
201 // sequence
202 BOOST_MPL_ASSERT((
203 equal<
204 vector<char_<'x'>, char_<'x'> >,
205 get_result<
206 apply_wrap2<
207 grammar<>
208 ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
209 ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
210 string<'x','x'>,
211 start
212 >
213 >::type
214 >
215 ));
216
217 // sequence_first_fail
218 BOOST_MPL_ASSERT((
219 is_error<
220 apply_wrap2<
221 grammar<>
222 ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
223 ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
224 string<'y','x'>,
225 start
226 >
227 >
228 ));
229
230 // sequence_second_fail
231 BOOST_MPL_ASSERT((
232 is_error<
233 apply_wrap2<
234 grammar<>
235 ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
236 ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
237 string<'x','y'>,
238 start
239 >
240 >
241 ));
242
243 // selection 1
244 BOOST_MPL_ASSERT((
245 equal_to<
246 char_<'x'>,
247 get_result<
248 apply_wrap2<
249 grammar<>
250 ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
251 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
252 ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
253 string<'x'>,
254 start
255 >
256 >::type
257 >
258 ));
259
260 // selection 2
261 BOOST_MPL_ASSERT((
262 equal_to<
263 char_<'y'>,
264 get_result<
265 apply_wrap2<
266 grammar<>
267 ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
268 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
269 ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
270 string<'y'>,
271 start
272 >
273 >::type
274 >
275 ));
276
277 // selection_fail
278 BOOST_MPL_ASSERT((
279 is_error<
280 apply_wrap2<
281 grammar<>
282 ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
283 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
284 ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
285 string<'z'>,
286 start
287 >
288 >
289 ));
290
291 // repeated_0
292 BOOST_MPL_ASSERT((
293 equal<
294 vector<>,
295 get_result<
296 apply_wrap2<
297 grammar<>
298 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
299 ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
300 string<'y'>,
301 start
302 >
303 >::type
304 >
305 ));
306
307 // repeated_1
308 BOOST_MPL_ASSERT((
309 equal<
310 vector<char_<'x'> >,
311 get_result<
312 apply_wrap2<
313 grammar<>
314 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
315 ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
316 string<'x','y'>,
317 start
318 >
319 >::type
320 >
321 ));
322
323 // repeated_2
324 BOOST_MPL_ASSERT((
325 equal<
326 vector<char_<'x'>, char_<'x'> >,
327 get_result<
328 apply_wrap2<
329 grammar<>
330 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
331 ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
332 string<'x','x','y'>,
333 start
334 >
335 >::type
336 >
337 ));
338
339 // bracket
340 BOOST_MPL_ASSERT((
341 equal_to<
342 char_<'x'>,
343 get_result<
344 apply_wrap2<
345 grammar<>
346 ::rule<
347 string<'S',' ',':',':','=',' ','(','\'','x','\'',')'>
348 >::type,
349 string<'x'>,
350 start
351 >
352 >::type
353 >
354 ));
355
356 // semantic_action
357 BOOST_MPL_ASSERT((
358 equal_to<
359 char_<'y'>,
360 get_result<
361 apply_wrap2<
362 grammar<>
363 ::rule<
364 string<'S',' ',':',':','=',' ','\'','x','\''>,
365 next_char
366 >::type,
367 string<'x'>,
368 start
369 >
370 >::type
371 >
372 ));
373
374 // repeated+_0
375 BOOST_MPL_ASSERT((
376 is_error<
377 apply_wrap2<
378 grammar<>
379 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
380 ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
381 string<'y'>,
382 start
383 >
384 >
385 ));
386
387 // repeated+_1
388 BOOST_MPL_ASSERT((
389 equal<
390 vector<char_<'x'> >,
391 get_result<
392 apply_wrap2<
393 grammar<>
394 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
395 ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
396 string<'x','y'>,
397 start
398 >
399 >::type
400 >
401 ));
402
403 // repeated+_2
404 BOOST_MPL_ASSERT((
405 equal<
406 vector<char_<'x'>, char_<'x'> >,
407 get_result<
408 apply_wrap2<
409 grammar<>
410 ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
411 ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
412 string<'x','x','y'>,
413 start
414 >
415 >::type
416 >
417 ));
418 }
419
420
421