2004/01/03 04:27:27
[org.ibex.core.git] / src / org / xwt / js / Lexer.java
index 172c593..6e541c1 100644 (file)
@@ -1,5 +1,4 @@
-// This file was derived from org.mozilla.javascript.TokenStream; it
-// is covered by the NPL 1.1.
+// Derived from org.mozilla.javascript.TokenStream [NPL]
 
 /**
  * The contents of this file are subject to the Netscape Public
 package org.xwt.js;
 import java.io.*;
 
-class Lexer {
+/** Lexes a stream of characters into a stream of Tokens */
+class Lexer implements Tokens {
 
-    private SmartReader in;
-    private boolean pushedBack = false;
-
-    private int op;
-    public Number number;
-    public String string;
-
-    public Lexer(Reader r) throws IOException { in = new SmartReader(r); }
-    public int peekToken() throws IOException { int ret = peekToken(); pushBackToken(); return ret; }
-    public void pushBackToken() { if (pushedBack) throw new Error("can't push back twice"); pushedBack = true; }
-
-    // Token Constants //////////////////////////////////////////////////////////
-
-    public final static int
-        EOL          = 1,   // end of line
-        RETURN       = 5,   // return
-        GOTO         = 6,   // goto
-        BITOR        = 11,  // |
-        ASSIGN_BITOR = 211, // |=
-        BITXOR       = 12,  // ^
-        ASSIGN_BITXOR= 212, // ^=
-        BITAND       = 13,  // &
-        ASSIGN_BITAND= 213, // &=
-        EQ           = 14,  // ==
-        NE           = 15,  // !=
-        LT           = 16,  // <
-        LE           = 17,  // <=
-        GT           = 18,  // >
-        GE           = 19,  // >=
-        LSH          = 20,  // <<
-        ASSIGN_LSH   = 220, // <<=
-        RSH          = 21,  // >>
-        ASSIGN_RSH   = 221, // >>=
-        URSH         = 22,  // >>>
-        ASSIGN_URSH  = 222, // >>>=
-        ADD          = 23,  // +
-        ASSIGN_ADD   = 223, // +=
-        SUB          = 24,  // -
-        ASSIGN_SUB   = 224, // -=
-        MUL          = 25,  // *
-        ASSIGN_MUL   = 225, // *=
-        DIV          = 26,  // /
-        ASSIGN_DIV   = 226, // /=
-        MOD          = 27,  // %
-        ASSIGN_MOD   = 227, // %=
-        BITNOT       = 28,  // ~
-        ASSIGN_BITNOT= 228, // ~=
-        DELPROP      = 31,  // delete
-        TYPEOF       = 32,  // typeof
-        NAME         = 44,  // *** identifiers ***
-        NUMBER       = 45,  // *** numeric literals ***
-        STRING       = 46,  // *** string literals ***
-        NULL         = 49,  // null
-        THIS         = 50,  // this
-        FALSE        = 51,  // false
-        TRUE         = 52,  // true
-        SHEQ         = 53,  // ===
-        SHNE         = 54,  // !==
-        THROW        = 62,  // throw
-        IN           = 63,  // in
-        INSTANCEOF   = 64,  // instanceof
-        TRY          = 75,  // try
-        SEMI         = 89,  // ;
-        LB           = 90,  // [
-        RB           = 91,  // ]
-        LC           = 92,  // {
-        RC           = 93,  // }
-        LP           = 94,  // (
-        RP           = 95,  // )
-        COMMA        = 96,  // ,
-        ASSIGN       = 97,  // =
-        HOOK         = 98,  // ?
-        COLON        = 99,  // :
-        OR           = 100, // ||
-        AND          = 101, // &&
-        INC          = 106, // ++
-        DEC          = 107, // --
-        DOT          = 108, // .
-        FUNCTION     = 110, // function
-       IF          = 113,  // if keyword
-        ELSE         = 114, // else keyword
-        SWITCH       = 115, // switch keyword
-        CASE         = 116, // case keyword
-        DEFAULT      = 117, // default keyword
-        WHILE        = 118, // while keyword
-        DO           = 119, // do keyword
-        FOR          = 120, // for keyword
-        BREAK        = 121, // break keyword
-        CONTINUE     = 122, // continue keyword
-        VAR          = 123, // var keyword
-        WITH         = 124, // with keyword
-        CATCH        = 125, // catch keyword
-        FINALLY      = 126, // finally keyword
-        RESERVED     = 127, // reserved keywords
-        NOP          = 128, // NOP
-        VOID         = 132, // void keyword
-        MOD_ASSIGN   = 133, // %=
-        BANG         = 134, // %=
-        ASSERT       = 150; // assert keyword
+    /** for debugging */
+    public static void main(String[] s) throws Exception {
+        Lexer l = new Lexer(new InputStreamReader(System.in), "stdin", 0);
+        int tok = 0;
+        while((tok = l.getToken()) != -1) System.out.println(codeToString[tok]);
+    }
 
+    /** the token that was just parsed */
+    protected int op;
+   /** the most recently parsed token, <i>regardless of pushbacks</i> */
+    protected int mostRecentlyReadToken;
 
-    // Predicates ///////////////////////////////////////////////////////////////////////
+    /** if the token just parsed was a NUMBER, this is the numeric value */
+    protected Number number = null;
 
-    protected static boolean isJSIdentifier(String s) {
-        int length = s.length();
-        if (length == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) return false;
-        for (int i=1; i<length; i++) {
-            char c = s.charAt(i);
-            if (!Character.isJavaIdentifierPart(c) && c == '\\' && !((i + 5) < length) &&
-                (s.charAt(i + 1) == 'u') && 0 <= xDigitToInt(s.charAt(i + 2)) && 0 <= xDigitToInt(s.charAt(i + 3)) && 
-                0 <= xDigitToInt(s.charAt(i + 4)) && 0 <= xDigitToInt(s.charAt(i + 5)))
-                return false;
-        }
-        return true;
+    /** if the token just parsed was a NAME or STRING, this is the string value */
+    protected String string = null;
+
+    /** the line number of the most recently <i>lexed</i> token */
+    private int line = 0;
+
+    /** the line number of the most recently <i>parsed</i> token */
+    protected int parserLine = 0;
+
+    /** the column number of the current token */
+    protected int col = 0;
+
+    /** the name of the source code file being lexed */
+    protected String sourceName;
+
+    private SmartReader in;
+    public Lexer(Reader r, String sourceName, int line) throws IOException {
+        this.sourceName = sourceName;
+        this.line = line;
+        this.parserLine = line;
+        in = new SmartReader(r);
     }
 
+
+    // Predicates ///////////////////////////////////////////////////////////////////////
+
     private static boolean isAlpha(int c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); }
     private static boolean isDigit(int c) { return (c >= '0' && c <= '9'); }
-    private static boolean isLineBreak(int c) { return (c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029); }
     private static int xDigitToInt(int c) {
         if ('0' <= c && c <= '9') return c - '0';
-        if ('a' <= c && c <= 'f') return c - ('a' - 10);
-        if ('A' <= c && c <= 'F') return c - ('A' - 10);
-        return -1;
-    }
-    public static boolean isWhiteSpace(int c) {
-        if (c == '\u0020' || c == '\u0009' || c == '\u000C' || c == '\u000B' || c == '\u00A0') return true;
-        return Character.getType((char)c) == Character.SPACE_SEPARATOR;
+        else if ('a' <= c && c <= 'f') return c - ('a' - 10);
+        else if ('A' <= c && c <= 'F') return c - ('A' - 10);
+        else return -1;
     }
 
     
     // Token Subtype Handlers /////////////////////////////////////////////////////////
 
     private int getKeyword(String name) throws IOException {
-        final int    
-            Id_break         = BREAK,
-            Id_case          = CASE,
-            Id_continue      = CONTINUE,
-            Id_default       = DEFAULT,
-            Id_delete        = DELPROP,
-            Id_do            = DO,
-            Id_else          = ELSE,
-            Id_false         = FALSE,
-            Id_for           = FOR,
-            Id_function      = FUNCTION,
-            Id_if            = IF,
-            Id_in            = IN,
-            Id_null          = NULL,
-            Id_return        = RETURN,
-            Id_switch        = SWITCH,
-            Id_this          = THIS,
-            Id_true          = TRUE,
-            Id_typeof        = TYPEOF,
-            Id_var           = VAR,
-            Id_void          = VOID,
-            Id_while         = WHILE,
-            Id_with          = WITH,
-
-            // the following are #ifdef RESERVE_JAVA_KEYWORDS in jsscan.c
-            Id_abstract      = RESERVED,
-            Id_boolean       = RESERVED,
-            Id_byte          = RESERVED,
-            Id_catch         = CATCH,
-            Id_char          = RESERVED,
-            Id_class         = RESERVED,
-            Id_const         = RESERVED,
-            Id_debugger      = RESERVED,
-            Id_double        = RESERVED,
-            Id_enum          = RESERVED,
-            Id_extends       = RESERVED,
-            Id_final         = RESERVED,
-            Id_finally       = FINALLY,
-            Id_float         = RESERVED,
-            Id_goto          = RESERVED,
-            Id_implements    = RESERVED,
-            Id_instanceof    = INSTANCEOF,
-            Id_int           = RESERVED,
-            Id_interface     = RESERVED,
-            Id_long          = RESERVED,
-            Id_native        = RESERVED,
-            Id_package       = RESERVED,
-            Id_private       = RESERVED,
-            Id_protected     = RESERVED,
-            Id_public        = RESERVED,
-            Id_assert        = ASSERT,
-            Id_short         = RESERVED,
-            Id_static        = RESERVED,
-            Id_super         = RESERVED,
-            Id_synchronized  = RESERVED,
-            Id_throw         = THROW,
-            Id_throws        = RESERVED,
-            Id_transient     = RESERVED,
-            Id_try           = TRY,
-            Id_volatile      = RESERVED;
-        
-        int id;
-        String s = name;
-        L0: { id = -1; String X = null; int c;
-            L: switch (s.length()) {
-            case 2: c=s.charAt(1);
-                if (c=='f') { if (s.charAt(0)=='i') {id=Id_if; break L0;} }
-                else if (c=='n') { if (s.charAt(0)=='i') {id=Id_in; break L0;} }
-                else if (c=='o') { if (s.charAt(0)=='d') {id=Id_do; break L0;} }
-                break L;
-            case 3: switch (s.charAt(0)) {
-                case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} break L;
-                case 'i': if (s.charAt(2)=='t' && s.charAt(1)=='n') {id=Id_int; break L0;} break L;
-                case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e')
-                    throw new IOException("the new keyword is not permitted in XWT scripts");
-                    break L;
-                case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') {id=Id_try; break L0;} break L;
-                case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_var; break L0;} break L;
-                } break L;
-            case 4: switch (s.charAt(0)) {
-                case 'b': X="byte";id=Id_byte; break L;
-                case 'c': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_case; break L0;} }
-                    else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') {id=Id_char; break L0;} }
-                    break L;
-                case 'e': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {id=Id_else; break L0;} }
-                    else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') {id=Id_enum; break L0;} }
-                    break L;
-                case 'g': X="goto";id=Id_goto; break L;
-                case 'l': X="long";id=Id_long; break L;
-                case 'n': X="null";id=Id_null; break L;
-                case 't': c=s.charAt(3);
-                    if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {id=Id_true; break L0;} }
-                    else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') {id=Id_this; break L0;} }
-                    break L;
-                case 'v': X="void";id=Id_void; break L;
-                case 'w': X="with";id=Id_with; break L;
-                } break L;
-            case 5: switch (s.charAt(2)) {
-                case 'a': X="class";id=Id_class; break L;
-                case 'e': X="break";id=Id_break; break L;
-                case 'i': X="while";id=Id_while; break L;
-                case 'l': X="false";id=Id_false; break L;
-                case 'n': c=s.charAt(0);
-                    if (c=='c') { X="const"; throw new IOException("the const keyword is not permitted in XWT"); }
-                    else if (c=='f') { X="final";id=Id_final; }
-                    break L;
-                case 'o': c=s.charAt(0);
-                    if (c=='f') { X="float";id=Id_float; }
-                    else if (c=='s') { X="short";id=Id_short; }
-                    break L;
-                case 'p': X="super";id=Id_super; break L;
-                case 'r': X="throw";id=Id_throw; break L;
-                case 't': X="catch";id=Id_catch; break L;
-                } break L;
-            case 6: switch (s.charAt(1)) {
-                case 'a': X="native";id=Id_native; break L;
-                case 'e': c=s.charAt(0);
-                    if (c=='d') { X="delete"; throw new IOException("the delete keyword is not permitted in XWT scripts"); }
-                    else if (c=='r') { X="return";id=Id_return; }
-                    break L;
-                case 'h': X="throws";id=Id_throws; break L;
-                case 'o': X="double";id=Id_double; break L;
-                case 's': X="assert";id=Id_assert; break L;
-                case 'u': X="public";id=Id_public; break L;
-                case 'w': X="switch";id=Id_switch; break L;
-                case 'y': X="typeof";id=Id_typeof; break L;
-                } break L;
-            case 7: switch (s.charAt(1)) {
-                case 'a': X="package";id=Id_package; break L;
-                case 'e': X="default";id=Id_default; break L;
-                case 'i': X="finally";id=Id_finally; break L;
-                case 'o': X="boolean";id=Id_boolean; break L;
-                case 'r': X="private";id=Id_private; break L;
-                case 'x': X="extends";id=Id_extends; break L;
-                } break L;
-            case 8: switch (s.charAt(0)) {
-                case 'a': X="abstract";id=Id_abstract; break L;
-                case 'c': X="continue";id=Id_continue; break L;
-                case 'd': X="debugger";id=Id_debugger; break L;
-                case 'f': X="function";id=Id_function; break L;
-                case 'v': X="volatile";id=Id_volatile; break L;
-                } break L;
-            case 9: c=s.charAt(0);
-                if (c=='i') { X="interface";id=Id_interface; }
-                else if (c=='p') { X="protected";id=Id_protected; }
-                else if (c=='t') { X="transient";id=Id_transient; }
-                break L;
-            case 10: c=s.charAt(1);
-                if (c=='m') { X="implements";id=Id_implements; }
-                else if (c=='n') { X="instanceof"; throw new IOException("the instanceof keyword is not permitted in XWT scripts"); }
-                break L;
-            case 12: X="synchronized";id=Id_synchronized; break L;
-            }
-            if (X!=null && X!=s && !X.equals(s)) id = -1;
-        }
-        if (id == -1) { return -1; }
-        this.op = id >> 8;
-        return id & 0xff;
+        //#switch(name)
+        case "if": return IF;
+        case "lt": return LT;
+        case "gt": return GT;
+        case "in": return IN;
+        case "do": return DO;
+        case "and": return AND;
+        case "for": return FOR;
+        case "int": return RESERVED;
+        case "new": return RESERVED;
+        case "try": return TRY;
+        case "var": return VAR;
+        case "byte": return RESERVED;
+        case "case": return CASE;
+        case "char": return RESERVED;
+        case "else": return ELSE;
+        case "enum": return RESERVED;
+        case "goto": return RESERVED;
+        case "long": return RESERVED;
+        case "null": return NULL;
+        case "true": return TRUE;
+        case "with": return RESERVED;
+        case "void": return RESERVED;
+        case "class": return RESERVED;
+        case "break": return BREAK;
+        case "while": return WHILE;
+        case "false": return FALSE;
+        case "const": return RESERVED;
+        case "final": return RESERVED;
+        case "super": return RESERVED;
+        case "throw": return THROW;
+        case "catch": return CATCH;
+        case "class": return RESERVED;
+        case "delete": return RESERVED;
+        case "return": return RETURN;
+        case "throws": return RESERVED;
+        case "double": return RESERVED;
+        case "assert": return ASSERT;
+        case "public": return RESERVED;
+        case "switch": return SWITCH;
+        case "typeof": return TYPEOF;
+        case "package": return RESERVED;
+        case "default": return DEFAULT;
+        case "finally": return FINALLY;
+        case "boolean": return RESERVED;
+        case "private": return RESERVED;
+        case "extends": return RESERVED;
+        case "abstract": return RESERVED;
+        case "continue": return CONTINUE;
+        case "debugger": return RESERVED;
+        case "function": return FUNCTION;
+        case "volatile": return RESERVED;
+        case "interface": return RESERVED;
+        case "protected": return RESERVED;
+        case "transient": return RESERVED;
+        case "implements": return RESERVED;
+        case "instanceof": return RESERVED;
+        case "synchronized": return RESERVED;
+        //#end
+        return -1;
     }
 
     private int getIdentifier(int c) throws IOException {
@@ -325,8 +146,9 @@ class Lexer {
         in.unread();
         String str = in.getString();
         int result = getKeyword(str);
+        if (result == RESERVED) throw new LexerException("The reserved word \"" + str + "\" is not permitted in XWT scripts");
         if (result != -1) return result;
-        this.string = str;
+        this.string = str.intern();
         return NAME;
     }
     
@@ -350,7 +172,7 @@ class Lexer {
             if (c == 'e' || c == 'E') {
                 c = in.read();
                 if (c == '+' || c == '-') c = in.read();
-                if (!isDigit(c)) throw new IOException("msg.missing.exponent");
+                if (!isDigit(c)) throw new LexerException("float listeral did not have an exponent value");
                 do { c = in.read(); } while (isDigit(c));
             }
         }
@@ -359,24 +181,20 @@ class Lexer {
         String numString = in.getString();
         if (base == 10 && !isInteger) {
             try { dval = (Double.valueOf(numString)).doubleValue(); }
-            catch (NumberFormatException ex) { throw new IOException("msg.caught.nfe"); }
+            catch (NumberFormatException ex) { throw new LexerException("invalid numeric literal: \"" + numString + "\""); }
         } else {
             if (isInteger) {
                 longval = Long.parseLong(numString, base);
                 dval = (double)longval;
-           } else {
-                // FIXME: we're not handling hex/octal fractions... does that matter?
+            } else {
                 dval = Double.parseDouble(numString);
                 longval = (long) dval;
                 if (longval == dval) isInteger = true;
             }
         }
         
-        if (!isInteger) this.number = new Double(dval);
-        else if (Byte.MIN_VALUE <= longval && longval <= Byte.MAX_VALUE) this.number = new Byte((byte)longval);
-        else if (Short.MIN_VALUE <= longval && longval <= Short.MAX_VALUE) this.number = new Short((short)longval);
-        else if (Integer.MIN_VALUE <= longval && longval <= Integer.MAX_VALUE) this.number = new Integer((int)longval);
-        else this.number = new Double(longval);
+        if (!isInteger) this.number = JS.N(dval);
+        else this.number = JS.N(longval);
         return NUMBER;
     }
     
@@ -387,7 +205,7 @@ class Lexer {
         c = in.read();
         in.startString(); // start after the first "
         while(c != quoteChar) {
-            if (c == '\n' || c == -1) throw new IOException("msg.unterminated.string.lit");
+            if (c == '\n' || c == -1) throw new LexerException("unterminated string literal");
             if (c == '\\') {
                 if (stringBuf == null) {
                     in.unread();   // Don't include the backslash
@@ -401,36 +219,43 @@ class Lexer {
                 case 'r': c = '\r'; break;
                 case 't': c = '\t'; break;
                 case 'v': c = '\u000B'; break;
-                default: throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");
+                case '\\': c = '\\'; break;
+                case 'u': {
+                    int v = 0;
+                    for(int i=0; i<4; i++) {
+                        int ci = in.read();
+                        if (!((ci >= '0' && ci <= '9') || (ci >= 'a' && ci <= 'f') || (ci >= 'A' && ci <= 'F')))
+                            throw new LexerException("illegal character '" + ((char)c) + "' in \\u unicode escape sequence");
+                        v = (v << 8) | Integer.parseInt(ci + "", 16);
+                    }
+                    c = (char)v;
+                    break;
+                }
+                default:
+                    // just use the character that was escaped
+                    break;
                 }
             }
             if (stringBuf != null) stringBuf.append((char) c);
             c = in.read();
         }
-        if (stringBuf != null) this.string = stringBuf.toString();
+        if (stringBuf != null) this.string = stringBuf.toString().intern();
         else {
             in.unread(); // miss the trailing "
-            this.string = in.getString();
+            this.string = in.getString().intern();
             in.read();
         }
         return STRING;
     }
 
-    public int getToken() throws IOException {
-       if (pushedBack) { pushedBack = false; return op; }
-       return (op = getToken());
-    }
-
-    public int _getToken() throws IOException {
+    private int _getToken() throws IOException {
         int c;
-        do { if ((c = in.read()) == '\n') break; } while (isWhiteSpace(c) || c == '\n');
+        do { c = in.read(); } while (c == '\u0020' || c == '\u0009' || c == '\u000C' || c == '\u000B' || c == '\n' );
         if (c == -1) return -1;
-        if (c == '\\' && in.peek() == 'u') throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");
-        if (Character.isJavaIdentifierStart((char)c)) return getIdentifier(c);
+        if (c == '\\' || Character.isJavaIdentifierStart((char)c)) return getIdentifier(c);
         if (isDigit(c) || (c == '.' && isDigit(in.peek()))) return getNumber(c);
         if (c == '"' || c == '\'') return getString(c);
         switch (c) {
-        case '\n': return EOL;
         case ';': return SEMI;
         case '[': return LB;
         case ']': return RB;
@@ -447,19 +272,14 @@ class Lexer {
         case '&': return in.match('&') ? AND : in.match('=') ? ASSIGN_BITAND : BITAND;
         case '=': return !in.match('=') ? ASSIGN : in.match('=') ? SHEQ : EQ;
         case '!': return !in.match('=') ? BANG : in.match('=') ? SHNE : NE;
-        case '%': return in.match('=') ? MOD_ASSIGN : MOD;
+        case '%': return in.match('=') ? ASSIGN_MOD : MOD;
         case '~': return BITNOT;
         case '+': return in.match('=') ? ASSIGN_ADD : in.match('+') ? INC : ADD;
         case '-': return in.match('=') ? ASSIGN_SUB: in.match('-') ? DEC : SUB;
         case '*': return in.match('=') ? ASSIGN_MUL : MUL;
-        case '<': return !in.match('<') ?
-                             (in.match('=') ? LE : LT) :
-                             in.match('=') ? ASSIGN_LSH : LSH;
-        case '>': return !in.match('>') ?
-                             (in.match('=') ? GE : GT) :
-                             in.match('>') ?
-                                (in.match('=') ? ASSIGN_URSH : URSH) :
-                                 (in.match('=') ? ASSIGN_RSH : RSH);
+        case '<': return !in.match('<') ? (in.match('=') ? LE : LT) : in.match('=') ? ASSIGN_LSH : LSH;
+        case '>': return !in.match('>') ? (in.match('=') ? GE : GT) :
+            in.match('>') ? (in.match('=') ? ASSIGN_URSH : URSH) : (in.match('=') ? ASSIGN_RSH : RSH);
         case '/':
             if (in.match('=')) return ASSIGN_DIV;
             if (in.match('/')) { while ((c = in.read()) != -1 && c != '\n'); in.unread(); return getToken(); }
@@ -467,20 +287,30 @@ class Lexer {
             while ((c = in.read()) != -1 && !(c == '*' && in.match('/'))) {
                 if (c == '\n' || c != '/' || !in.match('*')) continue;
                 if (in.match('/')) return getToken();
-                throw new IOException("msg.nested.comment");
+                throw new LexerException("nested comments are not permitted");
             }
-            if (c == -1) throw new IOException("msg.unterminated.comment");
+            if (c == -1) throw new LexerException("unterminated comment");
             return getToken();  // `goto retry'
-        default: throw new IOException("illegal character: " + c);
+        default: throw new LexerException("illegal character: \'" + ((char)c) + "\'");
         }
     }
 
-    private static class SmartReader {
+
+    // SmartReader ////////////////////////////////////////////////////////////////
+
+    /** a Reader that tracks line numbers and can push back tokens */
+    private class SmartReader {
         PushbackReader reader = null;
         int lastread = -1;
 
         public SmartReader(Reader r) { reader = new PushbackReader(r); }
-        public void unread() throws IOException { reader.unread(lastread); }
+        public void unread() throws IOException { unread((char)lastread); }
+        public void unread(char c) throws IOException {
+            reader.unread(c);
+            if(c == '\n') col = -1;
+            else col--;
+            if (accumulator != null) accumulator.setLength(accumulator.length() - 1);
+        }
         public boolean match(char c) throws IOException { if (peek() == c) { reader.read(); return true; } else return false; }
         public int peek() throws IOException {
             int peeked = reader.read();
@@ -489,18 +319,79 @@ class Lexer {
         }
         public int read() throws IOException {
             lastread = reader.read();
-            if (accumulator != null) accumulator.append(lastread);
+            if (accumulator != null) accumulator.append((char)lastread);
+            if (lastread != '\n' && lastread != '\r') col++;
+            if (lastread == '\n') {
+                // col is -1 if we just unread a newline, this is sort of ugly
+                if (col != -1) parserLine = ++line;
+                col = 0;
+            }
             return lastread;
         }
 
-        // FIXME: could be much more efficient
+        // FEATURE: could be much more efficient
         StringBuffer accumulator = null;
-        public void startString() { accumulator = new StringBuffer(); }
+        public void startString() {
+            accumulator = new StringBuffer();
+            accumulator.append((char)lastread);
+        }
         public String getString() throws IOException {
-            String ret = accumulator.toString();
+            String ret = accumulator.toString().intern();
             accumulator = null;
             return ret;
         }
     }
 
+
+    // Token PushBack code ////////////////////////////////////////////////////////////
+
+    private int pushBackDepth = 0;
+    private int[] pushBackInts = new int[10];
+    private Object[] pushBackObjects = new Object[10];
+
+    /** push back a token */
+    public final 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++;
+    }
+
+    /** push back the most recently read token */
+    public final void pushBackToken() { pushBackToken(op, number != null ? (Object)number : (Object)string); }
+
+    /** read a token but leave it in the stream */
+    public final int peekToken() throws IOException {
+        int ret = getToken();
+        pushBackToken();
+        return ret;
+    }
+
+    /** read a token */
+    public final int getToken() throws IOException {
+        number = null;
+        string = null;
+        if (pushBackDepth == 0) {
+            mostRecentlyReadToken = op;
+            return op = _getToken();
+        }
+        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;
+        }
+        return op;
+    }
+
+    class LexerException extends IOException {
+        public LexerException(String s) { super(sourceName + ":" + line + "," + col + ": " + s); }
+    }
 }