]> git.saurik.com Git - bison.git/blob - data/c++.m4
e2c20fbd612dad753accace8e5f1b8b2318196c7
[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
116
117 # b4_value_type_declare
118 # ---------------------
119 # Declare semantic_type.
120 m4_define([b4_value_type_declare],
121 [b4_value_type_setup[]dnl
122 [ /// Symbol semantic values.
123 ]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
124 [code],
125 [[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
126 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
127 [union\|union-directive],
128 [[ union semantic_type
129 {
130 ]b4_user_union_members[
131 };]])])dnl
132 ])
133
134
135 # b4_public_types_declare
136 # -----------------------
137 # Define the public types: token, semantic value, location, and so forth.
138 # Depending on %define token_lex, may be output in the header or source file.
139 m4_define([b4_public_types_declare],
140 [[#ifndef ]b4_api_PREFIX[STYPE
141 ]b4_value_type_declare[
142 #else
143 typedef ]b4_api_PREFIX[STYPE semantic_type;
144 #endif]b4_locations_if([
145 /// Symbol locations.
146 typedef b4_percent_define_get([[api.location.type]],
147 [[location]]) location_type;])[
148
149 /// Syntax errors thrown from user actions.
150 struct syntax_error : std::runtime_error
151 {
152 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
153 location_type location;])[
154 };
155
156 /// Tokens.
157 struct token
158 {
159 ]b4_token_enums[
160 };
161
162 /// (External) token type, as returned by yylex.
163 typedef token::yytokentype token_type;
164
165 /// Internal symbol number.
166 typedef int symbol_number_type;
167
168 /// Internal symbol number for tokens (subsumed by symbol_number_type).
169 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
170
171 /// A complete symbol.
172 ///
173 /// Expects its Base type to provide access to the symbol type
174 /// via type_get().
175 ///
176 /// Provide access to semantic value]b4_locations_if([ and location])[.
177 template <typename Base>
178 struct basic_symbol : Base
179 {
180 /// Alias to Base.
181 typedef Base super_type;
182
183 /// Default constructor.
184 basic_symbol ();
185
186 /// Copy constructor.
187 basic_symbol (const basic_symbol& other);
188 ]b4_variant_if([[
189 /// Constructor for valueless symbols, and symbols from each type.
190 ]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
191 /// Constructor for valueless symbols.
192 basic_symbol (typename Base::kind_type t]b4_locations_if([,
193 const location_type& l])[);]])[
194
195 /// Constructor for symbols with semantic value.
196 basic_symbol (typename Base::kind_type t,
197 const semantic_type& v]b4_locations_if([,
198 const location_type& l])[);
199
200 ~basic_symbol ();
201
202 /// Destructive move, \a s is emptied into this.
203 void move (basic_symbol& s);
204
205 /// The semantic value.
206 semantic_type value;]b4_locations_if([
207
208 /// The location.
209 location_type location;])[
210
211 private:
212 /// Assignment operator.
213 basic_symbol& operator= (const basic_symbol& other);
214 };
215
216 /// Type access provider for token (enum) based symbols.
217 struct by_type
218 {
219 /// Default constructor.
220 by_type ();
221
222 /// Copy constructor.
223 by_type (const by_type& other);
224
225 /// The symbol type as needed by the constructor.
226 typedef token_type kind_type;
227
228 /// Constructor from (external) token numbers.
229 by_type (kind_type t);
230
231 /// Steal the symbol type from \a that.
232 void move (by_type& that);
233
234 /// The (internal) type number (corresponding to \a type).
235 /// -1 when this symbol is empty.
236 symbol_number_type type_get () const;
237
238 /// The token.
239 token_type token () const;
240
241 enum { empty = 0 };
242
243 /// The symbol type.
244 /// -1 when this symbol is empty.
245 token_number_type type;
246 };
247
248 /// "External" symbols: returned by the scanner.
249 typedef basic_symbol<by_type> symbol_type;
250
251 ]b4_symbol_constructor_declare])
252
253
254 # b4_public_types_define
255 # ----------------------
256 # Provide the implementation needed by the public types.
257 m4_define([b4_public_types_define],
258 [[ inline
259 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
260 : std::runtime_error (m)]b4_locations_if([
261 , location (l)])[
262 {}
263
264 // basic_symbol.
265 template <typename Base>
266 inline
267 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
268 : value ()
269 {}
270
271 template <typename Base>
272 inline
273 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
274 : Base (other)
275 , value ()]b4_locations_if([
276 , location (other.location)])[
277 {
278 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
279 [other.value])],
280 [value = other.value;])[
281 }
282
283
284 template <typename Base>
285 inline
286 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
287 [typename Base::kind_type t],
288 [const semantic_type& v],
289 b4_locations_if([const location_type& l]))[)
290 : Base (t)
291 , value (]b4_variant_if([], [v])[)]b4_locations_if([
292 , location (l)])[
293 {]b4_variant_if([[
294 (void) v;
295 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
296
297 ]b4_variant_if([[
298 // Implementation of basic_symbol constructor for each type.
299 ]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
300 /// Constructor for valueless symbols.
301 template <typename Base>
302 inline
303 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
304 [typename Base::kind_type t],
305 b4_locations_if([const location_type& l]))[)
306 : Base (t)
307 , value ()]b4_locations_if([
308 , location (l)])[
309 {}]])[
310
311 template <typename Base>
312 inline
313 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
314 {]b4_variant_if([[
315 // User destructor.
316 symbol_number_type yytype = this->type_get ();
317 switch (yytype)
318 {
319 ]b4_symbol_foreach([b4_symbol_destructor])dnl
320 [ default:
321 break;
322 }
323
324 // Type destructor.
325 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
326 }
327
328 template <typename Base>
329 inline
330 void
331 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
332 {
333 super_type::move(s);
334 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
335 [s.value])],
336 [value = s.value;])[]b4_locations_if([
337 location = s.location;])[
338 }
339
340 // by_type.
341 inline
342 ]b4_parser_class_name[::by_type::by_type ()
343 : type (empty)
344 {}
345
346 inline
347 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
348 : type (other.type)
349 {}
350
351 inline
352 ]b4_parser_class_name[::by_type::by_type (token_type t)
353 : type (yytranslate_ (t))
354 {}
355
356 inline
357 void
358 ]b4_parser_class_name[::by_type::move (by_type& that)
359 {
360 type = that.type;
361 that.type = empty;
362 }
363
364 inline
365 int
366 ]b4_parser_class_name[::by_type::type_get () const
367 {
368 return type;
369 }
370 ]b4_token_ctor_if([[
371 inline
372 ]b4_parser_class_name[::token_type
373 ]b4_parser_class_name[::by_type::token () const
374 {
375 // YYTOKNUM[NUM] -- (External) token number corresponding to the
376 // (internal) symbol number NUM (which must be that of a token). */
377 static
378 const ]b4_int_type_for([b4_toknum])[
379 yytoken_number_[] =
380 {
381 ]b4_toknum[
382 };
383 return static_cast<token_type> (yytoken_number_[type]);
384 }
385 ]])[]dnl
386 b4_symbol_constructor_define])
387
388
389 # b4_symbol_constructor_declare
390 # b4_symbol_constructor_define
391 # -----------------------------
392 # Declare/define symbol constructors for all the value types.
393 # Use at class-level. Redefined in variant.hh.
394 m4_define([b4_symbol_constructor_declare], [])
395 m4_define([b4_symbol_constructor_define], [])
396
397
398 # b4_yytranslate_define
399 # ---------------------
400 # Define yytranslate_. Sometimes used in the header file,
401 # sometimes in the cc file.
402 m4_define([b4_yytranslate_define],
403 [[ // Symbol number corresponding to token number t.
404 inline
405 ]b4_parser_class_name[::token_number_type
406 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
407 [int])[ t)
408 {
409 static
410 const token_number_type
411 translate_table[] =
412 {
413 ]b4_translate[
414 };
415 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
416 const token_number_type undef_token_ = ]b4_undef_token_number[;
417
418 if (static_cast<int>(t) <= yyeof_)
419 return yyeof_;
420 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
421 return translate_table[t];
422 else
423 return undef_token_;
424 }
425 ]])
426
427
428 # b4_lhs_value([TYPE])
429 # --------------------
430 # Expansion of $<TYPE>$.
431 m4_define([b4_lhs_value],
432 [b4_symbol_value([yyval], [$1])])
433
434
435 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
436 # --------------------------------------
437 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
438 # symbols on RHS.
439 m4_define([b4_rhs_value],
440 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
441
442
443 # b4_lhs_location()
444 # -----------------
445 # Expansion of @$.
446 m4_define([b4_lhs_location],
447 [(yyloc)])
448
449
450 # b4_rhs_location(RULE-LENGTH, NUM)
451 # ---------------------------------
452 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
453 # on RHS.
454 m4_define([b4_rhs_location],
455 [(yylocation_stack_@{($1) - ($2)@})])
456
457
458 # b4_parse_param_decl
459 # -------------------
460 # Extra formal arguments of the constructor.
461 # Change the parameter names from "foo" into "foo_yyarg", so that
462 # there is no collision bw the user chosen attribute name, and the
463 # argument name in the constructor.
464 m4_define([b4_parse_param_decl],
465 [m4_ifset([b4_parse_param],
466 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
467
468 m4_define([b4_parse_param_decl_1],
469 [$1_yyarg])
470
471
472
473 # b4_parse_param_cons
474 # -------------------
475 # Extra initialisations of the constructor.
476 m4_define([b4_parse_param_cons],
477 [m4_ifset([b4_parse_param],
478 [
479 b4_cc_constructor_calls(b4_parse_param)])])
480 m4_define([b4_cc_constructor_calls],
481 [m4_map_sep([b4_cc_constructor_call], [,
482 ], [$@])])
483 m4_define([b4_cc_constructor_call],
484 [$2 ($2_yyarg)])
485
486 # b4_parse_param_vars
487 # -------------------
488 # Extra instance variables.
489 m4_define([b4_parse_param_vars],
490 [m4_ifset([b4_parse_param],
491 [
492 // User arguments.
493 b4_cc_var_decls(b4_parse_param)])])
494 m4_define([b4_cc_var_decls],
495 [m4_map_sep([b4_cc_var_decl], [
496 ], [$@])])
497 m4_define([b4_cc_var_decl],
498 [ $1;])
499
500
501 ## ---------##
502 ## Values. ##
503 ## ---------##
504
505 # b4_yylloc_default_define
506 # ------------------------
507 # Define YYLLOC_DEFAULT.
508 m4_define([b4_yylloc_default_define],
509 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
510 If N is 0, then set CURRENT to the empty location which ends
511 the previous symbol: RHS[0] (always defined). */
512
513 # ifndef YYLLOC_DEFAULT
514 # define YYLLOC_DEFAULT(Current, Rhs, N) \
515 do \
516 if (N) \
517 { \
518 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
519 (Current).end = YYRHSLOC (Rhs, N).end; \
520 } \
521 else \
522 { \
523 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
524 } \
525 while (/*CONSTCOND*/ false)
526 # endif
527 ]])
528
529 ## -------- ##
530 ## Checks. ##
531 ## -------- ##
532
533 b4_token_ctor_if([b4_variant_if([],
534 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
535 [cannot use '%s' without '%s'],
536 [%define api.token.constructor],
537 [%define api.value.type variant]))])])