From b7045aa7067504d697ad8a0d3844b2b2823ff5fa Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 26 Mar 2020 08:35:50 +0100 Subject: [PATCH] java: make yysyntaxErrorArguments a private detail * data/skeletons/lalr1.java (yysyntaxErrorArguments): Move it from the context, to the parser object. Generate only for detailed and verbose error messages. * tests/local.at (AT_YYERROR_DEFINE(java)): Use yyexpectedTokens instead. --- data/skeletons/lalr1.java | 81 ++++++++++++++++++++------------------- tests/local.at | 27 ++++++++----- 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java index c5cbd1be..195fed1a 100644 --- a/data/skeletons/lalr1.java +++ b/data/skeletons/lalr1.java @@ -933,46 +933,49 @@ b4_dollar_popdef[]dnl { return ]b4_parser_class[.yysymbolName (yysymbol); } - - int yysyntaxErrorArguments (int[] yyarg, int yyargn) - { - /* There are many possibilities here to consider: - - If this state is a consistent state with a default action, - then the only way this function was invoked is if the - default action is an error action. In that case, don't - check for expected tokens because there are none. - - The only way there can be no lookahead present (in tok) is - if this state is a consistent state with a default action. - Thus, detecting the absence of a lookahead is sufficient to - determine that there is no unexpected or expected token to - report. In that case, just report a simple "syntax error". - - Don't assume there isn't a lookahead just because this - state is a consistent state with a default action. There - might have been a previous inconsistent state, consistent - state with a non-default action, or user semantic action - that manipulated yychar. (However, yychar is currently out - of scope during semantic actions.) - - Of course, the expected token list depends on states to - have correct lookahead information, and it depends on the - parser not to perform extra reductions after fetching a - lookahead from the scanner and before detecting a syntax - error. Thus, state merging (from LALR or IELR) and default - reductions corrupt the expected token list. However, the - list is correct for canonical LR with one exception: it - will still contain any token that will not be accepted due - to an error action in a later state. - */ - int yycount = 0; - if (this.yytoken != yyempty_) - { - yyarg[yycount++] = this.yytoken; - yycount += this.yyexpectedTokens (yyarg, 1, yyargn); - } - return yycount; - } } - /** +]b4_parse_error_bmatch( +[detailed\|verbose], [[ + private int yysyntaxErrorArguments (Context yyctx, int[] yyarg, int yyargn) + { + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, + then the only way this function was invoked is if the + default action is an error action. In that case, don't + check for expected tokens because there are none. + - The only way there can be no lookahead present (in tok) is + if this state is a consistent state with a default action. + Thus, detecting the absence of a lookahead is sufficient to + determine that there is no unexpected or expected token to + report. In that case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this + state is a consistent state with a default action. There + might have been a previous inconsistent state, consistent + state with a non-default action, or user semantic action + that manipulated yychar. (However, yychar is currently out + of scope during semantic actions.) + - Of course, the expected token list depends on states to + have correct lookahead information, and it depends on the + parser not to perform extra reductions after fetching a + lookahead from the scanner and before detecting a syntax + error. Thus, state merging (from LALR or IELR) and default + reductions corrupt the expected token list. However, the + list is correct for canonical LR with one exception: it + will still contain any token that will not be accepted due + to an error action in a later state. + */ + int yycount = 0; + if (yyctx.getToken () != yyempty_) + { + yyarg[yycount++] = yyctx.getToken (); + yycount += yyctx.yyexpectedTokens (yyarg, 1, yyargn); + } + return yycount; + } +]])[ + +/** * Report a syntax error. */ private void yyreportSyntaxError (Context yyctx) @@ -984,7 +987,7 @@ b4_dollar_popdef[]dnl { final int argmax = 5; int[] yyarg = new int[argmax]; - int yycount = yyctx.yysyntaxErrorArguments (yyarg, argmax); + int yycount = yysyntaxErrorArguments (yyctx, yyarg, argmax); String[] yystr = new String[yycount]; for (int yyi = 0; yyi < yycount; ++yyi) yystr[yyi] = yysymbolName (yyarg[yyi]); diff --git a/tests/local.at b/tests/local.at index 5cc51e7e..fdb715c9 100644 --- a/tests/local.at +++ b/tests/local.at @@ -972,17 +972,24 @@ m4_define([AT_YYERROR_DEFINE(java)], ]AT_ERROR_CUSTOM_IF([[ public void yyreportSyntaxError (Calc.Context ctx) { - int[] arg = new int[ctx.NTOKENS]; - int n = ctx.yysyntaxErrorArguments (arg, ctx.NTOKENS); System.err.print (]AT_LOCATION_IF([[ctx.getLocation () + ": "]] - + )["syntax error on token @<:@" + ctx.yysymbolName (arg[0]) + "@:>@"); - if (1 < n) - { - System.err.print (" (expected:"); - for (int i = 1; i < n; ++i) - System.err.print (" @<:@" + ctx.yysymbolName (arg[i]) + "@:>@"); - System.err.print (")"); - } + + )["syntax error"); + { + int token = ctx.getToken (); + if (token != ctx.EMPTY) + System.err.print (" on token @<:@" + ctx.yysymbolName (token) + "@:>@"); + } + { + int[] arg = new int[ctx.NTOKENS]; + int n = ctx.yyexpectedTokens (arg, ctx.NTOKENS); + if (0 < n) + { + System.err.print (" (expected:"); + for (int i = 0; i < n; ++i) + System.err.print (" @<:@" + ctx.yysymbolName (arg[i]) + "@:>@"); + System.err.print (")"); + } + } System.err.println (""); } ]])