From 9b4f0970feba29cc3e4b04a313dd5c8ee743873c Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 30 Nov 2019 17:53:11 +0100 Subject: [PATCH] d, java: improve yytranslate and neighbors * data/skeletons/lalr1.d, data/skeletons/lalr1.java: Don't expose yyuser_token_number_max_ and yyundef_token_. Do as in C++: scope them into yytranslate_, and only when api.token.raw is not defined. (yyterror_): Rename as... (yy_error_token_): this. * data/skeletons/lalr1.d (token_number_type): New. Use it. Can't be done in the Java backend, as Java does not have type aliases. --- data/skeletons/lalr1.d | 27 +++++++++++++++------------ data/skeletons/lalr1.java | 21 +++++++++++---------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index e102e1b3..22f4899f 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -619,8 +619,8 @@ m4_popdef([b4_at_dollar])])dnl yyn = yypact_[yystate]; if (!yy_pact_value_is_default_ (yyn)) { - yyn += yyterror_; - if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) + yyn += yy_error_token_; + if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yy_error_token_) { yyn = yytable_[yyn]; if (0 < yyn) @@ -720,14 +720,14 @@ m4_popdef([b4_at_dollar])])dnl int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_; int count = 0; for (int x = yyxbegin; x < yyxend; ++x) - if (yycheck_[x + yyn] == x && x != yyterror_ + if (yycheck_[x + yyn] == x && x != yy_error_token_ && !yy_table_value_is_error_ (yytable_[x + yyn])) ++count; if (count < 5) { count = 0; for (int x = yyxbegin; x < yyxend; ++x) - if (yycheck_[x + yyn] == x && x != yyterror_ + if (yycheck_[x + yyn] == x && x != yy_error_token_ && !yy_table_value_is_error_ (yytable_[x + yyn])) { res ~= count++ == 0 ? ", expecting " : " or "; @@ -806,35 +806,38 @@ m4_popdef([b4_at_dollar])])dnl } ]])[ - private static ]b4_int_type_for([b4_translate])[ yytranslate_ (int t) + private static token_number_type yytranslate_ (int t) { ]b4_api_token_raw_if( [[ import std.conv : to; return to!byte (t);]], [[ /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ - immutable ]b4_int_type_for([b4_translate])[[] translate_table = + immutable token_number_type[] translate_table = @{ ]b4_translate[ @}; + immutable int user_token_number_max_ = ]b4_user_token_number_max[; + immutable token_number_type undef_token_ = ]b4_undef_token_number[; + if (t <= 0) return YYTokenType.EOF; - else if (t <= yyuser_token_number_max_) + else if (t <= user_token_number_max_) return translate_table[t]; else - return yyundef_token_;]])[ + return undef_token_;]])[ } + alias ]b4_int_type_for([b4_translate])[ token_number_type; + + private static immutable token_number_type yy_error_token_ = 1; + private static immutable int yylast_ = ]b4_last[; private static immutable int yynnts_ = ]b4_nterms_number[; private static immutable int yyempty_ = -2; private static immutable int yyfinal_ = ]b4_final_state_number[; - private static immutable int yyterror_ = 1; private static immutable int yyntokens_ = ]b4_tokens_number[; - private static immutable int yyuser_token_number_max_ = ]b4_user_token_number_max[; - private static immutable int yyundef_token_ = ]b4_undef_token_number[; - private final struct YYStackElement { int state; ]b4_yystype[ value;]b4_locations_if( diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java index 86a229a8..9ec0806b 100644 --- a/data/skeletons/lalr1.java +++ b/data/skeletons/lalr1.java @@ -751,8 +751,8 @@ b4_dollar_popdef[]dnl yyn = yypact_[yystate]; if (!yyPactValueIsDefault (yyn)) { - yyn += yyterror_; - if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) + yyn += yy_error_token_; + if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yy_error_token_) { yyn = yytable_[yyn]; if (0 < yyn) @@ -931,14 +931,14 @@ b4_dollar_popdef[]dnl int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_; int count = 0; for (int x = yyxbegin; x < yyxend; ++x) - if (yycheck_[x + yyn] == x && x != yyterror_ + if (yycheck_[x + yyn] == x && x != yy_error_token_ && !yyTableValueIsError (yytable_[x + yyn])) ++count; if (count < 5) { count = 0; for (int x = yyxbegin; x < yyxend; ++x) - if (yycheck_[x + yyn] == x && x != yyterror_ + if (yycheck_[x + yyn] == x && x != yy_error_token_ && !yyTableValueIsError (yytable_[x + yyn])) { res.append (count++ == 0 ? ", expecting " : " or "); @@ -1015,26 +1015,27 @@ b4_dollar_popdef[]dnl } ]], [[ { + int user_token_number_max_ = ]b4_user_token_number_max[; + ]b4_int_type_for([b4_translate])[ undef_token_ = ]b4_undef_token_number[; + if (t <= 0) return Lexer.EOF; - else if (t <= yyuser_token_number_max_) + else if (t <= user_token_number_max_) return yytranslate_table_[t]; else - return yyundef_token_; + return undef_token_; } ]b4_integral_parser_table_define([translate_table], [b4_translate])[ ]])[ + private static final ]b4_int_type_for([b4_translate])[ yy_error_token_ = 1; + private static final int yylast_ = ]b4_last[; private static final int yynnts_ = ]b4_nterms_number[; private static final int yyempty_ = -2; private static final int yyfinal_ = ]b4_final_state_number[; - private static final int yyterror_ = 1; private static final int yyntokens_ = ]b4_tokens_number[; - private static final int yyuser_token_number_max_ = ]b4_user_token_number_max[; - private static final int yyundef_token_ = ]b4_undef_token_number[; - /* User implementation code. */ ]b4_percent_code_get[]dnl