bison-patches
[Top][All Lists]
Advanced

[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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]