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