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