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