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