2003/06/18 07:38:54
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index 68cb87c..670f68e 100644 (file)
@@ -85,30 +85,30 @@ class Parser extends Lexer implements ByteCodes {
     static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
     static {
         isRightAssociative[ASSIGN] =
-           isRightAssociative[ASSIGN_BITOR] =
-           isRightAssociative[ASSIGN_BITXOR] =
-           isRightAssociative[ASSIGN_BITAND] =
-           isRightAssociative[ASSIGN_LSH] =
-           isRightAssociative[ASSIGN_RSH] =
-           isRightAssociative[ASSIGN_URSH] =
-           isRightAssociative[ASSIGN_ADD] =
-           isRightAssociative[ASSIGN_SUB] =
-           isRightAssociative[ASSIGN_MUL] =
-           isRightAssociative[ASSIGN_DIV] =
-           isRightAssociative[ASSIGN_MOD] = true;
+            isRightAssociative[ASSIGN_BITOR] =
+            isRightAssociative[ASSIGN_BITXOR] =
+            isRightAssociative[ASSIGN_BITAND] =
+            isRightAssociative[ASSIGN_LSH] =
+            isRightAssociative[ASSIGN_RSH] =
+            isRightAssociative[ASSIGN_URSH] =
+            isRightAssociative[ASSIGN_ADD] =
+            isRightAssociative[ASSIGN_SUB] =
+            isRightAssociative[ASSIGN_MUL] =
+            isRightAssociative[ASSIGN_DIV] =
+            isRightAssociative[ASSIGN_MOD] = true;
 
         precedence[ASSIGN] =
-           precedence[ASSIGN_BITOR] =
-           precedence[ASSIGN_BITXOR] =
-           precedence[ASSIGN_BITAND] =
-           precedence[ASSIGN_LSH] =
-           precedence[ASSIGN_RSH] =
-           precedence[ASSIGN_URSH] =
-           precedence[ASSIGN_ADD] =
-           precedence[ASSIGN_SUB] =
-           precedence[ASSIGN_MUL] =
-           precedence[ASSIGN_DIV] =
-           precedence[ASSIGN_MOD] = 1;
+            precedence[ASSIGN_BITOR] =
+            precedence[ASSIGN_BITXOR] =
+            precedence[ASSIGN_BITAND] =
+            precedence[ASSIGN_LSH] =
+            precedence[ASSIGN_RSH] =
+            precedence[ASSIGN_URSH] =
+            precedence[ASSIGN_ADD] =
+            precedence[ASSIGN_SUB] =
+            precedence[ASSIGN_MUL] =
+            precedence[ASSIGN_DIV] =
+            precedence[ASSIGN_MOD] = 1;
         precedence[HOOK] = 2;
         precedence[COMMA] = 3;
         precedence[OR] = precedence[AND] = 4;
@@ -133,7 +133,7 @@ class Parser extends Lexer implements ByteCodes {
 
     /** gets a token and throws an exception if it is not <tt>code</tt> */
     private void consume(int code) throws IOException {
-        if (getToken() != code) throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
+        if (getToken() != code) throw pe("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
     }
 
     /**
@@ -143,16 +143,16 @@ class Parser extends Lexer implements ByteCodes {
      *  appended bytecodes MUST grow the stack by exactly one element.
      */ 
     private void startExpr(CompiledFunctionImpl appendTo, int minPrecedence) throws IOException {
-       int saveParserLine = parserLine;
-       _startExpr(appendTo, minPrecedence);
-       parserLine = saveParserLine;
+        int saveParserLine = parserLine;
+        _startExpr(appendTo, minPrecedence);
+        parserLine = saveParserLine;
     }
     private void _startExpr(CompiledFunctionImpl appendTo, int minPrecedence) throws IOException {
         int tok = getToken();
         CompiledFunctionImpl b = appendTo;
 
         switch (tok) {
-        case -1: throw new ParserException("expected expression");
+        case -1: throw pe("expected expression");
 
         // all of these simply push values onto the stack
         case NUMBER: b.add(parserLine, LITERAL, number); break;
@@ -194,8 +194,8 @@ class Parser extends Lexer implements ByteCodes {
         }
         case INC: case DEC: {  // prefix (not postfix)
             startExpr(b, precedence[tok]);
-           if (b.get(b.size() - 1) != GET)
-               throw new ParserException("prefixed increment/decrement can only be performed on a valid assignment target");
+            if (b.get(b.size() - 1) != GET)
+                throw pe("prefixed increment/decrement can only be performed on a valid assignment target");
             b.set(b.size() - 1, tok, new Boolean(true));
             break;
         }
@@ -209,7 +209,7 @@ class Parser extends Lexer implements ByteCodes {
             if (peekToken() != RC)
                 while(true) {
                     if (peekToken() != NAME && peekToken() != STRING)
-                        throw new ParserException("expected NAME or STRING");
+                        throw pe("expected NAME or STRING");
                     getToken();
                     b.add(parserLine, LITERAL, string);                          // grab the key
                     consume(COLON);
@@ -224,10 +224,10 @@ class Parser extends Lexer implements ByteCodes {
             break;
         }
         case NAME: {
-           b.add(parserLine, TOPSCOPE);
-           b.add(parserLine, LITERAL, string);
-           continueExprAfterAssignable(b);
-           break;
+            b.add(parserLine, TOPSCOPE);
+            b.add(parserLine, LITERAL, string);
+            continueExprAfterAssignable(b);
+            break;
         }
         case FUNCTION: {
             consume(LP);
@@ -284,7 +284,7 @@ class Parser extends Lexer implements ByteCodes {
 
             break;
         }
-        default: throw new ParserException("expected expression, found " + codeToString[tok] + ", which cannot start an expression");
+        default: throw pe("expected expression, found " + codeToString[tok] + ", which cannot start an expression");
         }
 
         // attempt to continue the expression
@@ -300,17 +300,17 @@ class Parser extends Lexer implements ByteCodes {
      *  decreases the stack depth by exactly one element.
      */
     private void continueExprAfterAssignable(CompiledFunctionImpl b) throws IOException {
-       int saveParserLine = parserLine;
-       _continueExprAfterAssignable(b);
-       parserLine = saveParserLine;
+        int saveParserLine = parserLine;
+        _continueExprAfterAssignable(b);
+        parserLine = saveParserLine;
     }
     private void _continueExprAfterAssignable(CompiledFunctionImpl b) throws IOException {
         if (b == null) throw new Error("got null b; this should never happen");
         int tok = getToken();
-       switch(tok) {
+        switch(tok) {
         case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
         case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
-           b.add(parserLine, GET_PRESERVE);
+            b.add(parserLine, GET_PRESERVE);
             startExpr(b, -1);
             b.add(parserLine, tok - 1);
             b.add(parserLine, PUT);
@@ -319,29 +319,29 @@ class Parser extends Lexer implements ByteCodes {
             break;
         }
         case INC: case DEC: { // postfix
-           b.add(parserLine, GET_PRESERVE);
-           b.add(parserLine, LITERAL, new Integer(1));
-           b.add(parserLine, tok == INC ? ADD : SUB);
-           b.add(parserLine, PUT);
-           b.add(parserLine, SWAP);
-           b.add(parserLine, POP);
-           b.add(parserLine, LITERAL, new Integer(1));
-           b.add(parserLine, tok == INC ? SUB : ADD);
-           break;
-        }
-       case ASSIGN: {
+            b.add(parserLine, GET_PRESERVE);
+            b.add(parserLine, LITERAL, new Integer(1));
+            b.add(parserLine, tok == INC ? ADD : SUB);
+            b.add(parserLine, PUT);
+            b.add(parserLine, SWAP);
+            b.add(parserLine, POP);
+            b.add(parserLine, LITERAL, new Integer(1));
+            b.add(parserLine, tok == INC ? SUB : ADD);
+            break;
+        }
+        case ASSIGN: {
             startExpr(b, -1);
-           b.add(parserLine, PUT);
-           b.add(parserLine, SWAP);
-           b.add(parserLine, POP);
-           break;
-       }
-       default: {
-           pushBackToken();
-           b.add(parserLine, GET);
-           return;
-       }
-       }
+            b.add(parserLine, PUT);
+            b.add(parserLine, SWAP);
+            b.add(parserLine, POP);
+            break;
+        }
+        default: {
+            pushBackToken();
+            b.add(parserLine, GET);
+            return;
+        }
+        }
     }
 
 
@@ -357,9 +357,9 @@ class Parser extends Lexer implements ByteCodes {
      *  depth.
      */
     private void continueExpr(CompiledFunctionImpl b, int minPrecedence) throws IOException {
-       int saveParserLine = parserLine;
-       _continueExpr(b, minPrecedence);
-       parserLine = saveParserLine;
+        int saveParserLine = parserLine;
+        _continueExpr(b, minPrecedence);
+        parserLine = saveParserLine;
     }
     private void _continueExpr(CompiledFunctionImpl b, int minPrecedence) throws IOException {
         if (b == null) throw new Error("got null b; this should never happen");
@@ -398,21 +398,21 @@ class Parser extends Lexer implements ByteCodes {
             startExpr(b, precedence[tok]);                                     // otherwise check the second value
             b.add(parserLine, JMP, new Integer(2));                            // leave the second value on the stack and jump to the end
             b.add(parserLine, LITERAL, tok == AND ?
-                 new Boolean(false) : new Boolean(true));                     // target of the short-circuit jump is here
+                  new Boolean(false) : new Boolean(true));                     // target of the short-circuit jump is here
             b.set(size - 1, new Integer(b.size() - size));                     // write the target of the short-circuit jump
             break;
         }
         case DOT: {
             consume(NAME);
-           b.add(parserLine, LITERAL, string);
-           continueExprAfterAssignable(b);
-           break;
+            b.add(parserLine, LITERAL, string);
+            continueExprAfterAssignable(b);
+            break;
         }
         case LB: { // subscripting (not array constructor)
             startExpr(b, -1);
             consume(RB);
-           continueExprAfterAssignable(b);
-           break;
+            continueExprAfterAssignable(b);
+            break;
         }
         case HOOK: {
             b.add(parserLine, JF, new Integer(0));                // jump to the if-false expression
@@ -438,9 +438,9 @@ class Parser extends Lexer implements ByteCodes {
     /** Parse a block of statements which must be surrounded by LC..RC. */
     void parseBlock(CompiledFunctionImpl b) throws IOException { parseBlock(b, null); }
     void parseBlock(CompiledFunctionImpl b, String label) throws IOException {
-       int saveParserLine = parserLine;
-       _parseBlock(b, label);
-       parserLine = saveParserLine;
+        int saveParserLine = parserLine;
+        _parseBlock(b, label);
+        parserLine = saveParserLine;
     }
     void _parseBlock(CompiledFunctionImpl b, String label) throws IOException {
         if (peekToken() == -1) return;
@@ -454,9 +454,9 @@ class Parser extends Lexer implements ByteCodes {
 
     /** Parse a single statement, consuming the RC or SEMI which terminates it. */
     void parseStatement(CompiledFunctionImpl b, String label) throws IOException {
-       int saveParserLine = parserLine;
-       _parseStatement(b, label);
-       parserLine = saveParserLine;
+        int saveParserLine = parserLine;
+        _parseStatement(b, label);
+        parserLine = saveParserLine;
     }
     void _parseStatement(CompiledFunctionImpl b, String label) throws IOException {
         int tok = peekToken();
@@ -496,7 +496,7 @@ class Parser extends Lexer implements ByteCodes {
                 consume(COMMA);
             }
             b.add(parserLine, POP);                              // pop off the topscope
-            if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+            if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
             break;
         }
         case IF: {
@@ -523,7 +523,7 @@ class Parser extends Lexer implements ByteCodes {
             if (label != null) b.add(parserLine, LABEL, label);
             b.add(parserLine, LOOP);
             int size = b.size();
-           b.add(parserLine, POP);                                   // discard the first-iteration indicator
+            b.add(parserLine, POP);                                   // discard the first-iteration indicator
             startExpr(b, -1);
             b.add(parserLine, JT, new Integer(2));                    // if the while() clause is true, jump over the BREAK
             b.add(parserLine, BREAK);
@@ -561,7 +561,7 @@ class Parser extends Lexer implements ByteCodes {
                     b.add(parserLine, BREAK);                      // break out of the loop if we 'fall through'
                     break;
                 } else {
-                    throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
+                    throw pe("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
                 }
             b.set(size0 - 1, new Integer(b.size() - size0 + 1));      // end of the loop
             break;
@@ -708,7 +708,7 @@ class Parser extends Lexer implements ByteCodes {
                 pushBackToken(NAME, possiblyTheLabel);  
                 startExpr(b, -1);
                 b.add(parserLine, POP);
-                if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+                if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
                 break;
             }
         }
@@ -717,9 +717,9 @@ class Parser extends Lexer implements ByteCodes {
 
         case LC: {  // blocks are statements too
             pushBackToken();
-           b.add(parserLine, NEWSCOPE);
+            b.add(parserLine, NEWSCOPE);
             parseBlock(b, label);
-           b.add(parserLine, OLDSCOPE);
+            b.add(parserLine, OLDSCOPE);
             break;
         }
 
@@ -727,7 +727,7 @@ class Parser extends Lexer implements ByteCodes {
             pushBackToken();
             startExpr(b, -1);
             b.add(parserLine, POP);
-            if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+            if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
             break;
         }
         }
@@ -735,8 +735,7 @@ class Parser extends Lexer implements ByteCodes {
 
 
     // ParserException //////////////////////////////////////////////////////////////////////
-    
-    private class ParserException extends IOException { public ParserException(String s) { super(sourceName + ":" + parserLine + " " + s); } }
+    private IOException pe(String s) { return new IOException(sourceName + ":" + parserLine + " " + s); }
     
 }