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