2003/07/05 03:23:31
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index cd5dfc2..3a3dcd9 100644 (file)
@@ -111,13 +111,13 @@ class Parser extends Lexer implements ByteCodes {
             precedence[ASSIGN_MOD] = 1;
         precedence[HOOK] = 2;
         precedence[COMMA] = 3;
-        precedence[OR] = precedence[AND] = 4;
+        precedence[OR] = precedence[AND] = precedence[BANG] = 4;
         precedence[GT] = precedence[GE] = 5;
         precedence[BITOR] = 6;
         precedence[BITXOR] = 7;
         precedence[BITAND] = 8;
         precedence[EQ] = precedence[NE] = 9;
-        precedence[LT] = precedence[LE] = 10;
+        precedence[LT] = precedence[LE] = precedence[TYPEOF] = 10;
         precedence[SHEQ] = precedence[SHNE] = 11;
         precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
         precedence[ADD] = precedence[SUB] = 13;
@@ -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]));
     }
 
     /**
@@ -152,7 +152,7 @@ class Parser extends Lexer implements ByteCodes {
         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;
@@ -168,11 +168,11 @@ class Parser extends Lexer implements ByteCodes {
             if (peekToken() != RB)
                 while(true) {                                               // iterate over the initialization values
                     int size = b.size();
+                    b.add(parserLine, LITERAL, new Integer(i++));           // push the index in the array to place it into
                     if (peekToken() == COMMA || peekToken() == RB)
                         b.add(parserLine, LITERAL, null);                   // for stuff like [1,,2,]
                     else
                         startExpr(b, -1);                                   // push the value onto the stack
-                    b.add(parserLine, LITERAL, new Integer(i++));           // push the index in the array to place it into
                     b.add(parserLine, PUT);                                 // put it into the array
                     b.add(parserLine, POP);                                 // discard the value remaining on the stack
                     if (peekToken() == RB) break;
@@ -195,7 +195,7 @@ 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");
+                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);
@@ -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
@@ -336,6 +336,11 @@ class Parser extends Lexer implements ByteCodes {
             b.add(parserLine, POP);
             break;
         }
+        case LP: {
+            int n = parseArgs(b);
+            b.add(parserLine,CALLMETHOD,new Integer(n));
+            break;
+        }
         default: {
             pushBackToken();
             b.add(parserLine, GET);
@@ -372,17 +377,8 @@ class Parser extends Lexer implements ByteCodes {
 
         switch (tok) {
         case LP: {  // invocation (not grouping)
-            int i = 0;
-            while(peekToken() != RP) {
-                i++;
-                if (peekToken() != COMMA) {
-                    startExpr(b, -1);
-                    if (peekToken() == RP) break;
-                }
-                consume(COMMA);
-            }
-            consume(RP);
-            b.add(parserLine, CALL, new Integer(i));
+            int n = parseArgs(b);
+            b.add(parserLine, CALL, new Integer(n));
             break;
         }
         case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
@@ -435,6 +431,21 @@ class Parser extends Lexer implements ByteCodes {
         continueExpr(b, minPrecedence);                           // try to continue the expression
     }
     
+    // parse a set of comma separated function arguments, assume LP has already been consumed
+    private int parseArgs(CompiledFunctionImpl b) throws IOException {
+        int i = 0;
+        while(peekToken() != RP) {
+            i++;
+            if (peekToken() != COMMA) {
+                startExpr(b, -1);
+                if (peekToken() == RP) break;
+            }
+            consume(COMMA);
+        }
+        consume(RP);
+        return i;
+    }
+    
     /** 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 {
@@ -496,7 +507,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: {
@@ -561,7 +572,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 +719,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;
             }
         }
@@ -727,7 +738,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 +746,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); }
     
 }