Search in sources :

Example 56 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class Parser method unaryExpr.

private AstNode unaryExpr() throws IOException {
    AstNode node;
    int tt = peekToken();
    int line = ts.lineno;
    switch(tt) {
        case Token.VOID:
        case Token.NOT:
        case Token.BITNOT:
        case Token.TYPEOF:
            consumeToken();
            node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
            node.setLineno(line);
            return node;
        case Token.ADD:
            consumeToken();
            // Convert to special POS token in parse tree
            node = new UnaryExpression(Token.POS, ts.tokenBeg, unaryExpr());
            node.setLineno(line);
            return node;
        case Token.SUB:
            consumeToken();
            // Convert to special NEG token in parse tree
            node = new UnaryExpression(Token.NEG, ts.tokenBeg, unaryExpr());
            node.setLineno(line);
            return node;
        case Token.INC:
        case Token.DEC:
            consumeToken();
            UnaryExpression expr = new UnaryExpression(tt, ts.tokenBeg, memberExpr(true));
            expr.setLineno(line);
            checkBadIncDec(expr);
            return expr;
        case Token.DELPROP:
            consumeToken();
            node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
            node.setLineno(line);
            return node;
        case Token.ERROR:
            consumeToken();
            return makeErrorNode();
        case Token.LT:
            // XML stream encountered in expression.
            if (compilerEnv.isXmlAvailable()) {
                consumeToken();
                return memberExprTail(true, xmlInitializer());
            }
        default:
            AstNode pn = memberExpr(true);
            // Don't look across a newline boundary for a postfix incop.
            tt = peekTokenOrEOL();
            if (!(tt == Token.INC || tt == Token.DEC)) {
                return pn;
            }
            consumeToken();
            UnaryExpression uexpr = new UnaryExpression(tt, ts.tokenBeg, pn, true);
            uexpr.setLineno(line);
            checkBadIncDec(uexpr);
            return uexpr;
    }
}
Also used : UnaryExpression(org.mozilla.javascript.ast.UnaryExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 57 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class Parser method plainProperty.

private ObjectProperty plainProperty(AstNode property, int ptt) throws IOException {
    // Support, e.g., |var {x, y} = o| as destructuring shorthand
    // for |var {x: x, y: y} = o|, as implemented in spidermonkey JS 1.8.
    int tt = peekToken();
    if ((tt == Token.COMMA || tt == Token.RC) && ptt == Token.NAME && compilerEnv.getLanguageVersion() >= Context.VERSION_1_8) {
        if (!inDestructuringAssignment) {
            reportError("msg.bad.object.init");
        }
        AstNode nn = new Name(property.getPosition(), property.getString());
        ObjectProperty pn = new ObjectProperty();
        pn.putProp(Node.DESTRUCTURING_SHORTHAND, Boolean.TRUE);
        pn.setLeftAndRight(property, nn);
        return pn;
    }
    mustMatchToken(Token.COLON, "msg.no.colon.prop");
    ObjectProperty pn = new ObjectProperty();
    pn.setOperatorPosition(ts.tokenBeg);
    pn.setLeftAndRight(property, assignExpr());
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 58 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class Parser method switchStatement.

private SwitchStatement switchStatement() throws IOException {
    if (currentToken != Token.SWITCH)
        codeBug();
    consumeToken();
    int pos = ts.tokenBeg;
    SwitchStatement pn = new SwitchStatement(pos);
    if (mustMatchToken(Token.LP, "msg.no.paren.switch"))
        pn.setLp(ts.tokenBeg - pos);
    pn.setLineno(ts.lineno);
    AstNode discriminant = expr();
    pn.setExpression(discriminant);
    enterSwitch(pn);
    try {
        if (mustMatchToken(Token.RP, "msg.no.paren.after.switch"))
            pn.setRp(ts.tokenBeg - pos);
        mustMatchToken(Token.LC, "msg.no.brace.switch");
        boolean hasDefault = false;
        int tt;
        switchLoop: for (; ; ) {
            tt = nextToken();
            int casePos = ts.tokenBeg;
            int caseLineno = ts.lineno;
            AstNode caseExpression = null;
            switch(tt) {
                case Token.RC:
                    pn.setLength(ts.tokenEnd - pos);
                    break switchLoop;
                case Token.CASE:
                    caseExpression = expr();
                    mustMatchToken(Token.COLON, "msg.no.colon.case");
                    break;
                case Token.DEFAULT:
                    if (hasDefault) {
                        reportError("msg.double.switch.default");
                    }
                    hasDefault = true;
                    caseExpression = null;
                    mustMatchToken(Token.COLON, "msg.no.colon.case");
                    break;
                default:
                    reportError("msg.bad.switch");
                    break switchLoop;
            }
            SwitchCase caseNode = new SwitchCase(casePos);
            caseNode.setExpression(caseExpression);
            // include colon
            caseNode.setLength(ts.tokenEnd - pos);
            caseNode.setLineno(caseLineno);
            while ((tt = peekToken()) != Token.RC && tt != Token.CASE && tt != Token.DEFAULT && tt != Token.EOF) {
                // updates length
                caseNode.addStatement(statement());
            }
            pn.addCase(caseNode);
        }
    } finally {
        exitSwitch();
    }
    return pn;
}
Also used : SwitchStatement(org.mozilla.javascript.ast.SwitchStatement) SwitchCase(org.mozilla.javascript.ast.SwitchCase) AstNode(org.mozilla.javascript.ast.AstNode)

Example 59 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class Parser method withStatement.

private WithStatement withStatement() throws IOException {
    if (currentToken != Token.WITH)
        codeBug();
    consumeToken();
    Comment withComment = getAndResetJsDoc();
    int lineno = ts.lineno, pos = ts.tokenBeg, lp = -1, rp = -1;
    if (mustMatchToken(Token.LP, "msg.no.paren.with"))
        lp = ts.tokenBeg;
    AstNode obj = expr();
    if (mustMatchToken(Token.RP, "msg.no.paren.after.with"))
        rp = ts.tokenBeg;
    AstNode body = statement();
    WithStatement pn = new WithStatement(pos, getNodeEnd(body) - pos);
    pn.setJsDocNode(withComment);
    pn.setExpression(obj);
    pn.setStatement(body);
    pn.setParens(lp, rp);
    pn.setLineno(lineno);
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) WithStatement(org.mozilla.javascript.ast.WithStatement) AstNode(org.mozilla.javascript.ast.AstNode)

Example 60 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class Parser method variables.

/**
 * Parse a 'var' or 'const' statement, or a 'var' init list in a for
 * statement.
 * @param declType A token value: either VAR, CONST, or LET depending on
 * context.
 * @param pos the position where the node should start.  It's sometimes
 * the var/const/let keyword, and other times the beginning of the first
 * token in the first variable declaration.
 * @return the parsed variable list
 */
private VariableDeclaration variables(int declType, int pos, boolean isStatement) throws IOException {
    int end;
    VariableDeclaration pn = new VariableDeclaration(pos);
    pn.setType(declType);
    pn.setLineno(ts.lineno);
    Comment varjsdocNode = getAndResetJsDoc();
    if (varjsdocNode != null) {
        pn.setJsDocNode(varjsdocNode);
    }
    // var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar;
    for (; ; ) {
        AstNode destructuring = null;
        Name name = null;
        int tt = peekToken(), kidPos = ts.tokenBeg;
        end = ts.tokenEnd;
        if (tt == Token.LB || tt == Token.LC) {
            // Destructuring assignment, e.g., var [a,b] = ...
            destructuring = destructuringPrimaryExpr();
            end = getNodeEnd(destructuring);
            if (!(destructuring instanceof DestructuringForm))
                reportError("msg.bad.assign.left", kidPos, end - kidPos);
            markDestructuring(destructuring);
        } else {
            // Simple variable name
            mustMatchToken(Token.NAME, "msg.bad.var");
            name = createNameNode();
            name.setLineno(ts.getLineno());
            if (inUseStrictDirective) {
                String id = ts.getString();
                if ("eval".equals(id) || "arguments".equals(ts.getString())) {
                    reportError("msg.bad.id.strict", id);
                }
            }
            defineSymbol(declType, ts.getString(), inForInit);
        }
        int lineno = ts.lineno;
        Comment jsdocNode = getAndResetJsDoc();
        AstNode init = null;
        if (matchToken(Token.ASSIGN)) {
            init = assignExpr();
            end = getNodeEnd(init);
        }
        VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos);
        if (destructuring != null) {
            if (init == null && !inForInit) {
                reportError("msg.destruct.assign.no.init");
            }
            vi.setTarget(destructuring);
        } else {
            vi.setTarget(name);
        }
        vi.setInitializer(init);
        vi.setType(declType);
        vi.setJsDocNode(jsdocNode);
        vi.setLineno(lineno);
        pn.addVariable(vi);
        if (!matchToken(Token.COMMA))
            break;
    }
    pn.setLength(end - pos);
    pn.setIsStatement(isStatement);
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) DestructuringForm(org.mozilla.javascript.ast.DestructuringForm) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) XmlString(org.mozilla.javascript.ast.XmlString) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)80 FunctionNode (org.mozilla.javascript.ast.FunctionNode)22 LetNode (org.mozilla.javascript.ast.LetNode)22 ScriptNode (org.mozilla.javascript.ast.ScriptNode)19 XmlString (org.mozilla.javascript.ast.XmlString)15 InfixExpression (org.mozilla.javascript.ast.InfixExpression)13 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)9 Comment (org.mozilla.javascript.ast.Comment)8 Name (org.mozilla.javascript.ast.Name)8 ArrayList (java.util.ArrayList)6 Block (org.mozilla.javascript.ast.Block)5 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)5 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)5 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)4 ErrorNode (org.mozilla.javascript.ast.ErrorNode)4 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)4 DoLoop (org.mozilla.javascript.ast.DoLoop)3 Scope (org.mozilla.javascript.ast.Scope)3 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)3 IOException (java.io.IOException)2