3 # C++ skeleton for Bison
5 # Copyright (C) 2002-2022 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],
231 [b4_value_type_declare],
232 [[#ifdef ]b4_api_PREFIX[STYPE
234 # pragma GCC message "bison: do not #define ]b4_api_PREFIX[STYPE in C++, use %define api.value.type"
236 typedef ]b4_api_PREFIX[STYPE value_type;
238 ]b4_value_type_declare[
240 /// Backward compatibility (Bison 3.8).
241 typedef value_type semantic_type;
242 ]])[]b4_locations_if([
243 /// Symbol locations.
244 typedef b4_percent_define_get([[api.location.type]],
245 [[location]]) location_type;])[
247 /// Syntax errors thrown from user actions.
248 struct syntax_error : std::runtime_error
250 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
251 : std::runtime_error (m)]b4_locations_if([
255 syntax_error (const syntax_error& s)
256 : std::runtime_error (s.what ())]b4_locations_if([
257 , location (s.location)])[
260 ~syntax_error () YY_NOEXCEPT YY_NOTHROW;]b4_locations_if([
262 location_type location;])[
268 ]b4_token_enums[]b4_glr2_cc_if([], [[
269 /// Backward compatibility alias (Bison 3.6).
270 typedef token_kind_type yytokentype;]])[
273 /// Token kind, as returned by yylex.
274 typedef token::token_kind_type token_kind_type;]b4_glr2_cc_if([], [[
276 /// Backward compatibility alias (Bison 3.6).
277 typedef token_kind_type token_type;]])[
282 ]b4_declare_symbol_enum[
285 /// (Internal) symbol kind.
286 typedef symbol_kind::symbol_kind_type symbol_kind_type;
288 /// The number of tokens.
289 static const symbol_kind_type YYNTOKENS = symbol_kind::YYNTOKENS;
293 # b4_symbol_type_define
294 # ---------------------
295 # Define symbol_type, the external type for symbols used for symbol
297 m4_define([b4_symbol_type_define],
298 [[ /// A complete symbol.
300 /// Expects its Base type to provide access to the symbol kind
303 /// Provide access to semantic value]b4_locations_if([ and location])[.
304 template <typename Base>
305 struct basic_symbol : Base
308 typedef Base super_type;
310 /// Default constructor.
311 basic_symbol () YY_NOEXCEPT
312 : value ()]b4_locations_if([
316 #if 201103L <= YY_CPLUSPLUS
317 /// Move constructor.
318 basic_symbol (basic_symbol&& that)
319 : Base (std::move (that))
320 , value (]b4_variant_if([], [std::move (that.value)]))b4_locations_if([
321 , location (std::move (that.location))])[
323 b4_symbol_variant([this->kind ()], [value], [move],
324 [std::move (that.value)])
328 /// Copy constructor.
329 basic_symbol (const basic_symbol& that);]b4_variant_if([[
331 /// Constructors for typed symbols.
332 ]b4_type_foreach([b4_basic_symbol_constructor_define], [
334 /// Constructor for valueless symbols.
335 basic_symbol (typename Base::kind_type t]b4_locations_if([,
336 YY_MOVE_REF (location_type) l])[);
338 /// Constructor for symbols with semantic value.
339 basic_symbol (typename Base::kind_type t,
340 YY_RVREF (value_type) v]b4_locations_if([,
341 YY_RVREF (location_type) l])[);
343 /// Destroy the symbol.
349 #if 201103L <= YY_CPLUSPLUS
351 basic_symbol& operator= (const basic_symbol& that)
353 Base::operator= (that);]b4_variant_if([[
354 ]b4_symbol_variant([this->kind ()], [value], [copy],
356 value = that.value]])[;]b4_locations_if([[
357 location = that.location;]])[
362 basic_symbol& operator= (basic_symbol&& that)
364 Base::operator= (std::move (that));]b4_variant_if([[
365 ]b4_symbol_variant([this->kind ()], [value], [move],
366 [std::move (that.value)])], [[
367 value = std::move (that.value)]])[;]b4_locations_if([[
368 location = std::move (that.location);]])[
373 /// Destroy contents, and record that is empty.
374 void clear () YY_NOEXCEPT
377 symbol_kind_type yykind = this->kind ();
378 basic_symbol<Base>& yysym = *this;
382 ]b4_symbol_foreach([b4_symbol_destructor])dnl
387 // Value type destructor.
388 ]b4_symbol_variant([[yykind]], [[value]], [[template destroy]])])[
392 ]b4_parse_error_bmatch(
394 [[ /// The user-facing name of this symbol.
395 const char *name () const YY_NOEXCEPT
397 return ]b4_parser_class[::symbol_name (this->kind ());
400 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
401 /// The user-facing name of this symbol.
402 const char *name () const YY_NOEXCEPT
404 return ]b4_parser_class[::symbol_name (this->kind ());
406 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
409 [[ /// The user-facing name of this symbol.
410 std::string name () const YY_NOEXCEPT
412 return ]b4_parser_class[::symbol_name (this->kind ());
413 }]])[]b4_glr2_cc_if([], [[
415 /// Backward compatibility (Bison 3.6).
416 symbol_kind_type type_get () const YY_NOEXCEPT;]])[
419 bool empty () const YY_NOEXCEPT;
421 /// Destructive move, \a s is emptied into this.
422 void move (basic_symbol& s);
424 /// The semantic value.
425 value_type value;]b4_locations_if([
428 location_type location;])[
431 #if YY_CPLUSPLUS < 201103L
432 /// Assignment operator.
433 basic_symbol& operator= (const basic_symbol& that);
437 /// Type access provider for token (enum) based symbols.
440 /// The symbol kind as needed by the constructor.
441 typedef token_kind_type kind_type;
443 /// Default constructor.
444 by_kind () YY_NOEXCEPT;
446 #if 201103L <= YY_CPLUSPLUS
447 /// Move constructor.
448 by_kind (by_kind&& that) YY_NOEXCEPT;
451 /// Copy constructor.
452 by_kind (const by_kind& that) YY_NOEXCEPT;
454 /// Constructor from (external) token numbers.
455 by_kind (kind_type t) YY_NOEXCEPT;
457 #if 201103L <= YY_CPLUSPLUS
459 by_kind& operator= (const by_kind& that);
462 by_kind& operator= (by_kind&& that);
465 /// Record that this symbol is empty.
466 void clear () YY_NOEXCEPT;
468 /// Steal the symbol kind from \a that.
469 void move (by_kind& that);
471 /// The (internal) type number (corresponding to \a type).
472 /// \a empty when empty.
473 symbol_kind_type kind () const YY_NOEXCEPT;]b4_glr2_cc_if([], [[
475 /// Backward compatibility (Bison 3.6).
476 symbol_kind_type type_get () const YY_NOEXCEPT;]])[
479 /// \a ]b4_symbol_prefix[YYEMPTY when empty.
480 symbol_kind_type kind_;
481 };]b4_glr2_cc_if([], [[
483 /// Backward compatibility for a private implementation detail (Bison 3.6).
484 typedef by_kind by_type;]])[
486 /// "External" symbols: returned by the scanner.
487 struct symbol_type : basic_symbol<by_kind>
490 typedef basic_symbol<by_kind> super_type;
493 symbol_type () YY_NOEXCEPT {}
495 /// Constructor for valueless symbols, and symbols from each type.
496 ]b4_type_foreach([_b4_symbol_constructor_define])dnl
501 # b4_public_types_define(hh|cc)
502 # -----------------------------
503 # Provide the implementation needed by the public types.
504 m4_define([b4_public_types_define],
506 template <typename Base>
507 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (const basic_symbol& that)
509 , value (]b4_variant_if([], [that.value]))b4_locations_if([
510 , location (that.location)])[
512 b4_symbol_variant([this->kind ()], [value], [copy],
513 [YY_MOVE (that.value)])
516 ]b4_variant_if([], [[
517 /// Constructor for valueless symbols.
518 template <typename Base>
519 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
520 [typename Base::kind_type t],
521 b4_locations_if([YY_MOVE_REF (location_type) l]))[)
523 , value ()]b4_locations_if([
527 template <typename Base>
528 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
529 [typename Base::kind_type t],
530 [YY_RVREF (value_type) v],
531 b4_locations_if([YY_RVREF (location_type) l]))[)
533 , value (]b4_variant_if([], [YY_MOVE (v)])[)]b4_locations_if([
534 , location (YY_MOVE (l))])[
537 ]b4_symbol_variant([this->kind ()], [value], [YY_MOVE_OR_COPY], [YY_MOVE (v)])])[}]])[
539 ]b4_glr2_cc_if([], [[
540 template <typename Base>
541 ]b4_parser_class[::symbol_kind_type
542 ]b4_parser_class[::basic_symbol<Base>::type_get () const YY_NOEXCEPT
544 return this->kind ();
548 template <typename Base>
550 ]b4_parser_class[::basic_symbol<Base>::empty () const YY_NOEXCEPT
552 return this->kind () == ]b4_symbol(empty, kind)[;
555 template <typename Base>
557 ]b4_parser_class[::basic_symbol<Base>::move (basic_symbol& s)
559 super_type::move (s);
560 ]b4_variant_if([b4_symbol_variant([this->kind ()], [value], [move],
561 [YY_MOVE (s.value)])],
562 [value = YY_MOVE (s.value);])[]b4_locations_if([
563 location = YY_MOVE (s.location);])[
567 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind () YY_NOEXCEPT
568 : kind_ (]b4_symbol(empty, kind)[)
571 #if 201103L <= YY_CPLUSPLUS
572 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (by_kind&& that) YY_NOEXCEPT
579 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (const by_kind& that) YY_NOEXCEPT
583 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (token_kind_type t) YY_NOEXCEPT
584 : kind_ (yytranslate_ (t))
587 #if 201103L <= YY_CPLUSPLUS
588 ]b4_inline([$1])]b4_parser_class[::by_kind&
589 b4_parser_class[::by_kind::by_kind::operator= (const by_kind& that)
595 ]b4_inline([$1])]b4_parser_class[::by_kind&
596 b4_parser_class[::by_kind::by_kind::operator= (by_kind&& that)
604 ]b4_inline([$1])[void
605 ]b4_parser_class[::by_kind::clear () YY_NOEXCEPT
607 kind_ = ]b4_symbol(empty, kind)[;
610 ]b4_inline([$1])[void
611 ]b4_parser_class[::by_kind::move (by_kind& that)
617 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
618 ]b4_parser_class[::by_kind::kind () const YY_NOEXCEPT
623 ]b4_glr2_cc_if([], [[
624 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
625 ]b4_parser_class[::by_kind::type_get () const YY_NOEXCEPT
627 return this->kind ();
633 # b4_token_constructor_define
634 # ----------------------------
635 # Define make_FOO for all the token kinds.
636 # Use at class-level. Redefined in variant.hh.
637 m4_define([b4_token_constructor_define], [])
640 # b4_yytranslate_define(cc|hh)
641 # ----------------------------
642 # Define yytranslate_. Sometimes used in the header file ($1=hh),
643 # sometimes in the cc file.
644 m4_define([b4_yytranslate_define],
645 [ b4_inline([$1])b4_parser_class[::symbol_kind_type
646 ]b4_parser_class[::yytranslate_ (int t) YY_NOEXCEPT
648 ]b4_api_token_raw_if(
649 [[ return static_cast<symbol_kind_type> (t);]],
650 [[ // YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to
651 // TOKEN-NUM as returned by yylex.
653 const ]b4_int_type_for([b4_translate])[
658 // Last valid token kind.
659 const int code_max = ]b4_code_max[;
662 return symbol_kind::]b4_symbol_prefix[YYEOF;
663 else if (t <= code_max)
664 return static_cast <symbol_kind_type> (translate_table[t]);
666 return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
671 # b4_lhs_value([TYPE])
672 # --------------------
673 m4_define([b4_lhs_value],
674 [b4_symbol_value([yyval], [$1])])
677 # b4_rhs_value(RULE-LENGTH, POS, [TYPE])
678 # --------------------------------------
680 m4_define([b4_rhs_value],
681 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
687 m4_define([b4_lhs_location],
691 # b4_rhs_location(RULE-LENGTH, POS)
692 # ---------------------------------
693 # Expansion of @POS, where the current rule has RULE-LENGTH symbols
695 m4_define([b4_rhs_location],
696 [(yylocation_stack_@{($1) - ($2)@})])
699 # b4_parse_param_decl
700 # -------------------
701 # Extra formal arguments of the constructor.
702 # Change the parameter names from "foo" into "foo_yyarg", so that
703 # there is no collision bw the user chosen attribute name, and the
704 # argument name in the constructor.
705 m4_define([b4_parse_param_decl],
706 [m4_ifset([b4_parse_param],
707 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
709 m4_define([b4_parse_param_decl_1],
714 # b4_parse_param_cons
715 # -------------------
716 # Extra initialisations of the constructor.
717 m4_define([b4_parse_param_cons],
718 [m4_ifset([b4_parse_param],
720 b4_cc_constructor_calls(b4_parse_param)])])
721 m4_define([b4_cc_constructor_calls],
722 [m4_map_sep([b4_cc_constructor_call], [,
724 m4_define([b4_cc_constructor_call],
727 # b4_parse_param_vars
728 # -------------------
729 # Extra instance variables.
730 m4_define([b4_parse_param_vars],
731 [m4_ifset([b4_parse_param],
734 b4_cc_var_decls(b4_parse_param)])])
735 m4_define([b4_cc_var_decls],
736 [m4_map_sep([b4_cc_var_decl], [
738 m4_define([b4_cc_var_decl],
746 # b4_yylloc_default_define
747 # ------------------------
748 # Define YYLLOC_DEFAULT.
749 m4_define([b4_yylloc_default_define],
750 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
751 If N is 0, then set CURRENT to the empty location which ends
752 the previous symbol: RHS[0] (always defined). */
754 # ifndef YYLLOC_DEFAULT
755 # define YYLLOC_DEFAULT(Current, Rhs, N) \
759 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
760 (Current).end = YYRHSLOC (Rhs, N).end; \
764 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
774 b4_token_ctor_if([b4_variant_if([],
775 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
776 [cannot use '%s' without '%s'],
777 [%define api.token.constructor],
778 [%define api.value.type variant]))])])