]> git.saurik.com Git - bison.git/blob - tests/c++.at
e5cd069caf08826e92ae65fb499c3c0b771cfef6
[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 ## Variants. ##
23 ## ---------- ##
24
25 # AT_TEST([DIRECTIVES])
26 # ---------------------
27 # Check the support of variants in C++, with the additional DIRECTIVES.
28 m4_pushdef([AT_TEST],
29 [AT_SETUP([Variants $1])
30
31 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
32 # Store strings and integers in a list of strings.
33 AT_DATA_GRAMMAR([list.yy],
34 [[%debug
35 %skeleton "lalr1.cc"
36 %defines
37 %define api.value.type variant
38 ]m4_bpatsubst([$1], [\\n], [
39 ])[
40
41 %code requires // code for the .hh file
42 {
43 #include <list>
44 #include <string>
45 typedef std::list<std::string> strings_type;
46 }
47
48 %code // code for the .cc file
49 {
50 #include <cstdlib> // abort, getenv
51 #include <iostream>
52 #include <sstream>
53
54 namespace yy
55 {
56 static]AT_TOKEN_CTOR_IF([[
57 parser::symbol_type yylex ()]], [[
58 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
59 parser::location_type* yylloc])[)]])[;
60 }
61
62 // Printing a list of strings (for %printer).
63 // Koening look up will look into std, since that's an std::list.
64 namespace std
65 {
66 std::ostream&
67 operator<<(std::ostream& o, const strings_type& s)
68 {
69 o << '(';
70 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
71 {
72 if (i != s.begin ())
73 o << ", ";
74 o << *i;
75 }
76 return o << ')';
77 }
78 }
79
80 // Conversion to string.
81 template <typename T>
82 inline
83 std::string
84 string_cast (const T& t)
85 {
86 std::ostringstream o;
87 o << t;
88 return o.str ();
89 }
90 }
91
92 %token <::std::string> TEXT;
93 %token <int> NUMBER;
94 %token END_OF_FILE 0;
95
96 %type <::std::string> item;
97 // Using the template type to exercize its parsing.
98 // Starting with :: to ensure we don't output "<::" which starts by the
99 // digraph for the left square bracket.
100 %type <::std::list<std::string>> list result;
101
102 %printer { yyo << $][$; }
103 <int> <::std::string> <::std::list<std::string>>;
104 %%
105
106 result:
107 list { std::cout << $][1 << std::endl; }
108 ;
109
110 list:
111 /* nothing */ { /* Generates an empty string list */ }
112 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
113 | list error { std::swap ($][$,$][1); }
114 ;
115
116 item:
117 TEXT { std::swap ($][$,$][1); }
118 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
119 ;
120 %%
121
122 #ifdef TWO_STAGE_BUILD
123 # define BUILD(Type, Value) build<Type> () = Value
124 #else
125 # define BUILD(Type, Value) build (Value)
126 #endif
127
128 #define STAGE_MAX 5
129 namespace yy
130 {
131 static]AT_TOKEN_CTOR_IF([[
132 parser::symbol_type yylex ()]], [[
133 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
134 parser::location_type* yylloc])[)]])[
135 {]AT_LOCATION_IF([
136 typedef parser::location_type location;])[
137 static int stage = -1;
138 ++stage;
139 if (stage == STAGE_MAX)
140 {]AT_TOKEN_CTOR_IF([[
141 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
142 [AT_LOCATION_IF([
143 *yylloc = location ();])[
144 return parser::token::END_OF_FILE;]])[
145 }
146 else if (stage % 2)
147 {]AT_TOKEN_CTOR_IF([[
148 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
149 [[
150 yylval->BUILD (int, stage);]AT_LOCATION_IF([
151 *yylloc = location ();])[
152 return parser::token::NUMBER;]])[
153 }
154 else
155 {]AT_TOKEN_CTOR_IF([[
156 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
157 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
158 *yylloc = location ();])[
159 return parser::token::TEXT;]])[
160 }
161 abort ();
162 }
163 }
164
165 ]AT_YYERROR_DEFINE[
166 ]AT_MAIN_DEFINE[
167 ]])
168
169 AT_BISON_CHECK([-o list.cc list.yy])
170 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
171 AT_PARSER_CHECK([./list], 0,
172 [(0, 1, 2, 4)
173 ])
174
175 AT_BISON_OPTION_POPDEFS
176 AT_CLEANUP
177 ])
178
179 AT_TEST([])
180 AT_TEST([%define parse.assert])
181 AT_TEST([%locations %define parse.assert])
182 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
183 AT_TEST([[%define parse.assert %define api.token.constructor]])
184 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
185 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
186
187 m4_popdef([AT_TEST])
188
189
190 ## ----------------------- ##
191 ## Doxygen Documentation. ##
192 ## ----------------------- ##
193
194 m4_define([AT_CHECK_DOXYGEN],
195 [m4_case([$1],
196 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
197 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
198 [m4_fatal([invalid argument: $1])])
199 AT_SETUP([Doxygen $1 Documentation])
200
201 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
202 AT_DATA([input.yy],
203 [[%skeleton "lalr1.cc"
204 %locations
205 %debug
206 %defines
207 %%
208 exp: /* empty */;
209 %%
210 ]AT_YYERROR_DEFINE[
211 ]])
212
213 AT_BISON_CHECK([-o input.cc input.yy], 0)
214
215 AT_DATA([Doxyfile],
216 [# The PROJECT_NAME tag is a single word (or a sequence of words
217 # surrounded by quotes) that should identify the project.
218 PROJECT_NAME = "Bison C++ Parser"
219
220 # The QUIET tag can be used to turn on/off the messages that are
221 # generated by doxygen. Possible values are YES and NO. If left blank
222 # NO is used.
223 QUIET = YES
224
225 # The WARNINGS tag can be used to turn on/off the warning messages
226 # that are generated by doxygen. Possible values are YES and NO. If
227 # left blank NO is used.
228 WARNINGS = YES
229 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
230 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
231 # this flag will automatically be disabled.
232 WARN_IF_UNDOCUMENTED = YES
233 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
234 # for potential errors in the documentation, such as not documenting
235 # some parameters in a documented function, or documenting parameters
236 # that don't exist or using markup commands wrongly.
237 WARN_IF_DOC_ERROR = YES
238 # The WARN_FORMAT tag determines the format of the warning messages
239 # that doxygen can produce. The string should contain the $file,
240 # $line, and $text tags, which will be replaced by the file and line
241 # number from which the warning originated and the warning text.
242 WARN_FORMAT = "$file:$line: $text"
243
244 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
245 # entities in documentation are documented, even if no documentation
246 # was available. Private class members and static file members will
247 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
248 # to YES
249 EXTRACT_ALL = YES
250
251 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
252 # class will be included in the documentation.
253 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
254
255 # If the EXTRACT_STATIC tag is set to YES all static members of a file
256 # will be included in the documentation.
257 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
258 ])
259
260 AT_CHECK([doxygen --version || exit 77], 0, ignore)
261 AT_CHECK([doxygen], 0, [], [ignore])
262
263 AT_BISON_OPTION_POPDEFS
264 AT_CLEANUP
265
266 m4_popdef([AT_DOXYGEN_PRIVATE])
267 ])# AT_CHECK_DOXYGEN
268
269 AT_CHECK_DOXYGEN([Public])
270 AT_CHECK_DOXYGEN([Private])
271
272
273 ## ------------ ##
274 ## Namespaces. ##
275 ## ------------ ##
276
277 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
278 # ----------------------------------------
279 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
280 # is specified, then Bison should accept the input, but compilation will fail,
281 # so don't check compilation.
282 m4_pushdef([AT_TEST],
283 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"])
284 AT_DATA_GRAMMAR([[input.y]],
285 [[%language "C++"
286 %defines
287 %define api.namespace "]$1["
288 %union { int i; }
289 %define global_tokens_and_yystype
290 %locations
291
292 %code {
293 // YYSTYPE contains a namespace reference.
294 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
295 lval->i = 3;
296 return 0;
297 }
298 }
299
300 %%
301
302 start: ;
303
304 %%
305
306 void
307 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
308 const std::string &msg)
309 {
310 std::cerr << "At " << loc << ": " << msg << std::endl;
311 }
312
313 ]AT_MAIN_DEFINE[
314 ]])
315
316 AT_BISON_CHECK([[-o input.cc input.y]])
317
318 m4_if([$#], [1],
319 [AT_COMPILE_CXX([[input]], [[input.cc]])
320 AT_PARSER_CHECK([[./input]])])
321 AT_BISON_OPTION_POPDEFS
322 ])
323
324 AT_SETUP([[Relative namespace references]])
325 AT_TEST([[foo]])
326 AT_TEST([[foo::bar]])
327 AT_TEST([[foo::bar::baz]])
328 AT_CLEANUP
329
330 AT_SETUP([[Absolute namespace references]])
331 AT_TEST([[::foo]])
332 AT_TEST([[::foo::bar]])
333 AT_TEST([[::foo::bar::baz]])
334 AT_TEST([[ ::foo]])
335 AT_TEST([[ ::foo::bar]])
336 AT_TEST([[ ::foo::bar::baz]])
337 AT_CLEANUP
338
339 AT_SETUP([[Syntactically invalid namespace references]])
340 AT_TEST([[:foo:bar]], [[-]])
341 AT_TEST([[foo: :bar]], [[-]])
342 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
343 # contains single occurrences of `:'.
344 AT_TEST([[foo[3]::bar::baz]], [[-]])
345 AT_TEST([[foo::bar,baz]], [[-]])
346 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
347 AT_CLEANUP
348
349 m4_popdef([AT_TEST])
350
351 ## -------------------------------------- ##
352 ## Syntax error discarding no lookahead. ##
353 ## -------------------------------------- ##
354
355 # After a syntax error, lalr1.cc used to not check whether there
356 # actually is a lookahead before discarding the lookahead. As a result,
357 # it mistakenly invoked the destructor for the previous lookahead.
358
359 AT_SETUP([[Syntax error discarding no lookahead]])
360
361 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
362
363 AT_DATA_GRAMMAR([[input.yy]],
364 [[%skeleton "lalr1.cc"
365
366 %code {
367 #include <string>
368 int yylex (yy::parser::semantic_type *);
369 #define USE(Args)
370 }
371
372 %defines
373 %define parse.error verbose
374
375 %nonassoc 'a' ;
376
377 %destructor {
378 std::cerr << "Discarding 'a'." << std::endl;
379 } 'a'
380
381 %%
382
383 start: error-reduce consistent-error 'a' { USE ($3); };
384
385 error-reduce:
386 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
387 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
388 ;
389
390 consistent-error:
391 'a'
392 | /*empty*/ %prec 'a'
393 ;
394
395 // Provide another context in which all rules are useful so that this
396 // test case looks a little more realistic.
397 start: 'b' consistent-error ;
398
399 %%
400
401 int
402 yylex (yy::parser::semantic_type *)
403 {
404 static char const *input = "aa";
405 return *input++;
406 }
407
408 void
409 yy::parser::error (const std::string &m)
410 {
411 std::cerr << m << std::endl;
412 }
413
414 ]AT_MAIN_DEFINE[
415 ]])
416
417 AT_BISON_CHECK([[-o input.cc input.yy]])
418 AT_COMPILE_CXX([[input]])
419 # This used to print "Discarding 'a'." again at the end.
420 AT_PARSER_CHECK([[./input]], [[1]], [[]],
421 [[syntax error
422 Discarding 'a'.
423 Reducing 'a'.
424 ]])
425
426 AT_BISON_OPTION_POPDEFS
427 AT_CLEANUP
428
429
430 ## --------------------------- ##
431 ## Syntax error as exception. ##
432 ## --------------------------- ##
433
434 AT_SETUP([[Syntax error as exception]])
435
436 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
437
438 AT_DATA_GRAMMAR([[input.yy]],
439 [[%skeleton "lalr1.cc"
440
441 %code
442 {
443 #include <cstdlib>
444 int yylex (yy::parser::semantic_type *);
445 }
446
447 %defines
448 %define api.value.type variant
449 %define parse.error verbose
450 %define parse.trace
451 %%
452
453 start:
454 thing
455 | start thing
456 ;
457
458 thing:
459 error { std::cerr << "caught error" << std::endl; }
460 | item
461 ;
462
463 item:
464 'a'
465 | 's'
466 {
467 throw yy::parser::syntax_error ("invalid expression");
468 }
469
470 %%
471
472 int
473 yylex (yy::parser::semantic_type *)
474 {
475 // 's': syntax error, 'l': lexical error.
476 static char const *input = "asal";
477 switch (int res = *input++)
478 {
479 case 'l':
480 throw yy::parser::syntax_error ("invalid character");
481 default:
482 return res;
483 }
484 }
485
486 void
487 yy::parser::error (const std::string &m)
488 {
489 std::cerr << "error: " << m << std::endl;
490 }
491 ]AT_MAIN_DEFINE[
492 ]])
493
494 AT_BISON_CHECK([[-o input.cc input.yy]])
495 AT_COMPILE_CXX([[input]])
496
497 AT_PARSER_CHECK([[./input]], [[0]], [[]],
498 [[error: invalid expression
499 caught error
500 error: invalid character
501 caught error
502 ]])
503
504 AT_BISON_OPTION_POPDEFS
505 AT_CLEANUP
506
507
508 ## ------------------ ##
509 ## Exception safety. ##
510 ## ------------------ ##
511
512 AT_SETUP([[Exception safety]])
513
514 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
515
516 AT_DATA_GRAMMAR([[input.yy]],
517 [[%skeleton "lalr1.cc"
518 %defines // FIXME: Mandated in 2.6.
519 %debug
520 %error-verbose
521
522 %code requires
523 {
524 #include <cassert>
525 #include <cstdlib> // size_t and getenv.
526 #include <iostream>
527 #include <list>
528
529 bool debug = false;
530
531 /// A class that counts its number of instances.
532 struct Object
533 {
534 typedef std::list<const Object*> objects;
535 static objects instances;
536 char val;
537
538 static bool
539 empty ()
540 {
541 return instances.empty();
542 }
543
544 static void
545 log (Object const *o, const std::string& msg)
546 {
547 if (debug)
548 {
549 if (o)
550 std::cerr << o << "->";
551 std::cerr << msg << " {";
552 const char* sep = " ";
553 for (objects::const_iterator i = instances.begin(),
554 i_end = instances.end();
555 i != i_end;
556 ++i)
557 {
558 std::cerr << sep << *i;
559 sep = ", ";
560 }
561 std::cerr << " }" << std::endl;
562 }
563 }
564
565 Object (char v)
566 : val (v)
567 {
568 instances.push_back(this);
569 log (this, "Object::Object");
570 }
571
572 ~Object ()
573 {
574 instances.remove(this);
575 log (this, "Object::~Object");
576 }
577 };
578 }
579
580 %code
581 {
582 #include <cassert>
583 #include <cstring> // strchr
584 #include <stdexcept>
585 int yylex (yy::parser::semantic_type *);
586 Object::objects Object::instances;
587 static char const *input;
588 }
589
590 %union
591 {
592 Object *obj;
593 }
594
595 %initial-action
596 {
597 if (strchr (input, 'i'))
598 throw std::runtime_error ("initial-action");
599 }
600
601 %destructor { delete $$; } <obj>;
602 %printer
603 {
604 yyo << $$ << " '" << $$->val << '\'';
605 if ($$->val == 'p')
606 throw std::runtime_error ("printer");
607 } <obj>;
608
609 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
610 %type <obj> list item
611
612 %%
613
614 start: list { delete $1; };
615
616 list:
617 item { $$ = $1; }
618 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
619 ;
620
621 item:
622 'a' { $$ = $1; }
623 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
624 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
625 // then the stack is emptied, defeating the point of the test.
626 | 'E' 'a' { YYUSE($1); $$ = $2; }
627 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
628 | 'p' { $$ = $1; }
629 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
630 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
631 | error { $$ = YY_NULL; yyerrok; }
632 ;
633 %%
634
635 int
636 yylex (yy::parser::semantic_type *lvalp)
637 {
638 // 'a': no error.
639 // 'e': user action calls error.
640 // 'E': syntax error, with yyerror that throws.
641 // 'i': initial action throws.
642 // 'l': yylex throws.
643 // 'R': call YYERROR in the action
644 // 's': reduction throws.
645 // 'T': call YYABORT in the action
646 switch (int res = *input++)
647 {
648 case 'l':
649 throw std::runtime_error ("yylex");
650 default:
651 lvalp->obj = new Object (res);
652 // Fall through.
653 case 0:
654 return res;
655 }
656 }
657
658 /* A C++ error reporting function. */
659 void
660 yy::parser::error (const std::string& m)
661 {
662 throw std::runtime_error (m);
663 }
664
665 int
666 main (int argc, const char *argv[])
667 {
668 switch (argc)
669 {
670 case 2:
671 input = argv[1];
672 break;
673 case 3:
674 assert (std::string(argv[1]) == "--debug");
675 debug = 1;
676 input = argv[2];
677 break;
678 default:
679 abort ();
680 }
681
682 yy::parser parser;
683 debug |= !!getenv ("YYDEBUG");
684 parser.set_debug_level (debug);
685 int res = 2;
686 try
687 {
688 res = parser.parse ();
689 }
690 catch (const std::exception& e)
691 {
692 std::cerr << "exception caught: " << e.what () << std::endl;
693 }
694 catch (...)
695 {
696 std::cerr << "unknown exception caught" << std::endl;
697 }
698 Object::log (YY_NULL, "end");
699 assert (Object::empty());
700 return res;
701 }
702 ]])
703 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
704 AT_COMPILE_CXX([[input]])
705
706 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
707 [[exception caught: reduction
708 ]])
709
710 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
711 [[exception caught: yylex
712 ]])
713
714 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
715 [[exception caught: initial-action
716 ]])
717
718 AT_PARSER_CHECK([[./input aaaap]])
719
720 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
721 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
722
723 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
724 [[exception caught: syntax error
725 ]])
726
727 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
728 [[exception caught: syntax error, unexpected $end, expecting 'a'
729 ]])
730
731 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
732
733 # There is error-recovery, so exit success.
734 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
735
736 AT_BISON_OPTION_POPDEFS
737
738 AT_CLEANUP
739
740 ## ------------------------------------ ##
741 ## C++ GLR parser identifier shadowing ##
742 ## ------------------------------------ ##
743
744 AT_SETUP([[C++ GLR parser identifier shadowing]])
745
746 AT_DATA_GRAMMAR([input.yy], [
747 %skeleton "glr.cc"
748
749 %union
750 {
751 int ival;
752 }
753
754 %token <ival> ZERO;
755
756 %code
757 {
758 int yylex (yy::parser::semantic_type *yylval);
759 }
760
761 %%
762 exp: ZERO
763
764 %%
765
766 int yylex (yy::parser::semantic_type *yylval)
767 {
768 // Note: this argument is unused, but named on purpose. There used to be a
769 // bug with a macro that erroneously expanded this identifier to
770 // yystackp->yyval.
771 YYUSE (yylval);
772 return yy::parser::token::ZERO;
773 }
774
775 void yy::parser::error (std::string const&)
776 {}
777
778 int main()
779 {}
780 ])
781
782 AT_BISON_CHECK([[-o input.cc input.yy]])
783 AT_COMPILE_CXX([[input]])
784
785 AT_CLEANUP