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