3 # C++ skeleton for Bison
5 # Copyright (C) 2002-2021 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 <https://www.gnu.org/licenses/>.
20 # Sanity checks, before defaults installed by c.m4.
21 b4_percent_define_ifdef([[api.value.union.name]],
22 [b4_complain_at(b4_percent_define_get_loc([[api.value.union.name]]),
23 [named %union is invalid in C++])])
25 b4_percent_define_default([[api.symbol.prefix]], [[S_]])
27 m4_include(b4_skeletonsdir/[c.m4])
29 b4_percent_define_check_kind([api.namespace], [code], [deprecated])
30 b4_percent_define_check_kind([api.parser.class], [code], [deprecated])
37 # b4_comment(TEXT, [PREFIX])
38 # --------------------------
39 # Put TEXT in comment. Prefix all the output lines with PREFIX.
40 m4_define([b4_comment],
41 [_b4_comment([$1], [$2// ], [$2// ])])
46 # Expand to `inline\n ` if $1 is hh.
47 m4_define([b4_inline],
52 [m4_fatal([$0: invalid argument: $1])])])
57 m4_define([b4_cxx_portability],
58 [#if defined __cplusplus
59 # define YY_CPLUSPLUS __cplusplus
61 # define YY_CPLUSPLUS 199711L
64 // Support move semantics when possible.
65 #if 201103L <= YY_CPLUSPLUS
66 # define YY_MOVE std::move
67 # define YY_MOVE_OR_COPY move
68 # define YY_MOVE_REF(Type) Type&&
69 # define YY_RVREF(Type) Type&&
70 # define YY_COPY(Type) Type
73 # define YY_MOVE_OR_COPY copy
74 # define YY_MOVE_REF(Type) Type&
75 # define YY_RVREF(Type) const Type&
76 # define YY_COPY(Type) const Type&
79 // Support noexcept when possible.
80 #if 201103L <= YY_CPLUSPLUS
81 # define YY_NOEXCEPT noexcept
85 # define YY_NOTHROW throw ()
88 // Support constexpr when possible.
89 #if 201703 <= YY_CPLUSPLUS
90 # define YY_CONSTEXPR constexpr
97 ## ---------------- ##
99 ## ---------------- ##
101 b4_percent_define_default([[api.parser.class]], [[parser]])
103 # Don't do that so that we remember whether we're using a user
104 # request, or the default value.
106 # b4_percent_define_default([[api.location.type]], [[location]])
108 b4_percent_define_default([[api.filename.type]], [[const std::string]])
109 # Make it a warning for those who used betas of Bison 3.0.
110 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
112 b4_percent_define_default([[define_location_comparison]],
113 [m4_if(b4_percent_define_get([[filename_type]]),
114 [std::string], [[true]], [[false]])])
122 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
125 # Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
126 # would compile as an absolute reference with 'parser' in the global namespace.
127 # b4_namespace_open would open an anonymous namespace and thus establish
128 # internal linkage. This would compile. However, it's cryptic, and internal
129 # linkage for the parser would be specified in all translation units that
130 # include the header, which is always generated. If we ever need to permit
131 # internal linkage somehow, surely we can find a cleaner approach.
132 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
133 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
134 [[namespace reference is empty]])])
136 # Instead of assuming the C++ compiler will do it, Bison should reject any
137 # invalid b4_namespace_ref that would be converted to a valid
138 # b4_namespace_open. The problem is that Bison doesn't always output
139 # b4_namespace_ref to uncommented code but should reserve the ability to do so
140 # in future releases without risking breaking any existing user grammars.
141 # Specifically, don't allow empty names as b4_namespace_open would just convert
142 # those into anonymous namespaces, and that might tempt some users.
143 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
144 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
145 [[namespace reference has consecutive "::"]])])
146 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
147 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
148 [[namespace reference has a trailing "::"]])])
150 m4_define([b4_namespace_open],
151 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])dnl
152 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
153 [^\(.\)[ ]*::], [\1])),
154 [::], [ { namespace ])[ {]])])
156 m4_define([b4_namespace_close],
157 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])dnl
158 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
159 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
161 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
171 # Output the definition of the token kinds.
172 m4_define([b4_token_enums],
173 [[enum token_kind_type
175 ]b4_symbol([-2], [id])[ = -2,
176 ]b4_symbol_foreach([b4_token_enum])dnl
186 # b4_declare_symbol_enum
187 # ----------------------
188 # The definition of the symbol internal numbers as an enum.
189 # Defining YYEMPTY here is important: it forces the compiler
190 # to use a signed type, which matters for yytoken.
191 m4_define([b4_declare_symbol_enum],
192 [[enum symbol_kind_type
194 YYNTOKENS = ]b4_tokens_number[, ///< Number of tokens.
195 ]b4_symbol(empty, kind_base)[ = -2,
196 ]b4_symbol_foreach([ b4_symbol_enum])dnl
201 ## ----------------- ##
202 ## Semantic Values. ##
203 ## ----------------- ##
207 # b4_value_type_declare
208 # ---------------------
209 # Declare value_type.
210 m4_define([b4_value_type_declare],
211 [b4_value_type_setup[]dnl
212 [ /// Symbol semantic values.
213 ]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
215 [[ typedef ]b4_percent_define_get([[api.value.type]])[ value_type;]],
216 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
217 [union\|union-directive],
220 ]b4_user_union_members[
225 # b4_public_types_declare
226 # -----------------------
227 # Define the public types: token, semantic value, location, and so forth.
228 # Depending on %define token_lex, may be output in the header or source file.
229 m4_define([b4_public_types_declare],
230 [[#ifdef ]b4_api_PREFIX[STYPE
232 # pragma GCC message "bison: do not #define ]b4_api_PREFIX[STYPE in C++, use %define api.value.type"
234 typedef ]b4_api_PREFIX[STYPE value_type;
236 ]b4_value_type_declare[
238 /// Backward compatibility (Bison 3.8).
239 typedef value_type semantic_type;]b4_locations_if([
240 /// Symbol locations.
241 typedef b4_percent_define_get([[api.location.type]],
242 [[location]]) location_type;])[
244 /// Syntax errors thrown from user actions.
245 struct syntax_error : std::runtime_error
247 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
248 : std::runtime_error (m)]b4_locations_if([
252 syntax_error (const syntax_error& s)
253 : std::runtime_error (s.what ())]b4_locations_if([
254 , location (s.location)])[
257 ~syntax_error () YY_NOEXCEPT YY_NOTHROW;]b4_locations_if([
259 location_type location;])[
266 /// Backward compatibility alias (Bison 3.6).
267 typedef token_kind_type yytokentype;
270 /// Token kind, as returned by yylex.
271 typedef token::yytokentype token_kind_type;
273 /// Backward compatibility alias (Bison 3.6).
274 typedef token_kind_type token_type;
279 ]b4_declare_symbol_enum[
282 /// (Internal) symbol kind.
283 typedef symbol_kind::symbol_kind_type symbol_kind_type;
285 /// The number of tokens.
286 static const symbol_kind_type YYNTOKENS = symbol_kind::YYNTOKENS;
290 # b4_symbol_type_define
291 # ---------------------
292 # Define symbol_type, the external type for symbols used for symbol
294 m4_define([b4_symbol_type_define],
295 [[ /// A complete symbol.
297 /// Expects its Base type to provide access to the symbol kind
300 /// Provide access to semantic value]b4_locations_if([ and location])[.
301 template <typename Base>
302 struct basic_symbol : Base
305 typedef Base super_type;
307 /// Default constructor.
309 : value ()]b4_locations_if([
313 #if 201103L <= YY_CPLUSPLUS
314 /// Move constructor.
315 basic_symbol (basic_symbol&& that)
316 : Base (std::move (that))
317 , value (]b4_variant_if([], [std::move (that.value)]))b4_locations_if([
318 , location (std::move (that.location))])[
320 b4_symbol_variant([this->kind ()], [value], [move],
321 [std::move (that.value)])
325 /// Copy constructor.
326 basic_symbol (const basic_symbol& that);]b4_variant_if([[
328 /// Constructors for typed symbols.
329 ]b4_type_foreach([b4_basic_symbol_constructor_define], [
331 /// Constructor for valueless symbols.
332 basic_symbol (typename Base::kind_type t]b4_locations_if([,
333 YY_MOVE_REF (location_type) l])[);
335 /// Constructor for symbols with semantic value.
336 basic_symbol (typename Base::kind_type t,
337 YY_RVREF (value_type) v]b4_locations_if([,
338 YY_RVREF (location_type) l])[);
340 /// Destroy the symbol.
346 /// Destroy contents, and record that is empty.
347 void clear () YY_NOEXCEPT
350 symbol_kind_type yykind = this->kind ();
351 basic_symbol<Base>& yysym = *this;
355 ]b4_symbol_foreach([b4_symbol_destructor])dnl
360 // Value type destructor.
361 ]b4_symbol_variant([[yykind]], [[value]], [[template destroy]])])[
365 ]b4_parse_error_bmatch(
367 [[ /// The user-facing name of this symbol.
368 const char *name () const YY_NOEXCEPT
370 return ]b4_parser_class[::symbol_name (this->kind ());
373 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
374 /// The user-facing name of this symbol.
375 const char *name () const YY_NOEXCEPT
377 return ]b4_parser_class[::symbol_name (this->kind ());
379 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
382 [[ /// The user-facing name of this symbol.
383 std::string name () const YY_NOEXCEPT
385 return ]b4_parser_class[::symbol_name (this->kind ());
388 /// Backward compatibility (Bison 3.6).
389 symbol_kind_type type_get () const YY_NOEXCEPT;
392 bool empty () const YY_NOEXCEPT;
394 /// Destructive move, \a s is emptied into this.
395 void move (basic_symbol& s);
397 /// The semantic value.
398 value_type value;]b4_locations_if([
401 location_type location;])[
404 #if YY_CPLUSPLUS < 201103L
405 /// Assignment operator.
406 basic_symbol& operator= (const basic_symbol& that);
410 /// Type access provider for token (enum) based symbols.
413 /// Default constructor.
416 #if 201103L <= YY_CPLUSPLUS
417 /// Move constructor.
418 by_kind (by_kind&& that);
421 /// Copy constructor.
422 by_kind (const by_kind& that);
424 /// The symbol kind as needed by the constructor.
425 typedef token_kind_type kind_type;
427 /// Constructor from (external) token numbers.
428 by_kind (kind_type t);
430 /// Record that this symbol is empty.
431 void clear () YY_NOEXCEPT;
433 /// Steal the symbol kind from \a that.
434 void move (by_kind& that);
436 /// The (internal) type number (corresponding to \a type).
437 /// \a empty when empty.
438 symbol_kind_type kind () const YY_NOEXCEPT;
440 /// Backward compatibility (Bison 3.6).
441 symbol_kind_type type_get () const YY_NOEXCEPT;
444 /// \a ]b4_symbol_prefix[YYEMPTY when empty.
445 symbol_kind_type kind_;
448 /// Backward compatibility for a private implementation detail (Bison 3.6).
449 typedef by_kind by_type;
451 /// "External" symbols: returned by the scanner.
452 struct symbol_type : basic_symbol<by_kind>
455 typedef basic_symbol<by_kind> super_type;
460 /// Constructor for valueless symbols, and symbols from each type.
461 ]b4_type_foreach([_b4_symbol_constructor_define])dnl
466 # b4_public_types_define(hh|cc)
467 # -----------------------------
468 # Provide the implementation needed by the public types.
469 m4_define([b4_public_types_define],
471 template <typename Base>
472 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (const basic_symbol& that)
474 , value (]b4_variant_if([], [that.value]))b4_locations_if([
475 , location (that.location)])[
477 b4_symbol_variant([this->kind ()], [value], [copy],
478 [YY_MOVE (that.value)])
481 ]b4_variant_if([], [[
482 /// Constructor for valueless symbols.
483 template <typename Base>
484 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
485 [typename Base::kind_type t],
486 b4_locations_if([YY_MOVE_REF (location_type) l]))[)
488 , value ()]b4_locations_if([
492 template <typename Base>
493 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
494 [typename Base::kind_type t],
495 [YY_RVREF (value_type) v],
496 b4_locations_if([YY_RVREF (location_type) l]))[)
498 , value (]b4_variant_if([], [YY_MOVE (v)])[)]b4_locations_if([
499 , location (YY_MOVE (l))])[
502 ]b4_symbol_variant([this->kind ()], [value], [YY_MOVE_OR_COPY], [YY_MOVE (v)])])[}]])[
504 template <typename Base>
505 ]b4_parser_class[::symbol_kind_type
506 ]b4_parser_class[::basic_symbol<Base>::type_get () const YY_NOEXCEPT
508 return this->kind ();
511 template <typename Base>
513 ]b4_parser_class[::basic_symbol<Base>::empty () const YY_NOEXCEPT
515 return this->kind () == ]b4_symbol(empty, kind)[;
518 template <typename Base>
520 ]b4_parser_class[::basic_symbol<Base>::move (basic_symbol& s)
522 super_type::move (s);
523 ]b4_variant_if([b4_symbol_variant([this->kind ()], [value], [move],
524 [YY_MOVE (s.value)])],
525 [value = YY_MOVE (s.value);])[]b4_locations_if([
526 location = YY_MOVE (s.location);])[
530 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind ()
531 : kind_ (]b4_symbol(empty, kind)[)
534 #if 201103L <= YY_CPLUSPLUS
535 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (by_kind&& that)
542 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (const by_kind& that)
546 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (token_kind_type t)
547 : kind_ (yytranslate_ (t))
550 ]b4_inline([$1])[void
551 ]b4_parser_class[::by_kind::clear () YY_NOEXCEPT
553 kind_ = ]b4_symbol(empty, kind)[;
556 ]b4_inline([$1])[void
557 ]b4_parser_class[::by_kind::move (by_kind& that)
563 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
564 ]b4_parser_class[::by_kind::kind () const YY_NOEXCEPT
569 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
570 ]b4_parser_class[::by_kind::type_get () const YY_NOEXCEPT
572 return this->kind ();
577 # b4_token_constructor_define
578 # ----------------------------
579 # Define make_FOO for all the token kinds.
580 # Use at class-level. Redefined in variant.hh.
581 m4_define([b4_token_constructor_define], [])
584 # b4_yytranslate_define(cc|hh)
585 # ----------------------------
586 # Define yytranslate_. Sometimes used in the header file ($1=hh),
587 # sometimes in the cc file.
588 m4_define([b4_yytranslate_define],
589 [ b4_inline([$1])b4_parser_class[::symbol_kind_type
590 ]b4_parser_class[::yytranslate_ (int t)
592 ]b4_api_token_raw_if(
593 [[ return static_cast<symbol_kind_type> (t);]],
594 [[ // YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to
595 // TOKEN-NUM as returned by yylex.
597 const ]b4_int_type_for([b4_translate])[
602 // Last valid token kind.
603 const int code_max = ]b4_code_max[;
606 return symbol_kind::]b4_symbol_prefix[YYEOF;
607 else if (t <= code_max)
608 return static_cast <symbol_kind_type> (translate_table[t]);
610 return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
615 # b4_lhs_value([TYPE])
616 # --------------------
617 m4_define([b4_lhs_value],
618 [b4_symbol_value([yyval], [$1])])
621 # b4_rhs_value(RULE-LENGTH, POS, [TYPE])
622 # --------------------------------------
624 m4_define([b4_rhs_value],
625 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
631 m4_define([b4_lhs_location],
635 # b4_rhs_location(RULE-LENGTH, POS)
636 # ---------------------------------
637 # Expansion of @POS, where the current rule has RULE-LENGTH symbols
639 m4_define([b4_rhs_location],
640 [(yylocation_stack_@{($1) - ($2)@})])
643 # b4_parse_param_decl
644 # -------------------
645 # Extra formal arguments of the constructor.
646 # Change the parameter names from "foo" into "foo_yyarg", so that
647 # there is no collision bw the user chosen attribute name, and the
648 # argument name in the constructor.
649 m4_define([b4_parse_param_decl],
650 [m4_ifset([b4_parse_param],
651 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
653 m4_define([b4_parse_param_decl_1],
658 # b4_parse_param_cons
659 # -------------------
660 # Extra initialisations of the constructor.
661 m4_define([b4_parse_param_cons],
662 [m4_ifset([b4_parse_param],
664 b4_cc_constructor_calls(b4_parse_param)])])
665 m4_define([b4_cc_constructor_calls],
666 [m4_map_sep([b4_cc_constructor_call], [,
668 m4_define([b4_cc_constructor_call],
671 # b4_parse_param_vars
672 # -------------------
673 # Extra instance variables.
674 m4_define([b4_parse_param_vars],
675 [m4_ifset([b4_parse_param],
678 b4_cc_var_decls(b4_parse_param)])])
679 m4_define([b4_cc_var_decls],
680 [m4_map_sep([b4_cc_var_decl], [
682 m4_define([b4_cc_var_decl],
690 # b4_yylloc_default_define
691 # ------------------------
692 # Define YYLLOC_DEFAULT.
693 m4_define([b4_yylloc_default_define],
694 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
695 If N is 0, then set CURRENT to the empty location which ends
696 the previous symbol: RHS[0] (always defined). */
698 # ifndef YYLLOC_DEFAULT
699 # define YYLLOC_DEFAULT(Current, Rhs, N) \
703 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
704 (Current).end = YYRHSLOC (Rhs, N).end; \
708 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
718 b4_token_ctor_if([b4_variant_if([],
719 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
720 [cannot use '%s' without '%s'],
721 [%define api.token.constructor],
722 [%define api.value.type variant]))])])