X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/788355718faaaafcc78ba3b94903acb11c1a1da5..6ab1adbe1e533119e8a70bb34dbb13c6c8b5e793:/data/lalr1.cc diff --git a/data/lalr1.cc b/data/lalr1.cc index d6a48780..79e74175 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -19,37 +19,60 @@ m4_include(b4_pkgdatadir/[c++.m4]) -# b4_args(ARG1, ...) -# _b4_args(ARG1, ...) -# ------------------- -# Join with comma, skipping empty arguments. -# b4_args calls itself recursively until it sees the first non-empty -# argument, then calls _b4_args which prepends each non-empty argument -# with a comma. -m4_define([b4_args], -[m4_if([$#$1], - [1], [], - [m4_ifval([$1], - [$1[]_$0(m4_shift($@))], - [$0(m4_shift($@))])])]) - -# _b4_args(ARGS1, ...) -# -------------------- -m4_define([_b4_args], -[m4_if([$#$1], - [1], [], - [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])]) - +# b4_table_declare(TABLE-NAME, CONTENT, COMMENT) +# ---------------------------------------------- +# Declare "parser::yy_" which contents is CONTENT. +m4_define([b4_table_declare], +[m4_ifval([$3], [b4_c_comment([$3], [ ]) +])dnl + static const b4_int_type_for([$2]) yy$1_[[]];dnl +]) -# b4_table_define(TABLE-NAME, CONTENT) -# ------------------------------------ +# b4_table_define(TABLE-NAME, CONTENT, COMMENT) +# --------------------------------------------- # Define "parser::yy_" which contents is CONTENT. m4_define([b4_table_define], -[const b4_int_type_for([$2]) +[ const b4_int_type_for([$2]) b4_parser_class_name::yy$1_[[]] = { $2 - }dnl + };dnl +]) + +# b4_tables_map(MACRO) +# -------------------- +# Map MACRO on all the integral tables. MACRO is expected to have +# the signature MACRO(TABLE-NAME, CONTENT, COMMENT). +m4_define([b4_tables_map], +[$1([pact], [b4_pact], + [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing +STATE-NUM.]]) + +$1([defact], [b4_defact], + [[YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE +does not specify something else to do. Zero means the default is an +error.]]) + +$1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]]) + +$1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]]) + +$1([table], [b4_table], + [[YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If +positive, shift that token. If negative, reduce the rule which +number is the opposite. If zero, do what YYDEFACT says.]]) + +$1([check], [b4_check]) + +$1([stos], [b4_stos], + [[STOS_[STATE-NUM] -- The (internal number of the) accessing +symbol of state STATE-NUM.]]) + +$1([r1], [b4_r1], + [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]]) + +$1([r2], [b4_r2], + [[YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.]]) ]) # b4_symbol_value_template(VAL, [TYPE]) @@ -63,7 +86,7 @@ b4_variant_if([ # ---------------------------- m4_define([b4_symbol_value], [m4_ifval([$2], - [$1.as<$2>()], + [$1.as< $2 >()], [$1])]) # b4_symbol_value_template(VAL, [TYPE]) @@ -71,7 +94,7 @@ b4_variant_if([ # Same as b4_symbol_value, but used in a template method. m4_define([b4_symbol_value_template], [m4_ifval([$2], - [$1.template as<$2>()], + [$1.template as< $2 >()], [$1])]) ]) # b4_variant_if @@ -107,7 +130,7 @@ m4_define([b4_lhs_location], # Return the data corresponding to the symbol #NUM, where the current # rule has RULE-LENGTH symbols on RHS. m4_define([b4_rhs_data], - [yystack_@{($1) - ($2)@}]) + [yystack_@{b4_subtract($@)@}]) # b4_rhs_state(RULE-LENGTH, NUM) @@ -134,12 +157,23 @@ m4_define([b4_rhs_location], [b4_rhs_data([$1], [$2]).location]) +# b4_symbol_(NUM, FIELD) +# ---------------------- +# Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if +# undefined. +m4_define([b4_symbol_], +[m4_indir([b4_symbol($1, $2)])]) + + # b4_symbol(NUM, FIELD) # --------------------- # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if -# undefined. +# undefined. If FIELD = id, prepend the prefix. m4_define([b4_symbol], -[m4_indir([b4_symbol($1, $2)])]) +[m4_case([$2], + [id], [m4_do([b4_percent_define_get([token.prefix])], + [b4_symbol_([$1], [id])])], + [b4_symbol_($@)])]) # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE) @@ -195,66 +229,40 @@ m4_define([b4_type_action_], ])]) -# b4_symbol_constructor_declaration_(SYMBOL-NUMBERS) -# ---------------------------------------------------- +# b4_symbol_constructor_declaration_(SYMBOL-NUMBER) +# ------------------------------------------------- # Declare the overloaded version of make_symbol for the (common) type of # these SYMBOL-NUMBERS. Use at class-level. m4_define([b4_symbol_constructor_declaration_], -[ template - static inline symbol_type - make_symbol (b4_args(b4_symbol_if([$1], [has_type], - [const b4_symbol([$1], [type])& v]), - b4_locations_if([const location_type& l]))); - -]) - -# b4_symbol_constructor_declarations -# ---------------------------------- -# Declare the overloaded versions of make_symbol for all the value types. -# Use at class-level. -m4_define([b4_symbol_constructor_declarations], -[b4_variant_if([ - // Declaration of make_symbol for each value type. -m4_map([b4_symbol_constructor_declaration_], m4_defn([b4_type_names]))])]) - - - -# b4_symbol_constructor_specialization_(SYMBOL-NUMBER) -# ---------------------------------------------------- -# Declare the specialization of make_symbol for this each SYMBOL-NUMBER. -# Specializations cannot be declared at class-level, this must be done -# at namespace-level. -m4_define([b4_symbol_constructor_specialization_], [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], -[ template <> - inline - b4_parser_class_name::symbol_type - b4_parser_class_name::make_symbol (dnl +[ static inline + symbol_type + make_[]b4_symbol_([$1], [id]) (dnl b4_args(b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]), b4_locations_if([const location_type& l]))); + ])])]) -# b4_symbol_constructor_specializations -# ------------------------------------- -# Declare specializations of make_symbol. -m4_define([b4_symbol_constructor_specializations], + +# b4_symbol_constructor_declarations +# ---------------------------------- +# Declare symbol constructors for all the value types. +# Use at class-level. +m4_define([b4_symbol_constructor_declarations], [b4_variant_if([ - // Specializations of make_symbol for each symbol type. -m4_map([b4_symbol_constructor_specialization_], - m4_defn([b4_symbol_numbers]))])dnl -]) + // Symbol constructors declarations. +m4_map([b4_symbol_constructor_declaration_], m4_defn([b4_symbol_numbers]))])]) # b4_symbol_constructor_definition_(SYMBOL-NUMBER) # ------------------------------------------------ -# Define make_symbol for this SYMBOL-NUMBER. +# Define symbol constructor for this SYMBOL-NUMBER. m4_define([b4_symbol_constructor_definition_], [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], -[ template <> - b4_parser_class_name::symbol_type - b4_parser_class_name::make_symbol (dnl +[ b4_parser_class_name::symbol_type + b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl b4_args(b4_symbol_if([$1], [has_type], [const b4_symbol([$1], [type])& v]), b4_locations_if([const location_type& l]))) @@ -267,11 +275,101 @@ b4_args(b4_symbol_if([$1], [has_type], ])])]) -# b4_symbol_constructor_declarations +# b4_symbol_constructor_definitions # ---------------------------------- # Define the overloaded versions of make_symbol for all the value types. m4_define([b4_symbol_constructor_definitions], -[b4_variant_if( +[[ // symbol_base_type. + template + ]b4_parser_class_name[::symbol_base_type::symbol_base_type () + : value()]b4_locations_if([ + , location()])[ + { + }]b4_locations_if([[ + + template + ]b4_parser_class_name[::symbol_base_type::symbol_base_type (const location_type& l) + : value() + , location(l) + { + }]])[ + + template + ]b4_parser_class_name[::symbol_base_type::symbol_base_type (]b4_args( + [const semantic_type& v], + b4_locations_if([const location_type& l]))[) + : value(v)]b4_locations_if([ + , location(l)])[ + { + } + + template + const Exact& + ]b4_parser_class_name[::symbol_base_type::self () const + { + return static_cast(*this); + } + + template + Exact& + ]b4_parser_class_name[::symbol_base_type::self () + { + return static_cast(*this); + } + + template + int + ]b4_parser_class_name[::symbol_base_type::type_get () const + { + return self ().type_get_ (); + } + + // symbol_type. + ]b4_parser_class_name[::symbol_type::symbol_type () + : super_type () + , type () + { + } + + ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( + [int t], + b4_locations_if([const location_type& l]))[) + : super_type (]b4_locations_if([l])[) + , type (t) + { + } + + ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( + [int t], + [const semantic_type& v], + b4_locations_if([const location_type& l]))[) + : super_type (v]b4_locations_if([, l])[) + , type (t) + { + } + + int + ]b4_parser_class_name[::symbol_type::type_get_ () const + { + return type; + } +]b4_lex_symbol_if([[ + ]b4_parser_class_name[::token_type + ]b4_parser_class_name[::symbol_type::token () const + { + // YYTOKNUM[NUM] -- (External) token number corresponding to the + // (internal) symbol number NUM (which must be that of a token). */ + static + const ]b4_int_type_for([b4_toknum])[ + yytoken_number_[] = + { + ]b4_toknum[ + }; + return static_cast (yytoken_number_[type]); + } +]])[ + +]b4_variant_if( [ // Implementation of make_symbol for each symbol type. m4_map([b4_symbol_constructor_definition_], m4_defn([b4_symbol_numbers]))])]) @@ -282,7 +380,7 @@ m4_map([b4_symbol_constructor_definition_], m4_defn([b4_symbol_numbers]))])]) # YYTYPE. m4_define([b4_symbol_variant], [m4_pushdef([b4_dollar_dollar], - [$2.$3<$][3>(m4_shift3($@))])dnl + [$2.$3< $][3 >(m4_shift3($@))])dnl switch ($1) { m4_map([b4_type_action_], m4_defn([b4_type_names]))[]dnl @@ -335,7 +433,7 @@ m4_map([b4_char_sizeof_], [$@])dnl # Define yytranslate_. Sometimes we want it in the header file, # sometimes the cc file suffices. m4_define([b4_yytranslate_definition], -[[ // YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. +[[ // Symbol number corresponding to token number t. ]b4_parser_class_name[::token_number_type ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type], [int])[ t) @@ -609,36 +707,11 @@ m4_ifdef([b4_stype], /// Internal symbol numbers. typedef ]b4_int_type_for([b4_translate])[ token_number_type; - /* Tables. */ - /// For a state, the index in \a yytable_ of its portion. - static const ]b4_int_type_for([b4_pact])[ yypact_[]; static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_; - - /// For a state, default rule to reduce. - /// Unless\a yytable_ specifies something else to do. - /// Zero means the default is an error. - static const ]b4_int_type_for([b4_defact])[ yydefact_[]; - - static const ]b4_int_type_for([b4_pgoto])[ yypgoto_[]; - static const ]b4_int_type_for([b4_defgoto])[ yydefgoto_[]; - - /// What to do in a state. - /// \a yytable_[yypact_[s]]: what to do in state \a s. - /// - if positive, shift that token. - /// - if negative, reduce the rule which number is the opposite. - /// - if zero, do what YYDEFACT says. - static const ]b4_int_type_for([b4_table])[ yytable_[]; static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_; - static const ]b4_int_type_for([b4_check])[ yycheck_[]; - - /// For a state, its accessing symbol. - static const ]b4_int_type_for([b4_stos])[ yystos_[]; - - /// For a rule, its LHS. - static const ]b4_int_type_for([b4_r1])[ yyr1_[]; - /// For a rule, its RHS length. - static const ]b4_int_type_for([b4_r2])[ yyr2_[]; + /* Tables. */ +]b4_tables_map([b4_table_declare])[ #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE /// For a symbol, its name in clear. @@ -649,10 +722,7 @@ m4_ifdef([b4_stype], static std::string yytnamerr_ (const char *n);])[ #if YYDEBUG - /// For each rule, its source line number. - static const ]b4_int_type_for([b4_rline])[ yyrline_[]; - /// For each scanner token number, its symbol number. - static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[]; +]b4_table_declare([rline], [b4_rline])[ /// Report on the debug stream that the rule \a r is going to be reduced. virtual void yy_reduce_print_ (int r); /// Print the state stack on the debug stream. @@ -664,7 +734,7 @@ m4_ifdef([b4_stype], #endif /// Convert a scanner token number \a t to a symbol number. - static token_number_type yytranslate_ (]b4_lex_symbol_if([token_type], [int])[ t); + static inline token_number_type yytranslate_ (]b4_lex_symbol_if([token_type], [int])[ t); /// A complete symbol, with its type. template @@ -733,6 +803,9 @@ m4_ifdef([b4_stype], /// Return the type corresponding to this state. inline int type_get_ () const; + + /// Its token. + inline token_type token () const; }; ]b4_symbol_constructor_declarations[ @@ -798,7 +871,9 @@ m4_ifdef([b4_stype], ]b4_parse_param_vars[ }; -]b4_symbol_constructor_specializations[ + +]b4_lex_symbol_if([b4_yytranslate_definition])[ +]b4_lex_symbol_if([b4_symbol_constructor_definitions])[ ]b4_namespace_close[ ]b4_percent_define_flag_if([[global_tokens_and_yystype]], @@ -948,82 +1023,7 @@ b4_percent_code_get[]dnl | Symbol types. | `---------------*/ - // symbol_base_type. - template - ]b4_parser_class_name[::symbol_base_type::symbol_base_type () - : value()]b4_locations_if([ - , location()])[ - { - }]b4_locations_if([[ - - template - ]b4_parser_class_name[::symbol_base_type::symbol_base_type (const location_type& l) - : value() - , location(l) - { - }]])[ - - template - ]b4_parser_class_name[::symbol_base_type::symbol_base_type (]b4_args( - [const semantic_type& v], - b4_locations_if([const location_type& l]))[) - : value(v)]b4_locations_if([ - , location(l)])[ - { - } - - template - const Exact& - ]b4_parser_class_name[::symbol_base_type::self () const - { - return static_cast(*this); - } - - template - Exact& - ]b4_parser_class_name[::symbol_base_type::self () - { - return static_cast(*this); - } - - template - int - ]b4_parser_class_name[::symbol_base_type::type_get () const - { - return self ().type_get_ (); - } - - // symbol_type. - ]b4_parser_class_name[::symbol_type::symbol_type () - : super_type () - , type () - { - } - - ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( - [int t], - b4_locations_if([const location_type& l]))[) - : super_type (]b4_locations_if([l])[) - , type (t) - { - } - - ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( - [int t], - [const semantic_type& v], - b4_locations_if([const location_type& l]))[) - : super_type (v]b4_locations_if([, l])[) - , type (t) - { - } - - int - ]b4_parser_class_name[::symbol_type::type_get_ () const - { - return type; - } - -]b4_symbol_constructor_definitions[ +]b4_lex_symbol_if([], [b4_symbol_constructor_definitions])[ // stack_symbol_type. ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type () @@ -1530,46 +1530,11 @@ b4_error_verbose_if([int yystate, int yytoken], } - /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing - STATE-NUM. */ const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) b4_parser_class_name::yypact_ninf_ = b4_pact_ninf[; - ]b4_table_define([pact], [b4_pact])[; - - /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE - doesn't specify something else to do. Zero means the default is an - error. */ - ]b4_table_define([defact], [b4_defact])[; - - /* YYPGOTO[NTERM-NUM]. */ - ]b4_table_define([pgoto], [b4_pgoto])[; - - /* YYDEFGOTO[NTERM-NUM]. */ - ]b4_table_define([defgoto], [b4_defgoto])[; - /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If - positive, shift that token. If negative, reduce the rule which - number is the opposite. If zero, do what YYDEFACT says. */ const ]b4_int_type(b4_table_ninf, b4_table_ninf) b4_parser_class_name::yytable_ninf_ = b4_table_ninf[; - ]b4_table_define([table], [b4_table])[; - - /* YYCHECK. */ - ]b4_table_define([check], [b4_check])[; - - /* STOS_[STATE-NUM] -- The (internal number of the) accessing - symbol of state STATE-NUM. */ - ]b4_table_define([stos], [b4_stos])[; - -#if YYDEBUG - /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding - to YYLEX-NUM. */ - ]b4_table_define([token_number], [b4_toknum])[; -#endif - - /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ - ]b4_table_define([r1], [b4_r1])[; - /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ - ]b4_table_define([r2], [b4_r2])[; +]b4_tables_map([b4_table_define])[ #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. @@ -1582,8 +1547,8 @@ b4_error_verbose_if([int yystate, int yytoken], #endif #if YYDEBUG - /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ - ]b4_table_define([rline], [b4_rline])[; +]b4_table_define([rline], [b4_rline], + [YYRLINE[YYN] -- Source line where rule number YYN was defined.])[ // Print the state stack on the debug stream. void @@ -1614,7 +1579,7 @@ b4_error_verbose_if([int yystate, int yytoken], } #endif // YYDEBUG -]b4_yytranslate_definition[ +]b4_lex_symbol_if([], [b4_yytranslate_definition])[ ]b4_namespace_close[ ]b4_epilogue[]dnl