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