2003/05/26 10:38:33
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index f70a13d..ba343cf 100644 (file)
@@ -36,8 +36,8 @@ public class Parser extends Lexer {
        isRightAssociative[ASSIGN] = true;
        precedence[HOOK] = 2;
        precedence[COMMA] = 3;
-       precedence[GT] = precedence[GE] = 4;
-       precedence[OR] = precedence[AND] = 5;
+       precedence[OR] = precedence[AND] = 4;
+       precedence[GT] = precedence[GE] = 5;
        precedence[BITOR] = 6;
        precedence[BITXOR] = 7;
        precedence[BITAND] = 8;
@@ -61,6 +61,7 @@ public class Parser extends Lexer {
     public Expr parseBlock(boolean requireBraces) throws IOException {
        Expr ret = null;
        int tok = peekToken();
+       if (tok == -1) return null;
        boolean braced = tok == LC;
        if (requireBraces && !braced) throw new ParserException("expected {");
        if (braced) getToken();
@@ -75,6 +76,11 @@ public class Parser extends Lexer {
            case LC: smt = parseBlock(true); break;
            case THROW: case RETURN: case ASSERT:
                getToken();
+               if (peekToken() == SEMI) {
+                   getToken();
+                   smt = new Expr(curLine, tok);
+                   break;
+               }
                smt = new Expr(curLine, tok, parseMaximalExpr());
                if (getToken() != SEMI) throw new ParserException("expected ;");
                break;
@@ -122,11 +128,13 @@ public class Parser extends Lexer {
            default:
                smt = parseMaximalExpr();
                if (smt == null) {
-                   if (head == null) throw new ParserException("empty statement list; next token is " + codeToString[peekToken()]);
+                   if (head == null) return null;
                    break OUTER;
                }
+               if (peekToken() == SEMI) getToken();
                break;
            }
+           if (!braced) return smt;
            if (head == null) head = tail = smt; else tail = (tail.next = smt);
        }
        return new Expr(curLine, LC, head);
@@ -339,7 +347,7 @@ public class Parser extends Lexer {
            if (prefix != null) { pushBackToken(); return prefix; }
            while(true) {
                if (getToken() != NAME) throw new ParserException("variable declarations must start with a variable name");
-               Expr name = new Expr(curLine, NAME, string);
+               String name = string;
                Expr initVal = null;
                tok = peekToken();
                Expr e = null;
@@ -347,9 +355,13 @@ public class Parser extends Lexer {
                    getToken();
                    initVal = parseMaximalExpr();
                    tok = peekToken();
-                   e = new Expr(curLine, ASSIGN, name, initVal);
+                   e = new Expr(curLine, VAR,
+                                new Expr(curLine, name),
+                                new Expr(curLine, ASSIGN,
+                                         new Expr(curLine, name),
+                                         initVal));
                } else {
-                   e = new Expr(curLine, NAME, name);
+                   e = new Expr(curLine, VAR, new Expr(curLine, name));
                }
                if (head == null) head = tail = e; else tail = tail.next = e;
                if (tok != COMMA) break;
@@ -392,9 +404,7 @@ public class Parser extends Lexer {
            if (tok == IF && peekToken() == ELSE) {
                getToken();
                return new Expr(curLine, tok, parenExpr, new Expr(curLine, ELSE, firstBlock, parseBlock(false)));
-           } else if (tok == IF) {
-               return new Expr(curLine, tok, parenExpr, new Expr(curLine, ELSE, firstBlock, new Expr(curLine, LC)));
-           } else if (tok == WHILE) {
+           } else {
                return new Expr(curLine, tok, parenExpr, firstBlock);
            }
        }
@@ -404,20 +414,25 @@ public class Parser extends Lexer {
        case FOR:
            if (prefix != null) { pushBackToken(); return prefix; }
            if (getToken() != LP) throw new ParserException("expected left paren");
-           e1 = parseMaximalExpr(null, -1);
+           e1 = parseMaximalExpr();
            if (peekToken() == IN) {
                getToken();
-               e2 = parseMaximalExpr(null, -1);
+               e2 = parseMaximalExpr();
                if (getToken() != RP) throw new ParserException("expected right paren");
-               return new Expr(curLine, FOR, new Expr(curLine, IN, e1, e2), parseBlock(false));
+               return new Expr(curLine, FOR, new Expr(curLine, IN, e1.left, e2), parseBlock(false));
                
            } else {
-               if (getToken() != SEMI) throw new ParserException("expected ;");
-               e2 = parseMaximalExpr();
-               if (getToken() != SEMI) throw new ParserException("expected ;");
-               e3 = parseMaximalExpr();
-               if (getToken() != RP) throw new ParserException("expected right paren");
-               return new Expr(curLine, LC, e1, new Expr(curLine, WHILE, e2, new Expr(curLine, LC, parseBlock(false), e3)));
+               Expr initExpr = e1;
+               expect(SEMI); getToken();
+               Expr whileExpr = parseMaximalExpr();
+               expect(SEMI); getToken();
+               Expr incExpr = parseMaximalExpr();
+               expect(RP); getToken();
+               Expr body = parseBlock(false);
+               body.next = incExpr;
+               Expr loop = new Expr(curLine, WHILE, whileExpr, body);
+               if (initExpr == null) initExpr = loop; else initExpr.next = loop;
+               return new Expr(curLine, LC, initExpr);
            }
            
        case DO: {
@@ -518,13 +533,24 @@ public class Parser extends Lexer {
            case Lexer.RSH: return new Long(toLong(left.eval(s)) >> toLong(right.eval(s)));
            case Lexer.URSH: return new Long(toLong(left.eval(s)) >>> toLong(right.eval(s)));
 
+               // FIXME: these need to work on strings
            case Lexer.LT: return toDouble(left.eval(s)) < toDouble(right.eval(s)) ? Boolean.TRUE : Boolean.FALSE;
            case Lexer.LE: return toDouble(left.eval(s)) <= toDouble(right.eval(s)) ? Boolean.TRUE : Boolean.FALSE;
            case Lexer.GT: return toDouble(left.eval(s)) > toDouble(right.eval(s)) ? Boolean.TRUE : Boolean.FALSE;
            case Lexer.GE: return toDouble(left.eval(s)) >= toDouble(right.eval(s)) ? Boolean.TRUE : Boolean.FALSE;
 
-           case Lexer.OR: return new Boolean(toBoolean(left.eval(s)) || toBoolean(right.eval(s)));
-           case Lexer.AND: return new Boolean(toBoolean(left.eval(s)) && toBoolean(right.eval(s)));
+           case Lexer.OR: {
+               boolean b1 = toBoolean(left.eval(s));
+               if (b1) return Boolean.TRUE;
+               return new Boolean(b1 || toBoolean(right.eval(s)));
+           }
+
+           case Lexer.AND: {
+               boolean b1 = toBoolean(left.eval(s));
+               if (!b1) return Boolean.FALSE;
+               return new Boolean(b1 && toBoolean(right.eval(s)));
+           }
+
            case Lexer.BANG: return new Boolean(!toBoolean(left.eval(s)));
 
            case Lexer.EQ:
@@ -532,7 +558,14 @@ public class Parser extends Lexer {
                // FIXME: should use Javascript coercion-equality rules
                Object l = left.eval(s);
                Object r = right.eval(s);
-               boolean ret = (l == null && r == null) || (l != null && l.equals(r));
+               boolean ret;
+               if (l == null) { Object t = r; r = l; l = t; }
+               if (l == null && r == null) ret = true;
+               else if (l instanceof Boolean) ret = new Boolean(toBoolean(r)).equals(l);
+               else if (l instanceof Number) ret = toNumber(r).doubleValue() == toNumber(l).doubleValue();
+               else if (l instanceof String) ret = r != null && l.equals(r.toString());
+               else ret = l.equals(r);
+               
                return new Boolean(code == Lexer.EQ ? ret : !ret);
            }
 
@@ -549,7 +582,7 @@ public class Parser extends Lexer {
                    } else if (o instanceof Boolean) {
                        throw new EvaluatorException("can't set properties on a Boolean");
                    } else {
-                       JS target = (JS)left.left.eval(s);
+                       JS target = (JS)o;
                        if (target == null) throw new JS.Exn(new EvaluatorException("attempted to put to the null value"));
                        target.put(left.right.eval(s), v);
                        return v;
@@ -581,10 +614,10 @@ public class Parser extends Lexer {
            case Lexer.THROW: throw new JS.Exn(left.eval(s));
            case Lexer.NAME: return s.get(string);
            case Lexer.THIS: return s.isTransparent() ? s : this.eval(s.getParentScope());
-
            case Lexer.DOT: {
                final Object o = left.eval(s);
-               Object v = ((right.code == Lexer.NAME) || (right.code == Lexer.STRING)) ? right.string : right.eval(s);
+               if (o == null) throw new EvaluatorException("tried to get a property from the null value");
+               Object v = (right.code == Lexer.STRING) ? right.string : right.eval(s);
                if (o instanceof String) {
                    if (v.equals("length")) return new Integer(((String)o).length());
                    else if (v.equals("substring")) return new JS.Function() {
@@ -595,17 +628,35 @@ public class Parser extends Lexer {
                                else throw new EvaluatorException("String.substring() can only take one or two arguments");
                            }
                        };
+                   else if (v.equals("toLowerCase")) return new JS.Function() {
+                           public Object _call(JS.Array args) {
+                               return ((String)o).toLowerCase();
+                           } };
+                   else if (v.equals("toUpperCase")) return new JS.Function() {
+                           public Object _call(JS.Array args) {
+                               return ((String)o).toString().toUpperCase();
+                           } };
+                   else if (v.equals("charAt")) return new JS.Function() {
+                           public Object _call(JS.Array args) {
+                               return ((String)o).charAt(toNumber(args.elementAt(0)).intValue()) + "";
+                           } };
+                   else if (v.equals("lastIndexOf")) return new JS.Function() {
+                           public Object _call(JS.Array args) {
+                               if (args.length() != 1) return null;
+                               return new Integer(((String)o).lastIndexOf(args.elementAt(0).toString()));
+                           } };
                    else if (v.equals("indexOf")) return new JS.Function() {
                            public Object _call(JS.Array args) {
                                if (args.length() != 1) return null;
                                return new Integer(((String)o).indexOf(args.elementAt(0).toString()));
-                           }
-                       };
+                           } };
                    throw new EvaluatorException("Not Implemented: properties on String objects");
                } else if (o instanceof Boolean) {
                    throw new EvaluatorException("Not Implemented: properties on Boolean objects");
                } else if (o instanceof Number) {
-                   throw new EvaluatorException("Not Implemented: properties on Number objects");
+                   Log.log(this, "Not Implemented: properties on Number objects");
+                   return null;
+                   //throw new EvaluatorException("Not Implemented: properties on Number objects");
                } else if (o instanceof JS) {
                    return ((JS)o).get(v);
                }
@@ -652,7 +703,6 @@ public class Parser extends Lexer {
                            JS.Scope scope = new JS.Scope(s) {
                                    // FIXME
                                    public String getSourceName() { return sourceName; }
-                                   public boolean isTransparent() { return true; }
                                    public Object get(Object key) throws JS.Exn {
                                        if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
                                        else if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
@@ -661,7 +711,10 @@ public class Parser extends Lexer {
                                    }
                                };
                            int i = 0;
-                           for(Expr e = left; e != null; e = e.next) scope.put(e.string, args.get(new Integer(i++)));
+                           for(Expr e = left; e != null; e = e.next) {
+                               scope.declare(e.string);
+                               scope.put(e.string, args.get(new Integer(i++)));
+                           }
                            try {
                                return right.eval(scope);
                            } catch (ReturnException r) {
@@ -680,7 +733,8 @@ public class Parser extends Lexer {
                Object[] keys = ((JS)left.right.eval(s)).keys();
                for(int i=0; i<keys.length; i++) {
                    JS.Scope scope = new JS.Scope(s);
-                   scope.put(left.left.string, keys[i]);
+                   scope.declare(left.string);
+                   scope.put(left.string, keys[i]);
                    try {
                        right.eval(scope);
                    } catch (ContinueException c) {
@@ -713,28 +767,37 @@ public class Parser extends Lexer {
            }
            
            case Lexer.LC: // block
-               for(Expr e = left; e != null; e = e.next) e.eval(s);
+               JS.Scope scope = new JS.Scope(s);
+               for(Expr e = left; e != null; e = e.next) e.eval(scope);
                return null;
            
            case Lexer.RC: {   // Object ctor
                JS.Obj ret = new JS.Obj();
-               for(Expr e = left; e != null; e = e.next)
-                   ret.put(e.left.string, e.right.eval(s));
+               for(Expr e = left; e != null; e = e.next) {
+                   Object key = e.left.string;
+                   Object val = e.right.eval(s);
+                   ret.put(key, val);
+               }
                return ret;
            }
            
            case Lexer.VAR:
-               for(Expr e = left; e != null; e = e.next)
-                   if (e.code == Lexer.NAME) {
-                       s.declare(e.string);
-                   } else {
-                       s.declare(e.left.string);
-                       e.eval(s);
-                   }
+               for(Expr e = this.left; e != null; e = e.next) {
+                   s.declare(e.left.string);
+                   if (e.right != null) e.right.eval(s);
+               }
                return null;
 
            case Lexer.HOOK: return toBoolean(left.eval(s)) ? right.left.eval(s) : right.right.eval(s);
-           case Lexer.IF: return toBoolean(left.eval(s)) ? right.left.eval(s) : right.right.eval(s);
+           case Lexer.IF:
+               if (right.code == ELSE) {
+                   if (toBoolean(left.eval(s))) right.left.eval(s);
+                   else right.right.eval(s);
+                   return null;
+               } else {
+                   if (toBoolean(left.eval(s))) right.eval(s);
+                   return null;
+               }
            case Lexer.BREAK: throw new BreakException(string);
            case Lexer.CONTINUE: throw new ContinueException(string);
            case Lexer.RETURN: throw new ReturnException(left == null ? null : left.eval(s));
@@ -743,7 +806,8 @@ public class Parser extends Lexer {
            case Lexer.DO:
                try {
                    boolean first = true;
-                   while((first && code == Lexer.DO) || toBoolean(left.eval(s))) {
+                   Object bogus = null;
+                   for(;(first && code == Lexer.DO) || toBoolean(left.eval(s)); bogus = (right.next == null ? null : right.next.eval(s))) {
                        first = false;
                        try { right.eval(s);
                        } catch (ContinueException c) {
@@ -788,7 +852,7 @@ public class Parser extends Lexer {
        public static Number toNumber(Object o) {
            if (o == null) return new Long(0);
            if (o instanceof Number) return ((Number)o);
-           if (o instanceof String) return new Double((String)o);
+           if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(0); }
            if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
            if (o instanceof JS) return ((JS)o).coerceToNumber();
            // FIXME