java: don't expose the Context's members

* data/skeletons/lalr1.java (Context): Make data members private.
(Context.getLocation): New.
* examples/java/calc/Calc.y, tests/java.at, tests/local.at: Adjust.
This commit is contained in:
Akim Demaille
2020-02-11 08:14:03 +01:00
parent 8637f2c7d6
commit 126252333d
4 changed files with 19 additions and 12 deletions

View File

@@ -339,8 +339,7 @@ import java.text.MessageFormat;
public int size = 16;
public int height = -1;
public final void push (int state, ]b4_yystype[ value]dnl
b4_locations_if([, ]b4_location_type[ loc])[) {
public final void push (int state, ]b4_yystype[ value]b4_locations_if([, ]b4_location_type[ loc])[) {
height++;
if (size == height)
{
@@ -860,12 +859,20 @@ b4_dollar_popdef[]dnl
}
]])[
/**
* Information needed to get the list of expected tokens and to forge
* a syntax error diagnostic.
*/
public static final class Context
{
public YYStack yystack;
public int yytoken;]b4_locations_if([[
public ]b4_location_type[ yylocation;]])[
public static final int yyntokens = ]b4_parser_class[.yyntokens_;
private YYStack yystack;
private int yytoken;]b4_locations_if([[
public ]b4_location_type[ getLocation ()
{
return yylocation;
}
private ]b4_location_type[ yylocation;]])[
static final int yyntokens = ]b4_parser_class[.yyntokens_;
/* Put in YYARG at most YYARGN of the expected tokens given the
current YYCTX, and return the number of tokens stored in YYARG. If

View File

@@ -111,7 +111,7 @@ class CalcLexer implements Calc.Lexer {
final int ARGMAX = 10;
int[] arg = new int[ARGMAX];
int n = ctx.yysyntaxErrorArguments (arg, ARGMAX);
System.err.print (ctx.yylocation + ": syntax error");
System.err.print (ctx.getLocation () + ": syntax error");
for (int i = 1; i < n; ++i)
System.err.print ((i == 1 ? ": expected " : " or ")
+ ctx.yysymbolName (arg[i]));

View File

@@ -478,8 +478,8 @@ AT_CHECK_JAVA_MINIMAL([[
%define api.position.type {MyPos}
%code { class MyPos {} }]], [[$$ = $<java.awt.Color>1;]], [[MyPos]])
AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep -w 'Position']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep -w 'Location']], [1], [ignore])
AT_CHECK_JAVA_MINIMAL_W_LEXER([[
%define api.value.type {java.awt.Color}
@@ -490,8 +490,8 @@ AT_CHECK_JAVA_MINIMAL_W_LEXER([[
[[$$ = $<java.awt.Color>1;]],
[[java.awt.Color]], [[MyPos]], [[MyLoc]])
AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep -w 'Position']], [1], [ignore])
AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep -w 'Location']], [1], [ignore])
AT_CLEANUP

View File

@@ -935,7 +935,7 @@ m4_define([AT_YYERROR_DEFINE(java)],
{
int[] arg = new int[ctx.yyntokens];
int n = ctx.yysyntaxErrorArguments (arg, ctx.yyntokens);
System.err.print (]AT_LOCATION_IF([[ctx.yylocation + ": "]]
System.err.print (]AT_LOCATION_IF([[ctx.getLocation () + ": "]]
+ )["syntax error on token @<:@" + ctx.yysymbolName (arg[0]) + "@:>@");
if (1 < n)
{