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