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