java: beware not to alias the locations of the various symbols

* examples/java/calc/Calc.y, tests/calc.at, tests/local.at
(getStartPos, getEndPos): Always return a new object.
* doc/bison.texi: Clarify this.
This commit is contained in:
Akim Demaille
2020-02-11 20:42:05 +01:00
parent cdb42f7730
commit 163a35d6dd
4 changed files with 22 additions and 7 deletions

View File

@@ -12889,9 +12889,12 @@ Default is @code{java.io.IOException}.
@deftypemethod {Lexer} {Position} getStartPos () @deftypemethod {Lexer} {Position} getStartPos ()
@deftypemethodx {Lexer} {Position} getEndPos () @deftypemethodx {Lexer} {Position} getEndPos ()
Return respectively the first position of the last token that Return respectively the first position of the last token that @code{yylex}
@code{yylex} returned, and the first position beyond it. These returned, and the first position beyond it. These methods are not needed
methods are not needed unless location tracking is active. unless location tracking is active.
They should return new objects for each call, to avoid that all the symbol
share the same Position boundaries.
The return type can be changed using @code{%define api.position.type The return type can be changed using @code{%define api.position.type
@{@var{class-name}@}}. @{@var{class-name}@}}.

View File

@@ -99,11 +99,11 @@ class CalcLexer implements Calc.Lexer {
Position end = new Position (1, 0); Position end = new Position (1, 0);
public Position getStartPos () { public Position getStartPos () {
return start; return new Position (start);
} }
public Position getEndPos () { public Position getEndPos () {
return end; return new Position (end);
} }
public void yyreportSyntaxError (Calc.Context ctx) public void yyreportSyntaxError (Calc.Context ctx)
@@ -175,6 +175,12 @@ class Position {
column = t; column = t;
} }
public Position (Position p)
{
line = p.line;
column = p.column;
}
public void set (Position p) public void set (Position p)
{ {
line = p.line; line = p.line;

View File

@@ -352,11 +352,11 @@ m4_define([AT_CALC_YYLEX(java)],
Position end = new Position (1, 0); Position end = new Position (1, 0);
public Position getStartPos () { public Position getStartPos () {
return start; return new Position (start);
} }
public Position getEndPos () { public Position getEndPos () {
return end; return new Position (end);
} }
]])[ ]])[

View File

@@ -847,6 +847,12 @@ m4_define([AT_JAVA_POSITION_DEFINE],
column = t; column = t;
} }
public Position (Position p)
{
line = p.line;
column = p.column;
}
public void set (Position p) public void set (Position p)
{ {
line = p.line; line = p.line;