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