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