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