From 163a35d6ddf54fd3ba86e387eb3030d95e077719 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 11 Feb 2020 20:42:05 +0100 Subject: [PATCH] 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. --- doc/bison.texi | 9 ++++++--- examples/java/calc/Calc.y | 10 ++++++++-- tests/calc.at | 4 ++-- tests/local.at | 6 ++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/doc/bison.texi b/doc/bison.texi index 678101a3..3bf1018d 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -12889,9 +12889,12 @@ Default is @code{java.io.IOException}. @deftypemethod {Lexer} {Position} getStartPos () @deftypemethodx {Lexer} {Position} getEndPos () -Return respectively the first position of the last token that -@code{yylex} returned, and the first position beyond it. These -methods are not needed unless location tracking is active. +Return respectively the first position of the last token that @code{yylex} +returned, and the first position beyond it. These methods are not needed +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 @{@var{class-name}@}}. diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y index df86fb76..f634937d 100644 --- a/examples/java/calc/Calc.y +++ b/examples/java/calc/Calc.y @@ -99,11 +99,11 @@ class CalcLexer implements Calc.Lexer { Position end = new Position (1, 0); public Position getStartPos () { - return start; + return new Position (start); } public Position getEndPos () { - return end; + return new Position (end); } public void yyreportSyntaxError (Calc.Context ctx) @@ -175,6 +175,12 @@ class Position { column = t; } + public Position (Position p) + { + line = p.line; + column = p.column; + } + public void set (Position p) { line = p.line; diff --git a/tests/calc.at b/tests/calc.at index 5c443d03..0fd8c266 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -352,11 +352,11 @@ m4_define([AT_CALC_YYLEX(java)], Position end = new Position (1, 0); public Position getStartPos () { - return start; + return new Position (start); } public Position getEndPos () { - return end; + return new Position (end); } ]])[ diff --git a/tests/local.at b/tests/local.at index 94c8853b..0dffe4cc 100644 --- a/tests/local.at +++ b/tests/local.at @@ -847,6 +847,12 @@ m4_define([AT_JAVA_POSITION_DEFINE], column = t; } + public Position (Position p) + { + line = p.line; + column = p.column; + } + public void set (Position p) { line = p.line;