[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 06/13] java: examples: fix the tracking of locations
From: |
Akim Demaille |
Subject: |
[PATCH 06/13] java: examples: fix the tracking of locations |
Date: |
Wed, 5 Feb 2020 18:04:58 +0100 |
* 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.
---
examples/java/calc/Calc.test | 7 ++++++-
examples/java/calc/Calc.y | 15 ++++++++++++---
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/examples/java/calc/Calc.test b/examples/java/calc/Calc.test
index b0d238f1..8664e314 100644
--- a/examples/java/calc/Calc.test
+++ b/examples/java/calc/Calc.test
@@ -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"
diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y
index 7a682860..a8d09244 100644
--- a/examples/java/calc/Calc.y
+++ b/examples/java/calc/Calc.y
@@ -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;
+ }
}
--
2.25.0
- [PATCH 00/13] Clean up the Java skeleton and tests, Akim Demaille, 2020/02/05
- [PATCH 03/13] traces: don't print the stack before the gotos, Akim Demaille, 2020/02/05
- [PATCH 01/13] traces: write the "Reading a token" alone on its line, Akim Demaille, 2020/02/05
- [PATCH 02/13] traces: show the stack after reading a token, Akim Demaille, 2020/02/05
- [PATCH 05/13] java: examples: prefer switch to chains of else-if, Akim Demaille, 2020/02/05
- [PATCH 04/13] java: examples: split in two, Akim Demaille, 2020/02/05
- [PATCH 06/13] java: examples: fix the tracking of locations,
Akim Demaille <=
- [PATCH 07/13] java: style: prefer putting the square brackets on the type, Akim Demaille, 2020/02/05
- [PATCH 08/13] java: style: avoid useless initializers, Akim Demaille, 2020/02/05
- [PATCH 09/13] java: tests: prepare the replacement of calculator tests, Akim Demaille, 2020/02/05
- [PATCH 10/13] java: tests: check location tracking in the calculator, Akim Demaille, 2020/02/05
- [PATCH 12/13] java: tests: check push parsers, Akim Demaille, 2020/02/05
- [PATCH 11/13] java: tests: remove now redundant tests, Akim Demaille, 2020/02/05
- [PATCH 13/13] java: tests: remove now redundant tests, Akim Demaille, 2020/02/05