]> git.saurik.com Git - bison.git/blame - data/c++.m4
c: no longer require stdio.h when locations are enabled
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
7d6bad19 5# Copyright (C) 2002-2013 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
38de4e57
AD
22# b4_comment(TEXT, [PREFIX])
23# --------------------------
24# Put TEXT in comment. Prefix all the output lines with PREFIX.
25m4_define([b4_comment],
26[b4_comment_([$1], [$2// ], [$2// ])])
27
28
cf147260
AD
29## ---------------- ##
30## Default values. ##
31## ---------------- ##
32
a4e25e1d 33b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
34
35# Don't do that so that we remember whether we're using a user
36# request, or the default value.
37#
db8ab2be 38# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 39
a4e25e1d 40b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 41b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 42
c1d19e10 43b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
44b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 46 [std::string], [[true]], [[false]])])
cf147260
AD
47
48
67501061 49
793fbca5
JD
50## ----------- ##
51## Namespace. ##
52## ----------- ##
53
67501061
AD
54m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
55
793fbca5
JD
56
57# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
58# would compile as an absolute reference with `parser' in the global namespace.
59# b4_namespace_open would open an anonymous namespace and thus establish
60# internal linkage. This would compile. However, it's cryptic, and internal
61# linkage for the parser would be specified in all translation units that
62# include the header, which is always generated. If we ever need to permit
63# internal linkage somehow, surely we can find a cleaner approach.
64m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 65[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
66 [[namespace reference is empty]])])
67
68# Instead of assuming the C++ compiler will do it, Bison should reject any
69# invalid b4_namepsace_ref that would be converted to a valid
70# b4_namespace_open. The problem is that Bison doesn't always output
71# b4_namespace_ref to uncommented code but should reserve the ability to do so
72# in future releases without risking breaking any existing user grammars.
73# Specifically, don't allow empty names as b4_namespace_open would just convert
74# those into anonymous namespaces, and that might tempt some users.
75m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 76[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
77 [[namespace reference has consecutive "::"]])])
78m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 79[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
80 [[namespace reference has a trailing "::"]])])
81
82m4_define([b4_namespace_open],
67501061 83[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
84[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
87
88m4_define([b4_namespace_close],
67501061 89[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 90m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 92 [\1])),
b987342b 93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
94
95
e3990e3c
AD
96# b4_token_enums
97# --------------
c095d689 98# Output the definition of the tokens as enums.
cf147260 99m4_define([b4_token_enums],
e3990e3c
AD
100[[enum yytokentype
101 {
102 ]m4_join([,
103 ],
104 b4_symbol_map([b4_token_enum]))[
105 };]dnl
c095d689 106])
cf147260
AD
107
108
ef51bfa7
AD
109
110
cf147260
AD
111## ----------------- ##
112## Semantic Values. ##
113## ----------------- ##
114
b9e4eb5b
AD
115# b4_semantic_type_declare
116# ------------------------
117# Declare semantic_type.
118m4_define([b4_semantic_type_declare],
119[ /// Symbol semantic values.
120m4_ifdef([b4_stype],
121[ union semantic_type
d2e3c807
AD
122 {
123b4_user_stype
b9e4eb5b
AD
124 };],
125[m4_if(b4_tag_seen_flag, 0,
126[[ typedef int semantic_type;]],
5458913a 127[[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
b9e4eb5b
AD
128
129
4f84717d
AD
130# b4_public_types_declare
131# -----------------------
132# Define the public types: token, semantic value, location, and so forth.
1d6b689b 133# Depending on %define token_lex, may be output in the header or source file.
4f84717d 134m4_define([b4_public_types_declare],
5458913a 135[[#ifndef ]b4_api_PREFIX[STYPE
4f84717d
AD
136]b4_semantic_type_declare[
137#else
5458913a 138 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
139#endif]b4_locations_if([
140 /// Symbol locations.
f6b561d9 141 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 142 [[location]]) location_type;])[
1d6b689b 143
ff601366
AD
144 /// Syntax errors thrown from user actions.
145 struct syntax_error : std::runtime_error
146 {
147 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
148 location_type location;])[
149 };
150
4f84717d
AD
151 /// Tokens.
152 struct token
153 {
e3990e3c 154 ]b4_token_enums[
4f84717d 155 };
1d6b689b 156
4f84717d
AD
157 /// Token type.
158 typedef token::yytokentype token_type;
1d6b689b 159
1dbaf37f
TR
160 /// A complete symbol.
161 ///
162 /// Expects its Base type to provide access to the symbol type
163 /// via type_get().
164 ///
165 /// Provide access to semantic value]b4_locations_if([ and location])[.
166 template <typename Base>
167 struct basic_symbol : Base
1d6b689b
AD
168 {
169 /// Default constructor.
1dbaf37f
TR
170 inline basic_symbol ();
171]b4_locations_if([
172 /// Constructor.
173 inline basic_symbol (const location_type& l);])[
1d6b689b 174
1dbaf37f
TR
175 /// Copy constructor.
176 inline basic_symbol (const basic_symbol& other);
177
178 /// Constructor for valueless symbols.
179 inline basic_symbol (]b4_join(
180 [typename Base::value_type t],
1d6b689b
AD
181 b4_locations_if([const location_type& l]))[);
182
1dbaf37f
TR
183 /// Constructor for symbols with semantic value.
184 inline basic_symbol (]b4_join(
185 [typename Base::value_type t],
186 [const semantic_type& v],
187 b4_locations_if([const location_type& l]))[);
1d6b689b 188
04816a6f
TR
189 /// Assignment operator.
190 inline basic_symbol& operator= (const basic_symbol& other);
191
1dbaf37f
TR
192 /// Destructive move, \a s is emptied.
193 inline void move (basic_symbol& s);
1d6b689b
AD
194
195 /// The semantic value.
196 semantic_type value;]b4_locations_if([
197
198 /// The location.
199 location_type location;])[
200 };
201
1dbaf37f
TR
202 /// Type access provider for token (enum) based symbols.
203 struct by_type
1d6b689b 204 {
1d6b689b 205 /// Default constructor.
1dbaf37f 206 inline by_type ();
1d6b689b 207
1dbaf37f
TR
208 /// Copy constructor.
209 inline by_type (const by_type& other);
1d6b689b 210
1dbaf37f
TR
211 /// Constructor.
212 inline by_type (token_type t);
1d6b689b
AD
213
214 /// The symbol type.
215 int type;
216
1dbaf37f
TR
217 /// The type (corresponding to \a type).
218 inline int type_get () const;
1d6b689b 219
2b08bceb 220 /// The token.
1d6b689b 221 inline token_type token () const;
1dbaf37f
TR
222
223 typedef token_type value_type;
1d6b689b 224 };
1dbaf37f
TR
225
226 /// "External" symbols: returned by the scanner.
227 typedef basic_symbol<by_type> symbol_type;
228
0623bacc 229]b4_symbol_constructor_declare])
4f84717d
AD
230
231
1d6b689b
AD
232# b4_public_types_define
233# ----------------------
234# Provide the implementation needed by the public types.
235m4_define([b4_public_types_define],
403febca 236[[ inline
ff601366
AD
237 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
238 : std::runtime_error (m)]b4_locations_if([
239 , location (l)])[
240 {}
241
1dbaf37f
TR
242 // basic_symbol.
243 template <typename Base>
1dbaf37f
TR
244 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
245 : value ()
246 {}
247]b4_locations_if([
248 template <typename Base>
1dbaf37f
TR
249 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const location_type& l)
250 : value ()
251 , location (l)
252 {}])[
1d6b689b 253
04816a6f 254 template <typename Base>
04816a6f 255 ]b4_parser_class_name[::basic_symbol<Base>&
60607adb 256 ]b4_parser_class_name[::basic_symbol<Base>::operator= (const basic_symbol&)
04816a6f
TR
257 {
258 abort ();
259 }
260
1dbaf37f 261 template <typename Base>
1dbaf37f
TR
262 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
263 : Base (other)
264 , value ()]b4_locations_if([
265 , location (other.location)])[
1d6b689b 266 {
1dbaf37f
TR
267 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
268 [other.value])],
269 [value = other.value;])[
1d6b689b
AD
270 }
271
1d6b689b 272
1dbaf37f 273 template <typename Base>
1dbaf37f
TR
274 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
275 [typename Base::value_type t],
276 [const semantic_type& v],
277 b4_locations_if([const location_type& l]))[)
278 : Base (t)
279 , value ()]b4_locations_if([
280 , location (l)])[
1d6b689b 281 {
733fb7c5
TR
282 // FIXME: The YYUSE macro is only available in the .cc skeleton files. It
283 // is not available in .hh files, where this code is when using %defines.
284 (void) v;
1dbaf37f
TR
285 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [copy],
286 [v])],
287 [value = v;])[
1d6b689b
AD
288 }
289
1dbaf37f 290 template <typename Base>
1dbaf37f
TR
291 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
292 [typename Base::value_type t],
293 b4_locations_if([const location_type& l]))[)
294 : Base (t)]b4_locations_if([
295 , location (l)])[
296 {}
1d6b689b 297
1dbaf37f 298 template <typename Base>
1dbaf37f
TR
299 void
300 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
1d6b689b 301 {
1dbaf37f
TR
302 this->type = s.type_get ();]b4_locations_if([
303 location = s.location;])[
6656c9b5 304 ]b4_variant_if([b4_symbol_variant([s.type_get ()], [value], [move],
1dbaf37f
TR
305 [s.value])],
306 [value = s.value;])[
1d6b689b
AD
307 }
308
1dbaf37f 309 // by_type.
1dbaf37f
TR
310 ]b4_parser_class_name[::by_type::by_type ()
311 : type ()
312 {}
1d6b689b 313
1dbaf37f
TR
314 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
315 : type (other.type)
316 {}
1d6b689b 317
1dbaf37f
TR
318 ]b4_parser_class_name[::by_type::by_type (token_type t)
319 : type (yytranslate_ (t))
320 {}
7be08dfb 321
1d6b689b 322 int
1dbaf37f 323 ]b4_parser_class_name[::by_type::type_get () const
1d6b689b
AD
324 {
325 return type;
326 }
e36ec1f4 327]b4_token_ctor_if([[
1d6b689b 328 ]b4_parser_class_name[::token_type
1dbaf37f 329 ]b4_parser_class_name[::by_type::token () const
1d6b689b
AD
330 {
331 // YYTOKNUM[NUM] -- (External) token number corresponding to the
332 // (internal) symbol number NUM (which must be that of a token). */
333 static
334 const ]b4_int_type_for([b4_toknum])[
335 yytoken_number_[] =
336 {
337 ]b4_toknum[
338 };
339 return static_cast<token_type> (yytoken_number_[type]);
340 }
0623bacc
AD
341]])[]dnl
342b4_symbol_constructor_define])
343
344
345# b4_symbol_constructor_declare
346# b4_symbol_constructor_define
347# -----------------------------
348# Declare/define symbol constructors for all the value types.
349# Use at class-level. Redefined in variant.hh.
350m4_define([b4_symbol_constructor_declare], [])
351m4_define([b4_symbol_constructor_define], [])
352
1d6b689b 353
cbf25ce7
AD
354# b4_yytranslate_define
355# ---------------------
356# Define yytranslate_. Sometimes used in the header file,
357# sometimes in the cc file.
358m4_define([b4_yytranslate_define],
359[[ // Symbol number corresponding to token number t.
360 ]b4_parser_class_name[::token_number_type
e36ec1f4 361 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
362 [int])[ t)
363 {
364 static
365 const token_number_type
366 translate_table[] =
367 {
368]b4_translate[
369 };
370 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
371 const token_number_type undef_token_ = ]b4_undef_token_number[;
372
373 if (static_cast<int>(t) <= yyeof_)
374 return yyeof_;
375 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
376 return translate_table[t];
377 else
378 return undef_token_;
379 }
380]])
381
1d6b689b 382
cf147260
AD
383# b4_lhs_value([TYPE])
384# --------------------
385# Expansion of $<TYPE>$.
386m4_define([b4_lhs_value],
1fa5d8bb 387[b4_symbol_value([yyval], [$1])])
cf147260
AD
388
389
390# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
391# --------------------------------------
392# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
393# symbols on RHS.
394m4_define([b4_rhs_value],
1fa5d8bb
AD
395[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
396
cf147260
AD
397
398# b4_lhs_location()
399# -----------------
400# Expansion of @$.
401m4_define([b4_lhs_location],
402[(yyloc)])
403
404
405# b4_rhs_location(RULE-LENGTH, NUM)
406# ---------------------------------
407# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
408# on RHS.
409m4_define([b4_rhs_location],
d1ff7a7c 410[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
411
412
413# b4_parse_param_decl
414# -------------------
415# Extra formal arguments of the constructor.
416# Change the parameter names from "foo" into "foo_yyarg", so that
417# there is no collision bw the user chosen attribute name, and the
418# argument name in the constructor.
419m4_define([b4_parse_param_decl],
420[m4_ifset([b4_parse_param],
421 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
422
423m4_define([b4_parse_param_decl_1],
424[$1_yyarg])
425
426
427
428# b4_parse_param_cons
429# -------------------
430# Extra initialisations of the constructor.
431m4_define([b4_parse_param_cons],
432 [m4_ifset([b4_parse_param],
e9690142 433 [
cf147260
AD
434 b4_cc_constructor_calls(b4_parse_param)])])
435m4_define([b4_cc_constructor_calls],
e9690142 436 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
437 ], [$@])])
438m4_define([b4_cc_constructor_call],
e9690142 439 [$2 ($2_yyarg)])
cf147260
AD
440
441# b4_parse_param_vars
442# -------------------
443# Extra instance variables.
444m4_define([b4_parse_param_vars],
445 [m4_ifset([b4_parse_param],
e9690142 446 [
ac826bc4 447 // User arguments.
cf147260
AD
448b4_cc_var_decls(b4_parse_param)])])
449m4_define([b4_cc_var_decls],
e9690142 450 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
451], [$@])])
452m4_define([b4_cc_var_decl],
e9690142 453 [ $1;])
426903aa
AD
454
455
456## ---------##
457## Values. ##
458## ---------##
459
460# b4_yylloc_default_define
461# ------------------------
462# Define YYLLOC_DEFAULT.
463m4_define([b4_yylloc_default_define],
464[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
465 If N is 0, then set CURRENT to the empty location which ends
466 the previous symbol: RHS[0] (always defined). */
467
468# ifndef YYLLOC_DEFAULT
469# define YYLLOC_DEFAULT(Current, Rhs, N) \
470 do \
471 if (N) \
472 { \
473 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
474 (Current).end = YYRHSLOC (Rhs, N).end; \
475 } \
476 else \
477 { \
478 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
479 } \
480 while (/*CONSTCOND*/ false)
481# endif
482]])