]>
Commit | Line | Data |
---|---|---|
1 | # Checking the C++ Features. -*- Autotest -*- | |
2 | ||
3 | # Copyright (C) 2004-2005, 2007-2013 Free Software Foundation, Inc. | |
4 | ||
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | AT_BANNER([[C++ Features.]]) | |
19 | ||
20 | ||
21 | ## --------------------------- ## | |
22 | ## C++ Variant-based Symbols. ## | |
23 | ## --------------------------- ## | |
24 | ||
25 | AT_SETUP([C++ Variant-based Symbols]) | |
26 | ||
27 | AT_KEYWORDS([variant]) | |
28 | ||
29 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1]) | |
30 | # Store strings and integers in a list of strings. | |
31 | AT_DATA_GRAMMAR([list.y], | |
32 | [[%skeleton "lalr1.cc" | |
33 | %define api.value.type variant | |
34 | %define parse.assert | |
35 | %debug | |
36 | ||
37 | %code top | |
38 | { | |
39 | // Get access to stack_symbol_type for the tests. | |
40 | # define private public | |
41 | } | |
42 | %code provides | |
43 | { | |
44 | ]AT_YYLEX_DECLARE[ | |
45 | } | |
46 | ||
47 | %token <int> INT "int" | |
48 | %type < std::list<int> > exp | |
49 | ||
50 | %printer { yyo << $$; } <int> | |
51 | %printer | |
52 | { | |
53 | for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i) | |
54 | { | |
55 | if (i != $$.begin ()) | |
56 | yyo << ", "; | |
57 | yyo << *i; | |
58 | } | |
59 | } < std::list<int> > | |
60 | ||
61 | %code requires { #include <list> } | |
62 | %code { int yylex (yy::parser::semantic_type* yylval); } | |
63 | ||
64 | %% | |
65 | exp: "int" { $$.push_back ($1); } | |
66 | %% | |
67 | ]AT_YYERROR_DEFINE[ | |
68 | ]AT_YYLEX_DEFINE[ | |
69 | ||
70 | int main() | |
71 | { | |
72 | { | |
73 | yy::parser::symbol_type s = yy::parser::make_INT(12); | |
74 | std::cerr << s.value.as<int>() << std::endl; | |
75 | } | |
76 | ||
77 | { | |
78 | yy::parser::symbol_type s = yy::parser::make_INT(123); | |
79 | yy::parser::stack_symbol_type ss(1, s); | |
80 | std::cerr << ss.value.as<int>() << std::endl; | |
81 | } | |
82 | ||
83 | { | |
84 | yy::parser::stack_type st; | |
85 | for (int i = 0; i < 100; ++i) | |
86 | { | |
87 | yy::parser::symbol_type s(yy::parser::make_INT(i)); | |
88 | yy::parser::stack_symbol_type ss(1, s); | |
89 | st.push(ss); | |
90 | } | |
91 | } | |
92 | } | |
93 | ]]) | |
94 | ||
95 | AT_FULL_COMPILE([list]) | |
96 | AT_PARSER_CHECK([./list], 0, [], | |
97 | [12 | |
98 | 123 | |
99 | ]) | |
100 | ||
101 | AT_BISON_OPTION_POPDEFS | |
102 | AT_CLEANUP | |
103 | ||
104 | ||
105 | ## ---------- ## | |
106 | ## Variants. ## | |
107 | ## ---------- ## | |
108 | ||
109 | # AT_TEST([DIRECTIVES]) | |
110 | # --------------------- | |
111 | # Check the support of variants in C++, with the additional DIRECTIVES. | |
112 | m4_pushdef([AT_TEST], | |
113 | [AT_SETUP([Variants $1]) | |
114 | ||
115 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1]) | |
116 | # Store strings and integers in a list of strings. | |
117 | AT_DATA_GRAMMAR([list.y], | |
118 | [[%debug | |
119 | %skeleton "lalr1.cc" | |
120 | %define api.value.type variant | |
121 | ]m4_bpatsubst([$1], [\\n], [ | |
122 | ])[ | |
123 | ||
124 | %code requires // code for the .hh file | |
125 | { | |
126 | #include <list> | |
127 | #include <string> | |
128 | typedef std::list<std::string> strings_type; | |
129 | } | |
130 | ||
131 | %code // code for the .cc file | |
132 | { | |
133 | #include <cstdlib> // abort, getenv | |
134 | #include <iostream> | |
135 | #include <sstream> | |
136 | ||
137 | namespace yy | |
138 | { | |
139 | static]AT_TOKEN_CTOR_IF([[ | |
140 | parser::symbol_type yylex ()]], [[ | |
141 | parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([, | |
142 | parser::location_type* yylloc])[)]])[; | |
143 | } | |
144 | ||
145 | // Printing a list of strings (for %printer). | |
146 | // Koening look up will look into std, since that's an std::list. | |
147 | namespace std | |
148 | { | |
149 | std::ostream& | |
150 | operator<<(std::ostream& o, const strings_type& s) | |
151 | { | |
152 | o << '('; | |
153 | for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i) | |
154 | { | |
155 | if (i != s.begin ()) | |
156 | o << ", "; | |
157 | o << *i; | |
158 | } | |
159 | return o << ')'; | |
160 | } | |
161 | } | |
162 | ||
163 | // Conversion to string. | |
164 | template <typename T> | |
165 | inline | |
166 | std::string | |
167 | string_cast (const T& t) | |
168 | { | |
169 | std::ostringstream o; | |
170 | o << t; | |
171 | return o.str (); | |
172 | } | |
173 | } | |
174 | ||
175 | %token <::std::string> TEXT; | |
176 | %token <int> NUMBER; | |
177 | %token END_OF_FILE 0; | |
178 | ||
179 | %type <::std::string> item; | |
180 | // Using the template type to exercize its parsing. | |
181 | // Starting with :: to ensure we don't output "<::" which starts by the | |
182 | // digraph for the left square bracket. | |
183 | %type <::std::list<std::string>> list result; | |
184 | ||
185 | %printer { yyo << $][$; } | |
186 | <int> <::std::string> <::std::list<std::string>>; | |
187 | %% | |
188 | ||
189 | result: | |
190 | list { std::cout << $][1 << std::endl; } | |
191 | ; | |
192 | ||
193 | list: | |
194 | /* nothing */ { /* Generates an empty string list */ } | |
195 | | list item { std::swap ($][$,$][1); $$.push_back ($][2); } | |
196 | | list error { std::swap ($][$,$][1); } | |
197 | ; | |
198 | ||
199 | item: | |
200 | TEXT { std::swap ($][$,$][1); } | |
201 | | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); } | |
202 | ; | |
203 | %% | |
204 | ]AT_TOKEN_CTOR_IF([], | |
205 | [[#ifdef TWO_STAGE_BUILD | |
206 | # define BUILD(Type, Value) build<Type> () = Value | |
207 | #else | |
208 | # define BUILD(Type, Value) build (Value) | |
209 | #endif | |
210 | ]])[ | |
211 | #define STAGE_MAX 5 | |
212 | namespace yy | |
213 | { | |
214 | static]AT_TOKEN_CTOR_IF([[ | |
215 | parser::symbol_type yylex ()]], [[ | |
216 | parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([, | |
217 | parser::location_type* yylloc])[)]])[ | |
218 | {]AT_LOCATION_IF([ | |
219 | typedef parser::location_type location;])[ | |
220 | static int stage = -1; | |
221 | ++stage; | |
222 | if (stage == STAGE_MAX) | |
223 | {]AT_TOKEN_CTOR_IF([[ | |
224 | return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]], | |
225 | [AT_LOCATION_IF([ | |
226 | *yylloc = location ();])[ | |
227 | return parser::token::END_OF_FILE;]])[ | |
228 | } | |
229 | else if (stage % 2) | |
230 | {]AT_TOKEN_CTOR_IF([[ | |
231 | return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], | |
232 | [[ | |
233 | yylval->BUILD (int, stage);]AT_LOCATION_IF([ | |
234 | *yylloc = location ();])[ | |
235 | return parser::token::NUMBER;]])[ | |
236 | } | |
237 | else | |
238 | {]AT_TOKEN_CTOR_IF([[ | |
239 | return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[ | |
240 | yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([ | |
241 | *yylloc = location ();])[ | |
242 | return parser::token::TEXT;]])[ | |
243 | } | |
244 | abort (); | |
245 | } | |
246 | } | |
247 | ||
248 | ]AT_YYERROR_DEFINE[ | |
249 | ]AT_MAIN_DEFINE[ | |
250 | ]]) | |
251 | ||
252 | AT_FULL_COMPILE([list]) | |
253 | AT_PARSER_CHECK([./list], 0, | |
254 | [(0, 1, 2, 4) | |
255 | ]) | |
256 | ||
257 | AT_BISON_OPTION_POPDEFS | |
258 | AT_CLEANUP | |
259 | ]) | |
260 | ||
261 | AT_TEST([]) | |
262 | AT_TEST([%define parse.assert]) | |
263 | AT_TEST([%locations %define parse.assert]) | |
264 | AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]]) | |
265 | AT_TEST([[%define parse.assert %define api.token.constructor]]) | |
266 | AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]]) | |
267 | AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]]) | |
268 | ||
269 | m4_popdef([AT_TEST]) | |
270 | ||
271 | ||
272 | ## ----------------------- ## | |
273 | ## Doxygen Documentation. ## | |
274 | ## ----------------------- ## | |
275 | ||
276 | m4_define([AT_CHECK_DOXYGEN], | |
277 | [m4_case([$1], | |
278 | [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])], | |
279 | [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])], | |
280 | [m4_fatal([invalid argument: $1])]) | |
281 | AT_SETUP([Doxygen $1 Documentation]) | |
282 | ||
283 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"]) | |
284 | AT_DATA([input.yy], | |
285 | [[%skeleton "lalr1.cc" | |
286 | %locations | |
287 | %debug | |
288 | %% | |
289 | exp: /* empty */; | |
290 | %% | |
291 | ]AT_YYERROR_DEFINE[ | |
292 | ]]) | |
293 | ||
294 | AT_BISON_CHECK([-o input.cc input.yy], 0) | |
295 | ||
296 | AT_DATA([Doxyfile], | |
297 | [# The PROJECT_NAME tag is a single word (or a sequence of words | |
298 | # surrounded by quotes) that should identify the project. | |
299 | PROJECT_NAME = "Bison C++ Parser" | |
300 | ||
301 | # The QUIET tag can be used to turn on/off the messages that are | |
302 | # generated by doxygen. Possible values are YES and NO. If left blank | |
303 | # NO is used. | |
304 | QUIET = YES | |
305 | ||
306 | # The WARNINGS tag can be used to turn on/off the warning messages | |
307 | # that are generated by doxygen. Possible values are YES and NO. If | |
308 | # left blank NO is used. | |
309 | WARNINGS = YES | |
310 | # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate | |
311 | # warnings for undocumented members. If EXTRACT_ALL is set to YES then | |
312 | # this flag will automatically be disabled. | |
313 | WARN_IF_UNDOCUMENTED = YES | |
314 | # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings | |
315 | # for potential errors in the documentation, such as not documenting | |
316 | # some parameters in a documented function, or documenting parameters | |
317 | # that don't exist or using markup commands wrongly. | |
318 | WARN_IF_DOC_ERROR = YES | |
319 | # The WARN_FORMAT tag determines the format of the warning messages | |
320 | # that doxygen can produce. The string should contain the $file, | |
321 | # $line, and $text tags, which will be replaced by the file and line | |
322 | # number from which the warning originated and the warning text. | |
323 | WARN_FORMAT = "$file:$line: $text" | |
324 | ||
325 | # If the EXTRACT_ALL tag is set to YES doxygen will assume all | |
326 | # entities in documentation are documented, even if no documentation | |
327 | # was available. Private class members and static file members will | |
328 | # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set | |
329 | # to YES | |
330 | EXTRACT_ALL = YES | |
331 | ||
332 | # If the EXTRACT_PRIVATE tag is set to YES all private members of a | |
333 | # class will be included in the documentation. | |
334 | EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE | |
335 | ||
336 | # If the EXTRACT_STATIC tag is set to YES all static members of a file | |
337 | # will be included in the documentation. | |
338 | EXTRACT_STATIC = AT_DOXYGEN_PRIVATE | |
339 | ]) | |
340 | ||
341 | AT_CHECK([doxygen --version || exit 77], 0, ignore) | |
342 | AT_CHECK([doxygen], 0, [], [ignore]) | |
343 | ||
344 | AT_BISON_OPTION_POPDEFS | |
345 | AT_CLEANUP | |
346 | ||
347 | m4_popdef([AT_DOXYGEN_PRIVATE]) | |
348 | ])# AT_CHECK_DOXYGEN | |
349 | ||
350 | AT_CHECK_DOXYGEN([Public]) | |
351 | AT_CHECK_DOXYGEN([Private]) | |
352 | ||
353 | ||
354 | ## ------------ ## | |
355 | ## Namespaces. ## | |
356 | ## ------------ ## | |
357 | ||
358 | # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR]) | |
359 | # ---------------------------------------- | |
360 | # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR | |
361 | # is specified, then Bison should accept the input, but compilation will fail, | |
362 | # so don't check compilation. | |
363 | m4_pushdef([AT_TEST], | |
364 | [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"]) | |
365 | AT_DATA_GRAMMAR([[input.y]], | |
366 | [[%language "C++" | |
367 | %define api.namespace "]$1[" | |
368 | %union { int i; } | |
369 | %define global_tokens_and_yystype | |
370 | %locations | |
371 | ||
372 | %code { | |
373 | // YYSTYPE contains a namespace reference. | |
374 | int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) { | |
375 | lval->i = 3; | |
376 | return 0; | |
377 | } | |
378 | } | |
379 | ||
380 | %% | |
381 | ||
382 | start: ; | |
383 | ||
384 | %% | |
385 | ||
386 | void | |
387 | ]$1[::parser::error (const ]$1[::parser::location_type &loc, | |
388 | const std::string &msg) | |
389 | { | |
390 | std::cerr << "At " << loc << ": " << msg << std::endl; | |
391 | } | |
392 | ||
393 | ]AT_MAIN_DEFINE[ | |
394 | ]]) | |
395 | ||
396 | ||
397 | AT_BISON_CHECK([[-o input.cc input.y]]) | |
398 | ||
399 | m4_if([$#], [1], | |
400 | [AT_COMPILE_CXX([[input]], [[input.cc]]) | |
401 | AT_PARSER_CHECK([[./input]])]) | |
402 | AT_BISON_OPTION_POPDEFS | |
403 | ]) | |
404 | ||
405 | AT_SETUP([[Relative namespace references]]) | |
406 | AT_TEST([[foo]]) | |
407 | AT_TEST([[foo::bar]]) | |
408 | AT_TEST([[foo::bar::baz]]) | |
409 | AT_CLEANUP | |
410 | ||
411 | AT_SETUP([[Absolute namespace references]]) | |
412 | AT_TEST([[::foo]]) | |
413 | AT_TEST([[::foo::bar]]) | |
414 | AT_TEST([[::foo::bar::baz]]) | |
415 | AT_TEST([[@tb@::foo]]) | |
416 | AT_TEST([[ @tb@ ::foo::bar]]) | |
417 | AT_TEST([[ ::foo::bar::baz]]) | |
418 | AT_CLEANUP | |
419 | ||
420 | AT_SETUP([[Syntactically invalid namespace references]]) | |
421 | AT_TEST([[:foo:bar]], [[-]]) | |
422 | AT_TEST([[foo: :bar]], [[-]]) | |
423 | # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which | |
424 | # contains single occurrences of `:'. | |
425 | AT_TEST([[foo[3]::bar::baz]], [[-]]) | |
426 | AT_TEST([[foo::bar,baz]], [[-]]) | |
427 | AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]]) | |
428 | AT_CLEANUP | |
429 | ||
430 | m4_popdef([AT_TEST]) | |
431 | ||
432 | ## -------------------------------------- ## | |
433 | ## Syntax error discarding no lookahead. ## | |
434 | ## -------------------------------------- ## | |
435 | ||
436 | # After a syntax error, lalr1.cc used to not check whether there | |
437 | # actually is a lookahead before discarding the lookahead. As a result, | |
438 | # it mistakenly invoked the destructor for the previous lookahead. | |
439 | ||
440 | AT_SETUP([[Syntax error discarding no lookahead]]) | |
441 | ||
442 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"]) | |
443 | ||
444 | AT_DATA_GRAMMAR([[input.yy]], | |
445 | [[%skeleton "lalr1.cc" | |
446 | ||
447 | %code { | |
448 | #include <string> | |
449 | int yylex (yy::parser::semantic_type *); | |
450 | #define USE(Args) | |
451 | } | |
452 | ||
453 | %define parse.error verbose | |
454 | ||
455 | %nonassoc 'a' ; | |
456 | ||
457 | %destructor { | |
458 | std::cerr << "Discarding 'a'." << std::endl; | |
459 | } 'a' | |
460 | ||
461 | %% | |
462 | ||
463 | start: error-reduce consistent-error 'a' { USE ($3); }; | |
464 | ||
465 | error-reduce: | |
466 | 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); } | |
467 | | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); } | |
468 | ; | |
469 | ||
470 | consistent-error: | |
471 | 'a' | |
472 | | /*empty*/ %prec 'a' | |
473 | ; | |
474 | ||
475 | // Provide another context in which all rules are useful so that this | |
476 | // test case looks a little more realistic. | |
477 | start: 'b' consistent-error ; | |
478 | ||
479 | %% | |
480 | ||
481 | int | |
482 | yylex (yy::parser::semantic_type *) | |
483 | { | |
484 | static char const *input = "aa"; | |
485 | return *input++; | |
486 | } | |
487 | ||
488 | void | |
489 | yy::parser::error (const std::string &m) | |
490 | { | |
491 | std::cerr << m << std::endl; | |
492 | } | |
493 | ||
494 | ]AT_MAIN_DEFINE[ | |
495 | ]]) | |
496 | ||
497 | AT_BISON_CHECK([[-o input.cc input.yy]]) | |
498 | AT_COMPILE_CXX([[input]]) | |
499 | # This used to print "Discarding 'a'." again at the end. | |
500 | AT_PARSER_CHECK([[./input]], [[1]], [[]], | |
501 | [[syntax error | |
502 | Discarding 'a'. | |
503 | Reducing 'a'. | |
504 | ]]) | |
505 | ||
506 | AT_BISON_OPTION_POPDEFS | |
507 | AT_CLEANUP | |
508 | ||
509 | ||
510 | ## --------------------------- ## | |
511 | ## Syntax error as exception. ## | |
512 | ## --------------------------- ## | |
513 | ||
514 | AT_SETUP([[Syntax error as exception]]) | |
515 | ||
516 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"]) | |
517 | ||
518 | AT_DATA_GRAMMAR([[input.yy]], | |
519 | [[%skeleton "lalr1.cc" | |
520 | ||
521 | %code | |
522 | { | |
523 | #include <cstdlib> | |
524 | int yylex (yy::parser::semantic_type *); | |
525 | } | |
526 | ||
527 | %define api.value.type variant | |
528 | %define parse.error verbose | |
529 | %define parse.trace | |
530 | %% | |
531 | ||
532 | start: | |
533 | thing | |
534 | | start thing | |
535 | ; | |
536 | ||
537 | thing: | |
538 | error { std::cerr << "caught error" << std::endl; } | |
539 | | item | |
540 | ; | |
541 | ||
542 | item: | |
543 | 'a' | |
544 | | 's' | |
545 | { | |
546 | throw yy::parser::syntax_error ("invalid expression"); | |
547 | } | |
548 | ||
549 | %% | |
550 | ||
551 | int | |
552 | yylex (yy::parser::semantic_type *) | |
553 | { | |
554 | // 's': syntax error, 'l': lexical error. | |
555 | static char const *input = "asal"; | |
556 | switch (int res = *input++) | |
557 | { | |
558 | case 'l': | |
559 | throw yy::parser::syntax_error ("invalid character"); | |
560 | default: | |
561 | return res; | |
562 | } | |
563 | } | |
564 | ||
565 | void | |
566 | yy::parser::error (const std::string &m) | |
567 | { | |
568 | std::cerr << "error: " << m << std::endl; | |
569 | } | |
570 | ]AT_MAIN_DEFINE[ | |
571 | ]]) | |
572 | ||
573 | AT_BISON_CHECK([[-o input.cc input.yy]]) | |
574 | AT_COMPILE_CXX([[input]]) | |
575 | ||
576 | AT_PARSER_CHECK([[./input]], [[0]], [[]], | |
577 | [[error: invalid expression | |
578 | caught error | |
579 | error: invalid character | |
580 | caught error | |
581 | ]]) | |
582 | ||
583 | AT_BISON_OPTION_POPDEFS | |
584 | AT_CLEANUP | |
585 | ||
586 | ||
587 | ## ------------------ ## | |
588 | ## Exception safety. ## | |
589 | ## ------------------ ## | |
590 | ||
591 | # AT_TEST([BISON-DIRECTIVES]) | |
592 | # --------------------------- | |
593 | # Check that no object is leaked when exceptions are thrown. | |
594 | m4_pushdef([AT_TEST], | |
595 | [AT_SETUP([[Exception safety $1]]) | |
596 | ||
597 | AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1]) | |
598 | ||
599 | AT_DATA_GRAMMAR([[input.yy]], | |
600 | [[%skeleton "lalr1.cc" | |
601 | %debug | |
602 | %error-verbose | |
603 | $1 | |
604 | %code requires | |
605 | { | |
606 | #include <cassert> | |
607 | #include <cstdlib> // size_t and getenv. | |
608 | #include <iostream> | |
609 | #include <list> | |
610 | ||
611 | bool debug = false; | |
612 | ||
613 | /// A class that counts its number of instances. | |
614 | struct Object | |
615 | { | |
616 | typedef std::list<const Object*> objects; | |
617 | static objects instances; | |
618 | char val; | |
619 | ||
620 | static bool | |
621 | empty () | |
622 | { | |
623 | return instances.empty(); | |
624 | } | |
625 | ||
626 | static void | |
627 | log (Object const *o, const std::string& msg) | |
628 | { | |
629 | if (debug) | |
630 | { | |
631 | if (o) | |
632 | std::cerr << o << "->"; | |
633 | std::cerr << msg << " {"; | |
634 | const char* sep = " "; | |
635 | for (objects::const_iterator i = instances.begin(), | |
636 | i_end = instances.end(); | |
637 | i != i_end; | |
638 | ++i) | |
639 | { | |
640 | std::cerr << sep << *i; | |
641 | sep = ", "; | |
642 | } | |
643 | std::cerr << " }" << std::endl; | |
644 | } | |
645 | } | |
646 | ||
647 | Object (char v) | |
648 | : val (v) | |
649 | { | |
650 | instances.push_back(this); | |
651 | log (this, "Object::Object"); | |
652 | } | |
653 | ||
654 | Object () | |
655 | : val ('?') | |
656 | { | |
657 | instances.push_back(this); | |
658 | log (this, "Object::Object"); | |
659 | } | |
660 | ||
661 | ~Object () | |
662 | { | |
663 | instances.remove(this); | |
664 | log (this, "Object::~Object"); | |
665 | } | |
666 | }; | |
667 | } | |
668 | ||
669 | %code | |
670 | { | |
671 | #include <cassert> | |
672 | #include <cstring> // strchr | |
673 | #include <stdexcept> | |
674 | int yylex (yy::parser::semantic_type *); | |
675 | Object::objects Object::instances; | |
676 | static char const *input; | |
677 | } | |
678 | ||
679 | ]AT_VARIANT_IF([[ | |
680 | %printer | |
681 | { | |
682 | yyo << &$$ << " '" << $$.val << '\''; | |
683 | if ($$.val == 'p') | |
684 | throw std::runtime_error ("printer"); | |
685 | } <Object>; | |
686 | ||
687 | %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T' | |
688 | %type <Object> list item | |
689 | ]], [[ | |
690 | %union | |
691 | { | |
692 | Object *obj; | |
693 | } | |
694 | %destructor { delete $$; } <obj>; | |
695 | %printer | |
696 | { | |
697 | yyo << $$ << " '" << $$->val << '\''; | |
698 | if ($$->val == 'p') | |
699 | throw std::runtime_error ("printer"); | |
700 | } <obj>; | |
701 | ||
702 | %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T' | |
703 | %type <obj> list item | |
704 | ]])[ | |
705 | ||
706 | %initial-action | |
707 | { | |
708 | if (strchr (input, 'i')) | |
709 | throw std::runtime_error ("initial-action"); | |
710 | } | |
711 | ||
712 | %% | |
713 | ||
714 | start: list {]AT_VARIANT_IF([], [ delete $][1]; )[}; | |
715 | ||
716 | list: | |
717 | item { $][$ = $][1; } | |
718 | // Right recursion to load the stack. | |
719 | | item list { $][$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[} | |
720 | ; | |
721 | ||
722 | item: | |
723 | 'a' { $$][ = $][1; } | |
724 | | 'e' { YYUSE ($][$); YYUSE($][1); error ("syntax error"); } | |
725 | // Not just 'E', otherwise we reduce when 'E' is the lookahead, and | |
726 | // then the stack is emptied, defeating the point of the test. | |
727 | | 'E' 'a' { YYUSE($][1); $][$ = $][2; } | |
728 | | 'R' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYERROR; } | |
729 | | 'p' { $][$ = $][1; } | |
730 | | 's' { $][$ = $][1; throw std::runtime_error ("reduction"); } | |
731 | | 'T' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYABORT; } | |
732 | | error { $][$ = YY_NULL; yyerrok; } | |
733 | ; | |
734 | %% | |
735 | ||
736 | int | |
737 | yylex (yy::parser::semantic_type *lvalp) | |
738 | { | |
739 | // 'a': no error. | |
740 | // 'e': user action calls error. | |
741 | // 'E': syntax error, with yyerror that throws. | |
742 | // 'i': initial action throws. | |
743 | // 'l': yylex throws. | |
744 | // 'R': call YYERROR in the action | |
745 | // 's': reduction throws. | |
746 | // 'T': call YYABORT in the action | |
747 | switch (int res = *input++) | |
748 | { | |
749 | case 'l': | |
750 | throw std::runtime_error ("yylex"); | |
751 | default: | |
752 | lvalp]AT_VARIANT_IF([->build (res)], [->obj = new Object (res)])[; | |
753 | // Fall through. | |
754 | case 0: | |
755 | return res; | |
756 | } | |
757 | } | |
758 | ||
759 | /* A C++ error reporting function. */ | |
760 | void | |
761 | yy::parser::error (const std::string& m) | |
762 | { | |
763 | throw std::runtime_error (m); | |
764 | } | |
765 | ||
766 | int | |
767 | main (int argc, const char *argv[]) | |
768 | { | |
769 | switch (argc) | |
770 | { | |
771 | case 2: | |
772 | input = argv[1]; | |
773 | break; | |
774 | case 3: | |
775 | assert (std::string(argv[1]) == "--debug"); | |
776 | debug = 1; | |
777 | input = argv[2]; | |
778 | break; | |
779 | default: | |
780 | abort (); | |
781 | } | |
782 | ||
783 | yy::parser parser; | |
784 | debug |= !!getenv ("YYDEBUG"); | |
785 | parser.set_debug_level (debug); | |
786 | int res = 2; | |
787 | try | |
788 | { | |
789 | res = parser.parse (); | |
790 | } | |
791 | catch (const std::exception& e) | |
792 | { | |
793 | std::cerr << "exception caught: " << e.what () << std::endl; | |
794 | } | |
795 | catch (...) | |
796 | { | |
797 | std::cerr << "unknown exception caught" << std::endl; | |
798 | } | |
799 | Object::log (YY_NULL, "end"); | |
800 | assert (Object::empty()); | |
801 | return res; | |
802 | } | |
803 | ]]) | |
804 | AT_BISON_CHECK([[-o input.cc --report=all input.yy]]) | |
805 | AT_COMPILE_CXX([[input]]) | |
806 | ||
807 | AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]], | |
808 | [[exception caught: reduction | |
809 | ]]) | |
810 | ||
811 | AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]], | |
812 | [[exception caught: yylex | |
813 | ]]) | |
814 | ||
815 | AT_PARSER_CHECK([[./input i]], [[2]], [[]], | |
816 | [[exception caught: initial-action | |
817 | ]]) | |
818 | ||
819 | AT_PARSER_CHECK([[./input aaaap]]) | |
820 | ||
821 | AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]]) | |
822 | AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore]) | |
823 | ||
824 | AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]], | |
825 | [[exception caught: syntax error | |
826 | ]]) | |
827 | ||
828 | AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]], | |
829 | [[exception caught: syntax error, unexpected $end, expecting 'a' | |
830 | ]]) | |
831 | ||
832 | AT_PARSER_CHECK([[./input aaaaT]], [[1]]) | |
833 | ||
834 | # There is error-recovery, so exit success. | |
835 | AT_PARSER_CHECK([[./input aaaaR]], [[0]]) | |
836 | ||
837 | AT_BISON_OPTION_POPDEFS | |
838 | ||
839 | AT_CLEANUP | |
840 | ]) | |
841 | ||
842 | AT_TEST | |
843 | AT_TEST([%define api.value.type variant]) | |
844 | ||
845 | m4_popdef([AT_TEST]) | |
846 | ||
847 | ## ------------------------------------ ## | |
848 | ## C++ GLR parser identifier shadowing ## | |
849 | ## ------------------------------------ ## | |
850 | ||
851 | AT_SETUP([[C++ GLR parser identifier shadowing]]) | |
852 | ||
853 | AT_DATA_GRAMMAR([input.yy], [ | |
854 | %skeleton "glr.cc" | |
855 | ||
856 | %union | |
857 | { | |
858 | int ival; | |
859 | } | |
860 | ||
861 | %token <ival> ZERO; | |
862 | ||
863 | %code | |
864 | { | |
865 | int yylex (yy::parser::semantic_type *yylval); | |
866 | } | |
867 | ||
868 | %% | |
869 | exp: ZERO | |
870 | ||
871 | %% | |
872 | ||
873 | int yylex (yy::parser::semantic_type *yylval) | |
874 | { | |
875 | // Note: this argument is unused, but named on purpose. There used to be a | |
876 | // bug with a macro that erroneously expanded this identifier to | |
877 | // yystackp->yyval. | |
878 | YYUSE (yylval); | |
879 | return yy::parser::token::ZERO; | |
880 | } | |
881 | ||
882 | void yy::parser::error (std::string const&) | |
883 | {} | |
884 | ||
885 | int main() | |
886 | {} | |
887 | ]) | |
888 | ||
889 | AT_BISON_CHECK([[-o input.cc input.yy]]) | |
890 | AT_COMPILE_CXX([[input]]) | |
891 | ||
892 | AT_CLEANUP |