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