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