3 # C++ skeleton for Bison
5 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
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.
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.
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/>.
20 m4_include(b4_pkgdatadir/[c.m4])
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// ])])
32 b4_percent_define_check_kind([api.namespace], [code], [deprecated])
33 b4_percent_define_check_kind([parser_class_name], [code], [deprecated])
35 ## ---------------- ##
37 ## ---------------- ##
39 b4_percent_define_default([[parser_class_name]], [[parser]])
41 # Don't do that so that we remember whether we're using a user
42 # request, or the default value.
44 # b4_percent_define_default([[api.location.type]], [[location]])
46 b4_percent_define_default([[filename_type]], [[std::string]])
47 # Make it a warning for those who used betas of Bison 3.0.
48 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
50 b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
51 b4_percent_define_default([[define_location_comparison]],
52 [m4_if(b4_percent_define_get([[filename_type]]),
53 [std::string], [[true]], [[false]])])
61 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
64 # Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
65 # would compile as an absolute reference with 'parser' in the global namespace.
66 # b4_namespace_open would open an anonymous namespace and thus establish
67 # internal linkage. This would compile. However, it's cryptic, and internal
68 # linkage for the parser would be specified in all translation units that
69 # include the header, which is always generated. If we ever need to permit
70 # internal linkage somehow, surely we can find a cleaner approach.
71 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
72 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
73 [[namespace reference is empty]])])
75 # Instead of assuming the C++ compiler will do it, Bison should reject any
76 # invalid b4_namepsace_ref that would be converted to a valid
77 # b4_namespace_open. The problem is that Bison doesn't always output
78 # b4_namespace_ref to uncommented code but should reserve the ability to do so
79 # in future releases without risking breaking any existing user grammars.
80 # Specifically, don't allow empty names as b4_namespace_open would just convert
81 # those into anonymous namespaces, and that might tempt some users.
82 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
83 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
84 [[namespace reference has consecutive "::"]])])
85 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
86 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
87 [[namespace reference has a trailing "::"]])])
89 m4_define([b4_namespace_open],
90 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
91 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
92 [^\(.\)[ ]*::], [\1])),
93 [::], [ { namespace ])[ {]])])
95 m4_define([b4_namespace_close],
96 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
97 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
98 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
100 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
105 # Output the definition of the tokens as enums.
106 m4_define([b4_token_enums],
111 b4_symbol_map([b4_token_enum]))[
118 ## ----------------- ##
119 ## Semantic Values. ##
120 ## ----------------- ##
124 # b4_value_type_declare
125 # ---------------------
126 # Declare semantic_type.
127 m4_define([b4_value_type_declare],
128 [b4_value_type_setup[]dnl
129 [ /// Symbol semantic values.
130 ]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
132 [[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
133 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
134 [union\|union-directive],
135 [[ union semantic_type
137 ]b4_user_union_members[
142 # b4_public_types_declare
143 # -----------------------
144 # Define the public types: token, semantic value, location, and so forth.
145 # Depending on %define token_lex, may be output in the header or source file.
146 m4_define([b4_public_types_declare],
147 [[#ifndef ]b4_api_PREFIX[STYPE
148 ]b4_value_type_declare[
150 typedef ]b4_api_PREFIX[STYPE semantic_type;
151 #endif]b4_locations_if([
152 /// Symbol locations.
153 typedef b4_percent_define_get([[api.location.type]],
154 [[location]]) location_type;])[
156 /// Syntax errors thrown from user actions.
157 struct syntax_error : std::runtime_error
159 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
160 location_type location;])[
169 /// (External) token type, as returned by yylex.
170 typedef token::yytokentype token_type;
172 /// Internal symbol number.
173 typedef int symbol_number_type;
175 /// Internal symbol number for tokens (subsumed by symbol_number_type).
176 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
178 /// A complete symbol.
180 /// Expects its Base type to provide access to the symbol type
183 /// Provide access to semantic value]b4_locations_if([ and location])[.
184 template <typename Base>
185 struct basic_symbol : Base
188 typedef Base super_type;
190 /// Default constructor.
193 /// Copy constructor.
194 basic_symbol (const basic_symbol& other);
196 /// Constructor for valueless symbols, and symbols from each type.
197 ]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
198 /// Constructor for valueless symbols.
199 basic_symbol (typename Base::kind_type t]b4_locations_if([,
200 const location_type& l])[);]])[
202 /// Constructor for symbols with semantic value.
203 basic_symbol (typename Base::kind_type t,
204 const semantic_type& v]b4_locations_if([,
205 const location_type& l])[);
209 /// Destructive move, \a s is emptied into this.
210 void move (basic_symbol& s);
212 /// The semantic value.
213 semantic_type value;]b4_locations_if([
216 location_type location;])[
219 /// Assignment operator.
220 basic_symbol& operator= (const basic_symbol& other);
223 /// Type access provider for token (enum) based symbols.
226 /// Default constructor.
229 /// Copy constructor.
230 by_type (const by_type& other);
232 /// The symbol type as needed by the constructor.
233 typedef token_type kind_type;
235 /// Constructor from (external) token numbers.
236 by_type (kind_type t);
238 /// Steal the symbol type from \a that.
239 void move (by_type& that);
241 /// The (internal) type number (corresponding to \a type).
242 /// -1 when this symbol is empty.
243 symbol_number_type type_get () const;
246 token_type token () const;
251 /// -1 when this symbol is empty.
252 token_number_type type;
255 /// "External" symbols: returned by the scanner.
256 typedef basic_symbol<by_type> symbol_type;
258 ]b4_symbol_constructor_declare])
261 # b4_public_types_define
262 # ----------------------
263 # Provide the implementation needed by the public types.
264 m4_define([b4_public_types_define],
266 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
267 : std::runtime_error (m)]b4_locations_if([
272 template <typename Base>
274 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
278 template <typename Base>
280 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
282 , value ()]b4_locations_if([
283 , location (other.location)])[
285 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
287 [value = other.value;])[
291 template <typename Base>
293 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
294 [typename Base::kind_type t],
295 [const semantic_type& v],
296 b4_locations_if([const location_type& l]))[)
298 , value (]b4_variant_if([], [v])[)]b4_locations_if([
302 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
305 // Implementation of basic_symbol constructor for each type.
306 ]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
307 /// Constructor for valueless symbols.
308 template <typename Base>
310 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
311 [typename Base::kind_type t],
312 b4_locations_if([const location_type& l]))[)
314 , value ()]b4_locations_if([
318 template <typename Base>
320 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
323 symbol_number_type yytype = this->type_get ();
326 ]b4_symbol_foreach([b4_symbol_destructor])dnl
332 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
335 template <typename Base>
338 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
341 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
343 [value = s.value;])[]b4_locations_if([
344 location = s.location;])[
349 ]b4_parser_class_name[::by_type::by_type ()
354 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
359 ]b4_parser_class_name[::by_type::by_type (token_type t)
360 : type (yytranslate_ (t))
365 ]b4_parser_class_name[::by_type::move (by_type& that)
373 ]b4_parser_class_name[::by_type::type_get () const
379 ]b4_parser_class_name[::token_type
380 ]b4_parser_class_name[::by_type::token () const
382 // YYTOKNUM[NUM] -- (External) token number corresponding to the
383 // (internal) symbol number NUM (which must be that of a token). */
385 const ]b4_int_type_for([b4_toknum])[
390 return static_cast<token_type> (yytoken_number_[type]);
393 b4_symbol_constructor_define])
396 # b4_symbol_constructor_declare
397 # b4_symbol_constructor_define
398 # -----------------------------
399 # Declare/define symbol constructors for all the value types.
400 # Use at class-level. Redefined in variant.hh.
401 m4_define([b4_symbol_constructor_declare], [])
402 m4_define([b4_symbol_constructor_define], [])
405 # b4_yytranslate_define
406 # ---------------------
407 # Define yytranslate_. Sometimes used in the header file,
408 # sometimes in the cc file.
409 m4_define([b4_yytranslate_define],
410 [[ // Symbol number corresponding to token number t.
412 ]b4_parser_class_name[::token_number_type
413 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
417 const token_number_type
422 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
423 const token_number_type undef_token_ = ]b4_undef_token_number[;
425 if (static_cast<int>(t) <= yyeof_)
427 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
428 return translate_table[t];
435 # b4_lhs_value([TYPE])
436 # --------------------
437 # Expansion of $<TYPE>$.
438 m4_define([b4_lhs_value],
439 [b4_symbol_value([yyval], [$1])])
442 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
443 # --------------------------------------
444 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
446 m4_define([b4_rhs_value],
447 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
453 m4_define([b4_lhs_location],
457 # b4_rhs_location(RULE-LENGTH, NUM)
458 # ---------------------------------
459 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
461 m4_define([b4_rhs_location],
462 [(yylocation_stack_@{($1) - ($2)@})])
465 # b4_parse_param_decl
466 # -------------------
467 # Extra formal arguments of the constructor.
468 # Change the parameter names from "foo" into "foo_yyarg", so that
469 # there is no collision bw the user chosen attribute name, and the
470 # argument name in the constructor.
471 m4_define([b4_parse_param_decl],
472 [m4_ifset([b4_parse_param],
473 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
475 m4_define([b4_parse_param_decl_1],
480 # b4_parse_param_cons
481 # -------------------
482 # Extra initialisations of the constructor.
483 m4_define([b4_parse_param_cons],
484 [m4_ifset([b4_parse_param],
486 b4_cc_constructor_calls(b4_parse_param)])])
487 m4_define([b4_cc_constructor_calls],
488 [m4_map_sep([b4_cc_constructor_call], [,
490 m4_define([b4_cc_constructor_call],
493 # b4_parse_param_vars
494 # -------------------
495 # Extra instance variables.
496 m4_define([b4_parse_param_vars],
497 [m4_ifset([b4_parse_param],
500 b4_cc_var_decls(b4_parse_param)])])
501 m4_define([b4_cc_var_decls],
502 [m4_map_sep([b4_cc_var_decl], [
504 m4_define([b4_cc_var_decl],
512 # b4_yylloc_default_define
513 # ------------------------
514 # Define YYLLOC_DEFAULT.
515 m4_define([b4_yylloc_default_define],
516 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
517 If N is 0, then set CURRENT to the empty location which ends
518 the previous symbol: RHS[0] (always defined). */
520 # ifndef YYLLOC_DEFAULT
521 # define YYLLOC_DEFAULT(Current, Rhs, N) \
525 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
526 (Current).end = YYRHSLOC (Rhs, N).end; \
530 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
532 while (/*CONSTCOND*/ false)
540 b4_token_ctor_if([b4_variant_if([],
541 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
542 [cannot use '%s' without '%s'],
543 [%define api.token.constructor],
544 [%define api.value.type variant]))])])