]> git.saurik.com Git - bison.git/blame - tests/c++.at
Merge remote-tracking branch 'origin/maint'
[bison.git] / tests / c++.at
CommitLineData
e019c247 1# Checking the output filenames. -*- Autotest -*-
7d424de1 2
34136e65 3# Copyright (C) 2004-2005, 2007-2012 Free Software Foundation, Inc.
e019c247 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
e019c247 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
e019c247
AD
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
f16b0819 14#
e019c247 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
e019c247
AD
17
18AT_BANNER([[C++ Features.]])
19
20
76307410
AD
21## ---------- ##
22## Variants. ##
23## ---------- ##
24
25# AT_CHECK_VARIANTS([DIRECTIVES])
26# -------------------------------
27# Check the support of variants in C++, with the additional DIRECTIVES.
28m4_define([AT_CHECK_VARIANTS],
29[AT_SETUP([Variants $1])
30
31# Store strings and integers in a list of strings.
68c989de 32AT_DATA_GRAMMAR([list.yy],
76307410
AD
33[[%debug
34%skeleton "lalr1.cc"
35%defines
36%define variant
dddec537 37%locations
e5eb92e7
AD
38]m4_bpatsubst([$1], [\\n], [
39])[
76307410
AD
40
41%code requires // code for the .hh file
42{
43#include <list>
44#include <string>
45typedef std::list<std::string> strings_type;
46}
47
48%code // code for the .cc file
49{
76307410 50#include <iostream>
76307410
AD
51#include <sstream>
52
cb823b6f 53 static
dddec537
AD
54#if defined USE_LEX_SYMBOL
55 yy::parser::symbol_type yylex ();
56#else
57 yy::parser::token_type yylex (yy::parser::semantic_type* yylval,
58 yy::parser::location_type* yylloc);
59#endif
76307410 60
dddec537 61 // Printing a list of strings (for %printer).
76307410
AD
62 // Koening look up will look into std, since that's an std::list.
63 namespace std
64 {
65 std::ostream&
66 operator<<(std::ostream& o, const strings_type& s)
67 {
f5da8149
AD
68 o << '(';
69 for (strings_type::const_iterator i = s.begin(); i != s.end (); ++i)
70 {
71 if (i != s.begin ())
72 o << ", ";
73 o << *i;
74 }
75 return o << ')';
76307410
AD
76 }
77 }
78
79 // Conversion to string.
80 template <typename T>
81 inline
82 std::string
83 string_cast (const T& t)
84 {
85 std::ostringstream o;
86 o << t;
87 return o.str();
88 }
89}
90
f5da8149 91%token <::std::string> TEXT;
76307410 92%token <int> NUMBER;
76307410
AD
93%token END_OF_FILE 0;
94
f5da8149 95%type <::std::string> item;
cb823b6f
AD
96// Using the template type to exercize its parsing.
97// Starting with :: to ensure we don't output "<::" which starts by the
98// digraph for the left square bracket.
99%type <::std::list<std::string>> list result;
76307410 100
cb823b6f 101%printer { debug_stream() << $][$; }
9641b918 102 <int> <::std::string> <::std::list<std::string>>;
76307410
AD
103%%
104
105result:
f5da8149 106 list { std::cout << $][1 << std::endl; }
76307410
AD
107;
108
109list:
cb823b6f
AD
110 /* nothing */ { /* Generates an empty string list */ }
111| list item { std::swap($][$,$][1); $$.push_back($][2); }
c4dc4c46 112| list error { std::swap($][$,$][1); }
76307410
AD
113;
114
115item:
cb823b6f 116 TEXT { std::swap($][$,$][1); }
c4dc4c46 117| NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast($][1); }
76307410
AD
118;
119%%
120
dddec537 121#define STAGE_MAX 5
76307410 122static
dddec537
AD
123#if defined USE_LEX_SYMBOL
124yy::parser::symbol_type yylex()
125#else
126yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
127 yy::parser::location_type* yylloc)
128#endif
76307410 129{
8f064948 130#ifndef USE_LEX_SYMBOL
dddec537 131 typedef yy::parser::token token;
8f064948 132#endif
dddec537
AD
133 typedef yy::parser::location_type location_type;
134 static int stage = -1;
135 ++stage;
136 if (stage == STAGE_MAX)
137 {
138#if defined USE_LEX_SYMBOL
139 return yy::parser::make_END_OF_FILE (location_type ());
e5eb92e7 140#else
dddec537
AD
141 *yylloc = location_type ();
142 return token::END_OF_FILE;
e5eb92e7 143#endif
dddec537
AD
144 }
145 else if (stage % 2)
146 {
147#if defined USE_LEX_SYMBOL
148 return yy::parser::make_NUMBER (stage, location_type ());
e5eb92e7 149#else
dddec537
AD
150# if defined ONE_STAGE_BUILD
151 yylval->build(stage);
152# else
76307410 153 yylval->build<int>() = stage;
dddec537
AD
154# endif
155 *yylloc = location_type ();
156 return token::NUMBER;
e5eb92e7 157#endif
dddec537
AD
158 }
159 else
160 {
161#if defined USE_LEX_SYMBOL
162 return yy::parser::make_TEXT (string_cast (stage), location_type ());
163#else
164# if defined ONE_STAGE_BUILD
165 yylval->build (string_cast (stage));
166# else
167 yylval->build<std::string>() = string_cast (stage);
168# endif
169 *yylloc = location_type ();
170 return token::TEXT;
171#endif
172 }
173 abort();
76307410
AD
174}
175
76307410 176void
dddec537 177yy::parser::error(const yy::parser::location_type&,
cb823b6f 178 const std::string& message)
76307410 179{
2ea7730c 180 std::cerr << message << std::endl;
76307410
AD
181}
182
e5eb92e7 183int
0bb5783b 184main ()
76307410
AD
185{
186 yy::parser p;
187 p.set_debug_level(!!getenv("YYDEBUG"));
188 return p.parse();
189}
190]])
191
e5eb92e7 192AT_BISON_CHECK([-o list.cc list.yy])
76307410
AD
193AT_COMPILE_CXX([list])
194AT_CHECK([./list], 0,
f5da8149 195 [(0, 1, 2, 4)
76307410
AD
196])
197
198AT_CLEANUP
199])
200
201AT_CHECK_VARIANTS([])
0c90a1f5
AD
202AT_CHECK_VARIANTS([%define parse.assert])
203AT_CHECK_VARIANTS([[%define parse.assert %code {\n#define ONE_STAGE_BUILD\n}]])
204AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
4c6622c2 205AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define api.tokens.prefix "TOK_"]])
76307410
AD
206
207
e019c247
AD
208## ----------------------- ##
209## Doxygen Documentation. ##
210## ----------------------- ##
211
212m4_define([AT_CHECK_DOXYGEN],
213[m4_case([$1],
214 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
215 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
e9690142 216 [m4_fatal([invalid argument: $1])])
e019c247
AD
217AT_SETUP([Doxygen $1 Documentation])
218
219AT_DATA([input.yy],
220[[%skeleton "lalr1.cc"
221%locations
222%debug
223%defines
224%%
225exp:;
226%%
227yy::parser::error (const location& l, const std::string& m)
228{
229 std::cerr << l << s << std::endl;
230}
231]])
232
da730230 233AT_BISON_CHECK([-o input.cc input.yy], 0)
e019c247
AD
234
235AT_DATA([Doxyfile],
236[# The PROJECT_NAME tag is a single word (or a sequence of words
237# surrounded by quotes) that should identify the project.
238PROJECT_NAME = "Bison C++ Parser"
239
240# The QUIET tag can be used to turn on/off the messages that are
241# generated by doxygen. Possible values are YES and NO. If left blank
242# NO is used.
243QUIET = YES
244
245# The WARNINGS tag can be used to turn on/off the warning messages
246# that are generated by doxygen. Possible values are YES and NO. If
247# left blank NO is used.
248WARNINGS = YES
249# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
250# warnings for undocumented members. If EXTRACT_ALL is set to YES then
251# this flag will automatically be disabled.
252WARN_IF_UNDOCUMENTED = YES
253# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
254# for potential errors in the documentation, such as not documenting
255# some parameters in a documented function, or documenting parameters
256# that don't exist or using markup commands wrongly.
257WARN_IF_DOC_ERROR = YES
258# The WARN_FORMAT tag determines the format of the warning messages
259# that doxygen can produce. The string should contain the $file,
260# $line, and $text tags, which will be replaced by the file and line
261# number from which the warning originated and the warning text.
262WARN_FORMAT = "$file:$line: $text"
263
264# If the EXTRACT_ALL tag is set to YES doxygen will assume all
265# entities in documentation are documented, even if no documentation
266# was available. Private class members and static file members will
267# be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
268# to YES
269EXTRACT_ALL = YES
270
271# If the EXTRACT_PRIVATE tag is set to YES all private members of a
272# class will be included in the documentation.
273EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
274
275# If the EXTRACT_STATIC tag is set to YES all static members of a file
276# will be included in the documentation.
277EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
278])
279
280AT_CHECK([doxygen --version || exit 77], 0, ignore)
281AT_CHECK([doxygen], 0, [], [ignore])
282
283AT_CLEANUP
284
285m4_popdef([AT_DOXYGEN_PRIVATE])
286])# AT_CHECK_DOXYGEN
287
288AT_CHECK_DOXYGEN([Public])
289AT_CHECK_DOXYGEN([Private])
793fbca5 290
76307410 291
793fbca5
JD
292## ------------ ##
293## Namespaces. ##
294## ------------ ##
295
296# AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
297# ---------------------------------------------------
298# See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
299# is specified, then Bison should accept the input, but compilation will fail,
300# so don't check compilation.
301m4_define([AT_CHECK_NAMESPACE],
302[
303
304AT_DATA_GRAMMAR([[input.y]],
305[[%language "C++"
306%defines
67501061 307%define api.namespace "]$1["
793fbca5
JD
308%union { int i; }
309%define global_tokens_and_yystype
2ea7730c 310%locations
793fbca5
JD
311
312%code {
313 // YYSTYPE contains a namespace reference.
d73e55e0 314 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
793fbca5
JD
315 lval->i = 3;
316 return 0;
317 }
318}
319
320%%
321
322start: ;
323
324%%
325
326void
327]$1[::parser::error (const ]$1[::parser::location_type &loc,
328 const std::string &msg)
329{
330 std::cerr << "At " << loc << ": " << msg << std::endl;
331}
332
333int
0bb5783b 334main ()
793fbca5
JD
335{
336 ]$1[::parser p;
337 return p.parse ();
338}
339]])
340
da730230 341AT_BISON_CHECK([[-o input.cc input.y]])
793fbca5
JD
342
343m4_if([$#], [1],
344[AT_COMPILE_CXX([[input]], [[input.cc]])
345AT_PARSER_CHECK([[./input]])])
346
347])
348
349AT_SETUP([[Relative namespace references]])
350AT_CHECK_NAMESPACE([[foo]])
351AT_CHECK_NAMESPACE([[foo::bar]])
352AT_CHECK_NAMESPACE([[foo::bar::baz]])
353AT_CLEANUP
354
355AT_SETUP([[Absolute namespace references]])
356AT_CHECK_NAMESPACE([[::foo]])
357AT_CHECK_NAMESPACE([[::foo::bar]])
358AT_CHECK_NAMESPACE([[::foo::bar::baz]])
359AT_CHECK_NAMESPACE([[ ::foo]])
360AT_CHECK_NAMESPACE([[ ::foo::bar]])
361AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
362AT_CLEANUP
363
364AT_SETUP([[Syntactically invalid namespace references]])
365AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
366AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
367# This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
368# contains single occurrences of `:'.
369AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
370AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
cb823b6f 371AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
793fbca5 372AT_CLEANUP
d59beda0
JD
373
374
375## -------------------------------------- ##
376## Syntax error discarding no lookahead. ##
377## -------------------------------------- ##
378
379# After a syntax error, lalr1.cc used to not check whether there
380# actually is a lookahead before discarding the lookahead. As a result,
381# it mistakenly invoked the destructor for the previous lookahead.
382
383AT_SETUP([[Syntax error discarding no lookahead]])
384
385AT_DATA_GRAMMAR([[input.yy]],
386[[%skeleton "lalr1.cc"
387
388%code {
389 #include <string>
a8c5aaa5 390 int yylex (yy::parser::semantic_type *);
d59beda0
JD
391 #define USE(Args)
392}
393
394%defines
d59beda0
JD
395%define parse.error verbose
396
397%nonassoc 'a' ;
398
399%destructor {
400 std::cerr << "Discarding 'a'." << std::endl;
401} 'a'
402
403%%
404
405start: error-reduce consistent-error 'a' { USE ($3); };
406
407error-reduce:
408 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
409| 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
410;
411
412consistent-error:
413 'a'
414| /*empty*/ %prec 'a'
415;
416
5335b65a
JD
417// Provide another context in which all rules are useful so that this
418// test case looks a little more realistic.
419start: 'b' consistent-error ;
420
d59beda0
JD
421%%
422
423int
a8c5aaa5 424yylex (yy::parser::semantic_type *)
d59beda0
JD
425{
426 static char const *input = "aa";
427 return *input++;
428}
429
430void
a8c5aaa5 431yy::parser::error (const std::string &m)
d59beda0
JD
432{
433 std::cerr << m << std::endl;
434}
435
436int
0bb5783b 437main ()
d59beda0
JD
438{
439 yy::parser parser;
440 return parser.parse ();
441}
442]])
5335b65a 443AT_BISON_CHECK([[-o input.cc input.yy]])
d59beda0
JD
444AT_COMPILE_CXX([[input]])
445# This used to print "Discarding 'a'." again at the end.
446AT_PARSER_CHECK([[./input]], [[1]], [[]],
447[[syntax error
448Discarding 'a'.
449Reducing 'a'.
450]])
451
452AT_CLEANUP
199a2d6d
AD
453
454
455## --------------------------- ##
456## Syntax error as exception. ##
457## --------------------------- ##
458
459AT_SETUP([[Syntax error as exception]])
460
461AT_DATA_GRAMMAR([[input.yy]],
462[[%skeleton "lalr1.cc"
463
464%code
465{
0bb5783b 466 #include <cstdlib>
199a2d6d
AD
467 int yylex (yy::parser::semantic_type *);
468}
469
470%defines
471%define variant
472%define parse.error verbose
473%define parse.trace
474%%
475
476start:
477 thing
478| start thing
479;
480
481thing:
482 error { std::cerr << "caught error" << std::endl; }
483| item
484;
485
486item:
487 'a'
488| 's'
489 {
490 throw yy::parser::syntax_error("invalid expression");
491 }
492
493%%
494
495int
496yylex (yy::parser::semantic_type *)
497{
a6552c5d
AD
498 // 's': syntax error, 'l': lexical error.
499 static char const *input = "asal";
199a2d6d
AD
500 switch (int res = *input++)
501 {
a6552c5d
AD
502 case 'l':
503 throw yy::parser::syntax_error("invalid character");
199a2d6d
AD
504 default:
505 return res;
506 }
507}
508
509void
510yy::parser::error (const std::string &m)
511{
512 std::cerr << "error: " << m << std::endl;
513}
514
515int
0bb5783b 516main ()
199a2d6d
AD
517{
518 yy::parser parser;
519 parser.set_debug_level(!!getenv("YYDEBUG"));
520 return parser.parse ();
521}
522]])
523AT_BISON_CHECK([[-o input.cc input.yy]])
524AT_COMPILE_CXX([[input]])
525
526AT_PARSER_CHECK([[./input]], [[0]], [[]],
527[[error: invalid expression
528caught error
a6552c5d
AD
529error: invalid character
530caught error
199a2d6d
AD
531]])
532
533AT_CLEANUP