]> git.saurik.com Git - bison.git/blame - data/c++.m4
xml: also use "%empty" with html output
[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
07e65a77
AD
28## -------- ##
29## Checks. ##
30## -------- ##
31
32b4_percent_define_check_kind([api.namespace], [code], [deprecated])
6ce4b4ff 33b4_percent_define_check_kind([parser_class_name], [code], [deprecated])
38de4e57 34
cf147260
AD
35## ---------------- ##
36## Default values. ##
37## ---------------- ##
38
a4e25e1d 39b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
40
41# Don't do that so that we remember whether we're using a user
42# request, or the default value.
43#
db8ab2be 44# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 45
a4e25e1d 46b4_percent_define_default([[filename_type]], [[std::string]])
07e65a77 47# Make it a warning for those who used betas of Bison 3.0.
171ad99d 48b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 49
c1d19e10 50b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
51b4_percent_define_default([[define_location_comparison]],
52 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 53 [std::string], [[true]], [[false]])])
cf147260
AD
54
55
67501061 56
793fbca5
JD
57## ----------- ##
58## Namespace. ##
59## ----------- ##
60
67501061
AD
61m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
62
793fbca5 63
45eebca4
AD
64# Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
65# would compile as an absolute reference with 'parser' in the global namespace.
793fbca5
JD
66# b4_namespace_open would open an anonymous namespace and thus establish
67# internal linkage. This would compile. However, it's cryptic, and internal
68# linkage for the parser would be specified in all translation units that
69# include the header, which is always generated. If we ever need to permit
70# internal linkage somehow, surely we can find a cleaner approach.
71m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 72[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
73 [[namespace reference is empty]])])
74
75# Instead of assuming the C++ compiler will do it, Bison should reject any
76# invalid b4_namepsace_ref that would be converted to a valid
77# b4_namespace_open. The problem is that Bison doesn't always output
78# b4_namespace_ref to uncommented code but should reserve the ability to do so
79# in future releases without risking breaking any existing user grammars.
80# Specifically, don't allow empty names as b4_namespace_open would just convert
81# those into anonymous namespaces, and that might tempt some users.
82m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 83[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
84 [[namespace reference has consecutive "::"]])])
85m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 86[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
87 [[namespace reference has a trailing "::"]])])
88
89m4_define([b4_namespace_open],
67501061 90[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
91[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
92 [^\(.\)[ ]*::], [\1])),
93 [::], [ { namespace ])[ {]])])
94
95m4_define([b4_namespace_close],
67501061 96[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 97m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 98 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 99 [\1])),
b987342b 100 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
101
102
e3990e3c
AD
103# b4_token_enums
104# --------------
c095d689 105# Output the definition of the tokens as enums.
cf147260 106m4_define([b4_token_enums],
e3990e3c
AD
107[[enum yytokentype
108 {
109 ]m4_join([,
110 ],
111 b4_symbol_map([b4_token_enum]))[
112 };]dnl
c095d689 113])
cf147260
AD
114
115
ef51bfa7
AD
116
117
cf147260
AD
118## ----------------- ##
119## Semantic Values. ##
120## ----------------- ##
121
7601a471
AD
122
123
124# b4_value_type_declare
125# ---------------------
b9e4eb5b 126# Declare semantic_type.
7601a471 127m4_define([b4_value_type_declare],
6574576c 128[b4_value_type_setup[]dnl
b9e4eb5b 129[ /// Symbol semantic values.
435575cb
AD
130]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
131[code],
132[[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
133[m4_bmatch(b4_percent_define_get([[api.value.type]]),
134[union\|union-directive],
6574576c 135[[ union semantic_type
d2e3c807 136 {
435575cb
AD
137 ]b4_user_union_members[
138 };]])])dnl
139])
b9e4eb5b
AD
140
141
4f84717d
AD
142# b4_public_types_declare
143# -----------------------
144# Define the public types: token, semantic value, location, and so forth.
1d6b689b 145# Depending on %define token_lex, may be output in the header or source file.
4f84717d 146m4_define([b4_public_types_declare],
5458913a 147[[#ifndef ]b4_api_PREFIX[STYPE
7601a471 148]b4_value_type_declare[
4f84717d 149#else
5458913a 150 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
151#endif]b4_locations_if([
152 /// Symbol locations.
f6b561d9 153 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 154 [[location]]) location_type;])[
1d6b689b 155
ff601366
AD
156 /// Syntax errors thrown from user actions.
157 struct syntax_error : std::runtime_error
158 {
159 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
160 location_type location;])[
161 };
162
4f84717d
AD
163 /// Tokens.
164 struct token
165 {
e3990e3c 166 ]b4_token_enums[
4f84717d 167 };
1d6b689b 168
26f95f5f 169 /// (External) token type, as returned by yylex.
4f84717d 170 typedef token::yytokentype token_type;
1d6b689b 171
26f95f5f
AD
172 /// Internal symbol number.
173 typedef int symbol_number_type;
174
175 /// Internal symbol number for tokens (subsumed by symbol_number_type).
176 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
177
1dbaf37f
TR
178 /// A complete symbol.
179 ///
180 /// Expects its Base type to provide access to the symbol type
181 /// via type_get().
182 ///
183 /// Provide access to semantic value]b4_locations_if([ and location])[.
184 template <typename Base>
185 struct basic_symbol : Base
1d6b689b 186 {
97ae878e
AD
187 /// Alias to Base.
188 typedef Base super_type;
189
1d6b689b 190 /// Default constructor.
b20e797a 191 basic_symbol ();
1d6b689b 192
1dbaf37f 193 /// Copy constructor.
b20e797a 194 basic_symbol (const basic_symbol& other);
ee9cf8c4
TR
195]b4_variant_if([[
196 /// Constructor for valueless symbols, and symbols from each type.
197]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
1dbaf37f 198 /// Constructor for valueless symbols.
7fc7df7a 199 basic_symbol (typename Base::kind_type t]b4_locations_if([,
ee9cf8c4 200 const location_type& l])[);]])[
1d6b689b 201
1dbaf37f 202 /// Constructor for symbols with semantic value.
7fc7df7a 203 basic_symbol (typename Base::kind_type t,
b20e797a
TR
204 const semantic_type& v]b4_locations_if([,
205 const location_type& l])[);
1d6b689b 206
97ae878e 207 ~basic_symbol ();
04816a6f 208
5f87211c 209 /// Destructive move, \a s is emptied into this.
b20e797a 210 void move (basic_symbol& s);
1d6b689b
AD
211
212 /// The semantic value.
213 semantic_type value;]b4_locations_if([
214
215 /// The location.
216 location_type location;])[
7d1aa2d6
AD
217
218 private:
219 /// Assignment operator.
220 basic_symbol& operator= (const basic_symbol& other);
1d6b689b
AD
221 };
222
1dbaf37f
TR
223 /// Type access provider for token (enum) based symbols.
224 struct by_type
1d6b689b 225 {
1d6b689b 226 /// Default constructor.
b20e797a 227 by_type ();
1d6b689b 228
1dbaf37f 229 /// Copy constructor.
b20e797a 230 by_type (const by_type& other);
1d6b689b 231
26f95f5f
AD
232 /// The symbol type as needed by the constructor.
233 typedef token_type kind_type;
1d6b689b 234
26f95f5f
AD
235 /// Constructor from (external) token numbers.
236 by_type (kind_type t);
97ae878e 237
26f95f5f
AD
238 /// Steal the symbol type from \a that.
239 void move (by_type& that);
1d6b689b 240
26f95f5f 241 /// The (internal) type number (corresponding to \a type).
97ae878e 242 /// -1 when this symbol is empty.
26f95f5f 243 symbol_number_type type_get () const;
1d6b689b 244
2b08bceb 245 /// The token.
b20e797a 246 token_type token () const;
1dbaf37f 247
675d9fe4
AD
248 enum { empty = 0 };
249
26f95f5f 250 /// The symbol type.
26f95f5f
AD
251 /// -1 when this symbol is empty.
252 token_number_type type;
1d6b689b 253 };
1dbaf37f
TR
254
255 /// "External" symbols: returned by the scanner.
256 typedef basic_symbol<by_type> symbol_type;
257
0623bacc 258]b4_symbol_constructor_declare])
4f84717d
AD
259
260
1d6b689b
AD
261# b4_public_types_define
262# ----------------------
263# Provide the implementation needed by the public types.
264m4_define([b4_public_types_define],
403febca 265[[ inline
ff601366
AD
266 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
267 : std::runtime_error (m)]b4_locations_if([
268 , location (l)])[
269 {}
270
1dbaf37f
TR
271 // basic_symbol.
272 template <typename Base>
b20e797a 273 inline
1dbaf37f
TR
274 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
275 : value ()
276 {}
04816a6f 277
1dbaf37f 278 template <typename Base>
b20e797a 279 inline
1dbaf37f
TR
280 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
281 : Base (other)
282 , value ()]b4_locations_if([
283 , location (other.location)])[
1d6b689b 284 {
1dbaf37f
TR
285 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
286 [other.value])],
287 [value = other.value;])[
1d6b689b
AD
288 }
289
1d6b689b 290
1dbaf37f 291 template <typename Base>
b20e797a 292 inline
1dbaf37f 293 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 294 [typename Base::kind_type t],
1dbaf37f
TR
295 [const semantic_type& v],
296 b4_locations_if([const location_type& l]))[)
297 : Base (t)
97ae878e 298 , value (]b4_variant_if([], [v])[)]b4_locations_if([
1dbaf37f 299 , location (l)])[
97ae878e 300 {]b4_variant_if([[
733fb7c5 301 (void) v;
97ae878e 302 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
1d6b689b 303
ee9cf8c4
TR
304]b4_variant_if([[
305 // Implementation of basic_symbol constructor for each type.
306]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
307 /// Constructor for valueless symbols.
1dbaf37f 308 template <typename Base>
b20e797a 309 inline
1dbaf37f 310 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 311 [typename Base::kind_type t],
1dbaf37f 312 b4_locations_if([const location_type& l]))[)
97ae878e
AD
313 : Base (t)
314 , value ()]b4_locations_if([
1dbaf37f 315 , location (l)])[
ee9cf8c4 316 {}]])[
1d6b689b 317
97ae878e
AD
318 template <typename Base>
319 inline
320 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
321 {]b4_variant_if([[
322 // User destructor.
26f95f5f 323 symbol_number_type yytype = this->type_get ();
97ae878e
AD
324 switch (yytype)
325 {
326]b4_symbol_foreach([b4_symbol_destructor])dnl
327[ default:
328 break;
329 }
330
331 // Type destructor.
332 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
333 }
334
1dbaf37f 335 template <typename Base>
b20e797a 336 inline
1dbaf37f
TR
337 void
338 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
1d6b689b 339 {
97ae878e
AD
340 super_type::move(s);
341 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
1dbaf37f 342 [s.value])],
97ae878e
AD
343 [value = s.value;])[]b4_locations_if([
344 location = s.location;])[
1d6b689b
AD
345 }
346
1dbaf37f 347 // by_type.
b20e797a 348 inline
1dbaf37f 349 ]b4_parser_class_name[::by_type::by_type ()
675d9fe4 350 : type (empty)
1dbaf37f 351 {}
1d6b689b 352
b20e797a 353 inline
1dbaf37f
TR
354 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
355 : type (other.type)
356 {}
1d6b689b 357
b20e797a 358 inline
1dbaf37f
TR
359 ]b4_parser_class_name[::by_type::by_type (token_type t)
360 : type (yytranslate_ (t))
361 {}
7be08dfb 362
97ae878e
AD
363 inline
364 void
365 ]b4_parser_class_name[::by_type::move (by_type& that)
366 {
367 type = that.type;
675d9fe4 368 that.type = empty;
97ae878e
AD
369 }
370
b20e797a 371 inline
1d6b689b 372 int
1dbaf37f 373 ]b4_parser_class_name[::by_type::type_get () const
1d6b689b
AD
374 {
375 return type;
376 }
e36ec1f4 377]b4_token_ctor_if([[
b20e797a 378 inline
1d6b689b 379 ]b4_parser_class_name[::token_type
1dbaf37f 380 ]b4_parser_class_name[::by_type::token () const
1d6b689b
AD
381 {
382 // YYTOKNUM[NUM] -- (External) token number corresponding to the
383 // (internal) symbol number NUM (which must be that of a token). */
384 static
385 const ]b4_int_type_for([b4_toknum])[
386 yytoken_number_[] =
387 {
388 ]b4_toknum[
389 };
390 return static_cast<token_type> (yytoken_number_[type]);
391 }
0623bacc
AD
392]])[]dnl
393b4_symbol_constructor_define])
394
395
396# b4_symbol_constructor_declare
397# b4_symbol_constructor_define
398# -----------------------------
399# Declare/define symbol constructors for all the value types.
400# Use at class-level. Redefined in variant.hh.
401m4_define([b4_symbol_constructor_declare], [])
402m4_define([b4_symbol_constructor_define], [])
403
1d6b689b 404
cbf25ce7
AD
405# b4_yytranslate_define
406# ---------------------
407# Define yytranslate_. Sometimes used in the header file,
408# sometimes in the cc file.
409m4_define([b4_yytranslate_define],
410[[ // Symbol number corresponding to token number t.
b20e797a 411 inline
cbf25ce7 412 ]b4_parser_class_name[::token_number_type
e36ec1f4 413 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
414 [int])[ t)
415 {
416 static
417 const token_number_type
418 translate_table[] =
419 {
420]b4_translate[
421 };
422 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
423 const token_number_type undef_token_ = ]b4_undef_token_number[;
424
425 if (static_cast<int>(t) <= yyeof_)
426 return yyeof_;
427 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
428 return translate_table[t];
429 else
430 return undef_token_;
431 }
432]])
433
1d6b689b 434
cf147260
AD
435# b4_lhs_value([TYPE])
436# --------------------
437# Expansion of $<TYPE>$.
438m4_define([b4_lhs_value],
1fa5d8bb 439[b4_symbol_value([yyval], [$1])])
cf147260
AD
440
441
442# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
443# --------------------------------------
444# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
445# symbols on RHS.
446m4_define([b4_rhs_value],
1fa5d8bb
AD
447[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
448
cf147260
AD
449
450# b4_lhs_location()
451# -----------------
452# Expansion of @$.
453m4_define([b4_lhs_location],
454[(yyloc)])
455
456
457# b4_rhs_location(RULE-LENGTH, NUM)
458# ---------------------------------
459# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
460# on RHS.
461m4_define([b4_rhs_location],
d1ff7a7c 462[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
463
464
465# b4_parse_param_decl
466# -------------------
467# Extra formal arguments of the constructor.
468# Change the parameter names from "foo" into "foo_yyarg", so that
469# there is no collision bw the user chosen attribute name, and the
470# argument name in the constructor.
471m4_define([b4_parse_param_decl],
472[m4_ifset([b4_parse_param],
473 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
474
475m4_define([b4_parse_param_decl_1],
476[$1_yyarg])
477
478
479
480# b4_parse_param_cons
481# -------------------
482# Extra initialisations of the constructor.
483m4_define([b4_parse_param_cons],
484 [m4_ifset([b4_parse_param],
e9690142 485 [
cf147260
AD
486 b4_cc_constructor_calls(b4_parse_param)])])
487m4_define([b4_cc_constructor_calls],
e9690142 488 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
489 ], [$@])])
490m4_define([b4_cc_constructor_call],
e9690142 491 [$2 ($2_yyarg)])
cf147260
AD
492
493# b4_parse_param_vars
494# -------------------
495# Extra instance variables.
496m4_define([b4_parse_param_vars],
497 [m4_ifset([b4_parse_param],
e9690142 498 [
ac826bc4 499 // User arguments.
cf147260
AD
500b4_cc_var_decls(b4_parse_param)])])
501m4_define([b4_cc_var_decls],
e9690142 502 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
503], [$@])])
504m4_define([b4_cc_var_decl],
e9690142 505 [ $1;])
426903aa
AD
506
507
508## ---------##
509## Values. ##
510## ---------##
511
512# b4_yylloc_default_define
513# ------------------------
514# Define YYLLOC_DEFAULT.
515m4_define([b4_yylloc_default_define],
516[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
517 If N is 0, then set CURRENT to the empty location which ends
518 the previous symbol: RHS[0] (always defined). */
519
520# ifndef YYLLOC_DEFAULT
521# define YYLLOC_DEFAULT(Current, Rhs, N) \
522 do \
523 if (N) \
524 { \
525 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
526 (Current).end = YYRHSLOC (Rhs, N).end; \
527 } \
528 else \
529 { \
530 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
531 } \
532 while (/*CONSTCOND*/ false)
533# endif
534]])
1dce289a
AD
535
536## -------- ##
537## Checks. ##
538## -------- ##
539
540b4_token_ctor_if([b4_variant_if([],
541 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
542 [cannot use '%s' without '%s'],
543 [%define api.token.constructor],
544 [%define api.value.type variant]))])])