mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
java: examples: fix the tracking of locations
* examples/java/calc/Calc.y: The StreamTokenizer cannot "peek" for the next character, it reads it, and keeps it for the next call. So the current location is one passed the end of the current token. To avoid this, keep the previous position, and use it to end the current token. * examples/java/calc/Calc.test: Adjust.
This commit is contained in:
@@ -30,4 +30,9 @@ run 0 '7
|
||||
cat >input <<EOF
|
||||
1 + 2 * * 3
|
||||
EOF
|
||||
run 0 "err: 1.8-1.9: syntax error, unexpected '*', expecting number or '-' or '(' or '!'"
|
||||
run 0 "err: 1.9-1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!'"
|
||||
|
||||
cat >input <<EOF
|
||||
12 222
|
||||
EOF
|
||||
run 0 "err: 1.6-1.9: syntax error, unexpected number"
|
||||
|
||||
@@ -115,7 +115,7 @@ class CalcLexer implements Calc.Lexer {
|
||||
}
|
||||
|
||||
public int yylex () throws IOException {
|
||||
start.set (end);
|
||||
start.set (reader.getPosition ());
|
||||
int ttype = st.nextToken ();
|
||||
end.set (reader.getPosition ());
|
||||
switch (ttype)
|
||||
@@ -128,6 +128,7 @@ class CalcLexer implements Calc.Lexer {
|
||||
return (int) '\n';
|
||||
case StreamTokenizer.TT_WORD:
|
||||
yylval = new Integer (st.sval);
|
||||
end.set (reader.getPreviousPosition ());
|
||||
return NUM;
|
||||
case ' ': case '\t':
|
||||
return yylex ();
|
||||
@@ -140,12 +141,12 @@ class CalcLexer implements Calc.Lexer {
|
||||
|
||||
class Position {
|
||||
public int line = 1;
|
||||
public int column = 0;
|
||||
public int column = 1;
|
||||
|
||||
public Position ()
|
||||
{
|
||||
line = 1;
|
||||
column = 0;
|
||||
column = 1;
|
||||
}
|
||||
|
||||
public Position (int l, int t)
|
||||
@@ -184,12 +185,16 @@ class Position {
|
||||
class PositionReader extends BufferedReader {
|
||||
|
||||
private Position position = new Position ();
|
||||
// Position before the latest call to "read", i.e. position
|
||||
// of the last character of the current token.
|
||||
private Position previousPosition = new Position ();
|
||||
|
||||
public PositionReader (Reader reader) {
|
||||
super (reader);
|
||||
}
|
||||
|
||||
public int read () throws IOException {
|
||||
previousPosition.set (position);
|
||||
int res = super.read ();
|
||||
if (res > -1) {
|
||||
char c = (char)res;
|
||||
@@ -206,4 +211,8 @@ class PositionReader extends BufferedReader {
|
||||
public Position getPosition () {
|
||||
return position;
|
||||
}
|
||||
|
||||
public Position getPreviousPosition () {
|
||||
return previousPosition;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user