1 #C++ GLR skeleton for Bison 2 3 #Copyright(C) 2002 - 2015, 2018 - 2021 Free Software Foundation, Inc. 4 5 #This program is free software : you can redistribute it and / or modify 6 #it under the terms of the GNU General Public License as published by 7 #the Free Software Foundation, either version 3 of the License, or 8 #(at your option) any later version. 9 # 10 #This program is distributed in the hope that it will be useful, 11 #but WITHOUT ANY WARRANTY; without even the implied warranty of 12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the 13 #GNU General Public License for more details. 14 # 15 #You should have received a copy of the GNU General Public License 16 #along with this program.If not, see < https: // www.gnu.org/licenses/>. 17 18 m4_include(b4_skeletonsdir/[c++.m4]) 19 20 #api.value.type = variant is valid. 21 m4_define([b4_value_type_setup_variant]) 22 23 #b4_tname_if(TNAME - NEEDED, TNAME - NOT - NEEDED) 24 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 25 m4_define([b4_tname_if], 26 [m4_case(b4_percent_define_get([[parse.error]]), 27 [verbose], [$1], 28 [b4_token_table_if([$1], 29 [$2])])]) 30 31 b4_bison_locations_if([ 32 m4_define([b4_location_constructors]) 33 m4_include(b4_skeletonsdir/[location.cc])]) 34 b4_variant_if([m4_include(b4_skeletonsdir/[variant.hh])]) 35 36 m4_define([b4_parser_class], 37 [b4_percent_define_get([[api.parser.class]])]) 38 39 ]m4_define([b4_define_symbol_kind], 40 [m4_format([#define %-15s %s], 41 b4_symbol($][1, kind_base), 42 b4_namespace_ref[::]b4_parser_class[::symbol_kind::]b4_symbol($1, kind_base)) 43 ]) 44 45 #b4_integral_parser_table_define(TABLE - NAME, CONTENT, COMMENT) 46 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 47 #Define "yy<TABLE-NAME>" whose contents is CONTENT.Does not use "static", 48 #should be in unnamed namespace. 49 m4_define([b4_integral_parser_table_define], 50 [m4_ifvaln([$3], [ b4_comment([$3])])dnl 51 const b4_int_type_for([$2]) yy$1[[]] = 52 { 53 $2 54 };dnl 55 ]) 56 57 58 ## ---------------- ## 59 ## Default values. ## 60 ## ---------------- ## 61 62 #Stack parameters. 63 m4_define_default([b4_stack_depth_max], [10000]) 64 m4_define_default([b4_stack_depth_init], [200]) 65 66 67 68 ## ------------ ## 69 ## Interfaces. ## 70 ## ------------ ## 71 72 #b4_user_formals 73 #-- -- -- -- -- -- -- - 74 #The possible parse - params formal arguments preceded by a comma. 75 # 76 #This is not shared with yacc.c in c.m4 because GLR relies on ISO C 77 #formal argument declarations. 78 m4_define([b4_user_formals], 79 [m4_ifset([b4_parse_param], [, b4_formals(b4_parse_param)])]) 80 81 #b4_symbol_kind(NUM) 82 #-- -- -- -- -- -- -- -- -- - 83 m4_define([b4_symbol_kind], 84 [symbol_kind::b4_symbol_kind_base($@)]) 85 86 87 ## ----------------- ## 88 ## Semantic Values. ## 89 ## ----------------- ## 90 91 #b4_lhs_value(SYMBOL - NUM, [TYPE]) 92 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 93 #See README. 94 m4_define([b4_lhs_value], 95 [b4_symbol_value([(*yyvalp)], [$1], [$2])]) 96 97 #b4_rhs_data(RULE - LENGTH, POS) 98 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- - 99 #See README. 100 m4_define([b4_rhs_data], 101 [(static_cast<glr_stack_item const *>(yyvsp))@{YYFILL (b4_subtract([$2], [$1]))@}.getState()]) 102 103 #b4_rhs_value(RULE - LENGTH, POS, SYMBOL - NUM, [TYPE]) 104 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 105 #Expansion of $$ or $ < TYPE> $, for symbol SYMBOL - NUM. 106 m4_define([b4_rhs_value], 107 [b4_symbol_value([b4_rhs_data([$1], [$2]).value ()], [$3], [$4])]) 108 109 110 111 ## ----------- ## 112 ## Locations. ## 113 ## ----------- ## 114 115 #b4_lhs_location() 116 #-- -- -- -- -- -- -- -- - 117 #Expansion of @$. 118 m4_define([b4_lhs_location], 119 [(*yylocp)]) 120 121 #b4_rhs_location(RULE - LENGTH, NUM) 122 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 123 #Expansion of @NUM, where the current rule has RULE - LENGTH symbols 124 #on RHS. 125 m4_define([b4_rhs_location], 126 [(b4_rhs_data([$1], [$2]).yyloc)]) 127 128 #b4_symbol_action(SYMBOL - NUM, KIND) 129 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 130 #Run the action KIND(destructor or printer) for SYMBOL - NUM. 131 #Same as in C, but using references instead of pointers. 132 # 133 #Currently we need two different b4_symbol_action : once for the 134 #self - contained symbols, and another time for yy_destroy_ and 135 #yy_symbol_value_print_, which don't use genuine symbols yet. 136 m4_define([b4_symbol_action], 137 [b4_symbol_if([$1], [has_$2], 138 [m4_pushdef([b4_symbol_value], m4_defn([b4_symbol_value_template]))[]dnl 139 b4_dollar_pushdef([yysym.value], 140 [$1], 141 [], 142 [yysym.location])dnl 143 _b4_symbol_case([$1])[]dnl 144 b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl 145 b4_symbol([$1], [$2]) 146 b4_syncline([@oline@], [@ofile@])dnl 147 break; 148 149 m4_popdef([b4_symbol_value])[]dnl 150 b4_dollar_popdef[]dnl 151 ])]) 152 153 #b4_symbol_action_for_yyval(SYMBOL - NUM, KIND) 154 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 155 #Run the action KIND(destructor or printer) for SYMBOL - NUM. 156 #Same as in C, but using references instead of pointers. 157 m4_define([b4_symbol_action_for_yyval], 158 [b4_symbol_if([$1], [has_$2], 159 [b4_dollar_pushdef([yyval], 160 [$1], 161 [], 162 [yyloc])dnl 163 _b4_symbol_case([$1])[]dnl 164 b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl 165 b4_symbol([$1], [$2]) 166 b4_syncline([@oline@], [@ofile@])dnl 167 break; 168 169 b4_dollar_popdef[]dnl 170 ])]) 171 172 #b4_call_merger(MERGER - NUM, MERGER - NAME, SYMBOL - SUM) 173 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 174 m4_define([b4_call_merger], 175 [b4_case([$1], 176 [ b4_symbol_if([$3], [has_type], 177 [b4_variant_if([yy0.as< b4_symbol($3, type) > () = $2 (yy0.as< b4_symbol($3, type) >(), yy1.as< b4_symbol($3, type) >());], 178 [yy0.b4_symbol($3, slot) = $2 (yy0, yy1);])], 179 [yy0 = $2 (yy0, yy1);])])]) 180 181 #b4_yylex 182 #-- -- -- -- 183 #Call yylex. 184 m4_define([b4_yylex], 185 [b4_token_ctor_if( 186 [b4_function_call([yylex], 187 [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))], 188 [b4_function_call([yylex], [int], 189 [[value_type *], [&this->yyla.value]][]dnl 190 b4_locations_if([, [[location_type *], [&this->yyla.location]]])dnl 191 m4_ifdef([b4_lex_param], [, ]b4_lex_param))])]) 192 193 #b4_shared_declarations(hh | cc) 194 #-- -- -- -- -- -- -- -- -- -- -- -- -- -- - 195 #Declaration that might either go into the header(if --header, $1 = hh) 196 # or in the implementation file. 197 m4_define([b4_shared_declarations], 198 [b4_percent_code_get([[requires]])[ 199 #include <stdint.h> 200 #include <algorithm> 201 #include <cstddef> // ptrdiff_t 202 #include <cstring> // memcpy 203 #include <iomanip> 204 #include <iostream> 205 #include <limits> 206 #include <stdexcept> 207 #include <string> 208 #include <vector> 209 210 ]b4_cxx_portability[ 211 ]m4_ifdef([b4_location_include], 212 [[# include ]b4_location_include])[ 213 ]b4_variant_if([b4_variant_includes])[ 214 215 ]b4_YYDEBUG_define[ 216 217 ]b4_namespace_open[ 218 219 ]b4_bison_locations_if([m4_ifndef([b4_location_file], 220 [b4_location_define])])[ 221 222 /// A Bison parser. 223 class ]b4_parser_class[ 224 { 225 public: 226 ]b4_public_types_declare[ 227 ]b4_symbol_type_define[ 228 229 // FIXME: should be private eventually. 230 class glr_stack; 231 class glr_state; 232 233 /// Build a parser object. 234 ]b4_parser_class[ (]b4_parse_param_decl[); 235 ~]b4_parser_class[ (); 236 237 /// Parse. An alias for parse (). 238 /// \returns 0 iff parsing succeeded. 239 int operator() (); 240 241 /// Parse. 242 /// \returns 0 iff parsing succeeded. 243 int parse (); 244 245 #if] b4_api_PREFIX[DEBUG 246 /// The current debugging stream. 247 std::ostream& debug_stream () const; 248 /// Set the current debugging stream. 249 void set_debug_stream (std::ostream &); 250 251 /// Type for debugging levels. 252 using debug_level_type = int; 253 /// The current debugging level. 254 debug_level_type debug_level () const; 255 /// Set the current debugging level. 256 void set_debug_level (debug_level_type l); 257 #endif 258 259 /// Report a syntax error.]b4_locations_if([[ 260 /// \param loc where the syntax error is found.]])[ 261 /// \param msg a description of the syntax error. 262 void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg); 263 264 ]b4_parse_error_bmatch( 265 [custom\|detailed], 266 [[ /// The user-facing name of the symbol whose (internal) number is 267 /// YYSYMBOL. No bounds checking. 268 static const char *symbol_name (symbol_kind_type yysymbol);]], 269 [simple], 270 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 271 /// The user-facing name of the symbol whose (internal) number is 272 /// YYSYMBOL. No bounds checking. 273 static const char *symbol_name (symbol_kind_type yysymbol); 274 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 275 ]], 276 [verbose], 277 [[ /// The user-facing name of the symbol whose (internal) number is 278 /// YYSYMBOL. No bounds checking. 279 static std::string symbol_name (symbol_kind_type yysymbol);]])[ 280 281 ]b4_token_constructor_define[ 282 ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[ 283 class context 284 { 285 public: 286 context(glr_stack & yystack, const symbol_type &yyla); 287 const symbol_type &lookahead() const YY_NOEXCEPT 288 { 289 return yyla_; 290 } 291 symbol_kind_type token() const YY_NOEXCEPT 292 { 293 return yyla_.kind(); 294 }]b4_locations_if([[ 295 const location_type& location () const YY_NOEXCEPT { 296 return yyla_.location; } 297 ]])[ 298 /// Put in YYARG at most YYARGN of the expected tokens, and return the 299 /// number of tokens stored in YYARG. If YYARG is null, return the 300 /// number of expected tokens (guaranteed to be less than YYNTOKENS). 301 int expected_tokens (symbol_kind_type yyarg[], int yyargn) const; 302 303 private: 304 glr_stack& yystack_; 305 const symbol_type& yyla_; 306 }; 307 ]])[ 308 #if] b4_api_PREFIX[DEBUG 309 public: 310 /// \brief Report a symbol value on the debug stream. 311 /// \param yykind The symbol kind. 312 /// \param yyval Its semantic value.]b4_locations_if([[ 313 /// \param yyloc Its location.]])[ 314 void yy_symbol_value_print_ (symbol_kind_type yykind, 315 const value_type& yyval]b4_locations_if([[, 316 const location_type& yyloc]])[) const; 317 /// \brief Report a symbol on the debug stream. 318 /// \param yykind The symbol kind. 319 /// \param yyval Its semantic value.]b4_locations_if([[ 320 /// \param yyloc Its location.]])[ 321 void yy_symbol_print_ (symbol_kind_type yykind, 322 const value_type& yyval]b4_locations_if([[, 323 const location_type& yyloc]])[) const; 324 private: 325 /// Debug stream. 326 std::ostream* yycdebug_; 327 #endif 328 329 ]b4_parse_error_bmatch( 330 [custom], [[ 331 private: 332 /// Report a syntax error 333 /// \param yyctx the context in which the error occurred. 334 void report_syntax_error (const context& yyctx) const;]], 335 [detailed\|verbose], [[ 336 private: 337 /// The arguments of the error message. 338 int yy_syntax_error_arguments_ (const context& yyctx, 339 symbol_kind_type yyarg[], int yyargn) const; 340 341 /// Generate an error message. 342 /// \param yyctx the context in which the error occurred. 343 virtual std::string yysyntax_error_ (const context& yyctx) const;]])[ 344 345 /// Convert a scanner token kind \a t to a symbol kind. 346 /// In theory \a t should be a token_kind_type, but character literals 347 /// are valid, yet not members of the token_kind_type enum. 348 static symbol_kind_type yytranslate_ (int t) YY_NOEXCEPT; 349 350 ]b4_parse_error_bmatch( 351 [simple], 352 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 353 /// For a symbol, its name in clear. 354 static const char* const yytname_[]; 355 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 356 ]], 357 [verbose], 358 [[ /// Convert the symbol name \a n to a form suitable for a diagnostic. 359 static std::string yytnamerr_ (const char *yystr); 360 361 /// For a symbol, its name in clear. 362 static const char* const yytname_[]; 363 ]])[ 364 365 /// \brief Reclaim the memory associated to a symbol. 366 /// \param yymsg Why this token is reclaimed. 367 /// If null, print nothing. 368 /// \param yykind The symbol kind. 369 void yy_destroy_ (const char* yymsg, symbol_kind_type yykind, 370 value_type& yyval]b4_locations_if([[, 371 location_type& yyloc]])[); 372 373 ]b4_parse_param_vars[ 374 // Needs access to yy_destroy_, report_syntax_error, etc. 375 friend glr_stack; 376 }; 377 378 ]b4_token_ctor_if([b4_yytranslate_define([$1])[ 379 ]b4_public_types_define([$1])])[ 380 ]b4_namespace_close[ 381 382 ]b4_percent_code_get([[provides]])[ 383 ]])[ 384 385 386 ## -------------- ## 387 ## Output files. ## 388 ## -------------- ## 389 390 #-- -- -- -- -- -- - # 391 #Header file.# 392 #-- -- -- -- -- -- - # 393 394 ]b4_header_if([[ 395 ]b4_output_begin([b4_spec_header_file])[ 396 ]b4_copyright([Skeleton interface for Bison GLR parsers in C++], 397 [2002-2015, 2018-2021])[ 398 // C++ GLR parser skeleton written by Valentin Tolmer. 399 400 ]b4_disclaimer[ 401 ]b4_cpp_guard_open([b4_spec_mapped_header_file])[ 402 ]b4_shared_declarations([hh])[ 403 ]b4_cpp_guard_close([b4_spec_mapped_header_file])[ 404 ]b4_output_end])[ 405 406 #-- -- -- -- -- -- -- -- -- -- - # 407 #Implementation file.# 408 #-- -- -- -- -- -- -- -- -- -- - # 409 410 ]b4_output_begin([b4_parser_file_name])[ 411 ]b4_copyright([Skeleton implementation for Bison GLR parsers in C], 412 [2002-2015, 2018-2021])[ 413 // C++ GLR parser skeleton written by Valentin Tolmer. 414 415 ]b4_disclaimer[ 416 ]b4_identification[ 417 418 ]b4_percent_code_get([[top]])[ 419 ]m4_if(b4_prefix, [yy], [], 420 [[/* Substitute the variable and function names. */ 421 #define yyparse ]b4_prefix[parse 422 #define yylex ]b4_prefix[lex 423 #define yyerror ]b4_prefix[error 424 #define yydebug ]b4_prefix[debug]])[ 425 426 ]b4_user_pre_prologue[ 427 428 ]b4_null_define[ 429 430 ]b4_header_if([[#include "@basename(]b4_spec_header_file[@)"]], 431 [b4_shared_declarations([cc])])[ 432 433 namespace 434 { 435 /* Default (constant) value used for initialization for null 436 right-hand sides. Unlike the standard yacc.c template, here we set 437 the default value of $$ to a zeroed-out value. Since the default 438 value is undefined, this behavior is technically correct. */ 439 ]b4_namespace_ref[::]b4_parser_class[::value_type yyval_default; 440 } 441 442 ]b4_user_post_prologue[ 443 ]b4_percent_code_get[ 444 445 #include <cstdio> 446 #include <cstdlib> 447 448 #ifndef YY_ 449 # if defined YYENABLE_NLS && YYENABLE_NLS 450 # if ENABLE_NLS 451 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */ 452 # define YY_(Msgid) dgettext("bison-runtime", Msgid) 453 # endif 454 # endif 455 # ifndef YY_ 456 # define YY_(Msgid) Msgid 457 # endif 458 #endif 459 460 // Whether we are compiled with exception support. 461 #ifndef YY_EXCEPTIONS 462 # if defined __GNUC__ && !defined __EXCEPTIONS 463 # define YY_EXCEPTIONS 0 464 # else 465 # define YY_EXCEPTIONS 1 466 # endif 467 #endif 468 469 #ifndef YYFREE 470 # define YYFREE free 471 #endif 472 #ifndef YYMALLOC 473 # define YYMALLOC malloc 474 #endif 475 476 #ifndef YYSETJMP 477 # include <setjmp.h> 478 # define YYJMP_BUF jmp_buf 479 # define YYSETJMP(Env) setjmp(Env) 480 /* Pacify Clang and ICC. */ 481 # define YYLONGJMP(Env, Val) \ 482 do \ 483 { \ 484 longjmp(Env, Val); \ 485 YYASSERT(0); \ 486 } while (false) 487 #endif 488 489 ]b4_attribute_define([noreturn])[ 490 491 #if defined __GNUC__ && !defined __ICC && 6 <= __GNUC__ 492 # define YY_IGNORE_NULL_DEREFERENCE_BEGIN \ 493 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wnull-dereference\"") 494 # define YY_IGNORE_NULL_DEREFERENCE_END _Pragma("GCC diagnostic pop") 495 #else 496 # define YY_IGNORE_NULL_DEREFERENCE_BEGIN 497 # define YY_IGNORE_NULL_DEREFERENCE_END 498 #endif 499 500 ]b4_null_define[ 501 ]b4_cast_define[ 502 503 // FIXME: Use the same conventions as lalr1.cc. 504 ]b4_parse_assert_if[ 505 #ifndef YYASSERT 506 # define YYASSERT(Condition) ((void)((Condition) || (abort(), 0))) 507 #endif 508 509 #ifdef YYDEBUG 510 # define YYDASSERT(Condition) YYASSERT(Condition) 511 #else 512 # define YYDASSERT(Condition) 513 #endif 514 515 /* YYFINAL -- State number of the termination state. */ 516 #define YYFINAL ]b4_final_state_number[ 517 /* YYLAST -- Last index in YYTABLE. */ 518 #define YYLAST ]b4_last[ 519 520 /* YYNTOKENS -- Number of terminals. */ 521 #define YYNTOKENS ]b4_tokens_number[ 522 /* YYNNTS -- Number of nonterminals. */ 523 #define YYNNTS ]b4_nterms_number[ 524 /* YYNRULES -- Number of rules. */ 525 #define YYNRULES ]b4_rules_number[ 526 /* YYNSTATES -- Number of states. */ 527 #define YYNSTATES ]b4_states_number[ 528 /* YYMAXRHS -- Maximum number of symbols on right-hand side of rule. */ 529 #define YYMAXRHS ]b4_r2_max[ 530 /* YYMAXLEFT -- Maximum number of symbols to the left of a handle 531 accessed by $0, $-1, etc., in any rule. */ 532 #define YYMAXLEFT ]b4_max_left_semantic_context[ 533 534 namespace 535 { 536 #if] b4_api_PREFIX[DEBUG 537 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ 538 const ]b4_int_type_for([b4_rline])[ yyrline[] = 539 { 540 ]b4_rline[ 541 }; 542 #endif 543 544 #define YYPACT_NINF ]b4_pact_ninf[ 545 #define YYTABLE_NINF ]b4_table_ninf[ 546 547 ]b4_parser_tables_define[ 548 549 /* YYDPREC[RULE-NUM] -- Dynamic precedence of rule #RULE-NUM (0 if none). */ 550 const ]b4_int_type_for([b4_dprec])[ yydprec[] = 551 { 552 ]b4_dprec[ 553 }; 554 555 /* YYMERGER[RULE-NUM] -- Index of merging function for rule #RULE-NUM. */ 556 const ]b4_int_type_for([b4_merger])[ yymerger[] = 557 { 558 ]b4_merger[ 559 }; 560 561 /* YYIMMEDIATE[RULE-NUM] -- True iff rule #RULE-NUM is not to be deferred, as 562 in the case of predicates. */ 563 const bool yyimmediate[] = 564 { 565 ]b4_immediate[ 566 }; 567 568 /* YYCONFLP[YYPACT[STATE-NUM]] -- Pointer into YYCONFL of start of 569 list of conflicting reductions corresponding to action entry for 570 state STATE-NUM in yytable. 0 means no conflicts. The list in 571 yyconfl is terminated by a rule number of 0. */ 572 const ]b4_int_type_for([b4_conflict_list_heads])[ yyconflp[] = 573 { 574 ]b4_conflict_list_heads[ 575 }; 576 577 /* YYCONFL[I] -- lists of conflicting rule numbers, each terminated by 578 0, pointed into by YYCONFLP. */ 579 ]dnl Do not use b4_int_type_for here, since there are places where 580 dnl pointers onto yyconfl are taken, whose type is "short*". 581 dnl We probably ought to introduce a type for confl. 582 [const short yyconfl[] = 583 { 584 ]b4_conflicting_rules[ 585 }; 586 } // namespace 587 588 /* Error token number */ 589 #define YYTERROR 1 590 591 ]b4_locations_if([[ 592 ]b4_yylloc_default_define[ 593 #define YYRHSLOC(Rhs, K) ((Rhs)[K].getState().yyloc) 594 ]])[ 595 596 enum YYRESULTTAG { yyok, yyaccept, yyabort, yyerr }; 597 598 #define YYCHK(YYE) \ 599 do \ 600 { \ 601 YYRESULTTAG yychk_flag = YYE; \ 602 if (yychk_flag != yyok) \ 603 return yychk_flag; \ 604 } while (false) 605 606 #if] b4_api_PREFIX[DEBUG 607 608 # define YYCDEBUG \ 609 if (!yydebug) \ 610 {} \ 611 else \ 612 std::cerr 613 614 # define YY_SYMBOL_PRINT(Title, Kind, Value, Location) \ 615 do \ 616 { \ 617 if (yydebug) \ 618 { \ 619 std::cerr << Title << ' '; \ 620 yyparser.yy_symbol_print_ (Kind, Value]b4_locations_if([, Location])[); \ 621 std::cerr << '\n'; \ 622 } \ 623 } while (false) 624 625 # define YY_REDUCE_PRINT(Args) \ 626 do \ 627 { \ 628 if (yydebug) \ 629 yystateStack.yy_reduce_print Args; \ 630 } while (false) 631 632 /* Nonzero means print parse trace. It is left uninitialized so that 633 multiple parsers can coexist. */ 634 int yydebug; 635 636 namespace 637 { 638 using glr_stack = ]b4_namespace_ref[::]b4_parser_class[::glr_stack; 639 using glr_state = ]b4_namespace_ref[::]b4_parser_class[::glr_state; 640 641 void yypstack (const glr_stack& yystack, size_t yyk) 642 YY_ATTRIBUTE_UNUSED; 643 void yypdumpstack (const glr_stack& yystack) 644 YY_ATTRIBUTE_UNUSED; 645 } 646 647 #else /* !]b4_api_PREFIX[DEBUG */ 648 649 # define YYCDEBUG \ 650 if (true) \ 651 {} \ 652 else \ 653 std::cerr 654 # define YY_SYMBOL_PRINT(Title, Kind, Value, Location) \ 655 {} 656 # define YY_REDUCE_PRINT(Args) \ 657 {} 658 659 #endif /* !]b4_api_PREFIX[DEBUG */ 660 661 /* YYINITDEPTH -- initial size of the parser's stacks. */ 662 #ifndef YYINITDEPTH 663 # define YYINITDEPTH ]b4_stack_depth_init[ 664 #endif 665 666 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only 667 if the built-in stack extension method is used). 668 669 Do not make this value too large; the results are undefined if 670 SIZE_MAX < YYMAXDEPTH * sizeof (GLRStackItem) 671 evaluated with infinite-precision integer arithmetic. */ 672 673 #ifndef YYMAXDEPTH 674 # define YYMAXDEPTH ]b4_stack_depth_max[ 675 #endif 676 677 /* Minimum number of free items on the stack allowed after an 678 allocation. This is to allow allocation and initialization 679 to be completed by functions that call yyexpandGLRStack before the 680 stack is expanded, thus insuring that all necessary pointers get 681 properly redirected to new data. */ 682 #define YYHEADROOM 2 683 684 #ifndef YYSTACKEXPANDABLE 685 # define YYSTACKEXPANDABLE 1 686 #endif 687 688 namespace 689 { 690 template <typename Parameter> 691 class strong_index_alias 692 { 693 public: 694 static strong_index_alias create(std::ptrdiff_t value) 695 { 696 strong_index_alias result; 697 result.value_ = value; 698 return result; 699 } 700 701 std::ptrdiff_t const &get() const { return value_; } 702 703 size_t uget() const { return static_cast<size_t>(value_); } 704 705 strong_index_alias operator+(std::ptrdiff_t other) const 706 { 707 return strong_index_alias(get() + other); 708 } 709 710 void operator+=(std::ptrdiff_t other) { value_ += other; } 711 712 strong_index_alias operator-(std::ptrdiff_t other) 713 { 714 return strong_index_alias(get() - other); 715 } 716 717 void operator-=(std::ptrdiff_t other) { value_ -= other; } 718 719 size_t operator-(strong_index_alias other) 720 { 721 return strong_index_alias(get() - other.get()); 722 } 723 724 strong_index_alias &operator++() 725 { 726 ++value_; 727 return *this; 728 } 729 730 bool isValid() const { return value_ != INVALID_INDEX; } 731 732 void setInvalid() { value_ = INVALID_INDEX; } 733 734 bool operator==(strong_index_alias other) { return get() == other.get(); } 735 736 bool operator!=(strong_index_alias other) { return get() != other.get(); } 737 738 bool operator<(strong_index_alias other) { return get() < other.get(); } 739 740 private: 741 static const std::ptrdiff_t INVALID_INDEX; 742 743 // WARNING: 0-initialized. 744 std::ptrdiff_t value_; 745 }; // class strong_index_alias 746 747 template <typename T> 748 const std::ptrdiff_t strong_index_alias<T>::INVALID_INDEX = 749 std::numeric_limits<std::ptrdiff_t>::max(); 750 751 using state_set_index = strong_index_alias<struct glr_state_set_tag>; 752 753 state_set_index create_state_set_index(std::ptrdiff_t value) 754 { 755 return state_set_index::create(value); 756 } 757 758 /** State numbers, as in LALR(1) machine */ 759 using state_num = int; 760 761 /** Rule numbers, as in LALR(1) machine */ 762 using rule_num = int; 763 764 using parser_type = ]b4_namespace_ref[::]b4_parser_class[; 765 using glr_state = parser_type::glr_state; 766 using symbol_kind = parser_type::symbol_kind; 767 using symbol_kind_type = parser_type::symbol_kind_type; 768 using symbol_type = parser_type::symbol_type; 769 using value_type = parser_type::value_type;]b4_locations_if([[ 770 using location_type = parser_type::location_type;]])[ 771 772 // Forward declarations. 773 class glr_stack_item; 774 class semantic_option; 775 } // namespace 776 777 namespace 778 { 779 /** Accessing symbol of state YYSTATE. */ 780 inline symbol_kind_type yy_accessing_symbol(state_num yystate) 781 { 782 return YY_CAST(symbol_kind_type, yystos[yystate]); 783 } 784 785 /** Left-hand-side symbol for rule #YYRULE. */ 786 inline symbol_kind_type yylhsNonterm(rule_num yyrule) 787 { 788 return static_cast<symbol_kind_type>(yyr1[yyrule]); 789 } 790 791 /** Number of symbols composing the right hand side of rule #RULE. */ 792 inline int yyrhsLength(rule_num yyrule) 793 { 794 return yyr2[yyrule]; 795 } 796 } 797 798 namespace ]b4_namespace_ref[ 799 { 800 class ]b4_parser_class[::glr_state 801 { 802 public: 803 glr_state () 804 : yyresolved (false) 805 , yylrState (0) 806 , yyposn (0) 807 , yypred (0) 808 , yyfirstVal (0)]b4_locations_if([[ 809 , yyloc ()]])[]b4_parse_assert_if([[ 810 , magic_ (MAGIC)]])[ 811 {} 812 813 /// Build with a semantic value. 814 glr_state (state_num lrState, size_t posn, const value_type& val]b4_locations_if([[, const location_type& loc]])[) 815 : yyresolved (true) 816 , yylrState (lrState) 817 , yyposn (posn) 818 , yypred (0) 819 , yyval (]b4_variant_if([], [[val]])[)]b4_locations_if([[ 820 , yyloc (loc)]])[]b4_parse_assert_if([[ 821 , magic_ (MAGIC)]])[ 822 {]b4_variant_if([[ 823 ]b4_symbol_variant([yy_accessing_symbol (lrState)], 824 [yyval], [copy], [val])])[} 825 826 /// Build with a semantic option. 827 glr_state (state_num lrState, size_t posn) 828 : yyresolved (false) 829 , yylrState (lrState) 830 , yyposn (posn) 831 , yypred (0) 832 , yyfirstVal (0)]b4_locations_if([[ 833 , yyloc ()]])[]b4_parse_assert_if([[ 834 , magic_ (MAGIC)]])[ 835 {} 836 837 glr_state (const glr_state& other) 838 : yyresolved (other.yyresolved) 839 , yylrState (other.yylrState) 840 , yyposn (other.yyposn) 841 , yypred (0)]b4_locations_if([[ 842 , yyloc (other.yyloc)]])[]b4_parse_assert_if([[ 843 , magic_ (MAGIC)]])[ 844 { 845 setPred(other.pred()); 846 if (other.yyresolved)]b4_variant_if([[ 847 { 848 new (&yyval) value_type(); 849 ]b4_symbol_variant([yy_accessing_symbol (other.yylrState)], 850 [yyval], [copy], [other.value ()])[ 851 }]], [[ 852 new (&yyval) value_type (other.value ());]])[ 853 else 854 { 855 yyfirstVal = 0; 856 setFirstVal(other.firstVal()); 857 }]b4_parse_assert_if([[ 858 check_();]])[ 859 } 860 861 ~glr_state () 862 {]b4_parse_assert_if([[ 863 check_ ();]])[ 864 if (yyresolved) 865 {]b4_variant_if([[ 866 symbol_kind_type yykind = yy_accessing_symbol (yylrState); 867 // FIXME: User destructors. 868 // Value type destructor. 869 ]b4_symbol_variant([[yykind]], [[yyval]], [[template destroy]])])[ 870 yyval.~value_type (); 871 }]b4_parse_assert_if([[ 872 magic_ = 0;]])[ 873 } 874 875 glr_state& operator= (const glr_state& other) 876 {]b4_parse_assert_if([[ 877 check_ (); 878 other.check_ ();]])[ 879 if (!yyresolved && other.yyresolved) 880 new (&yyval) value_type; 881 yyresolved = other.yyresolved; 882 yylrState = other.yylrState; 883 yyposn = other.yyposn; 884 setPred (other.pred ()); 885 if (other.yyresolved)]b4_variant_if([[ 886 ]b4_symbol_variant([yy_accessing_symbol (other.yylrState)], 887 [yyval], [copy], [other.value ()])], [[ 888 value () = other.value ();]])[ 889 else 890 setFirstVal (other.firstVal ());]b4_locations_if([[ 891 yyloc = other.yyloc;]])[ 892 return *this; 893 } 894 895 /** Type tag for the semantic value. If true, yyval applies, otherwise 896 * yyfirstVal applies. */ 897 bool yyresolved; 898 /** Number of corresponding LALR(1) machine state. */ 899 state_num yylrState; 900 /** Source position of the last token produced by my symbol */ 901 size_t yyposn; 902 903 /// Only call pred() and setPred() on objects in yyitems, not temporaries. 904 glr_state* pred (); 905 const glr_state* pred () const; 906 void setPred (const glr_state* state); 907 908 /// Only call firstVal() and setFirstVal() on objects in yyitems, not 909 /// temporaries. 910 semantic_option* firstVal (); 911 const semantic_option* firstVal () const; 912 void setFirstVal (const semantic_option* option); 913 914 value_type& value () 915 {]b4_parse_assert_if([[ 916 check_ ();]])[ 917 return yyval; 918 } 919 920 const value_type& value () const 921 {]b4_parse_assert_if([[ 922 check_ ();]])[ 923 return yyval; 924 } 925 926 void 927 destroy (char const *yymsg, ]b4_namespace_ref[::]b4_parser_class[& yyparser); 928 929 /* DEBUGGING ONLY */ 930 #if] b4_api_PREFIX[DEBUG 931 void yy_yypstack () const 932 {]b4_parse_assert_if([[ 933 check_ ();]])[ 934 if (pred () != YY_NULLPTR) 935 { 936 pred()->yy_yypstack(); 937 std::cerr << " -> "; 938 } 939 std::cerr << yylrState << "@@" << yyposn; 940 } 941 #endif 942 943 std::ptrdiff_t indexIn (const glr_stack_item* array) const YY_ATTRIBUTE_UNUSED; 944 945 glr_stack_item* asItem () 946 {]b4_parse_assert_if([[ 947 check_ ();]])[ 948 return asItem(this); 949 } 950 951 const glr_stack_item* asItem () const 952 {]b4_parse_assert_if([[ 953 check_ ();]])[ 954 return asItem (this); 955 } 956 957 private: 958 template <typename T> 959 static const glr_stack_item* asItem (const T* state) 960 { 961 return reinterpret_cast<const glr_stack_item *>(state); 962 } 963 template <typename T> 964 static glr_stack_item* asItem (T* state) 965 { 966 return reinterpret_cast<glr_stack_item *>(state); 967 } 968 static const char *as_pointer_ (const glr_state *state) 969 { 970 return reinterpret_cast<const char *>(state); 971 } 972 static char *as_pointer_ (glr_state *state) 973 { 974 return reinterpret_cast<char *>(state); 975 } 976 /** Preceding state in this stack */ 977 std::ptrdiff_t yypred; 978 union { 979 /** First in a chain of alternative reductions producing the 980 * nonterminal corresponding to this state, threaded through 981 * yyfirstVal. Value "0" means empty. */ 982 std::ptrdiff_t yyfirstVal; 983 /** Semantic value for this state. */ 984 value_type yyval; 985 };]b4_locations_if([[ 986 // FIXME: Why public? 987 public: 988 /** Source location for this state. */ 989 location_type yyloc;]])[ 990 991 ]b4_parse_assert_if([[ 992 public: 993 // Check invariants. 994 void check_ () const 995 { 996 YY_IGNORE_NULL_DEREFERENCE_BEGIN 997 YYASSERT(this->magic_ == MAGIC); 998 YY_IGNORE_NULL_DEREFERENCE_END 999 } 1000 1001 // A magic number to check our pointer arithmetic is sane. 1002 enum { MAGIC = 713705 }; 1003 unsigned int magic_;]])[ 1004 }; // class ]b4_parser_class[::glr_state 1005 } // namespace ]b4_namespace_ref[ 1006 1007 1008 namespace 1009 { 1010 /** A stack of GLRState representing the different heads during 1011 * nondeterministic evaluation. */ 1012 class glr_state_set 1013 { 1014 public: 1015 /** Initialize YYSET to a singleton set containing an empty stack. */ 1016 glr_state_set() : yylastDeleted(YY_NULLPTR) 1017 { 1018 yystates.push_back(YY_NULLPTR); 1019 yylookaheadNeeds.push_back(false); 1020 } 1021 1022 // Behave like a vector of states. 1023 glr_state *&operator[](state_set_index index) { return yystates[index.uget()]; } 1024 1025 glr_state *operator[](state_set_index index) const { return yystates[index.uget()]; } 1026 1027 size_t size() const { return yystates.size(); } 1028 1029 std::vector<glr_state *>::iterator begin() { return yystates.begin(); } 1030 1031 std::vector<glr_state *>::iterator end() { return yystates.end(); } 1032 1033 bool lookaheadNeeds(state_set_index index) const { return yylookaheadNeeds[index.uget()]; } 1034 1035 bool setLookaheadNeeds(state_set_index index, bool value) 1036 { 1037 return yylookaheadNeeds[index.uget()] = value; 1038 } 1039 1040 /** Invalidate stack #YYK. */ 1041 void yymarkStackDeleted(state_set_index yyk) 1042 { 1043 size_t k = yyk.uget(); 1044 if (yystates[k] != YY_NULLPTR) 1045 yylastDeleted = yystates[k]; 1046 yystates[k] = YY_NULLPTR; 1047 } 1048 1049 /** Undelete the last stack in *this that was marked as deleted. Can 1050 only be done once after a deletion, and only when all other stacks have 1051 been deleted. */ 1052 void yyundeleteLastStack() 1053 { 1054 if (yylastDeleted == YY_NULLPTR || !yystates.empty()) 1055 return; 1056 yystates.push_back(yylastDeleted); 1057 YYCDEBUG << "Restoring last deleted stack as stack #0.\n"; 1058 clearLastDeleted(); 1059 } 1060 1061 /** Remove the dead stacks (yystates[i] == YY_NULLPTR) and shift the later 1062 * ones. */ 1063 void yyremoveDeletes() 1064 { 1065 size_t newsize = yystates.size(); 1066 /* j is the number of live stacks we have seen. */ 1067 for (size_t i = 0, j = 0; j < newsize; ++i) 1068 { 1069 if (yystates[i] == YY_NULLPTR) 1070 { 1071 if (i == j) 1072 { 1073 YYCDEBUG << "Removing dead stacks.\n"; 1074 } 1075 newsize -= 1; 1076 } 1077 else 1078 { 1079 yystates[j] = yystates[i]; 1080 /* In the current implementation, it's unnecessary to copy 1081 yylookaheadNeeds[i] since, after 1082 yyremoveDeletes returns, the parser immediately either enters 1083 deterministic operation or shifts a token. However, it doesn't 1084 hurt, and the code might evolve to need it. */ 1085 yylookaheadNeeds[j] = yylookaheadNeeds[i]; 1086 if (j != i) 1087 { 1088 YYCDEBUG << "Rename stack " << i << " -> " << j << ".\n"; 1089 } 1090 j += 1; 1091 } 1092 } 1093 yystates.resize(newsize); 1094 yylookaheadNeeds.resize(newsize); 1095 } 1096 1097 state_set_index yysplitStack(state_set_index yyk) 1098 { 1099 const size_t k = yyk.uget(); 1100 yystates.push_back(yystates[k]); 1101 yylookaheadNeeds.push_back(yylookaheadNeeds[k]); 1102 return create_state_set_index(static_cast<std::ptrdiff_t>(yystates.size() - 1)); 1103 } 1104 1105 void clearLastDeleted() { yylastDeleted = YY_NULLPTR; } 1106 1107 private: 1108 std::vector<glr_state *> yystates; 1109 /** During nondeterministic operation, yylookaheadNeeds tracks which 1110 * stacks have actually needed the current lookahead. During deterministic 1111 * operation, yylookaheadNeeds[0] is not maintained since it would merely 1112 * duplicate !yyla.empty (). */ 1113 std::vector<bool> yylookaheadNeeds; 1114 1115 /** The last stack we invalidated. */ 1116 glr_state *yylastDeleted; 1117 }; // class glr_state_set 1118 } // namespace 1119 1120 namespace 1121 { 1122 class semantic_option 1123 { 1124 public: 1125 semantic_option () 1126 : yyrule (0) 1127 , yystate (0) 1128 , yynext (0) 1129 , yyla ()]b4_parse_assert_if([[ 1130 , magic_ (MAGIC)]])[ 1131 {} 1132 1133 semantic_option (rule_num rule) 1134 : yyrule (rule) 1135 , yystate (0) 1136 , yynext (0) 1137 , yyla ()]b4_parse_assert_if([[ 1138 , magic_ (MAGIC)]])[ 1139 {} 1140 1141 semantic_option (const semantic_option& that) 1142 : yyrule (that.yyrule) 1143 , yystate (that.yystate) 1144 , yynext (that.yynext) 1145 , yyla (that.yyla)]b4_parse_assert_if([[ 1146 , magic_ (MAGIC)]])[ 1147 {]b4_parse_assert_if([[ 1148 that.check_ ();]])[ 1149 } 1150 1151 // Needed for the assignment in yynewSemanticOption. 1152 semantic_option& operator= (const semantic_option& that) 1153 {]b4_parse_assert_if([[ 1154 check_ (); 1155 that.check_ ();]])[ 1156 yyrule = that.yyrule; 1157 yystate = that.yystate; 1158 yynext = that.yynext; 1159 yyla = that.yyla; 1160 return *this; 1161 } 1162 1163 /// Only call state() and setState() on objects in yyitems, not temporaries. 1164 glr_state* state(); 1165 const glr_state* state() const; 1166 void setState(const glr_state* s); 1167 1168 const semantic_option* next () const YY_ATTRIBUTE_UNUSED; 1169 semantic_option* next (); 1170 void setNext (const semantic_option* s); 1171 1172 std::ptrdiff_t indexIn (const glr_stack_item* array) const YY_ATTRIBUTE_UNUSED; 1173 1174 /** True iff YYY0 and YYY1 represent identical options at the top level. 1175 * That is, they represent the same rule applied to RHS symbols 1176 * that produce the same terminal symbols. */ 1177 bool 1178 isIdenticalTo (const semantic_option& yyy1) const 1179 {]b4_parse_assert_if([[ 1180 check_ (); 1181 yyy1.check_ ();]])[ 1182 if (this->yyrule == yyy1.yyrule) 1183 { 1184 const glr_state *yys0, *yys1; 1185 int yyn; 1186 for (yys0 = this->state(), yys1 = yyy1.state(), yyn = yyrhsLength(this->yyrule); 1187 yyn > 0; yys0 = yys0->pred(), yys1 = yys1->pred(), yyn -= 1) 1188 if (yys0->yyposn != yys1->yyposn) 1189 return false; 1190 return true; 1191 } 1192 else 1193 return false; 1194 } 1195 1196 /** Assuming identicalOptions (YYY0,YYY1), destructively merge the 1197 * alternative semantic values for the RHS-symbols of YYY1 and YYY0. */ 1198 void 1199 mergeWith (semantic_option& yyy1) 1200 {]b4_parse_assert_if([[ 1201 check_ (); 1202 yyy1.check_ ();]])[ 1203 glr_state *yys0 = this->state (); 1204 glr_state *yys1 = yyy1.state (); 1205 for (int yyn = yyrhsLength (this->yyrule); 1206 yyn > 0; 1207 yyn -= 1, yys0 = yys0->pred (), yys1 = yys1->pred ()) 1208 { 1209 if (yys0 == yys1) 1210 break; 1211 else if (yys0->yyresolved) 1212 { 1213 yys1->yyresolved = true;]b4_variant_if([[ 1214 YYASSERT (yys1->yylrState == yys0->yylrState); 1215 ]b4_symbol_variant([yy_accessing_symbol (yys0->yylrState)], 1216 [yys1->value ()], [copy], [yys0->value ()])], [[ 1217 yys1->value () = yys0->value ();]])[ 1218 } 1219 else if (yys1->yyresolved) 1220 { 1221 yys0->yyresolved = true;]b4_variant_if([[ 1222 YYASSERT (yys0->yylrState == yys1->yylrState); 1223 ]b4_symbol_variant([yy_accessing_symbol (yys1->yylrState)], 1224 [yys0->value ()], [copy], [yys1->value ()])], [[ 1225 yys0->value () = yys1->value ();]])[ 1226 } 1227 else 1228 { 1229 semantic_option *yyz0prev = YY_NULLPTR; 1230 semantic_option *yyz0 = yys0->firstVal(); 1231 semantic_option *yyz1 = yys1->firstVal(); 1232 while (true) 1233 { 1234 if (yyz1 == yyz0 || yyz1 == YY_NULLPTR) 1235 break; 1236 else if (yyz0 == YY_NULLPTR) 1237 { 1238 if (yyz0prev != YY_NULLPTR) 1239 yyz0prev->setNext(yyz1); 1240 else 1241 yys0->setFirstVal(yyz1); 1242 break; 1243 } 1244 else if (yyz0 < yyz1) 1245 { 1246 semantic_option *yyz = yyz0; 1247 if (yyz0prev != YY_NULLPTR) 1248 yyz0prev->setNext(yyz1); 1249 else 1250 yys0->setFirstVal(yyz1); 1251 yyz1 = yyz1->next(); 1252 yyz0->setNext(yyz); 1253 } 1254 yyz0prev = yyz0; 1255 yyz0 = yyz0->next(); 1256 } 1257 yys1->setFirstVal(yys0->firstVal()); 1258 } 1259 } 1260 } 1261 1262 #if] b4_api_PREFIX[DEBUG 1263 void yyreportTree (size_t yyindent = 2) const 1264 {]b4_parse_assert_if([[ 1265 check_ ();]])[ 1266 int yynrhs = yyrhsLength (this->yyrule); 1267 const glr_state* yystates[1 + YYMAXRHS]; 1268 glr_state yyleftmost_state; 1269 1270 { 1271 const glr_state *yys = this->state(); 1272 for (int yyi = yynrhs; 0 < yyi; yyi -= 1) 1273 { 1274 yystates[yyi] = yys; 1275 yys = yys->pred(); 1276 } 1277 if (yys == YY_NULLPTR) 1278 { 1279 yyleftmost_state.yyposn = 0; 1280 yystates[0] = &yyleftmost_state; 1281 } 1282 else 1283 yystates[0] = yys; 1284 } 1285 1286 std::string yylhs = ]b4_namespace_ref[::]b4_parser_class[::symbol_name (yylhsNonterm (this->yyrule)); 1287 YYASSERT(this->state()); 1288 if (this->state()->yyposn < yystates[0]->yyposn + 1) 1289 std::cerr << std::string(yyindent, ' ') << yylhs << " -> <Rule " 1290 << this->yyrule - 1 << ", empty>\n"; 1291 else 1292 std::cerr << std::string(yyindent, ' ') << yylhs << " -> <Rule " 1293 << this->yyrule - 1 << ", tokens " 1294 << yystates[0]->yyposn + 1 << " .. " 1295 << this->state()->yyposn << ">\n"; 1296 for (int yyi = 1; yyi <= yynrhs; yyi += 1) 1297 { 1298 if (yystates[yyi]->yyresolved) 1299 { 1300 std::string yysym = ]b4_namespace_ref[::]b4_parser_class[::symbol_name (yy_accessing_symbol (yystates[yyi]->yylrState)); 1301 if (yystates[yyi-1]->yyposn+1 > yystates[yyi]->yyposn) 1302 std::cerr << std::string(yyindent + 2, ' ') << yysym 1303 << " <empty>\n"; 1304 else 1305 std::cerr << std::string(yyindent + 2, ' ') << yysym 1306 << " <tokens " << yystates[yyi-1]->yyposn + 1 1307 << " .. " << yystates[yyi]->yyposn << ">\n"; 1308 } 1309 else 1310 yystates[yyi]->firstVal()->yyreportTree(yyindent + 2); 1311 } 1312 } 1313 #endif 1314 1315 /** Rule number for this reduction */ 1316 rule_num yyrule; 1317 1318 private: 1319 template <typename T> 1320 static const glr_stack_item* asItem(const T* state) 1321 { 1322 return reinterpret_cast<const glr_stack_item *>(state); 1323 } 1324 template <typename T> 1325 static glr_stack_item* asItem(T* state) 1326 { 1327 return reinterpret_cast<glr_stack_item *>(state); 1328 } 1329 /** The last RHS state in the list of states to be reduced. */ 1330 std::ptrdiff_t yystate; 1331 /** Next sibling in chain of options. To facilitate merging, 1332 * options are chained in decreasing order by address. */ 1333 std::ptrdiff_t yynext; 1334 1335 public: 1336 /** The lookahead for this reduction. */ 1337 symbol_type yyla; 1338 1339 ]b4_parse_assert_if([[ 1340 public: 1341 // Check invariants. 1342 void check_ () const 1343 { 1344 YY_IGNORE_NULL_DEREFERENCE_BEGIN 1345 YYASSERT(this->magic_ == MAGIC); 1346 YY_IGNORE_NULL_DEREFERENCE_END 1347 } 1348 1349 // A magic number to check our pointer arithmetic is sane. 1350 enum { MAGIC = 0xeff1cace }; 1351 unsigned int magic_;]])[ 1352 }; // class semantic_option 1353 } // namespace 1354 1355 namespace 1356 { 1357 /** Type of the items in the GLR stack. 1358 * It can be either a glr_state or a semantic_option. The is_state_ field 1359 * indicates which item of the union is valid. */ 1360 class glr_stack_item 1361 { 1362 public: 1363 glr_stack_item (bool state = true) 1364 : is_state_ (state)]b4_parse_assert_if([[ 1365 , magic_ (MAGIC)]])[ 1366 { 1367 if (is_state_) 1368 new (&raw_) glr_state; 1369 else 1370 new (&raw_) semantic_option; 1371 } 1372 1373 glr_stack_item (const glr_stack_item& other) YY_NOEXCEPT YY_NOTHROW 1374 : is_state_ (other.is_state_)]b4_parse_assert_if([[ 1375 , magic_ (MAGIC)]])[ 1376 {]b4_parse_assert_if([[ 1377 other.check_ ();]])[ 1378 std::memcpy (raw_, other.raw_, union_size); 1379 } 1380 1381 glr_stack_item& operator= (glr_stack_item other) 1382 {]b4_parse_assert_if([[ 1383 check_ (); 1384 other.check_ ();]])[ 1385 std::swap (is_state_, other.is_state_); 1386 std::swap (raw_, other.raw_); 1387 return *this; 1388 } 1389 1390 ~glr_stack_item () 1391 {]b4_parse_assert_if([[ 1392 check_ ();]])[ 1393 if (is_state ()) 1394 getState ().~glr_state (); 1395 else 1396 getOption ().~semantic_option (); 1397 } 1398 1399 void setState (const glr_state &state) 1400 {]b4_parse_assert_if([[ 1401 check_ (); 1402 state.check_ ();]])[ 1403 if (this != state.asItem ()) 1404 { 1405 if (is_state_) 1406 getState().~glr_state(); 1407 else 1408 getOption().~semantic_option(); 1409 new (&raw_) glr_state(state); 1410 is_state_ = true; 1411 } 1412 } 1413 1414 glr_state& getState () 1415 {]b4_parse_assert_if([[ 1416 check_ ();]])[ 1417 YYDASSERT (is_state ()); 1418 void *yyp = raw_; 1419 glr_state& res = *static_cast<glr_state*> (yyp);]b4_parse_assert_if([[ 1420 res.check_ ();]])[ 1421 return res; 1422 } 1423 1424 const glr_state& getState () const 1425 {]b4_parse_assert_if([[ 1426 check_ ();]])[ 1427 YYDASSERT (is_state ()); 1428 const void *yyp = raw_; 1429 const glr_state& res = *static_cast<const glr_state*> (yyp);]b4_parse_assert_if([[ 1430 res.check_ ();]])[ 1431 return res; 1432 } 1433 1434 semantic_option& getOption () 1435 {]b4_parse_assert_if([[ 1436 check_ ();]])[ 1437 YYDASSERT (!is_state ()); 1438 void *yyp = raw_; 1439 return *static_cast<semantic_option*> (yyp); 1440 } 1441 const semantic_option& getOption () const 1442 {]b4_parse_assert_if([[ 1443 check_ ();]])[ 1444 YYDASSERT (!is_state ()); 1445 const void *yyp = raw_; 1446 return *static_cast<const semantic_option*> (yyp); 1447 } 1448 bool is_state () const 1449 {]b4_parse_assert_if([[ 1450 check_ ();]])[ 1451 return is_state_; 1452 } 1453 1454 private: 1455 /// The possible contents of raw_. Since they have constructors, they cannot 1456 /// be directly included in the union. 1457 union contents 1458 { 1459 char yystate[sizeof(glr_state)]; 1460 char yyoption[sizeof(semantic_option)]; 1461 }; 1462 enum { union_size = sizeof (contents) }; 1463 union { 1464 /// Strongest alignment constraints. 1465 long double yyalign_me; 1466 /// A buffer large enough to store the contents. 1467 char raw_[union_size]; 1468 }; 1469 /** Type tag for the union. */ 1470 bool is_state_; 1471 ]b4_parse_assert_if([[ 1472 public: 1473 // Check invariants. 1474 void check_ () const 1475 { 1476 YYASSERT(this->magic_ == MAGIC); 1477 YYASSERT(this->is_state_ == false || this->is_state_ == true); 1478 } 1479 // A magic number to check our pointer arithmetic is sane. 1480 enum { MAGIC = 0xDEAD1ACC }; // 3735886540. 1481 const unsigned int magic_;]])[ 1482 }; // class glr_stack_item 1483 } // namespace 1484 1485 glr_state* glr_state::pred () 1486 {]b4_parse_assert_if([[ 1487 check_ ();]])[ 1488 YY_IGNORE_NULL_DEREFERENCE_BEGIN 1489 return yypred ? &asItem (as_pointer_ (this) - yypred)->getState () : YY_NULLPTR; 1490 YY_IGNORE_NULL_DEREFERENCE_END 1491 } 1492 1493 const glr_state* glr_state::pred () const 1494 {]b4_parse_assert_if([[ 1495 check_ ();]])[ 1496 YY_IGNORE_NULL_DEREFERENCE_BEGIN 1497 return yypred ? &asItem (as_pointer_ (this) - yypred)->getState () : YY_NULLPTR; 1498 YY_IGNORE_NULL_DEREFERENCE_END 1499 } 1500 1501 void glr_state::setPred (const glr_state* state) 1502 {]b4_parse_assert_if([[ 1503 check_ (); 1504 if (state) 1505 state->check_ ();]])[ 1506 yypred = state ? as_pointer_ (this) - as_pointer_ (state) : 0; 1507 } 1508 1509 semantic_option* glr_state::firstVal () 1510 {]b4_parse_assert_if([[ 1511 check_ ();]])[ 1512 return yyfirstVal ? &(asItem(this) - yyfirstVal)->getOption() : YY_NULLPTR; 1513 } 1514 1515 const semantic_option* glr_state::firstVal () const 1516 {]b4_parse_assert_if([[ 1517 check_ ();]])[ 1518 return yyfirstVal ? &(asItem(this) - yyfirstVal)->getOption() : YY_NULLPTR; 1519 } 1520 1521 void glr_state::setFirstVal (const semantic_option* option) 1522 {]b4_parse_assert_if([[ 1523 check_ ();]])[ 1524 yyfirstVal = option ? asItem(this) - asItem(option) : 0; 1525 } 1526 1527 std::ptrdiff_t glr_state::indexIn (const glr_stack_item* array) const 1528 {]b4_parse_assert_if([[ 1529 check_ ();]])[ 1530 return asItem(this) - array; 1531 } 1532 1533 std::ptrdiff_t semantic_option::indexIn (const glr_stack_item* array) const 1534 { 1535 return asItem(this) - array; 1536 } 1537 1538 glr_state* semantic_option::state () 1539 { 1540 YY_IGNORE_NULL_DEREFERENCE_BEGIN 1541 return yystate ? &(asItem(this) - yystate)->getState() : YY_NULLPTR; 1542 YY_IGNORE_NULL_DEREFERENCE_END 1543 } 1544 1545 const glr_state* semantic_option::state () const 1546 { 1547 return yystate ? &(asItem(this) - yystate)->getState() : YY_NULLPTR; 1548 } 1549 1550 void semantic_option::setState (const glr_state* s) 1551 { 1552 yystate = s ? asItem(this) - asItem(s) : 0; 1553 } 1554 1555 const semantic_option* semantic_option::next () const 1556 { 1557 return yynext ? &(asItem(this) - yynext)->getOption() : YY_NULLPTR; 1558 } 1559 1560 semantic_option* semantic_option::next () 1561 { 1562 return yynext ? &(asItem(this) - yynext)->getOption() : YY_NULLPTR; 1563 } 1564 1565 void semantic_option::setNext (const semantic_option* s) 1566 { 1567 yynext = s ? asItem(this) - asItem(s) : 0; 1568 } 1569 1570 void glr_state::destroy (char const* yymsg, ]b4_namespace_ref[::]b4_parser_class[& yyparser) 1571 {]b4_parse_assert_if([[ 1572 check_ ();]])[ 1573 if (yyresolved) 1574 yyparser.yy_destroy_ (yymsg, yy_accessing_symbol(yylrState), 1575 value ()]b4_locations_if([, yyloc])[); 1576 else 1577 { 1578 #if] b4_api_PREFIX[DEBUG 1579 YYCDEBUG << yymsg 1580 << (firstVal() ? " unresolved " : " incomplete ") 1581 << (yy_accessing_symbol (yylrState) < YYNTOKENS ? "token" : "nterm") 1582 << ' ' << yyparser.symbol_name (yy_accessing_symbol (yylrState)) 1583 << " ("]b4_locations_if([[ 1584 << yyloc << ": "]])[ 1585 << ")\n"; 1586 #endif 1587 if (firstVal() != YY_NULLPTR) 1588 { 1589 semantic_option &yyoption = *firstVal(); 1590 glr_state *yyrh = yyoption.state(); 1591 for (int yyn = yyrhsLength(yyoption.yyrule); yyn > 0; yyn -= 1) 1592 { 1593 yyrh->destroy(yymsg, yyparser); 1594 yyrh = yyrh->pred(); 1595 } 1596 } 1597 } 1598 } 1599 1600 #undef YYFILL 1601 #define YYFILL(N) yyfill(yyvsp, yylow, (N), yynormal) 1602 1603 namespace 1604 { 1605 class state_stack 1606 { 1607 public: 1608 using parser_type = ]b4_namespace_ref[::]b4_parser_class[; 1609 using symbol_kind = parser_type::symbol_kind; 1610 using value_type = parser_type::value_type;]b4_locations_if([[ 1611 using location_type = parser_type::location_type;]])[ 1612 1613 /** Initialize to a single empty stack, with total maximum 1614 * capacity for all stacks of YYSIZE. */ 1615 state_stack (size_t yysize) 1616 : yysplitPoint (YY_NULLPTR) 1617 { 1618 yyitems.reserve(yysize); 1619 } 1620 1621 #if YYSTACKEXPANDABLE 1622 /** Returns false if it tried to expand but could not. */ 1623 bool 1624 yyexpandGLRStackIfNeeded () 1625 { 1626 return YYHEADROOM <= spaceLeft() || yyexpandGLRStack(); 1627 } 1628 1629 private: 1630 /** If *this is expandable, extend it. WARNING: Pointers into the 1631 stack from outside should be considered invalid after this call. 1632 We always expand when there are 1 or fewer items left AFTER an 1633 allocation, so that we can avoid having external pointers exist 1634 across an allocation. */ 1635 bool 1636 yyexpandGLRStack () 1637 { 1638 const size_t oldsize = yyitems.size(); 1639 if (YYMAXDEPTH - YYHEADROOM < oldsize) 1640 return false; 1641 const size_t yynewSize = YYMAXDEPTH < 2 * oldsize ? YYMAXDEPTH : 2 * oldsize; 1642 const glr_stack_item *oldbase = &yyitems[0]; 1643 1644 yyitems.reserve(yynewSize); 1645 const glr_stack_item *newbase = &yyitems[0]; 1646 1647 // Adjust the pointers. Perform raw pointer arithmetic, as there 1648 // is no reason for objects to be aligned on their size. 1649 const ptrdiff_t disp = 1650 reinterpret_cast<const char *>(newbase) - reinterpret_cast<const char *>(oldbase); 1651 if (yysplitPoint) 1652 const_cast<glr_state *&>(yysplitPoint) = reinterpret_cast<glr_state *>( 1653 reinterpret_cast<char *>(const_cast<glr_state *>(yysplitPoint)) + disp); 1654 1655 for (std::vector<glr_state *>::iterator i = yytops.begin(), yyend = yytops.end(); 1656 i != yyend; ++i) 1657 if (glr_state_not_null(*i)) 1658 *i = reinterpret_cast<glr_state *>(reinterpret_cast<char *>(*i) + disp); 1659 1660 return true; 1661 } 1662 1663 public: 1664 #else 1665 bool yyexpandGLRStackIfNeeded () 1666 { 1667 return YYHEADROOM <= spaceLeft(); 1668 } 1669 #endif 1670 #undef YYSTACKEXPANDABLE 1671 1672 static bool glr_state_not_null (glr_state* s) 1673 { 1674 return s != YY_NULLPTR; 1675 } 1676 1677 bool 1678 reduceToOneStack () 1679 { 1680 using iterator = std::vector<glr_state *>::iterator; 1681 const iterator yybegin = yytops.begin(); 1682 const iterator yyend = yytops.end(); 1683 const iterator yyit = std::find_if(yybegin, yyend, glr_state_not_null); 1684 if (yyit == yyend) 1685 return false; 1686 for (state_set_index yyk = create_state_set_index(yyit + 1 - yybegin); 1687 yyk.uget() != numTops(); ++yyk) 1688 yytops.yymarkStackDeleted(yyk); 1689 yytops.yyremoveDeletes(); 1690 yycompressStack(); 1691 return true; 1692 } 1693 1694 /** Called when returning to deterministic operation to clean up the extra 1695 * stacks. */ 1696 void 1697 yycompressStack () 1698 { 1699 if (yytops.size() != 1 || !isSplit()) 1700 return; 1701 1702 // yyr is the state after the split point. 1703 glr_state *yyr = YY_NULLPTR; 1704 for (glr_state *yyp = firstTop(), *yyq = yyp->pred(); yyp != yysplitPoint; 1705 yyr = yyp, yyp = yyq, yyq = yyp->pred()) 1706 yyp->setPred(yyr); 1707 1708 // This const_cast is okay, since anyway we have access to the mutable 1709 // yyitems into which yysplitPoint points. 1710 glr_stack_item *nextFreeItem = const_cast<glr_state *>(yysplitPoint)->asItem() + 1; 1711 yysplitPoint = YY_NULLPTR; 1712 yytops.clearLastDeleted(); 1713 1714 while (yyr != YY_NULLPTR) 1715 { 1716 nextFreeItem->setState(*yyr); 1717 glr_state &nextFreeState = nextFreeItem->getState(); 1718 yyr = yyr->pred(); 1719 nextFreeState.setPred(&(nextFreeItem - 1)->getState()); 1720 setFirstTop(&nextFreeState); 1721 ++nextFreeItem; 1722 } 1723 yyitems.resize(static_cast<size_t>(nextFreeItem - yyitems.data())); 1724 } 1725 1726 bool isSplit() const { 1727 return yysplitPoint != YY_NULLPTR; 1728 } 1729 1730 // Present the interface of a vector of glr_stack_item. 1731 std::vector<glr_stack_item>::const_iterator begin () const 1732 { 1733 return yyitems.begin(); 1734 } 1735 1736 std::vector<glr_stack_item>::const_iterator end () const 1737 { 1738 return yyitems.end(); 1739 } 1740 1741 size_t size() const 1742 { 1743 return yyitems.size(); 1744 } 1745 1746 glr_stack_item& operator[] (size_t i) 1747 { 1748 return yyitems[i]; 1749 } 1750 1751 glr_stack_item& stackItemAt (size_t index) 1752 { 1753 return yyitems[index]; 1754 } 1755 1756 size_t numTops () const 1757 { 1758 return yytops.size(); 1759 } 1760 1761 glr_state* firstTop () const 1762 { 1763 return yytops[create_state_set_index(0)]; 1764 } 1765 1766 glr_state* topAt (state_set_index i) const 1767 { 1768 return yytops[i]; 1769 } 1770 1771 void setFirstTop (glr_state* value) 1772 { 1773 yytops[create_state_set_index(0)] = value; 1774 } 1775 1776 void setTopAt (state_set_index i, glr_state* value) 1777 { 1778 yytops[i] = value; 1779 } 1780 1781 void pop_back () 1782 { 1783 yyitems.pop_back(); 1784 } 1785 1786 void pop_back (size_t n) 1787 { 1788 yyitems.resize(yyitems.size() - n); 1789 } 1790 1791 state_set_index 1792 yysplitStack (state_set_index yyk) 1793 { 1794 if (!isSplit()) 1795 { 1796 YYASSERT(yyk.get() == 0); 1797 yysplitPoint = topAt(yyk); 1798 } 1799 return yytops.yysplitStack(yyk); 1800 } 1801 1802 /** Assuming that YYS is a GLRState somewhere on *this, update the 1803 * splitpoint of *this, if needed, so that it is at least as deep as 1804 * YYS. */ 1805 void 1806 yyupdateSplit (glr_state& yys) 1807 { 1808 if (isSplit() && &yys < yysplitPoint) 1809 yysplitPoint = &yys; 1810 } 1811 1812 /** Return a fresh GLRState. 1813 * Callers should call yyreserveStack afterwards to make sure there is 1814 * sufficient headroom. */ 1815 glr_state& yynewGLRState (const glr_state& newState) 1816 { 1817 glr_state &state = yyitems[yynewGLRStackItem(true)].getState(); 1818 #if false && 201103L <= YY_CPLUSPLUS 1819 state = std::move (newState); 1820 #else 1821 state = newState; 1822 #endif 1823 return state; 1824 } 1825 1826 /** Return a fresh SemanticOption. 1827 * Callers should call yyreserveStack afterwards to make sure there is 1828 * sufficient headroom. */ 1829 semantic_option& yynewSemanticOption (semantic_option newOption) 1830 { 1831 semantic_option &option = yyitems[yynewGLRStackItem(false)].getOption(); 1832 option = std::move(newOption); 1833 return option; 1834 } 1835 1836 /* Do nothing if YYNORMAL or if *YYLOW <= YYLOW1. Otherwise, fill in 1837 * YYVSP[YYLOW1 .. *YYLOW-1] as in yyfillin and set *YYLOW = YYLOW1. 1838 * For convenience, always return YYLOW1. */ 1839 int 1840 yyfill (glr_stack_item *yyvsp, int &yylow, int yylow1, bool yynormal) 1841 { 1842 if (!yynormal && yylow1 < yylow) 1843 { 1844 yyfillin(yyvsp, yylow, yylow1); 1845 yylow = yylow1; 1846 } 1847 return yylow1; 1848 } 1849 1850 /** Fill in YYVSP[YYLOW1 .. YYLOW0-1] from the chain of states starting 1851 * at YYVSP[YYLOW0].getState().pred(). Leaves YYVSP[YYLOW1].getState().pred() 1852 * containing the pointer to the next state in the chain. */ 1853 void 1854 yyfillin (glr_stack_item *yyvsp, int yylow0, int yylow1) 1855 { 1856 glr_state *s = yyvsp[yylow0].getState().pred(); 1857 YYASSERT(s != YY_NULLPTR); 1858 for (int i = yylow0 - 1; i >= yylow1; i -= 1, s = s->pred()) 1859 { 1860 glr_state &yys = yyvsp[i].getState(); 1861 #if] b4_api_PREFIX[DEBUG 1862 yys.yylrState = s->yylrState; 1863 #endif 1864 yys.yyresolved = s->yyresolved; 1865 if (s->yyresolved) 1866 {]b4_variant_if([[ 1867 new (&yys.value ()) value_type (); 1868 ]b4_symbol_variant([yy_accessing_symbol (s->yylrState)], 1869 [yys.value ()], [copy], [s->value ()])], [[ 1870 new (&yys.value ()) value_type (s->value ());]])[ 1871 } 1872 else 1873 /* The effect of using yyval or yyloc (in an immediate 1874 * rule) is undefined. */ 1875 yys.setFirstVal(YY_NULLPTR);]b4_locations_if([[ 1876 yys.yyloc = s->yyloc;]])[ 1877 yys.setPred(s->pred()); 1878 } 1879 } 1880 1881 #if] b4_api_PREFIX[DEBUG 1882 1883 /*----------------------------------------------------------------------. 1884 | Report that stack #YYK of *YYSTACKP is going to be reduced by YYRULE. | 1885 `----------------------------------------------------------------------*/ 1886 1887 void 1888 yy_reduce_print (bool yynormal, glr_stack_item* yyvsp, state_set_index yyk, 1889 rule_num yyrule, parser_type& yyparser) 1890 { 1891 int yynrhs = yyrhsLength(yyrule);]b4_locations_if([ 1892 int yylow = 1;])[ 1893 int yyi; 1894 std::cerr << "Reducing stack " << yyk.get() << " by rule " << yyrule - 1 1895 << " (line " << int (yyrline[yyrule]) << "):\n"; 1896 if (! yynormal) 1897 yyfillin (yyvsp, 1, -yynrhs); 1898 /* The symbols being reduced. */ 1899 for (yyi = 0; yyi < yynrhs; yyi++) 1900 { 1901 std::cerr << " $" << yyi + 1 << " = "; 1902 yyparser.yy_symbol_print_ 1903 (yy_accessing_symbol (yyvsp[yyi - yynrhs + 1].getState().yylrState), 1904 yyvsp[yyi - yynrhs + 1].getState().value ()]b4_locations_if([[, 1905 ]b4_rhs_location(yynrhs, yyi + 1)])[); 1906 if (!yyvsp[yyi - yynrhs + 1].getState().yyresolved) 1907 std::cerr << " (unresolved)"; 1908 std::cerr << '\n'; 1909 } 1910 } 1911 1912 # define YYINDEX(YYX) ((YYX) == YY_NULLPTR ? -1 : (YYX)->indexIn(yyitems.data())) 1913 1914 void 1915 dumpStack () const 1916 { 1917 for (size_t yyi = 0; yyi < size(); ++yyi) 1918 { 1919 const glr_stack_item &item = yyitems[yyi]; 1920 std::cerr << std::setw(3) << yyi << ". "; 1921 if (item.is_state()) 1922 { 1923 std::cerr << "Res: " << item.getState().yyresolved 1924 << ", LR State: " << item.getState().yylrState 1925 << ", posn: " << item.getState().yyposn 1926 << ", pred: " << YYINDEX(item.getState().pred()); 1927 if (!item.getState().yyresolved) 1928 std::cerr << ", firstVal: " << YYINDEX(item.getState().firstVal()); 1929 } 1930 else 1931 { 1932 std::cerr << "Option. rule: " << item.getOption().yyrule - 1 1933 << ", state: " << YYINDEX(item.getOption().state()) 1934 << ", next: " << YYINDEX(item.getOption().next()); 1935 } 1936 std::cerr << '\n'; 1937 } 1938 std::cerr << "Tops:"; 1939 for (state_set_index yyi = create_state_set_index(0); yyi.uget() < numTops(); ++yyi) 1940 { 1941 std::cerr << yyi.get() << ": " << YYINDEX(topAt(yyi)) << "; "; 1942 } 1943 std::cerr << '\n'; 1944 } 1945 1946 # undef YYINDEX 1947 #endif 1948 1949 YYRESULTTAG 1950 yyreportAmbiguity (const semantic_option& yyx0, 1951 const semantic_option& yyx1, parser_type& yyparser]b4_locations_if([, const location_type& yyloc])[) 1952 { 1953 YY_USE(yyx0); 1954 YY_USE(yyx1); 1955 1956 #if] b4_api_PREFIX[DEBUG 1957 std::cerr << "Ambiguity detected.\n" 1958 "Option 1,\n"; 1959 yyx0.yyreportTree(); 1960 std::cerr << "\nOption 2,\n"; 1961 yyx1.yyreportTree(); 1962 std::cerr << '\n'; 1963 #endif 1964 1965 yyparser.error (]b4_locations_if([yyloc, ])[YY_("syntax is ambiguous")); 1966 return yyabort; 1967 } 1968 1969 #if] b4_api_PREFIX[DEBUG 1970 /* Print YYS (possibly NULL) and its predecessors. */ 1971 void 1972 yypstates (const glr_state* yys) const 1973 { 1974 if (yys != YY_NULLPTR) 1975 yys->yy_yypstack(); 1976 else 1977 std::cerr << "<null>"; 1978 std::cerr << '\n'; 1979 } 1980 #endif 1981 1982 private: 1983 size_t spaceLeft() const 1984 { 1985 return yyitems.capacity() - yyitems.size(); 1986 } 1987 1988 /** Return a fresh GLRStackItem in this. The item is an LR state 1989 * if YYIS_STATE, and otherwise a semantic option. Callers should call 1990 * yyreserveStack afterwards to make sure there is sufficient 1991 * headroom. */ 1992 size_t 1993 yynewGLRStackItem (bool yyis_state) 1994 { 1995 YYDASSERT(yyitems.size() < yyitems.capacity()); 1996 yyitems.push_back(glr_stack_item(yyis_state)); 1997 return yyitems.size() - 1; 1998 } 1999 2000 2001 public: 2002 std::vector<glr_stack_item> yyitems; 2003 // Where the stack splits. Anything below this address is deterministic. 2004 const glr_state* yysplitPoint; 2005 glr_state_set yytops; 2006 }; // class state_stack 2007 } // namespace 2008 2009 #undef YYFILL 2010 #define YYFILL(N) yystateStack.yyfill(yyvsp, yylow, (N), yynormal) 2011 2012 namespace ]b4_namespace_ref[ 2013 { 2014 class ]b4_parser_class[::glr_stack 2015 { 2016 public: 2017 ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[ 2018 // Needs access to yypact_value_is_default, etc. 2019 friend context; 2020 ]])[ 2021 2022 glr_stack (size_t yysize, parser_type& yyparser_yyarg]m4_ifset([b4_parse_param], [, b4_parse_param_decl])[) 2023 : yyerrState (0) 2024 , yystateStack (yysize) 2025 , yyerrcnt (0) 2026 , yyla () 2027 , yyparser (yyparser_yyarg)]m4_ifset([b4_parse_param], [,b4_parse_param_cons])[ 2028 {} 2029 2030 ~glr_stack () 2031 { 2032 if (!this->yyla.empty()) 2033 yyparser.yy_destroy_ ("Cleanup: discarding lookahead", 2034 this->yyla.kind (), this->yyla.value]b4_locations_if([, this->yyla.location])[); 2035 popall_(); 2036 } 2037 2038 int yyerrState; 2039 ]b4_locations_if([[ /* To compute the location of the error token. */ 2040 glr_stack_item yyerror_range[3];]])[ 2041 state_stack yystateStack; 2042 int yyerrcnt; 2043 symbol_type yyla; 2044 YYJMP_BUF yyexception_buffer; 2045 parser_type& yyparser; 2046 2047 #define YYCHK1(YYE) \ 2048 do \ 2049 { \ 2050 switch (YYE) \ 2051 { \ 2052 case yyok: \ 2053 break; \ 2054 case yyabort: \ 2055 goto yyabortlab; \ 2056 case yyaccept: \ 2057 goto yyacceptlab; \ 2058 case yyerr: \ 2059 goto yyuser_error; \ 2060 default: \ 2061 goto yybuglab; \ 2062 } \ 2063 } while (false) 2064 2065 int 2066 parse () 2067 { 2068 int yyresult; 2069 size_t yyposn; 2070 2071 YYCDEBUG << "Starting parse\n"; 2072 2073 this->yyla.clear(); 2074 ]m4_ifdef([b4_initial_action], [ 2075 b4_dollar_pushdef([yyla.value], [], [], [yyla.location])dnl 2076 b4_user_initial_action 2077 b4_dollar_popdef])[]dnl 2078 [ 2079 switch (YYSETJMP (this->yyexception_buffer)) 2080 { 2081 case 0: 2082 break; 2083 case 1: 2084 goto yyabortlab; 2085 case 2: 2086 goto yyexhaustedlab; 2087 default: 2088 goto yybuglab; 2089 } 2090 this->yyglrShift (create_state_set_index(0), 0, 0, this->yyla.value]b4_locations_if([, this->yyla.location])[); 2091 yyposn = 0; 2092 2093 while (true) 2094 { 2095 /* For efficiency, we have two loops, the first of which is 2096 specialized to deterministic operation (single stack, no 2097 potential ambiguity). */ 2098 /* Standard mode */ 2099 while (true) 2100 { 2101 const state_num yystate = this->firstTopState()->yylrState; 2102 YYCDEBUG << "Entering state " << yystate << '\n'; 2103 if (yystate == YYFINAL) 2104 goto yyacceptlab; 2105 if (yy_is_defaulted_state(yystate)) 2106 { 2107 const rule_num yyrule = yy_default_action(yystate); 2108 if (yyrule == 0) 2109 {]b4_locations_if([[ 2110 this->yyerror_range[1].getState().yyloc = this->yyla.location;]])[ 2111 this->yyreportSyntaxError (); 2112 goto yyuser_error; 2113 } 2114 YYCHK1(this->yyglrReduce(create_state_set_index(0), yyrule, true)); 2115 } 2116 else 2117 { 2118 yyget_token(); 2119 const short *yyconflicts; 2120 const int yyaction = 2121 yygetLRActions(yystate, this->yyla.kind(), yyconflicts); 2122 if (*yyconflicts != 0) 2123 break; 2124 if (yy_is_shift_action(yyaction)) 2125 { 2126 YY_SYMBOL_PRINT("Shifting", this->yyla.kind(), this->yyla.value, 2127 this->yyla.location); 2128 yyposn += 1; 2129 // FIXME: we should move yylval. 2130 this->yyglrShift (create_state_set_index(0), yyaction, yyposn, this->yyla.value]b4_locations_if([, this->yyla.location])[); 2131 yyla.clear(); 2132 if (0 < this->yyerrState) 2133 this->yyerrState -= 1; 2134 } 2135 else if (yy_is_error_action(yyaction)) 2136 {]b4_locations_if([[ 2137 this->yyerror_range[1].getState().yyloc = this->yyla.location;]])[ 2138 /* Don't issue an error message again for exceptions 2139 thrown from the scanner. */ 2140 if (this->yyla.kind () != ]b4_symbol(error, kind)[) 2141 this->yyreportSyntaxError (); 2142 goto yyuser_error; 2143 } 2144 else 2145 YYCHK1(this->yyglrReduce(create_state_set_index(0), -yyaction, true)); 2146 } 2147 } 2148 2149 while (true) 2150 { 2151 for (state_set_index yys = create_state_set_index(0); 2152 yys.uget() < this->yystateStack.numTops(); ++yys) 2153 this->yystateStack.yytops.setLookaheadNeeds(yys, !this->yyla.empty()); 2154 2155 /* yyprocessOneStack returns one of three things: 2156 2157 - An error flag. If the caller is yyprocessOneStack, it 2158 immediately returns as well. When the caller is finally 2159 yyparse, it jumps to an error label via YYCHK1. 2160 2161 - yyok, but yyprocessOneStack has invoked yymarkStackDeleted 2162 (yys), which sets the top state of yys to NULL. Thus, 2163 yyparse's following invocation of yyremoveDeletes will remove 2164 the stack. 2165 2166 - yyok, when ready to shift a token. 2167 2168 Except in the first case, yyparse will invoke yyremoveDeletes and 2169 then shift the next token onto all remaining stacks. This 2170 synchronization of the shift (that is, after all preceding 2171 reductions on all stacks) helps prevent double destructor calls 2172 on yylval in the event of memory exhaustion. */ 2173 2174 for (state_set_index yys = create_state_set_index(0); 2175 yys.uget() < this->yystateStack.numTops(); ++yys) 2176 YYCHK1 (this->yyprocessOneStack (yys, yyposn]b4_locations_if([, &this->yyla.location])[)); 2177 this->yystateStack.yytops.yyremoveDeletes(); 2178 if (this->yystateStack.yytops.size() == 0) 2179 { 2180 this->yystateStack.yytops.yyundeleteLastStack(); 2181 if (this->yystateStack.yytops.size() == 0) 2182 this->yyFail (]b4_locations_if([&this->yyla.location, ])[YY_("syntax error")); 2183 YYCHK1(this->yyresolveStack()); 2184 YYCDEBUG << "Returning to deterministic operation.\n";]b4_locations_if([[ 2185 this->yyerror_range[1].getState ().yyloc = this->yyla.location;]])[ 2186 this->yyreportSyntaxError (); 2187 goto yyuser_error; 2188 } 2189 2190 /* If any yyglrShift call fails, it will fail after shifting. Thus, 2191 a copy of yylval will already be on stack 0 in the event of a 2192 failure in the following loop. Thus, yyla is emptied 2193 before the loop to make sure the user destructor for yylval isn't 2194 called twice. */ 2195 symbol_kind_type yytoken_to_shift = this->yyla.kind(); 2196 this->yyla.kind_ = ]b4_symbol(empty, kind)[; 2197 yyposn += 1; 2198 for (state_set_index yys = create_state_set_index (0); yys.uget () < this->yystateStack.numTops (); ++yys) 2199 { 2200 const state_num yystate = this->topState(yys)->yylrState; 2201 const short *yyconflicts; 2202 const int yyaction = yygetLRActions(yystate, yytoken_to_shift, yyconflicts); 2203 /* Note that yyconflicts were handled by yyprocessOneStack. */ 2204 YYCDEBUG << "On stack " << yys.get() << ", "; 2205 YY_SYMBOL_PRINT("shifting", yytoken_to_shift, this->yyla.value, 2206 this->yyla.location); 2207 this->yyglrShift (yys, yyaction, yyposn, this->yyla.value]b4_locations_if([, this->yyla.location])[); 2208 YYCDEBUG << "Stack " << yys.get() << " now in state " 2209 << this->topState(yys)->yylrState << '\n'; 2210 } 2211 ]b4_variant_if([[ 2212 // FIXME: User destructors. 2213 // Value type destructor. 2214 ]b4_symbol_variant([[yytoken_to_shift]], [[this->yyla.value]], [[template destroy]])])[ 2215 2216 if (this->yystateStack.yytops.size () == 1) 2217 { 2218 YYCHK1(this->yyresolveStack()); 2219 YYCDEBUG << "Returning to deterministic operation.\n"; 2220 this->yystateStack.yycompressStack(); 2221 break; 2222 } 2223 } 2224 continue; 2225 yyuser_error: 2226 this->yyrecoverSyntaxError (]b4_locations_if([&this->yyla.location])[); 2227 yyposn = this->firstTopState()->yyposn; 2228 } 2229 2230 yyacceptlab: 2231 yyresult = 0; 2232 goto yyreturn; 2233 2234 yybuglab: 2235 YYASSERT (false); 2236 goto yyabortlab; 2237 2238 yyabortlab: 2239 yyresult = 1; 2240 goto yyreturn; 2241 2242 yyexhaustedlab: 2243 yyparser.error (]b4_locations_if([this->yyla.location, ])[YY_("memory exhausted")); 2244 yyresult = 2; 2245 goto yyreturn; 2246 2247 yyreturn: 2248 return yyresult; 2249 } 2250 #undef YYCHK1 2251 2252 void yyreserveGlrStack () 2253 { 2254 if (!yystateStack.yyexpandGLRStackIfNeeded()) 2255 yyMemoryExhausted(); 2256 } 2257 2258 _Noreturn void 2259 yyMemoryExhausted () 2260 { 2261 YYLONGJMP(yyexception_buffer, 2); 2262 } 2263 2264 _Noreturn void 2265 yyFail (]b4_locations_if([location_type* yylocp, ])[const char* yymsg) 2266 { 2267 if (yymsg != YY_NULLPTR) 2268 yyparser.error (]b4_locations_if([*yylocp, ])[yymsg); 2269 YYLONGJMP(yyexception_buffer, 1); 2270 } 2271 2272 /* GLRStates */ 2273 2274 2275 /** Add a new semantic action that will execute the action for rule 2276 * YYRULE on the semantic values in YYRHS to the list of 2277 * alternative actions for YYSTATE. Assumes that YYRHS comes from 2278 * stack #YYK of *this. */ 2279 void 2280 yyaddDeferredAction (state_set_index yyk, glr_state* yystate, 2281 glr_state* yyrhs, rule_num yyrule) 2282 { 2283 semantic_option &yyopt = yystateStack.yynewSemanticOption(semantic_option(yyrule)); 2284 yyopt.setState(yyrhs); 2285 yyopt.setNext(yystate->firstVal()); 2286 if (yystateStack.yytops.lookaheadNeeds(yyk)) 2287 yyopt.yyla = this->yyla; 2288 yystate->setFirstVal(&yyopt); 2289 2290 yyreserveGlrStack(); 2291 } 2292 2293 #if] b4_api_PREFIX[DEBUG 2294 void yypdumpstack () const 2295 { 2296 yystateStack.dumpStack(); 2297 } 2298 #endif 2299 2300 void 2301 yyreportSyntaxError () 2302 { 2303 if (yyerrState != 0) 2304 return; 2305 ]b4_parse_error_case( 2306 [simple], [[ 2307 std::string msg = YY_("syntax error"); 2308 yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]], 2309 [custom], [[ 2310 context yyctx (*this, yyla); 2311 yyparser.report_syntax_error (yyctx);]], 2312 [[ 2313 context yyctx (*this, yyla); 2314 std::string msg = yyparser.yysyntax_error_ (yyctx); 2315 yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]])[ 2316 yyerrcnt += 1; 2317 } 2318 2319 /* Recover from a syntax error on this, assuming that yytoken, 2320 yylval, and yylloc are the syntactic category, semantic value, and location 2321 of the lookahead. */ 2322 void 2323 yyrecoverSyntaxError (]b4_locations_if([location_type* yylocp])[) 2324 { 2325 if (yyerrState == 3) 2326 /* We just shifted the error token and (perhaps) took some 2327 reductions. Skip tokens until we can proceed. */ 2328 while (true) 2329 { 2330 if (this->yyla.kind () == ]b4_symbol(eof, kind)[) 2331 yyFail (]b4_locations_if([yylocp, ])[YY_NULLPTR); 2332 if (this->yyla.kind () != ]b4_symbol(empty, kind)[) 2333 {]b4_locations_if([[ 2334 /* We throw away the lookahead, but the error range 2335 of the shifted error token must take it into account. */ 2336 glr_state *yys = firstTopState(); 2337 yyerror_range[1].getState().yyloc = yys->yyloc; 2338 yyerror_range[2].getState().yyloc = this->yyla.location; 2339 YYLLOC_DEFAULT ((yys->yyloc), yyerror_range, 2);]])[ 2340 yyparser.yy_destroy_ ("Error: discarding", 2341 this->yyla.kind (), this->yyla.value]b4_locations_if([, this->yyla.location])[);]b4_variant_if([[ 2342 // Value type destructor. 2343 ]b4_symbol_variant([[this->yyla.kind ()]], [[this->yyla.value]], [[template destroy]])])[ 2344 this->yyla.kind_ = ]b4_symbol(empty, kind)[; 2345 } 2346 yyget_token(); 2347 int yyj = yypact[firstTopState()->yylrState]; 2348 if (yypact_value_is_default(yyj)) 2349 return; 2350 yyj += this->yyla.kind(); 2351 if (yyj < 0 || YYLAST < yyj || yycheck[yyj] != this->yyla.kind()) 2352 { 2353 if (yydefact[firstTopState()->yylrState] != 0) 2354 return; 2355 } 2356 else if (!yytable_value_is_error(yytable[yyj])) 2357 return; 2358 } 2359 2360 if (!yystateStack.reduceToOneStack()) 2361 yyFail (]b4_locations_if([yylocp, ])[YY_NULLPTR); 2362 2363 /* Now pop stack until we find a state that shifts the error token. */ 2364 yyerrState = 3; 2365 while (firstTopState() != YY_NULLPTR) 2366 { 2367 glr_state *yys = firstTopState(); 2368 int yyj = yypact[yys->yylrState]; 2369 if (!yypact_value_is_default(yyj)) 2370 { 2371 yyj += YYTERROR; 2372 if (0 <= yyj && yyj <= YYLAST && yycheck[yyj] == YYTERROR && 2373 yy_is_shift_action(yytable[yyj])) 2374 { 2375 /* Shift the error token. */]b4_locations_if([[ 2376 /* First adjust its location.*/ 2377 location_type yyerrloc; 2378 yyerror_range[2].getState().yyloc = this->yyla.location; 2379 YYLLOC_DEFAULT (yyerrloc, (yyerror_range), 2);]])[ 2380 YY_SYMBOL_PRINT ("Shifting", yy_accessing_symbol (yytable[yyj]), 2381 this->yyla.value, yyerrloc); 2382 yyglrShift (create_state_set_index(0), yytable[yyj], 2383 yys->yyposn, yyla.value]b4_locations_if([, yyerrloc])[); 2384 yys = firstTopState(); 2385 break; 2386 } 2387 }]b4_locations_if([[ 2388 yyerror_range[1].getState().yyloc = yys->yyloc;]])[ 2389 if (yys->pred() != YY_NULLPTR) 2390 yys->destroy ("Error: popping", yyparser); 2391 yystateStack.setFirstTop(yys->pred()); 2392 yystateStack.pop_back(); 2393 } 2394 if (firstTopState() == YY_NULLPTR) 2395 yyFail (]b4_locations_if([yylocp, ])[YY_NULLPTR); 2396 } 2397 2398 YYRESULTTAG 2399 yyprocessOneStack (state_set_index yyk, 2400 size_t yyposn]b4_locations_if([, location_type* yylocp])[) 2401 { 2402 while (yystateStack.topAt(yyk) != YY_NULLPTR) 2403 { 2404 const state_num yystate = topState(yyk)->yylrState; 2405 YYCDEBUG << "Stack " << yyk.get() << " Entering state " << yystate << '\n'; 2406 2407 YYASSERT(yystate != YYFINAL); 2408 2409 if (yy_is_defaulted_state(yystate)) 2410 { 2411 const rule_num yyrule = yy_default_action(yystate); 2412 if (yyrule == 0) 2413 { 2414 YYCDEBUG << "Stack " << yyk.get() << " dies.\n"; 2415 yystateStack.yytops.yymarkStackDeleted(yyk); 2416 return yyok; 2417 } 2418 const YYRESULTTAG yyflag = yyglrReduce(yyk, yyrule, yyimmediate[yyrule]); 2419 if (yyflag == yyerr) 2420 { 2421 YYCDEBUG << "Stack " << yyk.get() 2422 << " dies" 2423 " (predicate failure or explicit user error).\n"; 2424 yystateStack.yytops.yymarkStackDeleted(yyk); 2425 return yyok; 2426 } 2427 if (yyflag != yyok) 2428 return yyflag; 2429 } 2430 else 2431 { 2432 yystateStack.yytops.setLookaheadNeeds(yyk, true); 2433 yyget_token(); 2434 const short *yyconflicts; 2435 const int yyaction = yygetLRActions(yystate, this->yyla.kind(), yyconflicts); 2436 2437 for (; *yyconflicts != 0; ++yyconflicts) 2438 { 2439 state_set_index yynewStack = yystateStack.yysplitStack(yyk); 2440 YYCDEBUG << "Splitting off stack " << yynewStack.get() << " from " 2441 << yyk.get() << ".\n"; 2442 YYRESULTTAG yyflag = 2443 yyglrReduce(yynewStack, *yyconflicts, yyimmediate[*yyconflicts]); 2444 if (yyflag == yyok) 2445 YYCHK (yyprocessOneStack (yynewStack, 2446 yyposn]b4_locations_if([, yylocp])[)); 2447 else if (yyflag == yyerr) 2448 { 2449 YYCDEBUG << "Stack " << yynewStack.get() << " dies.\n"; 2450 yystateStack.yytops.yymarkStackDeleted(yynewStack); 2451 } 2452 else 2453 return yyflag; 2454 } 2455 2456 if (yy_is_shift_action(yyaction)) 2457 break; 2458 else if (yy_is_error_action(yyaction)) 2459 { 2460 YYCDEBUG << "Stack " << yyk.get() << " dies.\n"; 2461 yystateStack.yytops.yymarkStackDeleted(yyk); 2462 break; 2463 } 2464 else 2465 { 2466 YYRESULTTAG yyflag = yyglrReduce(yyk, -yyaction, yyimmediate[-yyaction]); 2467 if (yyflag == yyerr) 2468 { 2469 YYCDEBUG << "Stack " << yyk.get() 2470 << " dies" 2471 " (predicate failure or explicit user error).\n"; 2472 yystateStack.yytops.yymarkStackDeleted(yyk); 2473 break; 2474 } 2475 else if (yyflag != yyok) 2476 return yyflag; 2477 } 2478 } 2479 } 2480 return yyok; 2481 } 2482 2483 /** Perform user action for rule number YYN, with RHS length YYRHSLEN, 2484 * and top stack item YYVSP. YYVALP points to place to put semantic 2485 * value ($$), and yylocp points to place for location information 2486 * (@@$). Returns yyok for normal return, yyaccept for YYACCEPT, 2487 * yyerr for YYERROR, yyabort for YYABORT. */ 2488 YYRESULTTAG 2489 yyuserAction (rule_num yyrule, int yyrhslen, glr_stack_item* yyvsp, state_set_index yyk, 2490 value_type* yyvalp]b4_locations_if([, location_type* yylocp])[) 2491 { 2492 bool yynormal YY_ATTRIBUTE_UNUSED = !yystateStack.isSplit(); 2493 int yylow = 1; 2494 ]b4_parse_param_use([yyvalp], [yylocp])dnl 2495 [ YY_USE (yyk); 2496 YY_USE (yyrhslen); 2497 #undef yyerrok 2498 #define yyerrok (yyerrState = 0) 2499 #undef YYACCEPT 2500 #define YYACCEPT return yyaccept 2501 #undef YYABORT 2502 #define YYABORT return yyabort 2503 #undef YYERROR 2504 #define YYERROR return yyerrok, yyerr 2505 #undef YYRECOVERING 2506 #define YYRECOVERING() (yyerrState != 0) 2507 #undef yytoken 2508 #define yytoken this->yyla.kind_ 2509 #undef yyclearin 2510 #define yyclearin (yytoken = ]b4_symbol(empty, kind)[) 2511 #undef YYBACKUP 2512 #define YYBACKUP(Token, Value) \ 2513 return yyparser.error (]b4_locations_if([*yylocp, ])[YY_("syntax error: cannot back up")), \ 2514 yyerrok, yyerr 2515 2516 ]b4_variant_if([[ 2517 /* Variants are always initialized to an empty instance of the 2518 correct type. The default '$$ = $1' action is NOT applied 2519 when using variants. */ 2520 // However we really need to prepare yyvsp now if we want to get 2521 // correct locations, so invoke YYFILL for $1 anyway. 2522 (void) YYFILL (1-yyrhslen); 2523 ]b4_symbol_variant([[yylhsNonterm (yyrule)]], [(*yyvalp)], [emplace])], [[ 2524 if (yyrhslen == 0) 2525 *yyvalp = yyval_default; 2526 else 2527 *yyvalp = yyvsp[YYFILL (1-yyrhslen)].getState().value ();]])[]b4_locations_if([[ 2528 /* Default location. */ 2529 YYLLOC_DEFAULT ((*yylocp), (yyvsp - yyrhslen), yyrhslen); 2530 yyerror_range[1].getState().yyloc = *yylocp; 2531 ]])[ 2532 /* If yyk == -1, we are running a deferred action on a temporary 2533 stack. In that case, YY_REDUCE_PRINT must not play with YYFILL, 2534 so pretend the stack is "normal". */ 2535 YY_REDUCE_PRINT ((yynormal || yyk == create_state_set_index (-1), yyvsp, yyk, yyrule, yyparser)); 2536 #if YY_EXCEPTIONS 2537 try 2538 { 2539 #endif // YY_EXCEPTIONS 2540 switch (yyrule) 2541 { 2542 ]b4_user_actions[ 2543 default: break; 2544 } 2545 #if YY_EXCEPTIONS 2546 } 2547 catch (const syntax_error& yyexc) 2548 { 2549 YYCDEBUG << "Caught exception: " << yyexc.what() << '\n';]b4_locations_if([ 2550 *yylocp = yyexc.location;])[ 2551 yyparser.error (]b4_locations_if([*yylocp, ])[yyexc.what ()); 2552 YYERROR; 2553 } 2554 #endif // YY_EXCEPTIONS 2555 YY_SYMBOL_PRINT ("-> $$ =", yylhsNonterm (yyrule), *yyvalp, *yylocp); 2556 2557 return yyok; 2558 #undef yyerrok 2559 #undef YYABORT 2560 #undef YYACCEPT 2561 #undef YYERROR 2562 #undef YYBACKUP 2563 #undef yytoken 2564 #undef yyclearin 2565 #undef YYRECOVERING 2566 } 2567 2568 YYRESULTTAG 2569 yyresolveStack () 2570 { 2571 if (yystateStack.isSplit()) 2572 { 2573 int yyn = 0; 2574 for (glr_state *yys = firstTopState(); yys != yystateStack.yysplitPoint; 2575 yys = yys->pred()) 2576 yyn += 1; 2577 YYCHK(yyresolveStates(*firstTopState(), yyn)); 2578 } 2579 return yyok; 2580 } 2581 2582 /** Pop the symbols consumed by reduction #YYRULE from the top of stack 2583 * #YYK of *YYSTACKP, and perform the appropriate semantic action on their 2584 * semantic values. Assumes that all ambiguities in semantic values 2585 * have been previously resolved. Set *YYVALP to the resulting value, 2586 * and *YYLOCP to the computed location (if any). Return value is as 2587 * for userAction. */ 2588 YYRESULTTAG 2589 yydoAction (state_set_index yyk, rule_num yyrule, 2590 value_type* yyvalp]b4_locations_if([, location_type* yylocp])[) 2591 { 2592 const int yynrhs = yyrhsLength(yyrule); 2593 2594 if (!yystateStack.isSplit()) 2595 { 2596 /* Standard special case: single stack. */ 2597 YYASSERT(yyk.get() == 0); 2598 glr_stack_item *yyrhs = yystateStack.firstTop()->asItem(); 2599 const YYRESULTTAG res 2600 = yyuserAction (yyrule, yynrhs, yyrhs, yyk, yyvalp]b4_locations_if([, yylocp])[); 2601 yystateStack.pop_back(static_cast<size_t>(yynrhs)); 2602 yystateStack.setFirstTop(&yystateStack[yystateStack.size() - 1].getState()); 2603 return res; 2604 } 2605 else 2606 { 2607 glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1]; 2608 glr_state *yys = yystateStack.topAt(yyk); 2609 yyrhsVals[YYMAXRHS + YYMAXLEFT].getState().setPred(yys);]b4_locations_if([[ 2610 if (yynrhs == 0) 2611 /* Set default location. */ 2612 yyrhsVals[YYMAXRHS + YYMAXLEFT - 1].getState().yyloc = yys->yyloc;]])[ 2613 for (int yyi = 0; yyi < yynrhs; yyi += 1) 2614 { 2615 yys = yys->pred(); 2616 YYASSERT(yys != YY_NULLPTR); 2617 } 2618 yystateStack.yyupdateSplit (*yys); 2619 yystateStack.setTopAt(yyk, yys); 2620 return yyuserAction (yyrule, yynrhs, yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, 2621 yyk, 2622 yyvalp]b4_locations_if([, yylocp])[); 2623 } 2624 } 2625 2626 /** Pop items off stack #YYK of *YYSTACKP according to grammar rule YYRULE, 2627 * and push back on the resulting nonterminal symbol. Perform the 2628 * semantic action associated with YYRULE and store its value with the 2629 * newly pushed state, if YYFORCEEVAL or if *YYSTACKP is currently 2630 * unambiguous. Otherwise, store the deferred semantic action with 2631 * the new state. If the new state would have an identical input 2632 * position, LR state, and predecessor to an existing state on the stack, 2633 * it is identified with that existing state, eliminating stack #YYK from 2634 * *YYSTACKP. In this case, the semantic value is 2635 * added to the options for the existing state's semantic value. 2636 */ 2637 YYRESULTTAG 2638 yyglrReduce (state_set_index yyk, rule_num yyrule, bool yyforceEval) 2639 { 2640 size_t yyposn = topState(yyk)->yyposn; 2641 2642 if (yyforceEval || !yystateStack.isSplit()) 2643 { 2644 value_type val;]b4_locations_if([[ 2645 location_type loc;]])[ 2646 2647 YYRESULTTAG yyflag = yydoAction (yyk, yyrule, &val]b4_locations_if([, &loc])[); 2648 if (yyflag == yyerr && yystateStack.isSplit()) 2649 {]b4_parse_trace_if([[ 2650 YYCDEBUG << "Parse on stack " << yyk.get () 2651 << " rejected by rule " << yyrule - 1 2652 << " (line " << int (yyrline[yyrule]) << ").\n"; 2653 ]])[} 2654 if (yyflag != yyok) 2655 return yyflag; 2656 yyglrShift (yyk, 2657 yyLRgotoState (topState(yyk)->yylrState, 2658 yylhsNonterm (yyrule)), 2659 yyposn, val]b4_locations_if([, loc])[);]b4_variant_if([[ 2660 // FIXME: User destructors. 2661 // Value type destructor. 2662 ]b4_symbol_variant([[yylhsNonterm (yyrule)]], [[val]], [[template destroy]])])[ 2663 } 2664 else 2665 { 2666 glr_state *yys = yystateStack.topAt(yyk); 2667 glr_state *yys0 = yys; 2668 for (int yyn = yyrhsLength(yyrule); 0 < yyn; yyn -= 1) 2669 { 2670 yys = yys->pred(); 2671 YYASSERT(yys != YY_NULLPTR); 2672 } 2673 yystateStack.yyupdateSplit(*yys); 2674 state_num yynewLRState = yyLRgotoState(yys->yylrState, yylhsNonterm(yyrule));]b4_parse_trace_if([[ 2675 YYCDEBUG << "Reduced stack " << yyk.get () 2676 << " by rule " << yyrule - 1 << " (line " << int (yyrline[yyrule]) 2677 << "); action deferred. Now in state " << yynewLRState 2678 << ".\n";]])[ 2679 for (state_set_index yyi = create_state_set_index(0); yyi.uget() < yystateStack.numTops(); ++yyi) 2680 if (yyi != yyk && yystateStack.topAt(yyi) != YY_NULLPTR) 2681 { 2682 const glr_state *yysplit = yystateStack.yysplitPoint; 2683 glr_state *yyp = yystateStack.topAt(yyi); 2684 while (yyp != yys && yyp != yysplit && yyp->yyposn >= yyposn) 2685 { 2686 if (yyp->yylrState == yynewLRState && yyp->pred() == yys) 2687 { 2688 yyaddDeferredAction(yyk, yyp, yys0, yyrule); 2689 yystateStack.yytops.yymarkStackDeleted(yyk); 2690 YYCDEBUG << "Merging stack " << yyk.get() << " into stack " << yyi.get() 2691 << ".\n"; 2692 return yyok; 2693 } 2694 yyp = yyp->pred(); 2695 } 2696 } 2697 yystateStack.setTopAt(yyk, yys); 2698 yyglrShiftDefer (yyk, yynewLRState, yyposn, yys0, yyrule); 2699 } 2700 return yyok; 2701 } 2702 2703 /** Shift stack #YYK of *YYSTACKP, to a new state corresponding to LR 2704 * state YYLRSTATE, at input position YYPOSN, with the (unresolved) 2705 * semantic value of YYRHS under the action for YYRULE. */ 2706 void 2707 yyglrShiftDefer (state_set_index yyk, state_num yylrState, 2708 size_t yyposn, glr_state* yyrhs, rule_num yyrule) 2709 { 2710 glr_state &yynewState = yystateStack.yynewGLRState(glr_state(yylrState, yyposn)); 2711 yynewState.setPred(yystateStack.topAt(yyk)); 2712 yystateStack.setTopAt(yyk, &yynewState); 2713 2714 /* Invokes yyreserveStack. */ 2715 yyaddDeferredAction(yyk, &yynewState, yyrhs, yyrule); 2716 } 2717 2718 /** Shift to a new state on stack #YYK of *YYSTACKP, corresponding to LR 2719 * state YYLRSTATE, at input position YYPOSN, with (resolved) semantic 2720 * value YYVAL_ARG and source location YYLOC_ARG. */ 2721 void 2722 yyglrShift (state_set_index yyk, state_num yylrState, 2723 size_t yyposn, 2724 const value_type& yyval_arg]b4_locations_if([, const location_type& yyloc_arg])[) 2725 { 2726 glr_state& yynewState = yystateStack.yynewGLRState ( 2727 glr_state (yylrState, yyposn, yyval_arg]b4_locations_if([, yyloc_arg])[)); 2728 yynewState.setPred(yystateStack.topAt(yyk)); 2729 yystateStack.setTopAt(yyk, &yynewState); 2730 yyreserveGlrStack(); 2731 } 2732 2733 #if] b4_api_PREFIX[DEBUG 2734 void 2735 yypstack (state_set_index yyk) const 2736 { 2737 yystateStack.yypstates(yystateStack.topAt(yyk)); 2738 } 2739 #endif 2740 2741 glr_state* topState(state_set_index i) { 2742 return yystateStack.topAt(i); 2743 } 2744 2745 glr_state* firstTopState() { 2746 return yystateStack.firstTop(); 2747 } 2748 2749 private: 2750 2751 void popall_ () 2752 { 2753 /* If the stack is well-formed, pop the stack until it is empty, 2754 destroying its entries as we go. But free the stack regardless 2755 of whether it is well-formed. */ 2756 for (state_set_index k = create_state_set_index(0); k.uget() < yystateStack.numTops(); 2757 k += 1) 2758 if (yystateStack.topAt(k) != YY_NULLPTR) 2759 { 2760 while (yystateStack.topAt(k) != YY_NULLPTR) 2761 { 2762 glr_state *state = topState(k);]b4_locations_if([[ 2763 yyerror_range[1].getState().yyloc = state->yyloc;]])[ 2764 if (state->pred() != YY_NULLPTR) 2765 state->destroy ("Cleanup: popping", yyparser); 2766 yystateStack.setTopAt(k, state->pred()); 2767 yystateStack.pop_back(); 2768 } 2769 break; 2770 } 2771 } 2772 2773 /** Resolve the previous YYN states starting at and including state YYS 2774 * on *YYSTACKP. If result != yyok, some states may have been left 2775 * unresolved possibly with empty semantic option chains. Regardless 2776 * of whether result = yyok, each state has been left with consistent 2777 * data so that destroy can be invoked if necessary. */ 2778 YYRESULTTAG 2779 yyresolveStates (glr_state& yys, int yyn) 2780 { 2781 if (0 < yyn) 2782 { 2783 YYASSERT(yys.pred() != YY_NULLPTR); 2784 YYCHK(yyresolveStates(*yys.pred(), yyn - 1)); 2785 if (!yys.yyresolved) 2786 YYCHK(yyresolveValue(yys)); 2787 } 2788 return yyok; 2789 } 2790 2791 static void 2792 yyuserMerge (int yyn, value_type& yy0, value_type& yy1) 2793 { 2794 YY_USE(yy0); 2795 YY_USE(yy1); 2796 2797 switch (yyn) 2798 { 2799 ]b4_mergers[ 2800 default: break; 2801 } 2802 } 2803 2804 /** Resolve the ambiguity represented in state YYS in *YYSTACKP, 2805 * perform the indicated actions, and set the semantic value of YYS. 2806 * If result != yyok, the chain of semantic options in YYS has been 2807 * cleared instead or it has been left unmodified except that 2808 * redundant options may have been removed. Regardless of whether 2809 * result = yyok, YYS has been left with consistent data so that 2810 * destroy can be invoked if necessary. */ 2811 YYRESULTTAG 2812 yyresolveValue (glr_state& yys) 2813 { 2814 semantic_option *yybest = yys.firstVal(); 2815 YYASSERT(yybest != YY_NULLPTR); 2816 bool yymerge = false; 2817 YYRESULTTAG yyflag;]b4_locations_if([ 2818 location_type *yylocp = &yys.yyloc;])[ 2819 2820 semantic_option* yypPrev = yybest; 2821 for (semantic_option* yyp = yybest->next(); 2822 yyp != YY_NULLPTR; ) 2823 { 2824 if (yybest->isIdenticalTo(*yyp)) 2825 { 2826 yybest->mergeWith(*yyp); 2827 yypPrev->setNext(yyp->next()); 2828 yyp = yypPrev->next(); 2829 } 2830 else 2831 { 2832 switch (yypreference(*yybest, *yyp)) 2833 { 2834 case 0:]b4_locations_if([[ 2835 yyresolveLocations (yys, 1);]])[ 2836 return yystateStack.yyreportAmbiguity (*yybest, *yyp, yyparser]b4_locations_if([, *yylocp])[); 2837 break; 2838 case 1: 2839 yymerge = true; 2840 break; 2841 case 2: 2842 break; 2843 case 3: 2844 yybest = yyp; 2845 yymerge = false; 2846 break; 2847 default: 2848 /* This cannot happen so it is not worth a YYASSERT (false), 2849 but some compilers complain if the default case is 2850 omitted. */ 2851 break; 2852 } 2853 yypPrev = yyp; 2854 yyp = yyp->next(); 2855 } 2856 } 2857 2858 value_type val; 2859 if (yymerge) 2860 { 2861 int yyprec = yydprec[yybest->yyrule]; 2862 yyflag = yyresolveAction (*yybest, &val]b4_locations_if([, yylocp])[); 2863 if (yyflag == yyok) 2864 for (semantic_option *yyp = yybest->next(); yyp != YY_NULLPTR; yyp = yyp->next()) 2865 { 2866 if (yyprec == yydprec[yyp->yyrule]) 2867 { 2868 value_type yyval_other;]b4_locations_if([ 2869 location_type yydummy;])[ 2870 yyflag = yyresolveAction (*yyp, &yyval_other]b4_locations_if([, &yydummy])[); 2871 if (yyflag != yyok) 2872 { 2873 yyparser.yy_destroy_ ("Cleanup: discarding incompletely merged value for", 2874 yy_accessing_symbol (yys.yylrState), 2875 this->yyla.value]b4_locations_if([, *yylocp])[); 2876 break; 2877 } 2878 yyuserMerge (yymerger[yyp->yyrule], val, yyval_other);]b4_variant_if([[ 2879 // FIXME: User destructors. 2880 // Value type destructor. 2881 ]b4_symbol_variant([[yy_accessing_symbol (yys.yylrState)]], [[yyval_other]], [[template destroy]])])[ 2882 } 2883 } 2884 } 2885 else 2886 yyflag = yyresolveAction (*yybest, &val]b4_locations_if([, yylocp])[); 2887 2888 if (yyflag == yyok) 2889 { 2890 yys.yyresolved = true; 2891 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN]b4_variant_if([[ 2892 new (&yys.value ()) value_type (); 2893 ]b4_symbol_variant([yy_accessing_symbol (yys.yylrState)], 2894 [yys.value ()], [copy], [val])], [[ 2895 new (&yys.value ()) value_type (val);]])[ 2896 2897 YY_IGNORE_MAYBE_UNINITIALIZED_END 2898 } 2899 else 2900 yys.setFirstVal(YY_NULLPTR); 2901 ]b4_variant_if([[ 2902 // FIXME: User destructors. 2903 // Value type destructor. 2904 ]b4_symbol_variant([[yy_accessing_symbol (yys.yylrState)]], [[val]], [[template destroy]])])[ 2905 return yyflag; 2906 } 2907 2908 /** Resolve the states for the RHS of YYOPT on *YYSTACKP, perform its 2909 * user action, and return the semantic value and location in *YYVALP 2910 * and *YYLOCP. Regardless of whether result = yyok, all RHS states 2911 * have been destroyed (assuming the user action destroys all RHS 2912 * semantic values if invoked). */ 2913 YYRESULTTAG 2914 yyresolveAction (semantic_option& yyopt, value_type* yyvalp]b4_locations_if([, location_type* yylocp])[) 2915 { 2916 glr_state *yyoptState = yyopt.state(); 2917 YYASSERT(yyoptState != YY_NULLPTR); 2918 int yynrhs = yyrhsLength(yyopt.yyrule); 2919 YYRESULTTAG yyflag = yyresolveStates(*yyoptState, yynrhs); 2920 if (yyflag != yyok) 2921 { 2922 for (glr_state *yys = yyoptState; yynrhs > 0; yys = yys->pred(), yynrhs -= 1) 2923 yys->destroy("Cleanup: popping", yyparser); 2924 return yyflag; 2925 } 2926 2927 glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1]; 2928 yyrhsVals[YYMAXRHS + YYMAXLEFT].getState().setPred(yyopt.state());]b4_locations_if([[ 2929 if (yynrhs == 0) 2930 /* Set default location. */ 2931 yyrhsVals[YYMAXRHS + YYMAXLEFT - 1].getState().yyloc = yyoptState->yyloc;]])[ 2932 { 2933 symbol_type yyla_current = std::move(this->yyla); 2934 this->yyla = std::move(yyopt.yyla); 2935 yyflag = yyuserAction (yyopt.yyrule, yynrhs, 2936 yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, 2937 create_state_set_index (-1), 2938 yyvalp]b4_locations_if([, yylocp])[); 2939 this->yyla = std::move(yyla_current); 2940 } 2941 return yyflag; 2942 }]b4_locations_if([[ 2943 2944 /** Resolve the locations for each of the YYN1 states in *YYSTACKP, 2945 * ending at YYS1. Has no effect on previously resolved states. 2946 * The first semantic option of a state is always chosen. */ 2947 void 2948 yyresolveLocations (glr_state &yys1, int yyn1) 2949 { 2950 if (0 < yyn1) 2951 { 2952 yyresolveLocations(*yys1.pred(), yyn1 - 1); 2953 if (!yys1.yyresolved) 2954 { 2955 glr_stack_item yyrhsloc[1 + YYMAXRHS]; 2956 YYASSERT(yys1.firstVal() != YY_NULLPTR); 2957 semantic_option &yyoption = *yys1.firstVal(); 2958 const int yynrhs = yyrhsLength(yyoption.yyrule); 2959 if (0 < yynrhs) 2960 { 2961 yyresolveLocations(*yyoption.state(), yynrhs); 2962 const glr_state *yys = yyoption.state(); 2963 for (int yyn = yynrhs; yyn > 0; yyn -= 1) 2964 { 2965 yyrhsloc[yyn].getState().yyloc = yys->yyloc; 2966 yys = yys->pred(); 2967 } 2968 } 2969 else 2970 { 2971 /* Both yyresolveAction and yyresolveLocations traverse the GSS 2972 in reverse rightmost order. It is only necessary to invoke 2973 yyresolveLocations on a subforest for which yyresolveAction 2974 would have been invoked next had an ambiguity not been 2975 detected. Thus the location of the previous state (but not 2976 necessarily the previous state itself) is guaranteed to be 2977 resolved already. */ 2978 YY_IGNORE_NULL_DEREFERENCE_BEGIN 2979 yyrhsloc[0].getState().yyloc = yyoption.state()->yyloc; 2980 YY_IGNORE_NULL_DEREFERENCE_END 2981 } 2982 YYLLOC_DEFAULT((yys1.yyloc), yyrhsloc, yynrhs); 2983 } 2984 } 2985 }]])[ 2986 2987 /** If yytoken is empty, fetch the next token. */ 2988 void 2989 yyget_token () 2990 { 2991 ]b4_parse_param_use()dnl 2992 [ if (this->yyla.empty ()) 2993 { 2994 YYCDEBUG << "Reading a token\n"; 2995 #if YY_EXCEPTIONS 2996 try 2997 #endif // YY_EXCEPTIONS 2998 {]b4_token_ctor_if([[ 2999 symbol_type yylookahead (]b4_yylex[); 3000 yyla.move (yylookahead);]], [[ 3001 yyla.kind_ = yyparser.yytranslate_ (]b4_yylex[);]])[ 3002 } 3003 #if YY_EXCEPTIONS 3004 catch (const parser_type::syntax_error &yyexc) 3005 { 3006 YYCDEBUG << "Caught exception: " << yyexc.what() << '\n';]b4_locations_if([ 3007 this->yyla.location = yyexc.location;])[ 3008 yyparser.error (]b4_locations_if([this->yyla.location, ])[yyexc.what ()); 3009 // Map errors caught in the scanner to the error token, so that error 3010 // handling is started. 3011 this->yyla.kind_ = ]b4_symbol(error, kind)[; 3012 } 3013 } 3014 #endif // YY_EXCEPTIONS 3015 if (this->yyla.kind () == ]b4_symbol(eof, kind)[) 3016 YYCDEBUG << "Now at end of input.\n"; 3017 else 3018 YY_SYMBOL_PRINT ("Next token is", this->yyla.kind (), this->yyla.value, this->yyla.location); 3019 } 3020 3021 3022 /* Bison grammar-table manipulation. */ 3023 3024 /** The action to take in YYSTATE on seeing YYTOKEN. 3025 * Result R means 3026 * R < 0: Reduce on rule -R. 3027 * R = 0: Error. 3028 * R > 0: Shift to state R. 3029 * Set *YYCONFLICTS to a pointer into yyconfl to a 0-terminated list 3030 * of conflicting reductions. 3031 */ 3032 static int 3033 yygetLRActions (state_num yystate, symbol_kind_type yytoken, const short*& yyconflicts) 3034 { 3035 int yyindex = yypact[yystate] + yytoken; 3036 if (yytoken == ]b4_symbol(error, kind)[) 3037 { 3038 // This is the error token. 3039 yyconflicts = yyconfl; 3040 return 0; 3041 } 3042 else if (yy_is_defaulted_state(yystate) || yyindex < 0 || YYLAST < yyindex || 3043 yycheck[yyindex] != yytoken) 3044 { 3045 yyconflicts = yyconfl; 3046 return -yydefact[yystate]; 3047 } 3048 else if (!yytable_value_is_error(yytable[yyindex])) 3049 { 3050 yyconflicts = yyconfl + yyconflp[yyindex]; 3051 return yytable[yyindex]; 3052 } 3053 else 3054 { 3055 yyconflicts = yyconfl + yyconflp[yyindex]; 3056 return 0; 3057 } 3058 } 3059 3060 /** Compute post-reduction state. 3061 * \param yystate the current state 3062 * \param yysym the nonterminal to push on the stack 3063 */ 3064 static state_num 3065 yyLRgotoState (state_num yystate, symbol_kind_type yysym) 3066 { 3067 const int yyr = yypgoto[yysym - YYNTOKENS] + yystate; 3068 if (0 <= yyr && yyr <= YYLAST && yycheck[yyr] == yystate) 3069 return yytable[yyr]; 3070 else 3071 return yydefgoto[yysym - YYNTOKENS]; 3072 } 3073 3074 static bool 3075 yypact_value_is_default (state_num yystate) 3076 { 3077 return ]b4_table_value_equals([[pact]], [[yystate]], [b4_pact_ninf], [YYPACT_NINF])[; 3078 } 3079 3080 static bool 3081 yytable_value_is_error (int yytable_value YY_ATTRIBUTE_UNUSED) 3082 { 3083 return ]b4_table_value_equals([[table]], [[yytable_value]], [b4_table_ninf], [YYTABLE_NINF])[; 3084 } 3085 3086 static bool 3087 yy_is_shift_action (int yyaction) YY_NOEXCEPT 3088 { 3089 return 0 < yyaction; 3090 } 3091 3092 static bool 3093 yy_is_error_action (int yyaction) YY_NOEXCEPT 3094 { 3095 return yyaction == 0; 3096 } 3097 3098 /** Whether LR state YYSTATE has only a default reduction 3099 * (regardless of token). */ 3100 static bool 3101 yy_is_defaulted_state (state_num yystate) 3102 { 3103 return yypact_value_is_default(yypact[yystate]); 3104 } 3105 3106 /** The default reduction for YYSTATE, assuming it has one. */ 3107 static rule_num 3108 yy_default_action (state_num yystate) 3109 { 3110 return yydefact[yystate]; 3111 } 3112 3113 /* GLRStacks */ 3114 3115 /** Y0 and Y1 represent two possible actions to take in a given 3116 * parsing state; return 0 if no combination is possible, 3117 * 1 if user-mergeable, 2 if Y0 is preferred, 3 if Y1 is preferred. */ 3118 static int 3119 yypreference (const semantic_option& y0, const semantic_option& y1) 3120 { 3121 const rule_num r0 = y0.yyrule, r1 = y1.yyrule; 3122 const int p0 = yydprec[r0], p1 = yydprec[r1]; 3123 3124 if (p0 == p1) 3125 { 3126 if (yymerger[r0] == 0 || yymerger[r0] != yymerger[r1]) 3127 return 0; 3128 else 3129 return 1; 3130 } 3131 else if (p0 == 0 || p1 == 0) 3132 return 0; 3133 else if (p0 < p1) 3134 return 3; 3135 else if (p1 < p0) 3136 return 2; 3137 else 3138 return 0; 3139 } 3140 3141 ]b4_parse_param_vars[ 3142 }; // class ]b4_parser_class[::glr_stack 3143 } // namespace ]b4_namespace_ref[ 3144 3145 #if] b4_api_PREFIX[DEBUG 3146 namespace 3147 { 3148 void 3149 yypstack (const glr_stack& yystack, size_t yyk) 3150 { 3151 yystack.yypstack (create_state_set_index (static_cast<std::ptrdiff_t> (yyk))); 3152 } 3153 3154 void yypdumpstack(const glr_stack &yystack) 3155 { 3156 yystack.yypdumpstack(); 3157 } 3158 } 3159 #endif 3160 3161 ]b4_namespace_open[ 3162 /// Build a parser object. 3163 ]b4_parser_class::b4_parser_class[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [ 3164 :])[ 3165 #if] b4_api_PREFIX[DEBUG 3166 ]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[ 3167 #endif] b4_parse_param_cons[ 3168 {} 3169 3170 ]b4_parser_class::~b4_parser_class[ () 3171 {} 3172 3173 ]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW 3174 {} 3175 3176 int 3177 ]b4_parser_class[::operator() () 3178 { 3179 return parse(); 3180 } 3181 3182 int 3183 ]b4_parser_class[::parse () 3184 { 3185 glr_stack yystack(YYINITDEPTH, *this]b4_user_args[); 3186 return yystack.parse(); 3187 } 3188 3189 ]b4_parse_error_bmatch([custom\|detailed], 3190 [[ const char * 3191 ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol) 3192 { 3193 static const char *const yy_sname[] = 3194 { 3195 ]b4_symbol_names[ 3196 };]b4_has_translations_if([[ 3197 /* YYTRANSLATABLE[SYMBOL-NUM] -- Whether YY_SNAME[SYMBOL-NUM] is 3198 internationalizable. */ 3199 static ]b4_int_type_for([b4_translatable])[ yytranslatable[] = 3200 { 3201 ]b4_translatable[ 3202 }; 3203 return (yysymbol < YYNTOKENS && yytranslatable[yysymbol] 3204 ? _(yy_sname[yysymbol]) 3205 : yy_sname[yysymbol]);]], [[ 3206 return yy_sname[yysymbol];]])[ 3207 } 3208 ]], 3209 [simple], 3210 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 3211 const char * 3212 ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol) 3213 { 3214 return yytname_[yysymbol]; 3215 } 3216 #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ 3217 ]], 3218 [verbose], 3219 [[ /* Return YYSTR after stripping away unnecessary quotes and 3220 backslashes, so that it's suitable for yyerror. The heuristic is 3221 that double-quoting is unnecessary unless the string contains an 3222 apostrophe, a comma, or backslash (other than backslash-backslash). 3223 YYSTR is taken from yytname. */ 3224 std::string 3225 ]b4_parser_class[::yytnamerr_ (const char *yystr) 3226 { 3227 if (*yystr == '"') 3228 { 3229 std::string yyr; 3230 char const *yyp = yystr; 3231 3232 for (;;) 3233 switch (*++yyp) 3234 { 3235 case '\'': 3236 case ',': 3237 goto do_not_strip_quotes; 3238 3239 case '\\': 3240 if (*++yyp != '\\') 3241 goto do_not_strip_quotes; 3242 else 3243 goto append; 3244 3245 append: 3246 default: 3247 yyr += *yyp; 3248 break; 3249 3250 case '"': 3251 return yyr; 3252 } 3253 do_not_strip_quotes:; 3254 } 3255 3256 return yystr; 3257 } 3258 3259 std::string 3260 ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol) 3261 { 3262 return yytnamerr_(yytname_[yysymbol]); 3263 } 3264 ]])[ 3265 3266 ]b4_parse_error_bmatch([simple\|verbose], 3267 [[#if ]b4_api_PREFIX[DEBUG]b4_tname_if([[ || 1]])[ 3268 // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. 3269 // First, the terminals, then, starting at \a YYNTOKENS, nonterminals. 3270 const char* 3271 const ]b4_parser_class[::yytname_[] = 3272 { 3273 ]b4_tname[ 3274 }; 3275 #endif 3276 ]])[ 3277 3278 ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[ 3279 // ]b4_parser_class[::context. 3280 ]b4_parser_class[::context::context (glr_stack& yystack, const symbol_type& yyla) 3281 : yystack_ (yystack) 3282 , yyla_ (yyla) 3283 {} 3284 3285 int 3286 ]b4_parser_class[::context::expected_tokens (symbol_kind_type yyarg[], int yyargn) const 3287 { 3288 // Actual number of expected tokens 3289 int yycount = 0; 3290 const int yyn = yypact[yystack_.firstTopState()->yylrState]; 3291 if (!yystack_.yypact_value_is_default(yyn)) 3292 { 3293 /* Start YYX at -YYN if negative to avoid negative indexes in 3294 YYCHECK. In other words, skip the first -YYN actions for this 3295 state because they are default actions. */ 3296 const int yyxbegin = yyn < 0 ? -yyn : 0; 3297 /* Stay within bounds of both yycheck and yytname. */ 3298 const int yychecklim = YYLAST - yyn + 1; 3299 const int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; 3300 for (int yyx = yyxbegin; yyx < yyxend; ++yyx) 3301 if (yycheck[yyx + yyn] == yyx && yyx != ]b4_symbol(error, kind)[ 3302 && !yystack_.yytable_value_is_error (yytable[yyx + yyn])) 3303 { 3304 if (!yyarg) 3305 ++yycount; 3306 else if (yycount == yyargn) 3307 return 0; 3308 else 3309 yyarg[yycount++] = YY_CAST(symbol_kind_type, yyx); 3310 } 3311 } 3312 if (yyarg && yycount == 0 && 0 < yyargn) 3313 yyarg[0] = ]b4_symbol(empty, kind)[; 3314 return yycount; 3315 } 3316 3317 ]])[ 3318 3319 ]b4_parse_error_bmatch([detailed\|verbose], [[ 3320 int 3321 ]b4_parser_class[::yy_syntax_error_arguments_ (const context& yyctx, 3322 symbol_kind_type yyarg[], int yyargn) const 3323 { 3324 /* There are many possibilities here to consider: 3325 - If this state is a consistent state with a default action, then 3326 the only way this function was invoked is if the default action 3327 is an error action. In that case, don't check for expected 3328 tokens because there are none. 3329 - The only way there can be no lookahead present (in yyla) is 3330 if this state is a consistent state with a default action. 3331 Thus, detecting the absence of a lookahead is sufficient to 3332 determine that there is no unexpected or expected token to 3333 report. In that case, just report a simple "syntax error". 3334 - Don't assume there isn't a lookahead just because this state is 3335 a consistent state with a default action. There might have 3336 been a previous inconsistent state, consistent state with a 3337 non-default action, or user semantic action that manipulated 3338 yyla. (However, yyla is currently not documented for users.) 3339 */ 3340 3341 if (!yyctx.lookahead().empty()) 3342 { 3343 if (yyarg) 3344 yyarg[0] = yyctx.token(); 3345 int yyn = yyctx.expected_tokens(yyarg ? yyarg + 1 : yyarg, yyargn - 1); 3346 return yyn + 1; 3347 } 3348 return 0; 3349 } 3350 3351 // Generate an error message. 3352 std::string 3353 ]b4_parser_class[::yysyntax_error_ (const context& yyctx) const 3354 { 3355 // Its maximum. 3356 enum 3357 { 3358 YYARGS_MAX = 5 3359 }; 3360 // Arguments of yyformat. 3361 symbol_kind_type yyarg[YYARGS_MAX]; 3362 int yycount = yy_syntax_error_arguments_(yyctx, yyarg, YYARGS_MAX); 3363 3364 char const *yyformat = YY_NULLPTR; 3365 switch (yycount) 3366 { 3367 #define YYCASE_(N, S) \ 3368 case N: \ 3369 yyformat = S; \ 3370 break 3371 default: // Avoid compiler warnings. 3372 YYCASE_(0, YY_("syntax error")); 3373 YYCASE_(1, YY_("syntax error, unexpected %s")); 3374 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); 3375 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); 3376 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); 3377 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); 3378 #undef YYCASE_ 3379 } 3380 3381 std::string yyres; 3382 // Argument number. 3383 std::ptrdiff_t yyi = 0; 3384 for (char const *yyp = yyformat; *yyp; ++yyp) 3385 if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount) 3386 { 3387 yyres += symbol_name(yyarg[yyi++]); 3388 ++yyp; 3389 } 3390 else 3391 yyres += *yyp; 3392 return yyres; 3393 }]])[ 3394 3395 void 3396 ]b4_parser_class[::yy_destroy_ (const char* yymsg, symbol_kind_type yykind, 3397 value_type& yyval]b4_locations_if([[, 3398 location_type& yyloc]])[) 3399 { 3400 YY_USE(yyval);]b4_locations_if([[ 3401 YY_USE (yyloc);]])[ 3402 if (!yymsg) 3403 yymsg = "Deleting"; 3404 ]b4_parser_class[& yyparser = *this; 3405 YY_USE (yyparser); 3406 YY_SYMBOL_PRINT (yymsg, yykind, yyval, yyloc); 3407 3408 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 3409 ]m4_do([m4_pushdef([b4_symbol_action], m4_defn([b4_symbol_action_for_yyval]))], 3410 [b4_symbol_actions([destructor])], 3411 [m4_popdef([b4_symbol_action])])[ 3412 YY_IGNORE_MAYBE_UNINITIALIZED_END 3413 } 3414 3415 #if] b4_api_PREFIX[DEBUG 3416 /*--------------------. 3417 | Print this symbol. | 3418 `--------------------*/ 3419 3420 void 3421 ]b4_parser_class[::yy_symbol_value_print_ (symbol_kind_type yykind, 3422 const value_type& yyval]b4_locations_if([[, 3423 const location_type& yyloc]])[) const 3424 {]b4_locations_if([[ 3425 YY_USE (yyloc);]])[ 3426 YY_USE (yyval); 3427 std::ostream& yyo = debug_stream (); 3428 YY_USE (yyo); 3429 ]m4_do([m4_pushdef([b4_symbol_action], m4_defn([b4_symbol_action_for_yyval]))], 3430 [b4_symbol_actions([printer])], 3431 [m4_popdef([b4_symbol_action])])[ 3432 } 3433 3434 void 3435 ]b4_parser_class[::yy_symbol_print_ (symbol_kind_type yykind, 3436 const value_type& yyval]b4_locations_if([[, 3437 const location_type& yyloc]])[) const 3438 { 3439 *yycdebug_ << (yykind < YYNTOKENS ? "token" : "nterm") 3440 << ' ' << symbol_name (yykind) << " ("]b4_locations_if([[ 3441 << yyloc << ": "]])[; 3442 yy_symbol_value_print_ (yykind, yyval]b4_locations_if([[, yyloc]])[); 3443 *yycdebug_ << ')'; 3444 } 3445 3446 std::ostream& 3447 ]b4_parser_class[::debug_stream () const 3448 { 3449 return *yycdebug_; 3450 } 3451 3452 void 3453 ]b4_parser_class[::set_debug_stream (std::ostream& o) 3454 { 3455 yycdebug_ = &o; 3456 } 3457 3458 3459 ]b4_parser_class[::debug_level_type 3460 ]b4_parser_class[::debug_level () const 3461 { 3462 return yydebug; 3463 } 3464 3465 void 3466 ]b4_parser_class[::set_debug_level (debug_level_type l) 3467 { 3468 // Actually, it is yydebug which is really used. 3469 yydebug = l; 3470 } 3471 #endif // ]b4_api_PREFIX[DEBUG 3472 3473 ]b4_token_ctor_if([], [b4_yytranslate_define([cc])])[ 3474 3475 ]b4_token_ctor_if([], [[ 3476 /*---------. 3477 | symbol. | 3478 `---------*/ 3479 ]b4_public_types_define([cc])])[ 3480 ]b4_namespace_close[]dnl 3481 b4_epilogue[]dnl 3482 b4_output_end 3483