]> git.saurik.com Git - bison.git/blob - tests/c++.at
symbol: provide an easy means to compare them in source order
[bison.git] / tests / c++.at
1 # Checking the C++ Features. -*- Autotest -*-
2
3 # Copyright (C) 2004-2005, 2007-2013 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 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
77 AT_FULL_COMPILE([input])
78 AT_PARSER_CHECK([./input], 0)
79 AT_BISON_OPTION_POPDEFS
80 AT_CLEANUP
81
82
83 ## --------------------------- ##
84 ## C++ Variant-based Symbols. ##
85 ## --------------------------- ##
86
87 AT_SETUP([C++ Variant-based Symbols])
88
89 AT_KEYWORDS([variant])
90
91 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
92 # Store strings and integers in a list of strings.
93 AT_DATA_GRAMMAR([list.y],
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 %%
127 exp: "int" { $$.push_back ($1); }
128 %%
129 ]AT_YYERROR_DEFINE[
130 ]AT_YYLEX_DEFINE[
131
132 int 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
157 AT_FULL_COMPILE([list])
158 AT_PARSER_CHECK([./list], 0, [],
159 [12
160 123
161 ])
162
163 AT_BISON_OPTION_POPDEFS
164 AT_CLEANUP
165
166
167 ## ---------- ##
168 ## Variants. ##
169 ## ---------- ##
170
171 # AT_TEST([DIRECTIVES])
172 # ---------------------
173 # Check the support of variants in C++, with the additional DIRECTIVES.
174 m4_pushdef([AT_TEST],
175 [AT_SETUP([Variants $1])
176
177 AT_BISON_OPTION_PUSHDEFS([%debug $1])
178 # Store strings and integers in a list of strings.
179 AT_DATA_GRAMMAR([list.y],
180 [[%debug
181 %define api.value.type variant
182 ]m4_bpatsubst([$1], [\\n], [
183 ])[
184
185 %code requires // code for the .hh file
186 {
187 #include <list>
188 #include <string>
189 typedef std::list<std::string> strings_type;
190 }
191
192 %code // code for the .cc file
193 {
194 #include <cstdlib> // abort, getenv
195 #include <iostream>
196 #include <sstream>
197
198 namespace yy
199 {
200 static]AT_TOKEN_CTOR_IF([[
201 parser::symbol_type yylex ()]], [[
202 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
203 parser::location_type* yylloc])[)]])[;
204 }
205
206 // Printing a list of strings (for %printer).
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 {
213 o << '(';
214 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
215 {
216 if (i != s.begin ())
217 o << ", ";
218 o << *i;
219 }
220 return o << ')';
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;
232 return o.str ();
233 }
234 }
235
236 %token <::std::string> TEXT;
237 %token <int> NUMBER;
238 %token END_OF_FILE 0;
239
240 %type <::std::string> item;
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;
245
246 %printer { yyo << $$; }
247 <int> <::std::string> <::std::list<std::string>>;
248 %%
249
250 result:
251 list { std::cout << $][1 << std::endl; }
252 ;
253
254 list:
255 /* nothing */ { /* Generates an empty string list */ }
256 | list item { std::swap ($$,$][1); $$.push_back ($][2); }
257 | list error { std::swap ($$,$][1); }
258 ;
259
260 item:
261 TEXT { std::swap ($$,$][1); }
262 | NUMBER { if ($][1 == 3) YYERROR; else $$ = string_cast ($][1); }
263 ;
264 %%
265 ]AT_TOKEN_CTOR_IF([],
266 [[#ifdef TWO_STAGE_BUILD
267 # define BUILD(Type, Value) build<Type> () = Value
268 #else
269 # define BUILD(Type, Value) build (Value)
270 #endif
271 ]])[
272 #define STAGE_MAX 5
273 namespace yy
274 {
275 static]AT_TOKEN_CTOR_IF([[
276 parser::symbol_type yylex ()]], [[
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;])[
281 static int stage = -1;
282 ++stage;
283 if (stage == STAGE_MAX)
284 {]AT_TOKEN_CTOR_IF([[
285 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
286 [AT_LOCATION_IF([
287 *yylloc = location ();])[
288 return parser::token::END_OF_FILE;]])[
289 }
290 else if (stage % 2)
291 {]AT_TOKEN_CTOR_IF([[
292 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
293 [[
294 yylval->BUILD (int, stage);]AT_LOCATION_IF([
295 *yylloc = location ();])[
296 return parser::token::NUMBER;]])[
297 }
298 else
299 {]AT_TOKEN_CTOR_IF([[
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 ();])[
303 return parser::token::TEXT;]])[
304 }
305 abort ();
306 }
307 }
308
309 ]AT_YYERROR_DEFINE[
310 ]AT_MAIN_DEFINE[
311 ]])
312
313 AT_FULL_COMPILE([list])
314 AT_PARSER_CHECK([./list], 0,
315 [(0, 1, 2, 4)
316 ])
317
318 AT_BISON_OPTION_POPDEFS
319 AT_CLEANUP
320 ])
321
322 AT_TEST([[%skeleton "lalr1.cc" ]])
323 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
324 AT_TEST([[%skeleton "lalr1.cc" %locations %define parse.assert]])
325 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
326 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
327 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
328 AT_TEST([[%skeleton "lalr1.cc" %locations %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
329
330 m4_popdef([AT_TEST])
331
332
333 ## ----------------------- ##
334 ## Doxygen Documentation. ##
335 ## ----------------------- ##
336
337 m4_define([AT_CHECK_DOXYGEN],
338 [m4_case([$1],
339 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
340 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
341 [m4_fatal([invalid argument: $1])])
342 AT_SETUP([Doxygen $1 Documentation])
343
344 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
345 AT_DATA([input.yy],
346 [[%skeleton "lalr1.cc"
347 %locations
348 %debug
349 %%
350 exp: /* empty */;
351 %%
352 ]AT_YYERROR_DEFINE[
353 ]])
354
355 AT_BISON_CHECK([-o input.cc input.yy], 0)
356
357 AT_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.
360 PROJECT_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.
365 QUIET = 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.
370 WARNINGS = 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.
374 WARN_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.
379 WARN_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.
384 WARN_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
391 EXTRACT_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.
395 EXTRACT_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.
399 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
400 ])
401
402 AT_CHECK([doxygen --version || exit 77], 0, ignore)
403 AT_CHECK([doxygen], 0, [], [ignore])
404
405 AT_BISON_OPTION_POPDEFS
406 AT_CLEANUP
407
408 m4_popdef([AT_DOXYGEN_PRIVATE])
409 ])# AT_CHECK_DOXYGEN
410
411 AT_CHECK_DOXYGEN([Public])
412 AT_CHECK_DOXYGEN([Private])
413
414
415 ## ------------ ##
416 ## Namespaces. ##
417 ## ------------ ##
418
419 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
420 # ----------------------------------------
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.
424 m4_pushdef([AT_TEST],
425 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
426 AT_DATA_GRAMMAR([[input.y]],
427 [[%language "C++"
428 %define api.namespace {]$1[}
429 %union { int i; }
430 %define global_tokens_and_yystype
431 %locations
432
433 %code {
434 // YYSTYPE contains a namespace reference.
435 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
436 lval->i = 3;
437 return 0;
438 }
439 }
440
441 %%
442
443 start: ;
444
445 %%
446
447 void
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
454 ]AT_MAIN_DEFINE[
455 ]])
456
457
458 AT_BISON_CHECK([[-o input.cc input.y]])
459
460 m4_if([$#], [1],
461 [AT_COMPILE_CXX([[input]], [[input.cc]])
462 AT_PARSER_CHECK([[./input]])])
463 AT_BISON_OPTION_POPDEFS
464 ])
465
466 AT_SETUP([[Relative namespace references]])
467 AT_TEST([[foo]])
468 AT_TEST([[foo::bar]])
469 AT_TEST([[foo::bar::baz]])
470 AT_CLEANUP
471
472 AT_SETUP([[Absolute namespace references]])
473 AT_TEST([[::foo]])
474 AT_TEST([[::foo::bar]])
475 AT_TEST([[::foo::bar::baz]])
476 AT_TEST([[@tb@::foo]])
477 AT_TEST([[ @tb@ ::foo::bar]])
478 AT_TEST([[ ::foo::bar::baz]])
479 AT_CLEANUP
480
481 AT_SETUP([[Syntactically invalid namespace references]])
482 AT_TEST([[:foo:bar]], [[-]])
483 AT_TEST([[foo: :bar]], [[-]])
484 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
485 # contains single occurrences of ':'.
486 AT_TEST([[foo[3]::bar::baz]], [[-]])
487 AT_TEST([[foo::bar,baz]], [[-]])
488 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
489 AT_CLEANUP
490
491 m4_popdef([AT_TEST])
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
501 AT_SETUP([[Syntax error discarding no lookahead]])
502
503 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
504
505 AT_DATA_GRAMMAR([[input.yy]],
506 [[%skeleton "lalr1.cc"
507
508 %code {
509 #include <string>
510 int yylex (yy::parser::semantic_type *);
511 #define USE(Args)
512 }
513
514 %define parse.error verbose
515
516 %nonassoc 'a' ;
517
518 %destructor {
519 std::cerr << "Discarding 'a'." << std::endl;
520 } 'a'
521
522 %%
523
524 start: error-reduce consistent-error 'a' { USE ($3); };
525
526 error-reduce:
527 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
528 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
529 ;
530
531 consistent-error:
532 'a'
533 | /*empty*/ %prec 'a'
534 ;
535
536 // Provide another context in which all rules are useful so that this
537 // test case looks a little more realistic.
538 start: 'b' consistent-error ;
539
540 %%
541
542 int
543 yylex (yy::parser::semantic_type *)
544 {
545 static char const *input = "aa";
546 return *input++;
547 }
548
549 void
550 yy::parser::error (const std::string &m)
551 {
552 std::cerr << m << std::endl;
553 }
554
555 ]AT_MAIN_DEFINE[
556 ]])
557
558 AT_BISON_CHECK([[-o input.cc input.yy]])
559 AT_COMPILE_CXX([[input]])
560 # This used to print "Discarding 'a'." again at the end.
561 AT_PARSER_CHECK([[./input]], [[1]], [[]],
562 [[syntax error
563 Discarding 'a'.
564 Reducing 'a'.
565 ]])
566
567 AT_BISON_OPTION_POPDEFS
568 AT_CLEANUP
569
570
571 ## --------------------------- ##
572 ## Syntax error as exception. ##
573 ## --------------------------- ##
574
575 AT_SETUP([[Syntax error as exception]])
576
577 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
578
579 AT_DATA_GRAMMAR([[input.yy]],
580 [[%skeleton "lalr1.cc"
581
582 %code
583 {
584 #include <cstdlib>
585 int yylex (yy::parser::semantic_type *);
586 }
587
588 %define api.value.type variant
589 %define parse.error verbose
590 %define parse.trace
591 %%
592
593 start:
594 thing
595 | start thing
596 ;
597
598 thing:
599 error { std::cerr << "caught error" << std::endl; }
600 | item
601 ;
602
603 item:
604 'a'
605 | 's'
606 {
607 throw yy::parser::syntax_error ("invalid expression");
608 }
609
610 %%
611
612 int
613 yylex (yy::parser::semantic_type *)
614 {
615 // 's': syntax error, 'l': lexical error.
616 static char const *input = "asal";
617 switch (int res = *input++)
618 {
619 case 'l':
620 throw yy::parser::syntax_error ("invalid character");
621 default:
622 return res;
623 }
624 }
625
626 void
627 yy::parser::error (const std::string &m)
628 {
629 std::cerr << "error: " << m << std::endl;
630 }
631 ]AT_MAIN_DEFINE[
632 ]])
633
634 AT_BISON_CHECK([[-o input.cc input.yy]])
635 AT_COMPILE_CXX([[input]])
636
637 AT_PARSER_CHECK([[./input]], [[0]], [[]],
638 [[error: invalid expression
639 caught error
640 error: invalid character
641 caught error
642 ]])
643
644 AT_BISON_OPTION_POPDEFS
645 AT_CLEANUP
646
647
648 ## ------------------ ##
649 ## Exception safety. ##
650 ## ------------------ ##
651
652 # AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
653 # ----------------------------------------------------------
654 # Check that no object is leaked when exceptions are thrown.
655 # WITH-RECOVERY = "with" or "without".
656 m4_pushdef([AT_TEST],
657 [AT_SETUP([[Exception safety $2 error recovery $1]])
658
659 AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
660
661 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
662
663 AT_DATA_GRAMMAR([[input.yy]],
664 [[%skeleton "lalr1.cc"
665 %debug
666 %error-verbose
667 $1
668 %code requires
669 {
670 #include <cassert>
671 #include <cstdlib> // size_t and getenv.
672 #include <iostream>
673 #include <set>
674
675 bool debug = false;
676
677 /// A class that tracks its instances.
678 struct Object
679 {
680 char val;
681
682 Object ()
683 : val ('?')
684 {
685 log (this, "Object::Object");
686 Object::instances.insert (this);
687 }
688
689 Object (const Object& that)
690 : val (that.val)
691 {
692 log (this, "Object::Object");
693 Object::instances.insert (this);
694 }
695
696 Object (char v)
697 : val (v)
698 {
699 log (this, "Object::Object");
700 Object::instances.insert (this);
701 }
702
703 ~Object ()
704 {
705 log (this, "Object::~Object");
706 objects::const_iterator i = instances.find (this);
707 // Make sure this object is alive.
708 assert (i != instances.end ());
709 Object::instances.erase (i);
710 }
711
712 Object& operator= (char v)
713 {
714 val = v;
715 return *this;
716 }
717
718 // Static part.
719 typedef std::set<const Object*> objects;
720 static objects instances;
721
722 static bool
723 empty ()
724 {
725 return instances.empty ();
726 }
727
728 static void
729 log (Object const *o, const std::string& msg)
730 {
731 if (debug)
732 {
733 if (o)
734 std::cerr << o << "->";
735 std::cerr << msg << " {";
736 const char* sep = " ";
737 for (objects::const_iterator i = instances.begin(),
738 i_end = instances.end();
739 i != i_end;
740 ++i)
741 {
742 std::cerr << sep << *i;
743 sep = ", ";
744 }
745 std::cerr << " }" << std::endl;
746 }
747 }
748 };
749 }
750
751 %code
752 {
753 #include <cassert>
754 #include <cstring> // strchr
755 #include <stdexcept>
756 int yylex (yy::parser::semantic_type *);
757 Object::objects Object::instances;
758 static char const *input;
759 }
760
761 ]AT_VARIANT_IF([[
762 %printer
763 {
764 yyo << &$$ << " '" << $$.val << '\'';
765 if ($$.val == 'p')
766 throw std::runtime_error ("printer");
767 } <Object>;
768
769 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
770 %type <Object> list item
771 ]], [[
772 %union
773 {
774 Object *obj;
775 }
776 %destructor { delete $$; } <obj>;
777 %printer
778 {
779 yyo << $$ << " '" << $$->val << '\'';
780 if ($$->val == 'p')
781 throw std::runtime_error ("printer");
782 } <obj>;
783
784 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
785 %type <obj> list item
786 ]])[
787
788 %initial-action
789 {
790 if (strchr (input, 'i'))
791 throw std::runtime_error ("initial-action");
792 }
793
794 %%
795
796 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
797
798 list:
799 item { $$ = $][1; }
800 // Right recursion to load the stack.
801 | item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
802 ;
803
804 item:
805 'a' { $$ = $][1; }
806 | 'e' { YYUSE ($$); YYUSE($][1); error ("syntax error"); }
807 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
808 // then the stack is emptied, defeating the point of the test.
809 | 'E' 'a' { YYUSE($][1); $$ = $][2; }
810 | 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
811 | 'p' { $$ = $][1; }
812 | 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
813 | 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
814 ]m4_if([$2], [with],
815 [[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
816 ;
817 %%
818
819 int
820 yylex (yy::parser::semantic_type *lvalp)
821 {
822 // 'a': no error.
823 // 'e': user action calls error.
824 // 'E': syntax error, with yyerror that throws.
825 // 'i': initial action throws.
826 // 'l': yylex throws.
827 // 'R': call YYERROR in the action
828 // 's': reduction throws.
829 // 'T': call YYABORT in the action
830 switch (int res = *input++)
831 {
832 case 'l':
833 throw std::runtime_error ("yylex");
834 default:
835 lvalp->]AT_VARIANT_IF([build (Object (res))],
836 [obj = new Object (res)])[;
837 // Fall through.
838 case 0:
839 return res;
840 }
841 }
842
843 /* A C++ error reporting function. */
844 void
845 yy::parser::error (const std::string& m)
846 {
847 throw std::runtime_error (m);
848 }
849
850 int
851 main (int argc, const char *argv[])
852 {
853 switch (argc)
854 {
855 case 2:
856 input = argv[1];
857 break;
858 case 3:
859 assert (std::string(argv[1]) == "--debug");
860 debug = 1;
861 input = argv[2];
862 break;
863 default:
864 abort ();
865 }
866
867 yy::parser parser;
868 debug |= !!getenv ("YYDEBUG");
869 parser.set_debug_level (debug);
870 int res = 2;
871 try
872 {
873 res = parser.parse ();
874 }
875 catch (const std::exception& e)
876 {
877 std::cerr << "exception caught: " << e.what () << std::endl;
878 }
879 catch (...)
880 {
881 std::cerr << "unknown exception caught" << std::endl;
882 }
883 Object::log (YY_NULLPTR, "end");
884 assert (Object::empty());
885 return res;
886 }
887 ]])
888 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
889 AT_COMPILE_CXX([[input]])
890
891 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
892 [[exception caught: reduction
893 ]])
894
895 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
896 [[exception caught: yylex
897 ]])
898
899 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
900 [[exception caught: initial-action
901 ]])
902
903 AT_PARSER_CHECK([[./input aaaap]])
904
905 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
906 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
907
908 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
909 [[exception caught: syntax error
910 ]])
911
912 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
913 [[exception caught: syntax error, unexpected $end, expecting 'a'
914 ]])
915
916 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
917
918 AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
919
920 AT_BISON_OPTION_POPDEFS
921
922 AT_CLEANUP
923 ])
924
925 AT_TEST([], [with])
926 AT_TEST([], [without])
927 AT_TEST([%define api.value.type variant], [with])
928 AT_TEST([%define api.value.type variant], [without])
929
930 m4_popdef([AT_TEST])
931
932 ## ------------------------------------ ##
933 ## C++ GLR parser identifier shadowing ##
934 ## ------------------------------------ ##
935
936 AT_SETUP([[C++ GLR parser identifier shadowing]])
937
938 AT_DATA_GRAMMAR([input.yy], [
939 %skeleton "glr.cc"
940
941 %union
942 {
943 int ival;
944 }
945
946 %token <ival> ZERO;
947
948 %code
949 {
950 int yylex (yy::parser::semantic_type *yylval);
951 }
952
953 %%
954 exp: ZERO
955
956 %%
957
958 int yylex (yy::parser::semantic_type *yylval)
959 {
960 // Note: this argument is unused, but named on purpose. There used to be a
961 // bug with a macro that erroneously expanded this identifier to
962 // yystackp->yyval.
963 YYUSE (yylval);
964 return yy::parser::token::ZERO;
965 }
966
967 void yy::parser::error (std::string const&)
968 {}
969
970 int main()
971 {}
972 ])
973
974 AT_BISON_CHECK([[-o input.cc input.yy]])
975 AT_COMPILE_CXX([[input]])
976
977 AT_CLEANUP