1 -*- Autoconf -*- 2 3# C++ skeleton for Bison 4 5# Copyright (C) 2002-2021 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 <https://www.gnu.org/licenses/>. 19 20# Sanity checks, before defaults installed by c.m4. 21b4_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++])]) 24 25b4_percent_define_default([[api.symbol.prefix]], [[S_]]) 26 27m4_include(b4_skeletonsdir/[c.m4]) 28 29b4_percent_define_check_kind([api.namespace], [code], [deprecated]) 30b4_percent_define_check_kind([api.parser.class], [code], [deprecated]) 31 32 33## ----- ## 34## C++. ## 35## ----- ## 36 37# b4_comment(TEXT, [PREFIX]) 38# -------------------------- 39# Put TEXT in comment. Prefix all the output lines with PREFIX. 40m4_define([b4_comment], 41[_b4_comment([$1], [$2// ], [$2// ])]) 42 43 44# b4_inline(hh|cc) 45# ---------------- 46# Expand to `inline\n ` if $1 is hh. 47m4_define([b4_inline], 48[m4_case([$1], 49 [cc], [], 50 [hh], [[inline 51 ]], 52 [m4_fatal([$0: invalid argument: $1])])]) 53 54 55# b4_cxx_portability 56# ------------------ 57m4_define([b4_cxx_portability], 58[#if defined __cplusplus 59# define YY_CPLUSPLUS __cplusplus 60#else 61# define YY_CPLUSPLUS 199711L 62#endif 63 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 71#else 72# define YY_MOVE 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& 77#endif 78 79// Support noexcept when possible. 80#if 201103L <= YY_CPLUSPLUS 81# define YY_NOEXCEPT noexcept 82# define YY_NOTHROW 83#else 84# define YY_NOEXCEPT 85# define YY_NOTHROW throw () 86#endif 87 88// Support constexpr when possible. 89#if 201703 <= YY_CPLUSPLUS 90# define YY_CONSTEXPR constexpr 91#else 92# define YY_CONSTEXPR 93#endif[]dnl 94]) 95 96 97## ---------------- ## 98## Default values. ## 99## ---------------- ## 100 101b4_percent_define_default([[api.parser.class]], [[parser]]) 102 103# Don't do that so that we remember whether we're using a user 104# request, or the default value. 105# 106# b4_percent_define_default([[api.location.type]], [[location]]) 107 108b4_percent_define_default([[api.filename.type]], [[const std::string]]) 109# Make it a warning for those who used betas of Bison 3.0. 110b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix])) 111 112b4_percent_define_default([[global_tokens_and_yystype]], [[false]]) 113b4_percent_define_default([[define_location_comparison]], 114 [m4_if(b4_percent_define_get([[filename_type]]), 115 [std::string], [[true]], [[false]])]) 116 117 118 119## ----------- ## 120## Namespace. ## 121## ----------- ## 122 123m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])]) 124 125 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. 133m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [], 134[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), 135 [[namespace reference is empty]])]) 136 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. 144m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [], 145[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), 146 [[namespace reference has consecutive "::"]])]) 147m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [], 148[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), 149 [[namespace reference has a trailing "::"]])]) 150 151m4_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 ])[ {]])]) 156 157m4_define([b4_namespace_close], 158[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])dnl 159m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]), 160 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*], 161 [\1])), 162 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])]) 163 164 165## ------------- ## 166## Token kinds. ## 167## ------------- ## 168 169 170# b4_token_enums 171# -------------- 172# Output the definition of the token kinds. 173m4_define([b4_token_enums], 174[[enum token_kind_type 175 { 176 ]b4_symbol([-2], [id])[ = -2, 177]b4_symbol_foreach([b4_token_enum])dnl 178[ };]dnl 179]) 180 181 182 183## -------------- ## 184## Symbol kinds. ## 185## -------------- ## 186 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. 192m4_define([b4_declare_symbol_enum], 193[[enum symbol_kind_type 194 { 195 YYNTOKENS = ]b4_tokens_number[, ///< Number of tokens. 196 ]b4_symbol(-2, kind_base)[ = -2, 197]b4_symbol_foreach([ b4_symbol_enum])dnl 198[ };]]) 199 200 201 202## ----------------- ## 203## Semantic Values. ## 204## ----------------- ## 205 206 207 208# b4_value_type_declare 209# --------------------- 210# Declare semantic_type. 211m4_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]]), 215[code], 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 220 { 221]b4_user_union_members[ 222 };]])])dnl 223]) 224 225 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. 230m4_define([b4_public_types_declare], 231[[#ifndef ]b4_api_PREFIX[STYPE 232]b4_value_type_declare[ 233#else 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;])[ 239 240 /// Syntax errors thrown from user actions. 241 struct syntax_error : std::runtime_error 242 { 243 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m) 244 : std::runtime_error (m)]b4_locations_if([ 245 , location (l)])[ 246 {} 247 248 syntax_error (const syntax_error& s) 249 : std::runtime_error (s.what ())]b4_locations_if([ 250 , location (s.location)])[ 251 {} 252 253 ~syntax_error () YY_NOEXCEPT YY_NOTHROW;]b4_locations_if([ 254 255 location_type location;])[ 256 }; 257 258 /// Token kinds. 259 struct token 260 { 261 ]b4_token_enums[ 262 /// Backward compatibility alias (Bison 3.6). 263 typedef token_kind_type yytokentype; 264 }; 265 266 /// Token kind, as returned by yylex. 267 typedef token::yytokentype token_kind_type; 268 269 /// Backward compatibility alias (Bison 3.6). 270 typedef token_kind_type token_type; 271 272 /// Symbol kinds. 273 struct symbol_kind 274 { 275 ]b4_declare_symbol_enum[ 276 }; 277 278 /// (Internal) symbol kind. 279 typedef symbol_kind::symbol_kind_type symbol_kind_type; 280 281 /// The number of tokens. 282 static const symbol_kind_type YYNTOKENS = symbol_kind::YYNTOKENS; 283]]) 284 285 286# b4_symbol_type_define 287# --------------------- 288# Define symbol_type, the external type for symbols used for symbol 289# constructors. 290m4_define([b4_symbol_type_define], 291[[ /// A complete symbol. 292 /// 293 /// Expects its Base type to provide access to the symbol kind 294 /// via kind (). 295 /// 296 /// Provide access to semantic value]b4_locations_if([ and location])[. 297 template <typename Base> 298 struct basic_symbol : Base 299 { 300 /// Alias to Base. 301 typedef Base super_type; 302 303 /// Default constructor. 304 basic_symbol () 305 : value ()]b4_locations_if([ 306 , location ()])[ 307 {} 308 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))])[ 315 {]b4_variant_if([ 316 b4_symbol_variant([this->kind ()], [value], [move], 317 [std::move (that.value)]) 318 ])[} 319#endif 320 321 /// Copy constructor. 322 basic_symbol (const basic_symbol& that);]b4_variant_if([[ 323 324 /// Constructors for typed symbols. 325]b4_type_foreach([b4_basic_symbol_constructor_define], [ 326])], [[ 327 /// Constructor for valueless symbols. 328 basic_symbol (typename Base::kind_type t]b4_locations_if([, 329 YY_MOVE_REF (location_type) l])[); 330 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])[); 335]])[ 336 /// Destroy the symbol. 337 ~basic_symbol () 338 { 339 clear (); 340 } 341 342 /// Destroy contents, and record that is empty. 343 void clear () YY_NOEXCEPT 344 {]b4_variant_if([[ 345 // User destructor. 346 symbol_kind_type yykind = this->kind (); 347 basic_symbol<Base>& yysym = *this; 348 (void) yysym; 349 switch (yykind) 350 { 351]b4_symbol_foreach([b4_symbol_destructor])dnl 352[ default: 353 break; 354 } 355 356 // Value type destructor. 357]b4_symbol_variant([[yykind]], [[value]], [[template destroy]])])[ 358 Base::clear (); 359 } 360 361]b4_parse_error_bmatch( 362[custom\|detailed], 363[[ /// The user-facing name of this symbol. 364 const char *name () const YY_NOEXCEPT 365 { 366 return ]b4_parser_class[::symbol_name (this->kind ()); 367 }]], 368[simple], 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 372 { 373 return ]b4_parser_class[::symbol_name (this->kind ()); 374 } 375#endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 376]], 377[verbose], 378[[ /// The user-facing name of this symbol. 379 std::string name () const YY_NOEXCEPT 380 { 381 return ]b4_parser_class[::symbol_name (this->kind ()); 382 }]])[ 383 384 /// Backward compatibility (Bison 3.6). 385 symbol_kind_type type_get () const YY_NOEXCEPT; 386 387 /// Whether empty. 388 bool empty () const YY_NOEXCEPT; 389 390 /// Destructive move, \a s is emptied into this. 391 void move (basic_symbol& s); 392 393 /// The semantic value. 394 semantic_type value;]b4_locations_if([ 395 396 /// The location. 397 location_type location;])[ 398 399 private: 400#if YY_CPLUSPLUS < 201103L 401 /// Assignment operator. 402 basic_symbol& operator= (const basic_symbol& that); 403#endif 404 }; 405 406 /// Type access provider for token (enum) based symbols. 407 struct by_kind 408 { 409 /// Default constructor. 410 by_kind (); 411 412#if 201103L <= YY_CPLUSPLUS 413 /// Move constructor. 414 by_kind (by_kind&& that); 415#endif 416 417 /// Copy constructor. 418 by_kind (const by_kind& that); 419 420 /// The symbol kind as needed by the constructor. 421 typedef token_kind_type kind_type; 422 423 /// Constructor from (external) token numbers. 424 by_kind (kind_type t); 425 426 /// Record that this symbol is empty. 427 void clear () YY_NOEXCEPT; 428 429 /// Steal the symbol kind from \a that. 430 void move (by_kind& that); 431 432 /// The (internal) type number (corresponding to \a type). 433 /// \a empty when empty. 434 symbol_kind_type kind () const YY_NOEXCEPT; 435 436 /// Backward compatibility (Bison 3.6). 437 symbol_kind_type type_get () const YY_NOEXCEPT; 438 439 /// The symbol kind. 440 /// \a ]b4_symbol_prefix[YYEMPTY when empty. 441 symbol_kind_type kind_; 442 }; 443 444 /// Backward compatibility for a private implementation detail (Bison 3.6). 445 typedef by_kind by_type; 446 447 /// "External" symbols: returned by the scanner. 448 struct symbol_type : basic_symbol<by_kind> 449 {]b4_variant_if([[ 450 /// Superclass. 451 typedef basic_symbol<by_kind> super_type; 452 453 /// Empty symbol. 454 symbol_type () {} 455 456 /// Constructor for valueless symbols, and symbols from each type. 457]b4_type_foreach([_b4_token_constructor_define])dnl 458 ])[}; 459]]) 460 461 462# b4_public_types_define(hh|cc) 463# ----------------------------- 464# Provide the implementation needed by the public types. 465m4_define([b4_public_types_define], 466[[ // basic_symbol. 467 template <typename Base> 468 ]b4_parser_class[::basic_symbol<Base>::basic_symbol (const basic_symbol& that) 469 : Base (that) 470 , value (]b4_variant_if([], [that.value]))b4_locations_if([ 471 , location (that.location)])[ 472 {]b4_variant_if([ 473 b4_symbol_variant([this->kind ()], [value], [copy], 474 [YY_MOVE (that.value)]) 475 ])[} 476 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]))[) 483 : Base (t) 484 , value ()]b4_locations_if([ 485 , location (l)])[ 486 {} 487 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]))[) 493 : Base (t) 494 , value (]b4_variant_if([], [YY_MOVE (v)])[)]b4_locations_if([ 495 , location (YY_MOVE (l))])[ 496 {]b4_variant_if([[ 497 (void) v; 498 ]b4_symbol_variant([this->kind ()], [value], [YY_MOVE_OR_COPY], [YY_MOVE (v)])])[}]])[ 499 500 template <typename Base> 501 ]b4_parser_class[::symbol_kind_type 502 ]b4_parser_class[::basic_symbol<Base>::type_get () const YY_NOEXCEPT 503 { 504 return this->kind (); 505 } 506 507 template <typename Base> 508 bool 509 ]b4_parser_class[::basic_symbol<Base>::empty () const YY_NOEXCEPT 510 { 511 return this->kind () == ]b4_symbol(-2, kind)[; 512 } 513 514 template <typename Base> 515 void 516 ]b4_parser_class[::basic_symbol<Base>::move (basic_symbol& s) 517 { 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);])[ 523 } 524 525 // by_kind. 526 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind () 527 : kind_ (]b4_symbol(-2, kind)[) 528 {} 529 530#if 201103L <= YY_CPLUSPLUS 531 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (by_kind&& that) 532 : kind_ (that.kind_) 533 { 534 that.clear (); 535 } 536#endif 537 538 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (const by_kind& that) 539 : kind_ (that.kind_) 540 {} 541 542 ]b4_inline([$1])b4_parser_class[::by_kind::by_kind (token_kind_type t) 543 : kind_ (yytranslate_ (t)) 544 {} 545 546 ]b4_inline([$1])[void 547 ]b4_parser_class[::by_kind::clear () YY_NOEXCEPT 548 { 549 kind_ = ]b4_symbol(-2, kind)[; 550 } 551 552 ]b4_inline([$1])[void 553 ]b4_parser_class[::by_kind::move (by_kind& that) 554 { 555 kind_ = that.kind_; 556 that.clear (); 557 } 558 559 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type 560 ]b4_parser_class[::by_kind::kind () const YY_NOEXCEPT 561 { 562 return kind_; 563 } 564 565 ]b4_inline([$1])[]b4_parser_class[::symbol_kind_type 566 ]b4_parser_class[::by_kind::type_get () const YY_NOEXCEPT 567 { 568 return this->kind (); 569 } 570]]) 571 572 573# b4_token_constructor_define 574# ---------------------------- 575# Define symbol constructors for all the value types. 576# Use at class-level. Redefined in variant.hh. 577m4_define([b4_token_constructor_define], []) 578 579 580# b4_yytranslate_define(cc|hh) 581# ---------------------------- 582# Define yytranslate_. Sometimes used in the header file ($1=hh), 583# sometimes in the cc file. 584m4_define([b4_yytranslate_define], 585[ b4_inline([$1])b4_parser_class[::symbol_kind_type 586 ]b4_parser_class[::yytranslate_ (int t) 587 { 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. 592 static 593 const ]b4_int_type_for([b4_translate])[ 594 translate_table[] = 595 { 596 ]b4_translate[ 597 }; 598 // Last valid token kind. 599 const int code_max = ]b4_code_max[; 600 601 if (t <= 0) 602 return symbol_kind::]b4_symbol_prefix[YYEOF; 603 else if (t <= code_max) 604 return YY_CAST (symbol_kind_type, translate_table[t]); 605 else 606 return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[ 607 } 608]]) 609 610 611# b4_lhs_value([TYPE]) 612# -------------------- 613m4_define([b4_lhs_value], 614[b4_symbol_value([yyval], [$1])]) 615 616 617# b4_rhs_value(RULE-LENGTH, POS, [TYPE]) 618# -------------------------------------- 619# FIXME: Dead code. 620m4_define([b4_rhs_value], 621[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])]) 622 623 624# b4_lhs_location() 625# ----------------- 626# Expansion of @$. 627m4_define([b4_lhs_location], 628[(yyloc)]) 629 630 631# b4_rhs_location(RULE-LENGTH, POS) 632# --------------------------------- 633# Expansion of @POS, where the current rule has RULE-LENGTH symbols 634# on RHS. 635m4_define([b4_rhs_location], 636[(yylocation_stack_@{($1) - ($2)@})]) 637 638 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. 645m4_define([b4_parse_param_decl], 646[m4_ifset([b4_parse_param], 647 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])]) 648 649m4_define([b4_parse_param_decl_1], 650[$1_yyarg]) 651 652 653 654# b4_parse_param_cons 655# ------------------- 656# Extra initialisations of the constructor. 657m4_define([b4_parse_param_cons], 658 [m4_ifset([b4_parse_param], 659 [ 660 b4_cc_constructor_calls(b4_parse_param)])]) 661m4_define([b4_cc_constructor_calls], 662 [m4_map_sep([b4_cc_constructor_call], [, 663 ], [$@])]) 664m4_define([b4_cc_constructor_call], 665 [$2 ($2_yyarg)]) 666 667# b4_parse_param_vars 668# ------------------- 669# Extra instance variables. 670m4_define([b4_parse_param_vars], 671 [m4_ifset([b4_parse_param], 672 [ 673 // User arguments. 674b4_cc_var_decls(b4_parse_param)])]) 675m4_define([b4_cc_var_decls], 676 [m4_map_sep([b4_cc_var_decl], [ 677], [$@])]) 678m4_define([b4_cc_var_decl], 679 [ $1;]) 680 681 682## ---------## 683## Values. ## 684## ---------## 685 686# b4_yylloc_default_define 687# ------------------------ 688# Define YYLLOC_DEFAULT. 689m4_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). */ 693 694# ifndef YYLLOC_DEFAULT 695# define YYLLOC_DEFAULT(Current, Rhs, N) \ 696 do \ 697 if (N) \ 698 { \ 699 (Current).begin = YYRHSLOC (Rhs, 1).begin; \ 700 (Current).end = YYRHSLOC (Rhs, N).end; \ 701 } \ 702 else \ 703 { \ 704 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \ 705 } \ 706 while (false) 707# endif 708]]) 709 710## -------- ## 711## Checks. ## 712## -------- ## 713 714b4_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]))])]) 719