]> git.saurik.com Git - bison.git/blob - tests/c++.at
maint: prepare to use date ranges in copyright notices.
[bison.git] / tests / c++.at
1 # Checking the output filenames. -*- Autotest -*-
2
3 # Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010 Free Software
4 # Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 AT_BANNER([[C++ Features.]])
20
21
22 ## ---------- ##
23 ## Variants. ##
24 ## ---------- ##
25
26 # AT_CHECK_VARIANTS([DIRECTIVES])
27 # -------------------------------
28 # Check the support of variants in C++, with the additional DIRECTIVES.
29 m4_define([AT_CHECK_VARIANTS],
30 [AT_SETUP([Variants $1])
31
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 variant
38 %locations
39 ]m4_bpatsubst([$1], [\\n], [
40 ])[
41
42 %code requires // code for the .hh file
43 {
44 #include <list>
45 #include <string>
46 typedef std::list<std::string> strings_type;
47 }
48
49 %code // code for the .cc file
50 {
51 #include <iostream>
52 #include <sstream>
53
54 static
55 #if defined USE_LEX_SYMBOL
56 yy::parser::symbol_type yylex ();
57 #else
58 yy::parser::token_type yylex (yy::parser::semantic_type* yylval,
59 yy::parser::location_type* yylloc);
60 #endif
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 { debug_stream() << $][$; }
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 #define STAGE_MAX 5
123 static
124 #if defined USE_LEX_SYMBOL
125 yy::parser::symbol_type yylex()
126 #else
127 yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
128 yy::parser::location_type* yylloc)
129 #endif
130 {
131 typedef yy::parser::token token;
132 typedef yy::parser::location_type location_type;
133 static int stage = -1;
134 ++stage;
135 if (stage == STAGE_MAX)
136 {
137 #if defined USE_LEX_SYMBOL
138 return yy::parser::make_END_OF_FILE (location_type ());
139 #else
140 *yylloc = location_type ();
141 return token::END_OF_FILE;
142 #endif
143 }
144 else if (stage % 2)
145 {
146 #if defined USE_LEX_SYMBOL
147 return yy::parser::make_NUMBER (stage, location_type ());
148 #else
149 # if defined ONE_STAGE_BUILD
150 yylval->build(stage);
151 # else
152 yylval->build<int>() = stage;
153 # endif
154 *yylloc = location_type ();
155 return token::NUMBER;
156 #endif
157 }
158 else
159 {
160 #if defined USE_LEX_SYMBOL
161 return yy::parser::make_TEXT (string_cast (stage), location_type ());
162 #else
163 # if defined ONE_STAGE_BUILD
164 yylval->build (string_cast (stage));
165 # else
166 yylval->build<std::string>() = string_cast (stage);
167 # endif
168 *yylloc = location_type ();
169 return token::TEXT;
170 #endif
171 }
172 abort();
173 }
174
175 void
176 yy::parser::error(const yy::parser::location_type&,
177 const std::string& message)
178 {
179 std::cerr << message << std::endl;
180 }
181
182 int
183 main (void)
184 {
185 yy::parser p;
186 p.set_debug_level(!!getenv("YYDEBUG"));
187 return p.parse();
188 }
189 ]])
190
191 AT_BISON_CHECK([-o list.cc list.yy])
192 AT_COMPILE_CXX([list])
193 AT_CHECK([./list], 0,
194 [(0, 1, 2, 4)
195 ])
196
197 AT_CLEANUP
198 ])
199
200 AT_CHECK_VARIANTS([])
201 AT_CHECK_VARIANTS([%define parse.assert])
202 AT_CHECK_VARIANTS([[%define parse.assert %code {\n#define ONE_STAGE_BUILD\n}]])
203 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
204 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define api.tokens.prefix "TOK_"]])
205
206
207 ## ----------------------- ##
208 ## Doxygen Documentation. ##
209 ## ----------------------- ##
210
211 m4_define([AT_CHECK_DOXYGEN],
212 [m4_case([$1],
213 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
214 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
215 [m4_fatal([invalid argument: $1])])
216 AT_SETUP([Doxygen $1 Documentation])
217
218 AT_DATA([input.yy],
219 [[%skeleton "lalr1.cc"
220 %locations
221 %debug
222 %defines
223 %%
224 exp:;
225 %%
226 yy::parser::error (const location& l, const std::string& m)
227 {
228 std::cerr << l << s << std::endl;
229 }
230 ]])
231
232 AT_BISON_CHECK([-o input.cc input.yy], 0)
233
234 AT_DATA([Doxyfile],
235 [# The PROJECT_NAME tag is a single word (or a sequence of words
236 # surrounded by quotes) that should identify the project.
237 PROJECT_NAME = "Bison C++ Parser"
238
239 # The QUIET tag can be used to turn on/off the messages that are
240 # generated by doxygen. Possible values are YES and NO. If left blank
241 # NO is used.
242 QUIET = YES
243
244 # The WARNINGS tag can be used to turn on/off the warning messages
245 # that are generated by doxygen. Possible values are YES and NO. If
246 # left blank NO is used.
247 WARNINGS = YES
248 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
249 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
250 # this flag will automatically be disabled.
251 WARN_IF_UNDOCUMENTED = YES
252 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
253 # for potential errors in the documentation, such as not documenting
254 # some parameters in a documented function, or documenting parameters
255 # that don't exist or using markup commands wrongly.
256 WARN_IF_DOC_ERROR = YES
257 # The WARN_FORMAT tag determines the format of the warning messages
258 # that doxygen can produce. The string should contain the $file,
259 # $line, and $text tags, which will be replaced by the file and line
260 # number from which the warning originated and the warning text.
261 WARN_FORMAT = "$file:$line: $text"
262
263 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
264 # entities in documentation are documented, even if no documentation
265 # was available. Private class members and static file members will
266 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
267 # to YES
268 EXTRACT_ALL = YES
269
270 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
271 # class will be included in the documentation.
272 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
273
274 # If the EXTRACT_STATIC tag is set to YES all static members of a file
275 # will be included in the documentation.
276 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
277 ])
278
279 AT_CHECK([doxygen --version || exit 77], 0, ignore)
280 AT_CHECK([doxygen], 0, [], [ignore])
281
282 AT_CLEANUP
283
284 m4_popdef([AT_DOXYGEN_PRIVATE])
285 ])# AT_CHECK_DOXYGEN
286
287 AT_CHECK_DOXYGEN([Public])
288 AT_CHECK_DOXYGEN([Private])
289
290
291 ## ------------ ##
292 ## Namespaces. ##
293 ## ------------ ##
294
295 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
296 # ---------------------------------------------------
297 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
298 # is specified, then Bison should accept the input, but compilation will fail,
299 # so don't check compilation.
300 m4_define([AT_CHECK_NAMESPACE],
301 [
302
303 AT_DATA_GRAMMAR([[input.y]],
304 [[%language "C++"
305 %defines
306 %define api.namespace "]$1["
307 %union { int i; }
308 %define global_tokens_and_yystype
309 %locations
310
311 %code {
312 // YYSTYPE contains a namespace reference.
313 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
314 lval->i = 3;
315 return 0;
316 }
317 }
318
319 %%
320
321 start: ;
322
323 %%
324
325 void
326 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
327 const std::string &msg)
328 {
329 std::cerr << "At " << loc << ": " << msg << std::endl;
330 }
331
332 int
333 main (void)
334 {
335 ]$1[::parser p;
336 return p.parse ();
337 }
338 ]])
339
340 AT_BISON_CHECK([[-o input.cc input.y]])
341
342 m4_if([$#], [1],
343 [AT_COMPILE_CXX([[input]], [[input.cc]])
344 AT_PARSER_CHECK([[./input]])])
345
346 ])
347
348 AT_SETUP([[Relative namespace references]])
349 AT_CHECK_NAMESPACE([[foo]])
350 AT_CHECK_NAMESPACE([[foo::bar]])
351 AT_CHECK_NAMESPACE([[foo::bar::baz]])
352 AT_CLEANUP
353
354 AT_SETUP([[Absolute namespace references]])
355 AT_CHECK_NAMESPACE([[::foo]])
356 AT_CHECK_NAMESPACE([[::foo::bar]])
357 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
358 AT_CHECK_NAMESPACE([[ ::foo]])
359 AT_CHECK_NAMESPACE([[ ::foo::bar]])
360 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
361 AT_CLEANUP
362
363 AT_SETUP([[Syntactically invalid namespace references]])
364 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
365 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
366 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
367 # contains single occurrences of `:'.
368 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
369 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
370 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
371 AT_CLEANUP
372
373
374 ## -------------------------------------- ##
375 ## Syntax error discarding no lookahead. ##
376 ## -------------------------------------- ##
377
378 # After a syntax error, lalr1.cc used to not check whether there
379 # actually is a lookahead before discarding the lookahead. As a result,
380 # it mistakenly invoked the destructor for the previous lookahead.
381
382 AT_SETUP([[Syntax error discarding no lookahead]])
383
384 AT_DATA_GRAMMAR([[input.yy]],
385 [[%skeleton "lalr1.cc"
386
387 %code {
388 #include <string>
389 int yylex (yy::parser::semantic_type *, yy::location *);
390 #define USE(Args)
391 }
392
393 %defines
394 %locations
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 *, yy::location *)
425 {
426 static char const *input = "aa";
427 return *input++;
428 }
429
430 void
431 yy::parser::error (const location_type &, const std::string &m)
432 {
433 std::cerr << m << std::endl;
434 }
435
436 int
437 main (void)
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