]> git.saurik.com Git - bison.git/blob - tests/c++.at
java: fix parser tracing bug.
[bison.git] / tests / c++.at
1 # Checking the output filenames. -*- Autotest -*-
2
3 # Copyright (C) 2004-2005, 2007-2011 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_CHECK_VARIANTS([DIRECTIVES])
26 # -------------------------------
27 # Check the support of variants in C++, with the additional DIRECTIVES.
28 m4_define([AT_CHECK_VARIANTS],
29 [AT_SETUP([Variants $1])
30
31 # Store strings and integers in a list of strings.
32 AT_DATA_GRAMMAR([list.yy],
33 [[%debug
34 %skeleton "lalr1.cc"
35 %defines
36 %define variant
37 %locations
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 <iostream>
51 #include <sstream>
52
53 static
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
60
61 // Printing a list of strings (for %printer).
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 {
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 << ')';
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
91 %token <::std::string> TEXT;
92 %token <int> NUMBER;
93 %token END_OF_FILE 0;
94
95 %type <::std::string> item;
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;
100
101 %printer { debug_stream() << $][$; }
102 <int> <::std::string> <::std::list<::std::string>>;
103 %%
104
105 result:
106 list { std::cout << $][1 << std::endl; }
107 ;
108
109 list:
110 /* nothing */ { /* Generates an empty string list */ }
111 | list item { std::swap($][$,$][1); $$.push_back($][2); }
112 | list error { std::swap($][$,$][1); }
113 ;
114
115 item:
116 TEXT { std::swap($][$,$][1); }
117 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast($][1); }
118 ;
119 %%
120
121 #define STAGE_MAX 5
122 static
123 #if defined USE_LEX_SYMBOL
124 yy::parser::symbol_type yylex()
125 #else
126 yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
127 yy::parser::location_type* yylloc)
128 #endif
129 {
130 typedef yy::parser::token token;
131 typedef yy::parser::location_type location_type;
132 static int stage = -1;
133 ++stage;
134 if (stage == STAGE_MAX)
135 {
136 #if defined USE_LEX_SYMBOL
137 return yy::parser::make_END_OF_FILE (location_type ());
138 #else
139 *yylloc = location_type ();
140 return token::END_OF_FILE;
141 #endif
142 }
143 else if (stage % 2)
144 {
145 #if defined USE_LEX_SYMBOL
146 return yy::parser::make_NUMBER (stage, location_type ());
147 #else
148 # if defined ONE_STAGE_BUILD
149 yylval->build(stage);
150 # else
151 yylval->build<int>() = stage;
152 # endif
153 *yylloc = location_type ();
154 return token::NUMBER;
155 #endif
156 }
157 else
158 {
159 #if defined USE_LEX_SYMBOL
160 return yy::parser::make_TEXT (string_cast (stage), location_type ());
161 #else
162 # if defined ONE_STAGE_BUILD
163 yylval->build (string_cast (stage));
164 # else
165 yylval->build<std::string>() = string_cast (stage);
166 # endif
167 *yylloc = location_type ();
168 return token::TEXT;
169 #endif
170 }
171 abort();
172 }
173
174 void
175 yy::parser::error(const yy::parser::location_type&,
176 const std::string& message)
177 {
178 std::cerr << message << std::endl;
179 }
180
181 int
182 main (void)
183 {
184 yy::parser p;
185 p.set_debug_level(!!getenv("YYDEBUG"));
186 return p.parse();
187 }
188 ]])
189
190 AT_BISON_CHECK([-o list.cc list.yy])
191 AT_COMPILE_CXX([list])
192 AT_CHECK([./list], 0,
193 [(0, 1, 2, 4)
194 ])
195
196 AT_CLEANUP
197 ])
198
199 AT_CHECK_VARIANTS([])
200 AT_CHECK_VARIANTS([%define parse.assert])
201 AT_CHECK_VARIANTS([[%define parse.assert %code {\n#define ONE_STAGE_BUILD\n}]])
202 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
203 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define api.tokens.prefix "TOK_"]])
204
205
206 ## ----------------------- ##
207 ## Doxygen Documentation. ##
208 ## ----------------------- ##
209
210 m4_define([AT_CHECK_DOXYGEN],
211 [m4_case([$1],
212 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
213 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
214 [m4_fatal([invalid argument: $1])])
215 AT_SETUP([Doxygen $1 Documentation])
216
217 AT_DATA([input.yy],
218 [[%skeleton "lalr1.cc"
219 %locations
220 %debug
221 %defines
222 %%
223 exp:;
224 %%
225 yy::parser::error (const location& l, const std::string& m)
226 {
227 std::cerr << l << s << std::endl;
228 }
229 ]])
230
231 AT_BISON_CHECK([-o input.cc input.yy], 0)
232
233 AT_DATA([Doxyfile],
234 [# The PROJECT_NAME tag is a single word (or a sequence of words
235 # surrounded by quotes) that should identify the project.
236 PROJECT_NAME = "Bison C++ Parser"
237
238 # The QUIET tag can be used to turn on/off the messages that are
239 # generated by doxygen. Possible values are YES and NO. If left blank
240 # NO is used.
241 QUIET = YES
242
243 # The WARNINGS tag can be used to turn on/off the warning messages
244 # that are generated by doxygen. Possible values are YES and NO. If
245 # left blank NO is used.
246 WARNINGS = YES
247 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
248 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
249 # this flag will automatically be disabled.
250 WARN_IF_UNDOCUMENTED = YES
251 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
252 # for potential errors in the documentation, such as not documenting
253 # some parameters in a documented function, or documenting parameters
254 # that don't exist or using markup commands wrongly.
255 WARN_IF_DOC_ERROR = YES
256 # The WARN_FORMAT tag determines the format of the warning messages
257 # that doxygen can produce. The string should contain the $file,
258 # $line, and $text tags, which will be replaced by the file and line
259 # number from which the warning originated and the warning text.
260 WARN_FORMAT = "$file:$line: $text"
261
262 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
263 # entities in documentation are documented, even if no documentation
264 # was available. Private class members and static file members will
265 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
266 # to YES
267 EXTRACT_ALL = YES
268
269 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
270 # class will be included in the documentation.
271 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
272
273 # If the EXTRACT_STATIC tag is set to YES all static members of a file
274 # will be included in the documentation.
275 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
276 ])
277
278 AT_CHECK([doxygen --version || exit 77], 0, ignore)
279 AT_CHECK([doxygen], 0, [], [ignore])
280
281 AT_CLEANUP
282
283 m4_popdef([AT_DOXYGEN_PRIVATE])
284 ])# AT_CHECK_DOXYGEN
285
286 AT_CHECK_DOXYGEN([Public])
287 AT_CHECK_DOXYGEN([Private])
288
289
290 ## ------------ ##
291 ## Namespaces. ##
292 ## ------------ ##
293
294 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
295 # ---------------------------------------------------
296 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
297 # is specified, then Bison should accept the input, but compilation will fail,
298 # so don't check compilation.
299 m4_define([AT_CHECK_NAMESPACE],
300 [
301
302 AT_DATA_GRAMMAR([[input.y]],
303 [[%language "C++"
304 %defines
305 %define api.namespace "]$1["
306 %union { int i; }
307 %define global_tokens_and_yystype
308 %locations
309
310 %code {
311 // YYSTYPE contains a namespace reference.
312 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
313 lval->i = 3;
314 return 0;
315 }
316 }
317
318 %%
319
320 start: ;
321
322 %%
323
324 void
325 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
326 const std::string &msg)
327 {
328 std::cerr << "At " << loc << ": " << msg << std::endl;
329 }
330
331 int
332 main (void)
333 {
334 ]$1[::parser p;
335 return p.parse ();
336 }
337 ]])
338
339 AT_BISON_CHECK([[-o input.cc input.y]])
340
341 m4_if([$#], [1],
342 [AT_COMPILE_CXX([[input]], [[input.cc]])
343 AT_PARSER_CHECK([[./input]])])
344
345 ])
346
347 AT_SETUP([[Relative namespace references]])
348 AT_CHECK_NAMESPACE([[foo]])
349 AT_CHECK_NAMESPACE([[foo::bar]])
350 AT_CHECK_NAMESPACE([[foo::bar::baz]])
351 AT_CLEANUP
352
353 AT_SETUP([[Absolute namespace references]])
354 AT_CHECK_NAMESPACE([[::foo]])
355 AT_CHECK_NAMESPACE([[::foo::bar]])
356 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
357 AT_CHECK_NAMESPACE([[ ::foo]])
358 AT_CHECK_NAMESPACE([[ ::foo::bar]])
359 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
360 AT_CLEANUP
361
362 AT_SETUP([[Syntactically invalid namespace references]])
363 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
364 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
365 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
366 # contains single occurrences of `:'.
367 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
368 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
369 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
370 AT_CLEANUP
371
372
373 ## -------------------------------------- ##
374 ## Syntax error discarding no lookahead. ##
375 ## -------------------------------------- ##
376
377 # After a syntax error, lalr1.cc used to not check whether there
378 # actually is a lookahead before discarding the lookahead. As a result,
379 # it mistakenly invoked the destructor for the previous lookahead.
380
381 AT_SETUP([[Syntax error discarding no lookahead]])
382
383 AT_DATA_GRAMMAR([[input.yy]],
384 [[%skeleton "lalr1.cc"
385
386 %code {
387 #include <string>
388 int yylex (yy::parser::semantic_type *, yy::location *);
389 #define USE(Args)
390 }
391
392 %defines
393 %locations
394 %define parse.error verbose
395
396 %nonassoc 'a' ;
397
398 %destructor {
399 std::cerr << "Discarding 'a'." << std::endl;
400 } 'a'
401
402 %%
403
404 start: error-reduce consistent-error 'a' { USE ($3); };
405
406 error-reduce:
407 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
408 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
409 ;
410
411 consistent-error:
412 'a'
413 | /*empty*/ %prec 'a'
414 ;
415
416 // Provide another context in which all rules are useful so that this
417 // test case looks a little more realistic.
418 start: 'b' consistent-error ;
419
420 %%
421
422 int
423 yylex (yy::parser::semantic_type *, yy::location *)
424 {
425 static char const *input = "aa";
426 return *input++;
427 }
428
429 void
430 yy::parser::error (const location_type &, const std::string &m)
431 {
432 std::cerr << m << std::endl;
433 }
434
435 int
436 main (void)
437 {
438 yy::parser parser;
439 return parser.parse ();
440 }
441 ]])
442 AT_BISON_CHECK([[-o input.cc input.yy]])
443 AT_COMPILE_CXX([[input]])
444 # This used to print "Discarding 'a'." again at the end.
445 AT_PARSER_CHECK([[./input]], [[1]], [[]],
446 [[syntax error
447 Discarding 'a'.
448 Reducing 'a'.
449 ]])
450
451 AT_CLEANUP