X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FLexer.java;h=3a80f5a1d157faa71f35b8b193b95f6459f39260;hb=fc3d3e7efa26add1306f565f8c739f9e0a64588c;hp=e8cd2d47d72f8e07da7200020e6e20fe0d8dfd75;hpb=11fb6d3f994008abe6c16b4402c3904a543e240e;p=org.ibex.core.git diff --git a/src/org/xwt/js/Lexer.java b/src/org/xwt/js/Lexer.java index e8cd2d4..3a80f5a 100644 --- a/src/org/xwt/js/Lexer.java +++ b/src/org/xwt/js/Lexer.java @@ -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; + } + }