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