java: add support for parse.error custom

* data/skeletons/lalr1.java: Add support for custom parse errors.
(yyntokens_): Make it public.  Under...
(yyntokens): this name.
(Context): Capture the location too.
* examples/c/bistromathic/parse.y,
* examples/c/bistromathic/bistromathic.test:
Improve error message.
* examples/java/calc/Calc.test, examples/java/calc/Calc.y: Use custom
error messages.
* tests/calc.at, tests/local.at: Check custom error messages.
This commit is contained in:
Akim Demaille
2020-02-08 14:19:03 +01:00
parent 0c90c59795
commit ef097719ea
8 changed files with 95 additions and 29 deletions

View File

@@ -1185,8 +1185,11 @@ m4_define([AT_CHECK_CALC_LALR1_JAVA],
[AT_CHECK_CALC([%language "Java" $1], [$2])])
AT_CHECK_CALC_LALR1_JAVA
AT_CHECK_CALC_LALR1_JAVA([%define parse.error custom])
AT_CHECK_CALC_LALR1_JAVA([%define parse.error detailed])
AT_CHECK_CALC_LALR1_JAVA([%define parse.error verbose])
AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error custom])
AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error detailed])
AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error verbose])
AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error verbose])
AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error verbose %locations %lex-param {InputStream is}])

View File

@@ -908,6 +908,9 @@ class PositionReader extends BufferedReader {
}]])
# AT_YYERROR_DEFINE(java)
# -----------------------
# FIXME: We should not hard-code "Calc".
m4_define([AT_YYERROR_DEFINE(java)],
[AT_LOCATION_IF([[public void yyerror (Calc.Location l, String m)
{]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
@@ -925,6 +928,24 @@ m4_define([AT_YYERROR_DEFINE(java)],
++*nerrs;]])[
System.err.println (m);
}
]])[
]AT_ERROR_CUSTOM_IF([[
public void yyreportSyntaxError (Calc.Context ctx)
{
int[] arg = new int[ctx.yyntokens];
int n = Calc.yysyntaxErrorArguments (ctx, arg, ctx.yyntokens);
System.err.print (]AT_LOCATION_IF([[ctx.yylocation + ": "]]
+ )["syntax error on token @<:@" + Calc.yysymbolName(arg[0]) + "@:>@");
if (1 < n)
{
System.err.print (" (expected:");
for (int i = 1; i < n; ++i)
System.err.print (" @<:@" + Calc.yysymbolName (arg[i]) + "@:>@");
System.err.print (")");
}
System.err.println ("");
}
]])
])
@@ -962,6 +983,8 @@ m4_define([AT_MAIN_DEFINE(java)],
m4_define([AT_LANG_FOR_EACH_STD(java)],
[$1])
## --------------- ##
## Running Bison. ##
## --------------- ##