diff options
author | Akim Demaille <akim.demaille@gmail.com> | 2020-02-06 08:25:44 +0100 |
---|---|---|
committer | Akim Demaille <akim.demaille@gmail.com> | 2020-02-08 11:24:53 +0100 |
commit | 52db24b2bc46ca1cc9af2ce1cb15b00bdab99d90 (patch) | |
tree | 5d449f81f3cb1b6002250185639691cda6e8e881 /data | |
parent | 650b253843727980af2a14e3bc2b667e62d7d0d5 (diff) | |
download | bison-52db24b2bc46ca1cc9af2ce1cb15b00bdab99d90.tar.gz |
java: add support for parse.error=detailed
In Java there is no need for N_ and yytranslate_. So instead of
hard-coding the use of N_ in the table of the symbol names, rely on
b4_symbol_translate.
* src/output.c (prepare_symbol_names): Use b4_symbol_translate instead
of N_.
* data/skeletons/c.m4 (b4_symbol_translate): New.
* data/skeletons/lalr1.java (yysymbolName): New.
Use it.
* examples/java/calc/Calc.y: Use parse.error=detailed.
* tests/calc.at: Check parse.error=detailed.
Diffstat (limited to 'data')
-rw-r--r-- | data/skeletons/c.m4 | 7 | ||||
-rw-r--r-- | data/skeletons/java.m4 | 7 | ||||
-rw-r--r-- | data/skeletons/lalr1.java | 96 |
3 files changed, 71 insertions, 39 deletions
diff --git a/data/skeletons/c.m4 b/data/skeletons/c.m4 index c98bed1a..31b22300 100644 --- a/data/skeletons/c.m4 +++ b/data/skeletons/c.m4 @@ -470,6 +470,13 @@ m4_define([b4_token_enums_defines], [b4_token_enums[]b4_yacc_if([b4_token_defines])]) +# b4_symbol_translate(STRING) +# --------------------------- +m4_define([b4_symbol_translate], +[[N_($1)]]) + + + ## ----------------- ## ## Semantic Values. ## ## ----------------- ## diff --git a/data/skeletons/java.m4 b/data/skeletons/java.m4 index b36ee3d2..5c015640 100644 --- a/data/skeletons/java.m4 +++ b/data/skeletons/java.m4 @@ -220,6 +220,13 @@ m4_define([b4_position_type], [b4_percent_define_get([[api.position.type]])]) ## ----------------- ## +# b4_symbol_translate(STRING) +# --------------------------- +m4_define([b4_symbol_translate], +[[_($1)]]) + + + # b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG]) # ---------------------------------------------- # See README. diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java index 24b50795..1005fafb 100644 --- a/data/skeletons/lalr1.java +++ b/data/skeletons/lalr1.java @@ -488,42 +488,6 @@ m4_define([b4_define_state],[[ return YYNEWSTATE; } -]b4_error_verbose_if([[ - /* Return YYSTR after stripping away unnecessary quotes and - backslashes, so that it's suitable for yyerror. The heuristic is - that double-quoting is unnecessary unless the string contains an - apostrophe, a comma, or backslash (other than backslash-backslash). - YYSTR is taken from yytname. */ - private final String yytnamerr_ (String yystr) - { - if (yystr.charAt (0) == '"') - { - StringBuffer yyr = new StringBuffer (); - strip_quotes: for (int i = 1; i < yystr.length (); i++) - switch (yystr.charAt (i)) - { - case '\'': - case ',': - break strip_quotes; - - case '\\': - if (yystr.charAt(++i) != '\\') - break strip_quotes; - /* Fall through. */ - default: - yyr.append (yystr.charAt (i)); - break; - - case '"': - return yyr.toString (); - } - } - else if (yystr.equals ("$end")) - return "end of input"; - - return yystr; - } -]])[ ]b4_parse_trace_if([[ /*--------------------------------. | Print this symbol on YYOUTPUT. | @@ -534,7 +498,7 @@ m4_define([b4_define_state],[[ b4_locations_if([, Object yylocationp])[) { yycdebug (s + (yytype < yyntokens_ ? " token " : " nterm ") - + yytname_[yytype] + " ("]b4_locations_if([ + + yysymbolName (yytype) + " ("]b4_locations_if([ + yylocationp + ": "])[ + (yyvaluep == null ? "(null)" : yyvaluep.toString ()) + ")"); }]])[ @@ -924,7 +888,7 @@ b4_dollar_popdef[]dnl with internationalization. */ StringBuffer res = new StringBuffer ("syntax error, unexpected "); - res.append (yytnamerr_ (yytname_[tok])); + res.append (yysymbolName (tok)); int yyn = yypact_[yystate]; if (!yyPactValueIsDefault (yyn)) { @@ -949,7 +913,7 @@ b4_dollar_popdef[]dnl && !yyTableValueIsError (yytable_[x + yyn])) { res.append (count++ == 0 ? ", expecting " : " or "); - res.append (yytnamerr_ (yytname_[x])); + res.append (yysymbolName (x)); } } } @@ -984,10 +948,64 @@ b4_dollar_popdef[]dnl ]b4_parser_tables_define[ +]m4_bmatch(b4_percent_define_get([[parse.error]]), + [simple\|verbose], +[[ /* Return YYSTR after stripping away unnecessary quotes and + backslashes, so that it's suitable for yyerror. The heuristic is + that double-quoting is unnecessary unless the string contains an + apostrophe, a comma, or backslash (other than backslash-backslash). + YYSTR is taken from yytname. */ + private static String yytnamerr_ (String yystr) + { + if (yystr.charAt (0) == '"') + { + StringBuffer yyr = new StringBuffer (); + strip_quotes: for (int i = 1; i < yystr.length (); i++) + switch (yystr.charAt (i)) + { + case '\'': + case ',': + break strip_quotes; + + case '\\': + if (yystr.charAt(++i) != '\\') + break strip_quotes; + /* Fall through. */ + default: + yyr.append (yystr.charAt (i)); + break; + + case '"': + return yyr.toString (); + } + } + else if (yystr.equals ("$end")) + return "end of input"; + + return yystr; + } + /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. First, the terminals, then, starting at \a yyntokens_, nonterminals. */ ]b4_typed_parser_table_define([String], [tname], [b4_tname])[ + static String yysymbolName (int yysymbol) + { + return yytnamerr_ (yytname_[yysymbol]); + } +]], + [custom\|detailed], +[[/* The user-facing name of the symbol whose (internal) number is + YYSYMBOL. No bounds checking. */ +static String yysymbolName (int yysymbol) +{ + String[] yy_sname = + { + ]b4_symbol_names[ + }; + return yy_sname[yysymbol]; +}]])[ + ]b4_parse_trace_if([[ ]b4_integral_parser_table_define([rline], [b4_rline], [[YYRLINE[YYN] -- Source line where rule number YYN was defined.]])[ |