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([[global_tokens_and_yystype]], [[false]])
113 b4_percent_define_default([[define_location_comparison]],
114 [m4_if(b4_percent_define_get([[filename_type]]),
115 [std::string], [[true]], [[false]])])
123 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
126 # Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
127 # would compile as an absolute reference with 'parser' in the global namespace.
128 # b4_namespace_open would open an anonymous namespace and thus establish
129 # internal linkage. This would compile. However, it's cryptic, and internal
130 # linkage for the parser would be specified in all translation units that
131 # include the header, which is always generated. If we ever need to permit
132 # internal linkage somehow, surely we can find a cleaner approach.
133 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
134 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
135 [[namespace reference is empty]])])
137 # Instead of assuming the C++ compiler will do it, Bison should reject any
138 # invalid b4_namespace_ref that would be converted to a valid
139 # b4_namespace_open. The problem is that Bison doesn't always output
140 # b4_namespace_ref to uncommented code but should reserve the ability to do so
141 # in future releases without risking breaking any existing user grammars.
142 # Specifically, don't allow empty names as b4_namespace_open would just convert
143 # those into anonymous namespaces, and that might tempt some users.
144 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
145 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
146 [[namespace reference has consecutive "::"]])])
147 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
148 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
149 [[namespace reference has a trailing "::"]])])
151 m4_define([b4_namespace_open],
152 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])dnl
153 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
154 [^\(.\)[ ]*::], [\1])),
155 [::], [ { namespace ])[ {]])])
157 m4_define([b4_namespace_close],
158 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])dnl
159 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
160 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
162 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
172 # Output the definition of the token kinds.
173 m4_define([b4_token_enums],
174 [[enum token_kind_type
176 ]b4_symbol([-2], [id])[ = -2,
177 ]b4_symbol_foreach([b4_token_enum])dnl
187 # b4_declare_symbol_enum
188 # ----------------------
189 # The definition of the symbol internal numbers as an enum.
190 # Defining YYEMPTY here is important: it forces the compiler
191 # to use a signed type, which matters for yytoken.
192 m4_define([b4_declare_symbol_enum],
193 [[enum symbol_kind_type
195 YYNTOKENS = ]b4_tokens_number[, ///< Number of tokens.
196 ]b4_symbol(-2, kind_base)[ = -2,
197 ]b4_symbol_foreach([ b4_symbol_enum])dnl
202 ## ----------------- ##
203 ## Semantic Values. ##
204 ## ----------------- ##
208 # b4_value_type_declare
209 # ---------------------
210 # Declare semantic_type.
211 m4_define([b4_value_type_declare],
212 [b4_value_type_setup[]dnl
213 [ /// Symbol semantic values.
214 ]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
216 [[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
217 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
218 [union\|union-directive],
219 [[ union semantic_type
221 ]b4_user_union_members[
226 # b4_public_types_declare
227 # -----------------------
228 # Define the public types: token, semantic value, location, and so forth.
229 # Depending on %define token_lex, may be output in the header or source file.
230 m4_define([b4_public_types_declare],
231 [[#ifndef ]b4_api_PREFIX[STYPE
232 ]b4_value_type_declare[
234 typedef ]b4_api_PREFIX[STYPE semantic_type;
235 #endif]b4_locations_if([
236 /// Symbol locations.
237 typedef b4_percent_define_get([[api.location.type]],
238 [[location]]) location_type;])[
240 /// Syntax errors thrown from user actions.
241 struct syntax_error : std::runtime_error
243 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
244 : std::runtime_error (m)]b4_locations_if([
248 syntax_error (const syntax_error& s)
249 : std::runtime_error (s.what ())]b4_locations_if([
250 , location (s.location)])[
253 ~syntax_error () YY_NOEXCEPT YY_NOTHROW;]b4_locations_if([
255 location_type location;])[
262 /// Backward compatibility alias (Bison 3.6).
263 typedef token_kind_type yytokentype;
266 /// Token kind, as returned by yylex.
267 typedef token::yytokentype token_kind_type;
269 /// Backward compatibility alias (Bison 3.6).
270 typedef token_kind_type token_type;
275 ]b4_declare_symbol_enum[
278 /// (Internal) symbol kind.
279 typedef symbol_kind::symbol_kind_type symbol_kind_type;
281 /// The number of tokens.
282 static const symbol_kind_type YYNTOKENS = symbol_kind::YYNTOKENS;
286 # b4_symbol_type_define
287 # ---------------------
288 # Define symbol_type, the external type for symbols used for symbol
290 m4_define([b4_symbol_type_define],
291 [[ /// A complete symbol.
293 /// Expects its Base type to provide access to the symbol kind
296 /// Provide access to semantic value]b4_locations_if([ and location])[.
297 template <typename Base>
298 struct basic_symbol : Base
301 typedef Base super_type;
303 /// Default constructor.
305 : value ()]b4_locations_if([
309 #if 201103L <= YY_CPLUSPLUS
310 /// Move constructor.
311 basic_symbol (basic_symbol&& that)
312 : Base (std::move (that))
313 , value (]b4_variant_if([], [std::move (that.value)]))b4_locations_if([
314 , location (std::move (that.location))])[
316 b4_symbol_variant([this->kind ()], [value], [move],
317 [std::move (that.value)])
321 /// Copy constructor.
322 basic_symbol (const basic_symbol& that);]b4_variant_if([[
324 /// Constructors for typed symbols.
325 ]b4_type_foreach([b4_basic_symbol_constructor_define], [
327 /// Constructor for valueless symbols.
328 basic_symbol (typename Base::kind_type t]b4_locations_if([,
329 YY_MOVE_REF (location_type) l])[);
331 /// Constructor for symbols with semantic value.
332 basic_symbol (typename Base::kind_type t,
333 YY_RVREF (semantic_type) v]b4_locations_if([,
334 YY_RVREF (location_type) l])[);
336 /// Destroy the symbol.
342 /// Destroy contents, and record that is empty.
343 void clear () YY_NOEXCEPT
346 symbol_kind_type yykind = this->kind ();
347 basic_symbol<Base>& yysym = *this;
351 ]b4_symbol_foreach([b4_symbol_destructor])dnl
356 // Value type destructor.
357 ]b4_symbol_variant([[yykind]], [[value]], [[template destroy]])])[
361 ]b4_parse_error_bmatch(
363 [[ /// The user-facing name of this symbol.
364 const char *name () const YY_NOEXCEPT
366 return ]b4_parser_class[::symbol_name (this->kind ());
369 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
370 /// The user-facing name of this symbol.
371 const char *name () const YY_NOEXCEPT
373 return ]b4_parser_class[::symbol_name (this->kind ());
375 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
378 [[ /// The user-facing name of this symbol.
379 std::string name () const YY_NOEXCEPT
381 return ]b4_parser_class[::symbol_name (this->kind ());
384 /// Backward compatibility (Bison 3.6).
385 symbol_kind_type type_get () const YY_NOEXCEPT;
388 bool empty () const YY_NOEXCEPT;
390 /// Destructive move, \a s is emptied into this.
391 void move (basic_symbol& s);
393 /// The semantic value.
394 semantic_type value;]b4_locations_if([
397 location_type location;])[
400 #if YY_CPLUSPLUS < 201103L
401 /// Assignment operator.
402 basic_symbol& operator= (const basic_symbol& that);
406 /// Type access provider for token (enum) based symbols.
409 /// Default constructor.
412 #if 201103L <= YY_CPLUSPLUS
413 /// Move constructor.
414 by_kind (by_kind&& that);
417 /// Copy constructor.
418 by_kind (const by_kind& that);
420 /// The symbol kind as needed by the constructor.
421 typedef token_kind_type kind_type;
423 /// Constructor from (external) token numbers.
424 by_kind (kind_type t);
426 /// Record that this symbol is empty.
427 void clear () YY_NOEXCEPT;
429 /// Steal the symbol kind from \a that.
430 void move (by_kind& that);
432 /// The (internal) type number (corresponding to \a type).
433 /// \a empty when empty.
434 symbol_kind_type kind () const YY_NOEXCEPT;
436 /// Backward compatibility (Bison 3.6).
437 symbol_kind_type type_get () const YY_NOEXCEPT;
440 /// \a ]b4_symbol_prefix[YYEMPTY when empty.
441 symbol_kind_type kind_;
444 /// Backward compatibility for a private implementation detail (Bison 3.6).
445 typedef by_kind by_type;
447 /// "External" symbols: returned by the scanner.
448 struct symbol_type : basic_symbol<by_kind>
451 typedef basic_symbol<by_kind> super_type;
456 /// Constructor for valueless symbols, and symbols from each type.
457 ]b4_type_foreach([_b4_token_constructor_define])dnl
462 # b4_public_types_define(hh|cc)
463 # -----------------------------
464 # Provide the implementation needed by the public types.
465 m4_define([b4_public_types_define],
467 template <typename Base>
468 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (const basic_symbol& that)
470 , value (]b4_variant_if([], [that.value]))b4_locations_if([
471 , location (that.location)])[
473 b4_symbol_variant([this->kind ()], [value], [copy],
474 [YY_MOVE (that.value)])
477 ]b4_variant_if([], [[
478 /// Constructor for valueless symbols.
479 template <typename Base>
480 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
481 [typename Base::kind_type t],
482 b4_locations_if([YY_MOVE_REF (location_type) l]))[)
484 , value ()]b4_locations_if([
488 template <typename Base>
489 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (]b4_join(
490 [typename Base::kind_type t],
491 [YY_RVREF (semantic_type) v],
492 b4_locations_if([YY_RVREF (location_type) l]))[)
494 , value (]b4_variant_if([], [YY_MOVE (v)])[)]b4_locations_if([
495 , location (YY_MOVE (l))])[
498 ]b4_symbol_variant([this->kind ()], [value], [YY_MOVE_OR_COPY], [YY_MOVE (v)])])[}]])[
500 template <typename Base>
501 ]b4_parser_class[::symbol_kind_type
502 ]b4_parser_class[::basic_symbol<Base>::type_get () const YY_NOEXCEPT
504 return this->kind ();
507 template <typename Base>
509 ]b4_parser_class[::basic_symbol<Base>::empty () const YY_NOEXCEPT
511 return this->kind () == ]b4_symbol(-2, kind)[;
514 template <typename Base>
516 ]b4_parser_class[::basic_symbol<Base>::move (basic_symbol& s)
518 super_type::move (s);
519 ]b4_variant_if([b4_symbol_variant([this->kind ()], [value], [move],
520 [YY_MOVE (s.value)])],
521 [value = YY_MOVE (s.value);])[]b4_locations_if([
522 location = YY_MOVE (s.location);])[
526 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind ()
527 : kind_ (]b4_symbol(-2, kind)[)
530 #if 201103L <= YY_CPLUSPLUS
531 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (by_kind&& that)
538 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (const by_kind& that)
542 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (token_kind_type t)
543 : kind_ (yytranslate_ (t))
546 ]b4_inline([$1])[void
547 ]b4_parser_class[::by_kind::clear () YY_NOEXCEPT
549 kind_ = ]b4_symbol(-2, kind)[;
552 ]b4_inline([$1])[void
553 ]b4_parser_class[::by_kind::move (by_kind& that)
559 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
560 ]b4_parser_class[::by_kind::kind () const YY_NOEXCEPT
565 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type
566 ]b4_parser_class[::by_kind::type_get () const YY_NOEXCEPT
568 return this->kind ();
573 # b4_token_constructor_define
574 # ----------------------------
575 # Define symbol constructors for all the value types.
576 # Use at class-level. Redefined in variant.hh.
577 m4_define([b4_token_constructor_define], [])
580 # b4_yytranslate_define(cc|hh)
581 # ----------------------------
582 # Define yytranslate_. Sometimes used in the header file ($1=hh),
583 # sometimes in the cc file.
584 m4_define([b4_yytranslate_define],
585 [ b4_inline([$1])b4_parser_class[::symbol_kind_type
586 ]b4_parser_class[::yytranslate_ (int t)
588 ]b4_api_token_raw_if(
589 [[ return static_cast<symbol_kind_type> (t);]],
590 [[ // YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to
591 // TOKEN-NUM as returned by yylex.
593 const ]b4_int_type_for([b4_translate])[
598 // Last valid token kind.
599 const int code_max = ]b4_code_max[;
602 return symbol_kind::]b4_symbol_prefix[YYEOF;
603 else if (t <= code_max)
604 return YY_CAST (symbol_kind_type, translate_table[t]);
606 return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
611 # b4_lhs_value([TYPE])
612 # --------------------
613 m4_define([b4_lhs_value],
614 [b4_symbol_value([yyval], [$1])])
617 # b4_rhs_value(RULE-LENGTH, POS, [TYPE])
618 # --------------------------------------
620 m4_define([b4_rhs_value],
621 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
627 m4_define([b4_lhs_location],
631 # b4_rhs_location(RULE-LENGTH, POS)
632 # ---------------------------------
633 # Expansion of @POS, where the current rule has RULE-LENGTH symbols
635 m4_define([b4_rhs_location],
636 [(yylocation_stack_@{($1) - ($2)@})])
639 # b4_parse_param_decl
640 # -------------------
641 # Extra formal arguments of the constructor.
642 # Change the parameter names from "foo" into "foo_yyarg", so that
643 # there is no collision bw the user chosen attribute name, and the
644 # argument name in the constructor.
645 m4_define([b4_parse_param_decl],
646 [m4_ifset([b4_parse_param],
647 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
649 m4_define([b4_parse_param_decl_1],
654 # b4_parse_param_cons
655 # -------------------
656 # Extra initialisations of the constructor.
657 m4_define([b4_parse_param_cons],
658 [m4_ifset([b4_parse_param],
660 b4_cc_constructor_calls(b4_parse_param)])])
661 m4_define([b4_cc_constructor_calls],
662 [m4_map_sep([b4_cc_constructor_call], [,
664 m4_define([b4_cc_constructor_call],
667 # b4_parse_param_vars
668 # -------------------
669 # Extra instance variables.
670 m4_define([b4_parse_param_vars],
671 [m4_ifset([b4_parse_param],
674 b4_cc_var_decls(b4_parse_param)])])
675 m4_define([b4_cc_var_decls],
676 [m4_map_sep([b4_cc_var_decl], [
678 m4_define([b4_cc_var_decl],
686 # b4_yylloc_default_define
687 # ------------------------
688 # Define YYLLOC_DEFAULT.
689 m4_define([b4_yylloc_default_define],
690 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
691 If N is 0, then set CURRENT to the empty location which ends
692 the previous symbol: RHS[0] (always defined). */
694 # ifndef YYLLOC_DEFAULT
695 # define YYLLOC_DEFAULT(Current, Rhs, N) \
699 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
700 (Current).end = YYRHSLOC (Rhs, N).end; \
704 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
714 b4_token_ctor_if([b4_variant_if([],
715 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
716 [cannot use '%s' without '%s'],
717 [%define api.token.constructor],
718 [%define api.value.type variant]))])])