2003/06/16 08:03:15
[org.ibex.core.git] / src / org / xwt / js / Lexer.java
index eb3f69a..1fa432d 100644 (file)
@@ -27,9 +27,9 @@ class Lexer implements Tokens {
 
     /** 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]);
+        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 */
@@ -58,10 +58,10 @@ class Lexer implements Tokens {
 
     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);
+        this.sourceName = sourceName;
+        this.line = line;
+        this.parserLine = line;
+        in = new SmartReader(r);
     }
 
 
@@ -80,8 +80,8 @@ class Lexer implements Tokens {
     // Token Subtype Handlers /////////////////////////////////////////////////////////
 
     private int getKeyword(String s) throws IOException {
-       char c;
-       switch (s.length()) {
+        char c;
+        switch (s.length()) {
             case 2: c=s.charAt(1);
                 if (c=='f') { if (s.charAt(0)=='i') return IF; }
                 else if (c=='n') { if (s.charAt(0)=='i') return IN; }
@@ -100,45 +100,45 @@ class Lexer implements Tokens {
                 case 'c': c=s.charAt(3);
                     if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') return CASE; }
                     else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') return RESERVED; }
-                   return -1;
+                    return -1;
                 case 'e': c=s.charAt(3);
                     if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') return ELSE; }
                     else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') return RESERVED; }
-                   return -1;
+                    return -1;
                 case 'g': return s.equals("goto") ? RESERVED : -1;
-               case 'l': return s.equals("long") ? RESERVED : -1;
+                case 'l': return s.equals("long") ? RESERVED : -1;
                 case 'n': return s.equals("null") ? NULL : -1;
                 case 't': c=s.charAt(3);
                     if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') return TRUE; }
                     else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') return THIS; }
-                   return -1;
-               case 'w': if (s.equals("with")) return RESERVED; else return -1;
-               case 'v': if (s.equals("void")) return RESERVED; else return -1;
+                    return -1;
+                case 'w': if (s.equals("with")) return RESERVED; else return -1;
+                case 'v': if (s.equals("void")) return RESERVED; else return -1;
                 } break;
             case 5: switch (s.charAt(2)) {
                 case 'a': return s.equals("class") ? RESERVED : -1;
-               case 'e': return s.equals("break") ? BREAK : -1;
+                case 'e': return s.equals("break") ? BREAK : -1;
                 case 'i': return s.equals("while") ? WHILE : -1;
-               case 'l': return s.equals("false") ? FALSE : -1;
+                case 'l': return s.equals("false") ? FALSE : -1;
                 case 'n': c=s.charAt(0);
-                   if (s.equals("const")) return RESERVED;
+                    if (s.equals("const")) return RESERVED;
                     else if (s.equals("final")) return RESERVED;
-                   return -1;
-               case 'o': c=s.charAt(0);
-                   if (c == 'c') return s.equals("float") ? RESERVED : -1;
-                   else if (c=='s') return s.equals("final") ? RESERVED : -1;
-                   break;
-               case 'p': return s.equals("super") ? RESERVED : -1;
+                    return -1;
+                case 'o': c=s.charAt(0);
+                    if (c == 'c') return s.equals("float") ? RESERVED : -1;
+                    else if (c=='s') return s.equals("final") ? RESERVED : -1;
+                    break;
+                case 'p': return s.equals("super") ? RESERVED : -1;
                 case 'r': return s.equals("throw") ? THROW : -1;
                 case 't': return s.equals("catch") ? CATCH : -1;
                 } break;
             case 6: switch (s.charAt(1)) {
-               case 'a': return s.equals("class") ? RESERVED : -1;
+                case 'a': return s.equals("class") ? RESERVED : -1;
                 case 'e': c=s.charAt(0);
                     if (s.equals("delete")) return RESERVED;
                     else if (c=='r') return s.equals("return") ? RETURN : -1;
                     break;
-               case 'h': return s.equals("throws") ? RESERVED : -1;
+                case 'h': return s.equals("throws") ? RESERVED : -1;
                 case 'o': return s.equals("double") ? RESERVED : -1;
                 case 's': return s.equals("assert") ? ASSERT : -1;
                 case 'u': return s.equals("public") ? RESERVED : -1;
@@ -171,7 +171,7 @@ class Lexer implements Tokens {
                 break;
             case 12: return s.equals("synchronized") ? RESERVED : -1;
             }
-       return -1;
+        return -1;
     }
 
     private int getIdentifier(int c) throws IOException {
@@ -180,7 +180,7 @@ class Lexer implements Tokens {
         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 == RESERVED) throw new LexerException("The reserved word \"" + str + "\" is not permitted in XWT scripts");
         if (result != -1) return result;
         this.string = str.intern();
         return NAME;
@@ -220,7 +220,7 @@ class Lexer implements Tokens {
             if (isInteger) {
                 longval = Long.parseLong(numString, base);
                 dval = (double)longval;
-           } else {
+            } else {
                 dval = Double.parseDouble(numString);
                 longval = (long) dval;
                 if (longval == dval) isInteger = true;
@@ -257,20 +257,20 @@ class Lexer implements Tokens {
                 case 't': c = '\t'; break;
                 case 'v': c = '\u000B'; break;
                 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;
-               }
+                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;
+                    // just use the character that was escaped
+                    break;
                 }
             }
             if (stringBuf != null) stringBuf.append((char) c);
@@ -316,7 +316,7 @@ class Lexer implements Tokens {
         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);
+            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(); }
@@ -343,9 +343,9 @@ class Lexer implements Tokens {
         public SmartReader(Reader r) { reader = new PushbackReader(r); }
         public void unread() throws IOException { unread((char)lastread); }
         public void unread(char c) throws IOException {
-           reader.unread(c);
-           if (accumulator != null) accumulator.setLength(accumulator.length() - 1);
-       }
+            reader.unread(c);
+            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();
@@ -355,17 +355,17 @@ class Lexer implements Tokens {
         public int read() throws IOException {
             lastread = reader.read();
             if (accumulator != null) accumulator.append((char)lastread);
-           if (lastread != '\n' && lastread != '\r') col++;
-           if (lastread == '\n') { parserLine = ++line; col = 0; }
+            if (lastread != '\n' && lastread != '\r') col++;
+            if (lastread == '\n') { parserLine = ++line; col = 0; }
             return lastread;
         }
 
         // FEATURE: could be much more efficient
         StringBuffer accumulator = null;
         public void startString() {
-           accumulator = new StringBuffer();
-           accumulator.append((char)lastread);
-       }
+            accumulator = new StringBuffer();
+            accumulator.append((char)lastread);
+        }
         public String getString() throws IOException {
             String ret = accumulator.toString();
             accumulator = null;
@@ -382,17 +382,17 @@ class Lexer implements Tokens {
 
     /** push back a token */
     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++;
+        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 */
@@ -400,29 +400,29 @@ class Lexer implements Tokens {
 
     /** read a token but leave it in the stream */
     public int peekToken() throws IOException {
-       int ret = getToken();
-       pushBackToken();
-       return ret;
+        int ret = getToken();
+        pushBackToken();
+        return ret;
     }
 
     /** read a token */
     public 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;
+        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); }
+        public LexerException(String s) { super(sourceName + ":" + line + "," + col + ": " + s); }
     }
 }