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