]> git.saurik.com Git - bison.git/blame - data/c++.m4
maint: update for changes to gnulib's announce-gen.
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
e141f4d4 5# Copyright (C) 2002-2010 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
cf147260
AD
22## ---------------- ##
23## Default values. ##
24## ---------------- ##
25
a4e25e1d
JD
26b4_percent_define_default([[parser_class_name]], [[parser]])
27b4_percent_define_default([[location_type]], [[location]])
28b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 29b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 30
c1d19e10 31b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
32b4_percent_define_default([[define_location_comparison]],
33 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 34 [std::string], [[true]], [[false]])])
cf147260
AD
35
36
67501061 37
793fbca5
JD
38## ----------- ##
39## Namespace. ##
40## ----------- ##
41
67501061
AD
42m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
43
793fbca5
JD
44
45# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
46# would compile as an absolute reference with `parser' in the global namespace.
47# b4_namespace_open would open an anonymous namespace and thus establish
48# internal linkage. This would compile. However, it's cryptic, and internal
49# linkage for the parser would be specified in all translation units that
50# include the header, which is always generated. If we ever need to permit
51# internal linkage somehow, surely we can find a cleaner approach.
52m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 53[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
54 [[namespace reference is empty]])])
55
56# Instead of assuming the C++ compiler will do it, Bison should reject any
57# invalid b4_namepsace_ref that would be converted to a valid
58# b4_namespace_open. The problem is that Bison doesn't always output
59# b4_namespace_ref to uncommented code but should reserve the ability to do so
60# in future releases without risking breaking any existing user grammars.
61# Specifically, don't allow empty names as b4_namespace_open would just convert
62# those into anonymous namespaces, and that might tempt some users.
63m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 64[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
65 [[namespace reference has consecutive "::"]])])
66m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 67[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
68 [[namespace reference has a trailing "::"]])])
69
70m4_define([b4_namespace_open],
67501061 71[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
72[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
73 [^\(.\)[ ]*::], [\1])),
74 [::], [ { namespace ])[ {]])])
75
76m4_define([b4_namespace_close],
67501061 77[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 78m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 79 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 80 [\1])),
b987342b 81 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
82
83
cf147260
AD
84# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
85# -----------------------------------------------------
c095d689 86# Output the definition of the tokens as enums.
cf147260 87m4_define([b4_token_enums],
cf147260
AD
88[/* Tokens. */
89 enum yytokentype {
90m4_map_sep([ b4_token_enum], [,
91],
92 [$@])
93 };
c095d689 94])
cf147260
AD
95
96
97## ----------------- ##
98## Semantic Values. ##
99## ----------------- ##
100
b9e4eb5b
AD
101# b4_semantic_type_declare
102# ------------------------
103# Declare semantic_type.
104m4_define([b4_semantic_type_declare],
105[ /// Symbol semantic values.
106m4_ifdef([b4_stype],
107[ union semantic_type
108 {b4_user_stype
109 };],
110[m4_if(b4_tag_seen_flag, 0,
111[[ typedef int semantic_type;]],
112[[ typedef YYSTYPE semantic_type;]])])])
113
114
4f84717d
AD
115# b4_public_types_declare
116# -----------------------
117# Define the public types: token, semantic value, location, and so forth.
1d6b689b 118# Depending on %define token_lex, may be output in the header or source file.
4f84717d
AD
119m4_define([b4_public_types_declare],
120[[#ifndef YYSTYPE
121]b4_semantic_type_declare[
122#else
123 typedef YYSTYPE semantic_type;
124#endif]b4_locations_if([
125 /// Symbol locations.
126 typedef b4_percent_define_get([[location_type]]) location_type;])[
1d6b689b 127
ff601366
AD
128 /// Syntax errors thrown from user actions.
129 struct syntax_error : std::runtime_error
130 {
131 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
132 location_type location;])[
133 };
134
4f84717d
AD
135 /// Tokens.
136 struct token
137 {
138 ]b4_token_enums(b4_tokens)[
139 };
1d6b689b 140
4f84717d
AD
141 /// Token type.
142 typedef token::yytokentype token_type;
1d6b689b
AD
143
144 /// A complete symbol, with its type.
145 template <typename Exact>
146 struct symbol_base_type
147 {
148 /// Default constructor.
149 inline symbol_base_type ();
150
151 /// Constructor.]b4_locations_if([
152 inline symbol_base_type (const location_type& l)])[;
153 inline symbol_base_type (]b4_args(
154 [const semantic_type& v],
155 b4_locations_if([const location_type& l]))[);
156
157 /// Return this with its exact type.
158 const Exact& self () const;
159 Exact& self ();
160
161 /// Return the type of this symbol.
162 int type_get () const;
163
164 /// The semantic value.
165 semantic_type value;]b4_locations_if([
166
167 /// The location.
168 location_type location;])[
169 };
170
171 /// External form of a symbol: its type and attributes.
172 struct symbol_type : symbol_base_type<symbol_type>
173 {
174 /// The parent class.
175 typedef symbol_base_type<symbol_type> super_type;
176
177 /// Default constructor.
178 inline symbol_type ();
179
2b08bceb
AD
180 /// Constructor for tokens with semantic value.
181 inline symbol_type (]b4_args([token_type t],
1d6b689b
AD
182 [const semantic_type& v],
183 b4_locations_if([const location_type& l]))[);
184
2b08bceb
AD
185 /// Constructor for valueless tokens.
186 inline symbol_type (]b4_args([token_type t],
1d6b689b
AD
187 b4_locations_if([const location_type& l]))[);
188
189 /// The symbol type.
190 int type;
191
2b08bceb 192 /// The symbol type.
1d6b689b
AD
193 inline int type_get_ () const;
194
2b08bceb 195 /// The token.
1d6b689b
AD
196 inline token_type token () const;
197 };
0623bacc 198]b4_symbol_constructor_declare])
4f84717d
AD
199
200
1d6b689b
AD
201# b4_public_types_define
202# ----------------------
203# Provide the implementation needed by the public types.
204m4_define([b4_public_types_define],
ff601366
AD
205[[ inline
206 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
207 : std::runtime_error (m)]b4_locations_if([
208 , location (l)])[
209 {}
210
211 // symbol_base_type.
1d6b689b 212 template <typename Exact>
ff601366 213 inline
1d6b689b
AD
214 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
215 : value()]b4_locations_if([
216 , location()])[
217 {
218 }]b4_locations_if([[
219
220 template <typename Exact>
29660062 221 inline
1d6b689b
AD
222 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
223 : value()
224 , location(l)
225 {
226 }]])[
227
228 template <typename Exact>
29660062 229 inline
1d6b689b
AD
230 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
231 [const semantic_type& v],
232 b4_locations_if([const location_type& l]))[)
233 : value(v)]b4_locations_if([
234 , location(l)])[
235 {
236 }
237
238 template <typename Exact>
29660062 239 inline
1d6b689b
AD
240 const Exact&
241 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
242 {
243 return static_cast<const Exact&>(*this);
244 }
245
246 template <typename Exact>
29660062 247 inline
1d6b689b
AD
248 Exact&
249 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
250 {
251 return static_cast<Exact&>(*this);
252 }
253
254 template <typename Exact>
29660062 255 inline
1d6b689b
AD
256 int
257 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
258 {
259 return self ().type_get_ ();
260 }
261
262 // symbol_type.
29660062 263 inline
1d6b689b
AD
264 ]b4_parser_class_name[::symbol_type::symbol_type ()
265 : super_type ()
266 , type ()
267 {
268 }
269
29660062 270 inline
1d6b689b 271 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
2b08bceb 272 [token_type t],
1d6b689b
AD
273 b4_locations_if([const location_type& l]))[)
274 : super_type (]b4_locations_if([l])[)
2b08bceb 275 , type (yytranslate_ (t))
1d6b689b
AD
276 {
277 }
278
29660062 279 inline
1d6b689b 280 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
2b08bceb 281 [token_type t],
1d6b689b
AD
282 [const semantic_type& v],
283 b4_locations_if([const location_type& l]))[)
284 : super_type (v]b4_locations_if([, l])[)
2b08bceb 285 , type (yytranslate_ (t))
1d6b689b
AD
286 {
287 }
288
29660062 289 inline
1d6b689b
AD
290 int
291 ]b4_parser_class_name[::symbol_type::type_get_ () const
292 {
293 return type;
294 }
295]b4_lex_symbol_if([[
29660062 296 inline
1d6b689b
AD
297 ]b4_parser_class_name[::token_type
298 ]b4_parser_class_name[::symbol_type::token () const
299 {
300 // YYTOKNUM[NUM] -- (External) token number corresponding to the
301 // (internal) symbol number NUM (which must be that of a token). */
302 static
303 const ]b4_int_type_for([b4_toknum])[
304 yytoken_number_[] =
305 {
306 ]b4_toknum[
307 };
308 return static_cast<token_type> (yytoken_number_[type]);
309 }
0623bacc
AD
310]])[]dnl
311b4_symbol_constructor_define])
312
313
314# b4_symbol_constructor_declare
315# b4_symbol_constructor_define
316# -----------------------------
317# Declare/define symbol constructors for all the value types.
318# Use at class-level. Redefined in variant.hh.
319m4_define([b4_symbol_constructor_declare], [])
320m4_define([b4_symbol_constructor_define], [])
321
1d6b689b 322
cbf25ce7
AD
323# b4_yytranslate_define
324# ---------------------
325# Define yytranslate_. Sometimes used in the header file,
326# sometimes in the cc file.
327m4_define([b4_yytranslate_define],
328[[ // Symbol number corresponding to token number t.
329 ]b4_parser_class_name[::token_number_type
330 ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
331 [int])[ t)
332 {
333 static
334 const token_number_type
335 translate_table[] =
336 {
337]b4_translate[
338 };
339 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
340 const token_number_type undef_token_ = ]b4_undef_token_number[;
341
342 if (static_cast<int>(t) <= yyeof_)
343 return yyeof_;
344 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
345 return translate_table[t];
346 else
347 return undef_token_;
348 }
349]])
350
1d6b689b 351
cf147260
AD
352# b4_lhs_value([TYPE])
353# --------------------
354# Expansion of $<TYPE>$.
355m4_define([b4_lhs_value],
1fa5d8bb 356[b4_symbol_value([yyval], [$1])])
cf147260
AD
357
358
359# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
360# --------------------------------------
361# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
362# symbols on RHS.
363m4_define([b4_rhs_value],
1fa5d8bb
AD
364[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
365
cf147260
AD
366
367# b4_lhs_location()
368# -----------------
369# Expansion of @$.
370m4_define([b4_lhs_location],
371[(yyloc)])
372
373
374# b4_rhs_location(RULE-LENGTH, NUM)
375# ---------------------------------
376# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
377# on RHS.
378m4_define([b4_rhs_location],
d1ff7a7c 379[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
380
381
382# b4_parse_param_decl
383# -------------------
384# Extra formal arguments of the constructor.
385# Change the parameter names from "foo" into "foo_yyarg", so that
386# there is no collision bw the user chosen attribute name, and the
387# argument name in the constructor.
388m4_define([b4_parse_param_decl],
389[m4_ifset([b4_parse_param],
390 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
391
392m4_define([b4_parse_param_decl_1],
393[$1_yyarg])
394
395
396
397# b4_parse_param_cons
398# -------------------
399# Extra initialisations of the constructor.
400m4_define([b4_parse_param_cons],
401 [m4_ifset([b4_parse_param],
fa7b79c0 402 [
cf147260
AD
403 b4_cc_constructor_calls(b4_parse_param)])])
404m4_define([b4_cc_constructor_calls],
405 [m4_map_sep([b4_cc_constructor_call], [,
406 ], [$@])])
407m4_define([b4_cc_constructor_call],
408 [$2 ($2_yyarg)])
409
410# b4_parse_param_vars
411# -------------------
412# Extra instance variables.
413m4_define([b4_parse_param_vars],
414 [m4_ifset([b4_parse_param],
415 [
416 /* User arguments. */
417b4_cc_var_decls(b4_parse_param)])])
418m4_define([b4_cc_var_decls],
419 [m4_map_sep([b4_cc_var_decl], [
420], [$@])])
421m4_define([b4_cc_var_decl],
422 [ $1;])