]> git.saurik.com Git - bison.git/blob - tests/c++.at
Merge branch 'maint'
[bison.git] / tests / c++.at
1 # Checking the output filenames. -*- Autotest -*-
2
3 # Copyright (C) 2004-2005, 2007-2012 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 { yyo << $][$; }
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 #ifndef USE_LEX_SYMBOL
131 typedef yy::parser::token token;
132 #endif
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 ());
140 #else
141 *yylloc = location_type ();
142 return token::END_OF_FILE;
143 #endif
144 }
145 else if (stage % 2)
146 {
147 #if defined USE_LEX_SYMBOL
148 return yy::parser::make_NUMBER (stage, location_type ());
149 #else
150 # if defined ONE_STAGE_BUILD
151 yylval->build (stage);
152 # else
153 yylval->build<int>() = stage;
154 # endif
155 *yylloc = location_type ();
156 return token::NUMBER;
157 #endif
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 ();
174 }
175
176 void
177 yy::parser::error (const yy::parser::location_type&,
178 const std::string& message)
179 {
180 std::cerr << message << std::endl;
181 }
182
183 int
184 main ()
185 {
186 yy::parser p;
187 p.set_debug_level (!!getenv ("YYDEBUG"));
188 return p.parse ();
189 }
190 ]])
191
192 AT_BISON_CHECK([-o list.cc list.yy])
193 AT_COMPILE_CXX([list])
194 AT_CHECK([./list], 0,
195 [(0, 1, 2, 4)
196 ])
197
198 AT_CLEANUP
199 ])
200
201 AT_CHECK_VARIANTS([])
202 AT_CHECK_VARIANTS([%define parse.assert])
203 AT_CHECK_VARIANTS([[%define parse.assert %code {\n#define ONE_STAGE_BUILD\n}]])
204 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
205 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define api.tokens.prefix "TOK_"]])
206
207
208 ## ----------------------- ##
209 ## Doxygen Documentation. ##
210 ## ----------------------- ##
211
212 m4_define([AT_CHECK_DOXYGEN],
213 [m4_case([$1],
214 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
215 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
216 [m4_fatal([invalid argument: $1])])
217 AT_SETUP([Doxygen $1 Documentation])
218
219 AT_DATA([input.yy],
220 [[%skeleton "lalr1.cc"
221 %locations
222 %debug
223 %defines
224 %%
225 exp:;
226 %%
227 yy::parser::error (const location& l, const std::string& m)
228 {
229 std::cerr << l << s << std::endl;
230 }
231 ]])
232
233 AT_BISON_CHECK([-o input.cc input.yy], 0)
234
235 AT_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.
238 PROJECT_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.
243 QUIET = 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.
248 WARNINGS = 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.
252 WARN_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.
257 WARN_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.
262 WARN_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
269 EXTRACT_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.
273 EXTRACT_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.
277 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
278 ])
279
280 AT_CHECK([doxygen --version || exit 77], 0, ignore)
281 AT_CHECK([doxygen], 0, [], [ignore])
282
283 AT_CLEANUP
284
285 m4_popdef([AT_DOXYGEN_PRIVATE])
286 ])# AT_CHECK_DOXYGEN
287
288 AT_CHECK_DOXYGEN([Public])
289 AT_CHECK_DOXYGEN([Private])
290
291
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.
301 m4_define([AT_CHECK_NAMESPACE],
302 [
303
304 AT_DATA_GRAMMAR([[input.y]],
305 [[%language "C++"
306 %defines
307 %define api.namespace "]$1["
308 %union { int i; }
309 %define global_tokens_and_yystype
310 %locations
311
312 %code {
313 // YYSTYPE contains a namespace reference.
314 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
315 lval->i = 3;
316 return 0;
317 }
318 }
319
320 %%
321
322 start: ;
323
324 %%
325
326 void
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
333 int
334 main ()
335 {
336 ]$1[::parser p;
337 return p.parse ();
338 }
339 ]])
340
341 AT_BISON_CHECK([[-o input.cc input.y]])
342
343 m4_if([$#], [1],
344 [AT_COMPILE_CXX([[input]], [[input.cc]])
345 AT_PARSER_CHECK([[./input]])])
346
347 ])
348
349 AT_SETUP([[Relative namespace references]])
350 AT_CHECK_NAMESPACE([[foo]])
351 AT_CHECK_NAMESPACE([[foo::bar]])
352 AT_CHECK_NAMESPACE([[foo::bar::baz]])
353 AT_CLEANUP
354
355 AT_SETUP([[Absolute namespace references]])
356 AT_CHECK_NAMESPACE([[::foo]])
357 AT_CHECK_NAMESPACE([[::foo::bar]])
358 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
359 AT_CHECK_NAMESPACE([[ ::foo]])
360 AT_CHECK_NAMESPACE([[ ::foo::bar]])
361 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
362 AT_CLEANUP
363
364 AT_SETUP([[Syntactically invalid namespace references]])
365 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
366 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
367 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
368 # contains single occurrences of `:'.
369 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
370 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
371 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
372 AT_CLEANUP
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
383 AT_SETUP([[Syntax error discarding no lookahead]])
384
385 AT_DATA_GRAMMAR([[input.yy]],
386 [[%skeleton "lalr1.cc"
387
388 %code {
389 #include <string>
390 int yylex (yy::parser::semantic_type *);
391 #define USE(Args)
392 }
393
394 %defines
395 %define parse.error verbose
396
397 %nonassoc 'a' ;
398
399 %destructor {
400 std::cerr << "Discarding 'a'." << std::endl;
401 } 'a'
402
403 %%
404
405 start: error-reduce consistent-error 'a' { USE ($3); };
406
407 error-reduce:
408 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
409 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
410 ;
411
412 consistent-error:
413 'a'
414 | /*empty*/ %prec 'a'
415 ;
416
417 // Provide another context in which all rules are useful so that this
418 // test case looks a little more realistic.
419 start: 'b' consistent-error ;
420
421 %%
422
423 int
424 yylex (yy::parser::semantic_type *)
425 {
426 static char const *input = "aa";
427 return *input++;
428 }
429
430 void
431 yy::parser::error (const std::string &m)
432 {
433 std::cerr << m << std::endl;
434 }
435
436 int
437 main ()
438 {
439 yy::parser parser;
440 return parser.parse ();
441 }
442 ]])
443 AT_BISON_CHECK([[-o input.cc input.yy]])
444 AT_COMPILE_CXX([[input]])
445 # This used to print "Discarding 'a'." again at the end.
446 AT_PARSER_CHECK([[./input]], [[1]], [[]],
447 [[syntax error
448 Discarding 'a'.
449 Reducing 'a'.
450 ]])
451
452 AT_CLEANUP
453
454
455 ## --------------------------- ##
456 ## Syntax error as exception. ##
457 ## --------------------------- ##
458
459 AT_SETUP([[Syntax error as exception]])
460
461 AT_DATA_GRAMMAR([[input.yy]],
462 [[%skeleton "lalr1.cc"
463
464 %code
465 {
466 #include <cstdlib>
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
476 start:
477 thing
478 | start thing
479 ;
480
481 thing:
482 error { std::cerr << "caught error" << std::endl; }
483 | item
484 ;
485
486 item:
487 'a'
488 | 's'
489 {
490 throw yy::parser::syntax_error ("invalid expression");
491 }
492
493 %%
494
495 int
496 yylex (yy::parser::semantic_type *)
497 {
498 // 's': syntax error, 'l': lexical error.
499 static char const *input = "asal";
500 switch (int res = *input++)
501 {
502 case 'l':
503 throw yy::parser::syntax_error ("invalid character");
504 default:
505 return res;
506 }
507 }
508
509 void
510 yy::parser::error (const std::string &m)
511 {
512 std::cerr << "error: " << m << std::endl;
513 }
514
515 int
516 main ()
517 {
518 yy::parser parser;
519 parser.set_debug_level (!!getenv ("YYDEBUG"));
520 return parser.parse ();
521 }
522 ]])
523 AT_BISON_CHECK([[-o input.cc input.yy]])
524 AT_COMPILE_CXX([[input]])
525
526 AT_PARSER_CHECK([[./input]], [[0]], [[]],
527 [[error: invalid expression
528 caught error
529 error: invalid character
530 caught error
531 ]])
532
533 AT_CLEANUP