2003/06/13 05:25:57
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:01:08 +0000 (07:01 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:01:08 +0000 (07:01 +0000)
darcs-hash:20040130070108-2ba56-e8b2c929f8be6a33dbafda780b2ceb5b297cdc2c.gz

src/org/xwt/js/Parser.java

index 9aed73a..fa36d6a 100644 (file)
@@ -51,29 +51,17 @@ class Parser extends Lexer implements ByteCodes {
 
     // Parsing Logic /////////////////////////////////////////////////////////
 
-    /** the label for the current statement */
-    private String label = null;
-
     /** gets a token and throws an exception if it is not <tt>code</tt> */
     public void consume(int code) throws IOException {
        if (getToken() != code)
            throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
     }
 
-    /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
-    public void startExpr(CompiledFunction block) throws IOException { startExpr(-1, block); }
-    /*
-    public CompiledFunction startExpr(int minPrecedence) throws IOException {
-       CompiledFunction ret = new CompiledFunction(line, sourceName);
-       startExpr(minPrecedence, ret);
-       return ret.size() == 0 ? null : ret;
-    }
-    */
-
+    /** append the largest possible expression containing no operators of precedence below <tt>minPrecedence</tt> */
     public void startExpr(int minPrecedence, CompiledFunction appendTo) throws IOException {
        int tok = getToken();
-       int line = this.line;
        CompiledFunction b = appendTo;
+
        switch (tok) {
        case -1: return;
        case NUMBER: continueExpr(b.add(line, CompiledFunction.LITERAL, number), minPrecedence); return;
@@ -86,7 +74,7 @@ class Parser extends Lexer implements ByteCodes {
            int i = 0;
            while(true) {
                int size = b.size();
-               startExpr(b);
+               startExpr(-1, b);
                if (size == b.size())
                    if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
                b.add(line, LITERAL, new Integer(i++));
@@ -104,7 +92,7 @@ class Parser extends Lexer implements ByteCodes {
            return;
        }
        case LP: {
-           startExpr(b);
+           startExpr(-1, b);
            consume(RP);
            continueExpr(b, minPrecedence);
            return;
@@ -129,7 +117,7 @@ class Parser extends Lexer implements ByteCodes {
                getToken();
                b.add(line, LITERAL, string);
                consume(COLON);
-               startExpr(b);
+               startExpr(-1, b);
                b.add(line, PUT);
                b.add(line, POP);
                if (peekToken() == RC) break;
@@ -252,7 +240,7 @@ class Parser extends Lexer implements ByteCodes {
            int i = 0;
            while(peekToken() != RP) {
                i++;
-               startExpr(b);
+               startExpr(-1, b);
                if (peekToken() == RP) break;
                consume(COMMA);
            }
@@ -288,7 +276,7 @@ class Parser extends Lexer implements ByteCodes {
            if (peekToken() == ASSIGN) {
                consume(ASSIGN);
                b.add(line, LITERAL, target);
-               startExpr(b);
+               startExpr(-1, b);
                b.add(line, PUT);
                b.add(line, SWAP);
                b.add(line, POP);
@@ -301,11 +289,11 @@ class Parser extends Lexer implements ByteCodes {
        }
 
        case LB: {
-           startExpr(b);
+           startExpr(-1, b);
            consume(RB);
            if (peekToken() == ASSIGN) {
                consume(ASSIGN);
-               startExpr(b);
+               startExpr(-1, b);
                b.add(line, PUT);
                b.add(line, SWAP);
                b.add(line, POP);
@@ -319,12 +307,12 @@ class Parser extends Lexer implements ByteCodes {
        case HOOK: {
            b.add(line, JF, new Integer(0));
            int size = b.size();
-           startExpr(b);
+           startExpr(-1, b);
            b.set(size - 1, new Integer(b.size() - size + 2));
            b.add(line, JMP, new Integer(0));
            consume(COLON);
            size = b.size();
-           startExpr(b);
+           startExpr(-1, b);
            b.set(size - 1, new Integer(b.size() - size + 1));
            continueExpr(b, minPrecedence);
            return;
@@ -346,7 +334,8 @@ class Parser extends Lexer implements ByteCodes {
        return ret;
     }
 
-    public void parseStatement(boolean requireBraces, CompiledFunction b) throws IOException {
+    public void parseStatement(boolean r, CompiledFunction b) throws IOException { parseStatement(r, b, null); }
+    public void parseStatement(boolean requireBraces, CompiledFunction b, String label) throws IOException {
        int tok = peekToken();
        if (tok == -1) return;
        int line = this.line;
@@ -359,7 +348,7 @@ class Parser extends Lexer implements ByteCodes {
            case THROW: case RETURN: case ASSERT: {
                getToken();
                if (tok == RETURN && peekToken() == SEMI) b.add(line, LITERAL, null);
-               else startExpr(b);
+               else startExpr(-1, b);
                consume(SEMI);
                b.add(line, tok);
                break;
@@ -390,7 +379,7 @@ class Parser extends Lexer implements ByteCodes {
                    if (peekToken() == ASSIGN) {           // if there is an '=' after the variable name
                        b.add(line, LITERAL, name);            // put the var name back on the stack
                        consume(ASSIGN);
-                       startExpr(b);
+                       startExpr(-1, b);
                        b.add(line, PUT);
                        b.add(line, POP);
                    }
@@ -405,7 +394,7 @@ class Parser extends Lexer implements ByteCodes {
            case IF: {
                consume(IF);
                consume(LP);
-               startExpr(b);
+               startExpr(-1, b);
                consume(RP);
 
                b.add(line, JF, new Integer(0));
@@ -430,7 +419,7 @@ class Parser extends Lexer implements ByteCodes {
                b.add(line, LOOP);
                int size = b.size();
                b.add(line, POP);
-               startExpr(b);
+               startExpr(-1, b);
                b.add(line, JT, new Integer(2));
                b.add(line, BREAK);
                consume(RP);
@@ -446,14 +435,14 @@ class Parser extends Lexer implements ByteCodes {
                if (label != null) b.add(line, LABEL, label);
                b.add(line, LOOP);
                int size0 = b.size();
-               startExpr(b);
+               startExpr(-1, b);
                consume(RP);
                consume(LC);
                while(true)
                    if (peekToken() == CASE) {
                        consume(CASE);
                        b.add(line, DUP);
-                       startExpr(b);
+                       startExpr(-1, b);
                        consume(COLON);
                        b.add(line, EQ);
                        b.add(line, JF, new Integer(0));
@@ -492,7 +481,7 @@ class Parser extends Lexer implements ByteCodes {
                parseStatement(false, b);
                consume(WHILE);
                consume(LP);
-               startExpr(b);
+               startExpr(-1, b);
                b.add(line, JT, new Integer(2));
                b.add(line, BREAK);
                b.add(line, CONTINUE);
@@ -554,7 +543,7 @@ class Parser extends Lexer implements ByteCodes {
                    // FIXME: break needs to work in here
                    consume(NAME);
                    consume(IN);
-                   startExpr(b);
+                   startExpr(-1, b);
                    b.add(line, PUSHKEYS);
                    b.add(line, LITERAL, "length");
                    b.add(line, GET);
@@ -586,7 +575,7 @@ class Parser extends Lexer implements ByteCodes {
                    
                    parseStatement(false, b);
                    CompiledFunction e2 = new CompiledFunction(sourceName, line, null);
-                   startExpr(e2);
+                   startExpr(-1, e2);
                    consume(SEMI);
                    if (e2 == null) e2 = new CompiledFunction(sourceName, line, null).add(line, b.LITERAL, Boolean.TRUE);
                    
@@ -596,7 +585,7 @@ class Parser extends Lexer implements ByteCodes {
                    
                    b.add(line, JT, new Integer(0));
                    int size = b.size();
-                   startExpr(b);
+                   startExpr(-1, b);
                    if (b.size() > size) b.add(line, POP);
                    b.set(size - 1, new Integer(b.size() - size + 1));
                    consume(RP);
@@ -615,16 +604,15 @@ class Parser extends Lexer implements ByteCodes {
                
            case NAME: {
                consume(NAME);
-               String oldlabel = label;
-               label = string;
+               String possiblyTheLabel = string;
                if (peekToken() == COLON) {
                    consume(COLON);
-                   parseStatement(false, b);
-                   label = oldlabel;
+                   label = possiblyTheLabel;
+                   System.out.println("label == " + label);
+                   parseStatement(false, b, label);
                    break;
                } else {
-                   pushBackToken(NAME, label);
-                   label = oldlabel;
+                   pushBackToken(NAME, possiblyTheLabel);
                    // fall through to default case
                }
            }
@@ -634,7 +622,7 @@ class Parser extends Lexer implements ByteCodes {
             // fall through
            default: {
                int size = b.size();
-               startExpr(b);
+               startExpr(-1, b);
                if (size == b.size()) return;
                b.add(line, POP);
                if (peekToken() == SEMI) consume(SEMI);