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