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