]> git.saurik.com Git - bison.git/blob - tests/c++.at
Test token.prefix.
[bison.git] / tests / c++.at
1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
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.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER([[C++ Features.]])
18
19
20 ## ---------- ##
21 ## Variants. ##
22 ## ---------- ##
23
24 # AT_CHECK_VARIANTS([DIRECTIVES])
25 # -------------------------------
26 # Check the support of variants in C++, with the additional DIRECTIVES.
27 m4_define([AT_CHECK_VARIANTS],
28 [AT_SETUP([Variants $1])
29
30 # Store strings and integers in a list of strings.
31 AT_DATA([list.yy],
32 [[%debug
33 %skeleton "lalr1.cc"
34 %defines
35 %define variant
36 %locations
37 ]m4_bpatsubst([$1], [\\n], [
38 ])[
39
40 %code requires // code for the .hh file
41 {
42 #include <list>
43 #include <string>
44 typedef std::list<std::string> strings_type;
45 }
46
47 %code // code for the .cc file
48 {
49 #include <algorithm>
50 #include <iostream>
51 #include <iterator>
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 std::copy(s.begin(), s.end(),
70 std::ostream_iterator<strings_type::value_type>(o, "\n"));
71 return o;
72 }
73 }
74
75 // Conversion to string.
76 template <typename T>
77 inline
78 std::string
79 string_cast (const T& t)
80 {
81 std::ostringstream o;
82 o << t;
83 return o.str();
84 }
85 }
86
87 %token <std::string> TEXT;
88 %token <int> NUMBER;
89 %printer { debug_stream() << $][$; } <int> <std::string> <strings_type>;
90 %token END_OF_FILE 0;
91
92 %type <std::string> item;
93 %type <strings_type> list result;
94
95 %%
96
97 result:
98 list { std::cout << $][1; }
99 ;
100
101 list:
102 /* nothing */ { /* Generates an empty string list */ }
103 | list item { std::swap($][$,$][1); $$.push_back($][2); }
104 ;
105
106 item:
107 TEXT { std::swap($][$,$][1); }
108 | NUMBER { $][$ = string_cast($][1); }
109 ;
110 %%
111
112 #define STAGE_MAX 5
113 static
114 #if defined USE_LEX_SYMBOL
115 yy::parser::symbol_type yylex()
116 #else
117 yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
118 yy::parser::location_type* yylloc)
119 #endif
120 {
121 typedef yy::parser::token token;
122 typedef yy::parser::location_type location_type;
123 static int stage = -1;
124 ++stage;
125 if (stage == STAGE_MAX)
126 {
127 #if defined USE_LEX_SYMBOL
128 return yy::parser::make_END_OF_FILE (location_type ());
129 #else
130 *yylloc = location_type ();
131 return token::END_OF_FILE;
132 #endif
133 }
134 else if (stage % 2)
135 {
136 #if defined USE_LEX_SYMBOL
137 return yy::parser::make_NUMBER (stage, location_type ());
138 #else
139 # if defined ONE_STAGE_BUILD
140 yylval->build(stage);
141 # else
142 yylval->build<int>() = stage;
143 # endif
144 *yylloc = location_type ();
145 return token::NUMBER;
146 #endif
147 }
148 else
149 {
150 #if defined USE_LEX_SYMBOL
151 return yy::parser::make_TEXT (string_cast (stage), location_type ());
152 #else
153 # if defined ONE_STAGE_BUILD
154 yylval->build (string_cast (stage));
155 # else
156 yylval->build<std::string>() = string_cast (stage);
157 # endif
158 *yylloc = location_type ();
159 return token::TEXT;
160 #endif
161 }
162 abort();
163 }
164
165 void
166 yy::parser::error(const yy::parser::location_type&,
167 const std::string& message)
168 {
169 std::cerr << message << std::endl;
170 }
171
172 int
173 main(int argc, char *argv[])
174 {
175 yy::parser p;
176 p.set_debug_level(!!getenv("YYDEBUG"));
177 return p.parse();
178 }
179 ]])
180
181 AT_BISON_CHECK([-o list.cc list.yy])
182 AT_COMPILE_CXX([list])
183 AT_CHECK([./list], 0,
184 [0
185 1
186 2
187 3
188 4
189 ])
190
191 AT_CLEANUP
192 ])
193
194 AT_CHECK_VARIANTS([])
195 AT_CHECK_VARIANTS([%define assert])
196 AT_CHECK_VARIANTS([[%define assert %code {\n#define ONE_STAGE_BUILD\n}]])
197 AT_CHECK_VARIANTS([[%define assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
198 AT_CHECK_VARIANTS([[%define assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define token.prefix "TOK_"]])
199
200
201 ## ----------------------- ##
202 ## Doxygen Documentation. ##
203 ## ----------------------- ##
204
205 m4_define([AT_CHECK_DOXYGEN],
206 [m4_case([$1],
207 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
208 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
209 [m4_fatal([invalid argument: $1])])
210 AT_SETUP([Doxygen $1 Documentation])
211
212 AT_DATA([input.yy],
213 [[%skeleton "lalr1.cc"
214 %locations
215 %debug
216 %defines
217 %%
218 exp:;
219 %%
220 yy::parser::error (const location& l, const std::string& m)
221 {
222 std::cerr << l << s << std::endl;
223 }
224 ]])
225
226 AT_BISON_CHECK([-o input.cc input.yy], 0)
227
228 AT_DATA([Doxyfile],
229 [# The PROJECT_NAME tag is a single word (or a sequence of words
230 # surrounded by quotes) that should identify the project.
231 PROJECT_NAME = "Bison C++ Parser"
232
233 # The QUIET tag can be used to turn on/off the messages that are
234 # generated by doxygen. Possible values are YES and NO. If left blank
235 # NO is used.
236 QUIET = YES
237
238 # The WARNINGS tag can be used to turn on/off the warning messages
239 # that are generated by doxygen. Possible values are YES and NO. If
240 # left blank NO is used.
241 WARNINGS = YES
242 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
243 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
244 # this flag will automatically be disabled.
245 WARN_IF_UNDOCUMENTED = YES
246 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
247 # for potential errors in the documentation, such as not documenting
248 # some parameters in a documented function, or documenting parameters
249 # that don't exist or using markup commands wrongly.
250 WARN_IF_DOC_ERROR = YES
251 # The WARN_FORMAT tag determines the format of the warning messages
252 # that doxygen can produce. The string should contain the $file,
253 # $line, and $text tags, which will be replaced by the file and line
254 # number from which the warning originated and the warning text.
255 WARN_FORMAT = "$file:$line: $text"
256
257 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
258 # entities in documentation are documented, even if no documentation
259 # was available. Private class members and static file members will
260 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
261 # to YES
262 EXTRACT_ALL = YES
263
264 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
265 # class will be included in the documentation.
266 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
267
268 # If the EXTRACT_STATIC tag is set to YES all static members of a file
269 # will be included in the documentation.
270 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
271 ])
272
273 AT_CHECK([doxygen --version || exit 77], 0, ignore)
274 AT_CHECK([doxygen], 0, [], [ignore])
275
276 AT_CLEANUP
277
278 m4_popdef([AT_DOXYGEN_PRIVATE])
279 ])# AT_CHECK_DOXYGEN
280
281 AT_CHECK_DOXYGEN([Public])
282 AT_CHECK_DOXYGEN([Private])
283
284
285
286
287 ## ------------ ##
288 ## Namespaces. ##
289 ## ------------ ##
290
291 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
292 # ---------------------------------------------------
293 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
294 # is specified, then Bison should accept the input, but compilation will fail,
295 # so don't check compilation.
296 m4_define([AT_CHECK_NAMESPACE],
297 [
298
299 AT_DATA_GRAMMAR([[input.y]],
300 [[%language "C++"
301 %defines
302 %define namespace "]$1["
303 %union { int i; }
304 %define global_tokens_and_yystype
305 %locations
306
307 %code {
308 // YYSTYPE contains a namespace reference.
309 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type* lloc) {
310 lval->i = 3;
311 return 0;
312 }
313 }
314
315 %%
316
317 start: ;
318
319 %%
320
321 void
322 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
323 const std::string &msg)
324 {
325 std::cerr << "At " << loc << ": " << msg << std::endl;
326 }
327
328 int
329 main (void)
330 {
331 ]$1[::parser p;
332 return p.parse ();
333 }
334 ]])
335
336 AT_BISON_CHECK([[-o input.cc input.y]])
337
338 m4_if([$#], [1],
339 [AT_COMPILE_CXX([[input]], [[input.cc]])
340 AT_PARSER_CHECK([[./input]])])
341
342 ])
343
344 AT_SETUP([[Relative namespace references]])
345 AT_CHECK_NAMESPACE([[foo]])
346 AT_CHECK_NAMESPACE([[foo::bar]])
347 AT_CHECK_NAMESPACE([[foo::bar::baz]])
348 AT_CLEANUP
349
350 AT_SETUP([[Absolute namespace references]])
351 AT_CHECK_NAMESPACE([[::foo]])
352 AT_CHECK_NAMESPACE([[::foo::bar]])
353 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
354 AT_CHECK_NAMESPACE([[ ::foo]])
355 AT_CHECK_NAMESPACE([[ ::foo::bar]])
356 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
357 AT_CLEANUP
358
359 AT_SETUP([[Syntactically invalid namespace references]])
360 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
361 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
362 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
363 # contains single occurrences of `:'.
364 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
365 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
366 AT_CHECK_NAMESPACE([[foo::bar::(baz]], [[-]])
367 AT_CLEANUP