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