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