]> git.saurik.com Git - bison.git/blame - tests/c++.at
Test the make_TOKEN interface.
[bison.git] / tests / c++.at
CommitLineData
e019c247 1# Checking the output filenames. -*- Autotest -*-
76307410 2# Copyright (C) 2004, 2005, 2007, 2008 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.
31AT_DATA([list.yy],
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{
49#include <algorithm>
50#include <iostream>
51#include <iterator>
52#include <sstream>
53
dddec537
AD
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
76307410 61
dddec537 62 // Printing a list of strings (for %printer).
76307410
AD
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
97result:
e5eb92e7 98 list { std::cout << $][1; }
76307410
AD
99;
100
101list:
102 /* nothing */ { /* Generates an empty string list */ }
103| list item { std::swap($][$,$][1); $$.push_back($][2); }
104;
105
106item:
107 TEXT { std::swap($][$,$][1); }
108| NUMBER { $][$ = string_cast($][1); }
109;
110%%
111
dddec537 112#define STAGE_MAX 5
76307410 113static
dddec537
AD
114#if defined USE_LEX_SYMBOL
115yy::parser::symbol_type yylex()
116#else
117yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
118 yy::parser::location_type* yylloc)
119#endif
76307410 120{
dddec537
AD
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 ());
e5eb92e7 129#else
dddec537
AD
130 *yylloc = location_type ();
131 return token::END_OF_FILE;
e5eb92e7 132#endif
dddec537
AD
133 }
134 else if (stage % 2)
135 {
136#if defined USE_LEX_SYMBOL
137 return yy::parser::make_NUMBER (stage, location_type ());
e5eb92e7 138#else
dddec537
AD
139# if defined ONE_STAGE_BUILD
140 yylval->build(stage);
141# else
76307410 142 yylval->build<int>() = stage;
dddec537
AD
143# endif
144 *yylloc = location_type ();
145 return token::NUMBER;
e5eb92e7 146#endif
dddec537
AD
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();
76307410
AD
163}
164
76307410 165void
dddec537
AD
166yy::parser::error(const yy::parser::location_type&,
167 const std::string& message)
76307410 168{
2ea7730c 169 std::cerr << message << std::endl;
76307410
AD
170}
171
e5eb92e7
AD
172int
173main(int argc, char *argv[])
76307410
AD
174{
175 yy::parser p;
176 p.set_debug_level(!!getenv("YYDEBUG"));
177 return p.parse();
178}
179]])
180
e5eb92e7 181AT_BISON_CHECK([-o list.cc list.yy])
76307410
AD
182AT_COMPILE_CXX([list])
183AT_CHECK([./list], 0,
e5eb92e7 184[0
76307410
AD
1851
1862
1873
e5eb92e7 1884
76307410
AD
189])
190
191AT_CLEANUP
192])
193
194AT_CHECK_VARIANTS([])
195AT_CHECK_VARIANTS([%define assert])
e5eb92e7 196AT_CHECK_VARIANTS([[%define assert %code {\n#define ONE_STAGE_BUILD\n}]])
dddec537 197AT_CHECK_VARIANTS([[%define assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
76307410
AD
198
199
e019c247
AD
200## ----------------------- ##
201## Doxygen Documentation. ##
202## ----------------------- ##
203
204m4_define([AT_CHECK_DOXYGEN],
205[m4_case([$1],
206 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
207 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
208 [m4_fatal([invalid argument: $1])])
209AT_SETUP([Doxygen $1 Documentation])
210
211AT_DATA([input.yy],
212[[%skeleton "lalr1.cc"
213%locations
214%debug
215%defines
216%%
217exp:;
218%%
219yy::parser::error (const location& l, const std::string& m)
220{
221 std::cerr << l << s << std::endl;
222}
223]])
224
da730230 225AT_BISON_CHECK([-o input.cc input.yy], 0)
e019c247
AD
226
227AT_DATA([Doxyfile],
228[# The PROJECT_NAME tag is a single word (or a sequence of words
229# surrounded by quotes) that should identify the project.
230PROJECT_NAME = "Bison C++ Parser"
231
232# The QUIET tag can be used to turn on/off the messages that are
233# generated by doxygen. Possible values are YES and NO. If left blank
234# NO is used.
235QUIET = YES
236
237# The WARNINGS tag can be used to turn on/off the warning messages
238# that are generated by doxygen. Possible values are YES and NO. If
239# left blank NO is used.
240WARNINGS = YES
241# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
242# warnings for undocumented members. If EXTRACT_ALL is set to YES then
243# this flag will automatically be disabled.
244WARN_IF_UNDOCUMENTED = YES
245# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
246# for potential errors in the documentation, such as not documenting
247# some parameters in a documented function, or documenting parameters
248# that don't exist or using markup commands wrongly.
249WARN_IF_DOC_ERROR = YES
250# The WARN_FORMAT tag determines the format of the warning messages
251# that doxygen can produce. The string should contain the $file,
252# $line, and $text tags, which will be replaced by the file and line
253# number from which the warning originated and the warning text.
254WARN_FORMAT = "$file:$line: $text"
255
256# If the EXTRACT_ALL tag is set to YES doxygen will assume all
257# entities in documentation are documented, even if no documentation
258# was available. Private class members and static file members will
259# be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
260# to YES
261EXTRACT_ALL = YES
262
263# If the EXTRACT_PRIVATE tag is set to YES all private members of a
264# class will be included in the documentation.
265EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
266
267# If the EXTRACT_STATIC tag is set to YES all static members of a file
268# will be included in the documentation.
269EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
270])
271
272AT_CHECK([doxygen --version || exit 77], 0, ignore)
273AT_CHECK([doxygen], 0, [], [ignore])
274
275AT_CLEANUP
276
277m4_popdef([AT_DOXYGEN_PRIVATE])
278])# AT_CHECK_DOXYGEN
279
280AT_CHECK_DOXYGEN([Public])
281AT_CHECK_DOXYGEN([Private])
793fbca5 282
76307410
AD
283
284
285
793fbca5
JD
286## ------------ ##
287## Namespaces. ##
288## ------------ ##
289
290# AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
291# ---------------------------------------------------
292# See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
293# is specified, then Bison should accept the input, but compilation will fail,
294# so don't check compilation.
295m4_define([AT_CHECK_NAMESPACE],
296[
297
298AT_DATA_GRAMMAR([[input.y]],
299[[%language "C++"
300%defines
301%define namespace "]$1["
302%union { int i; }
303%define global_tokens_and_yystype
2ea7730c 304%locations
793fbca5
JD
305
306%code {
307 // YYSTYPE contains a namespace reference.
2ea7730c 308 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type* lloc) {
793fbca5
JD
309 lval->i = 3;
310 return 0;
311 }
312}
313
314%%
315
316start: ;
317
318%%
319
320void
321]$1[::parser::error (const ]$1[::parser::location_type &loc,
322 const std::string &msg)
323{
324 std::cerr << "At " << loc << ": " << msg << std::endl;
325}
326
327int
328main (void)
329{
330 ]$1[::parser p;
331 return p.parse ();
332}
333]])
334
da730230 335AT_BISON_CHECK([[-o input.cc input.y]])
793fbca5
JD
336
337m4_if([$#], [1],
338[AT_COMPILE_CXX([[input]], [[input.cc]])
339AT_PARSER_CHECK([[./input]])])
340
341])
342
343AT_SETUP([[Relative namespace references]])
344AT_CHECK_NAMESPACE([[foo]])
345AT_CHECK_NAMESPACE([[foo::bar]])
346AT_CHECK_NAMESPACE([[foo::bar::baz]])
347AT_CLEANUP
348
349AT_SETUP([[Absolute namespace references]])
350AT_CHECK_NAMESPACE([[::foo]])
351AT_CHECK_NAMESPACE([[::foo::bar]])
352AT_CHECK_NAMESPACE([[::foo::bar::baz]])
353AT_CHECK_NAMESPACE([[ ::foo]])
354AT_CHECK_NAMESPACE([[ ::foo::bar]])
355AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
356AT_CLEANUP
357
358AT_SETUP([[Syntactically invalid namespace references]])
359AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
360AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
361# This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
362# contains single occurrences of `:'.
363AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
364AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
365AT_CHECK_NAMESPACE([[foo::bar::(baz]], [[-]])
366AT_CLEANUP