2003/06/03 08:56:56
[org.ibex.core.git] / src / org / xwt / js / Lexer.java
index e8cd2d4..3a80f5a 100644 (file)
@@ -29,21 +29,18 @@ class Lexer {
            System.out.println(codeToString[tok]);
     }
 
-    private SmartReader in;
-    private boolean pushedBack = false;
-
     public int op;
-    public int twoBack;
+    public Number number = null;
+    public String string = null;
+
     public int line = 0;
     public int col = 0;
+
+    private SmartReader in;
     public String sourceName = "unknown";
-    public Number number;
-    public String string;
 
     public Lexer(Reader r) throws IOException { in = new SmartReader(r); }
-    public int peekToken() throws IOException { int ret = getToken(); pushBackToken(); return ret; }
-    public void pushBackToken() { pushBackToken(op); }
-    public void pushBackToken(int i) { if (pushedBack) throw new Error("can't push back twice"); pushedBack = true; op = i; }
+
 
     // Token Constants //////////////////////////////////////////////////////////
 
@@ -389,16 +386,6 @@ class Lexer {
         return STRING;
     }
 
-    public int getToken() throws IOException {
-       if (pushedBack) { pushedBack = false; return op; }
-       do {
-           if (op != EOL) twoBack = op;
-           op = _getToken();
-           if (op == EOL) { line++; col = 0; }
-       } while (op == EOL);
-       return op;
-    }
-
     public int _getToken() throws IOException {
         int c;
         do { if ((c = in.read()) == '\n') break; } while (isWhiteSpace(c) || c == '\n');
@@ -488,4 +475,52 @@ class Lexer {
         }
     }
 
+
+    // PushBack Stuff ////////////////////////////////////////////////////////////
+
+    int pushBackDepth = 0;
+    int[] pushBackInts = new int[10];
+    Object[] pushBackObjects = new Object[10];
+
+    public void pushBackToken(int op, Object obj) {
+       if (pushBackDepth >= pushBackInts.length - 1) {
+           int[] newInts = new int[pushBackInts.length * 2];
+           System.arraycopy(pushBackInts, 0, newInts, 0, pushBackInts.length);
+           pushBackInts = newInts;
+           Object[] newObjects = new Object[pushBackObjects.length * 2];
+           System.arraycopy(pushBackObjects, 0, newObjects, 0, pushBackObjects.length);
+           pushBackObjects = newObjects;
+       }
+       pushBackInts[pushBackDepth] = op;
+       pushBackObjects[pushBackDepth] = obj;
+       pushBackDepth++;
+    }
+
+    public void pushBackToken() { pushBackToken(op, number != null ? (Object)number : (Object)string); }
+
+    public int peekToken() throws IOException {
+       int ret = getToken();
+       pushBackToken();
+       return ret;
+    }
+
+    public int getToken() throws IOException {
+       number = null;
+       string = null;
+       if (pushBackDepth > 0) {
+           pushBackDepth--;
+           op = pushBackInts[pushBackDepth];
+           if (pushBackObjects[pushBackDepth] != null) {
+               number = pushBackObjects[pushBackDepth] instanceof Number ? (Number)pushBackObjects[pushBackDepth] : null;
+               string = pushBackObjects[pushBackDepth] instanceof String ? (String)pushBackObjects[pushBackDepth] : null;
+           }
+       } else {
+           do {
+               op = _getToken();
+               if (op == EOL) { line++; col = 0; }
+           } while (op == EOL);
+       }
+       return op;
+    }
+
 }