Search in sources :

Example 16 with AstNode

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

the class Parser method objectLiteral.

private ObjectLiteral objectLiteral() throws IOException {
    int pos = ts.tokenBeg, lineno = ts.lineno;
    int afterComma = -1;
    List<ObjectProperty> elems = new ArrayList<ObjectProperty>();
    Set<String> getterNames = null;
    Set<String> setterNames = null;
    if (this.inUseStrictDirective) {
        getterNames = new HashSet<String>();
        setterNames = new HashSet<String>();
    }
    Comment objJsdocNode = getAndResetJsDoc();
    commaLoop: for (; ; ) {
        String propertyName = null;
        int entryKind = PROP_ENTRY;
        int tt = peekToken();
        Comment jsdocNode = getAndResetJsDoc();
        if (tt == Token.RC) {
            if (afterComma != -1)
                warnTrailingComma(pos, elems, afterComma);
            break commaLoop;
        } else {
            AstNode pname = objliteralProperty();
            if (pname == null) {
                propertyName = null;
                reportError("msg.bad.prop");
            } else {
                propertyName = ts.getString();
                int ppos = ts.tokenBeg;
                consumeToken();
                // This code path needs to handle both destructuring object
                // literals like:
                // var {get, b} = {get: 1, b: 2};
                // and getters like:
                // var x = {get 1() { return 2; };
                // So we check a whitelist of tokens to check if we're at the
                // first case. (Because of keywords, the second case may be
                // many tokens.)
                int peeked = peekToken();
                if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
                    if (peeked == Token.LP) {
                        entryKind = METHOD_ENTRY;
                    } else if (pname.getType() == Token.NAME) {
                        if ("get".equals(propertyName)) {
                            entryKind = GET_ENTRY;
                        } else if ("set".equals(propertyName)) {
                            entryKind = SET_ENTRY;
                        }
                    }
                    if (entryKind == GET_ENTRY || entryKind == SET_ENTRY) {
                        pname = objliteralProperty();
                        if (pname == null) {
                            reportError("msg.bad.prop");
                        }
                        consumeToken();
                    }
                    if (pname == null) {
                        propertyName = null;
                    } else {
                        propertyName = ts.getString();
                        ObjectProperty objectProp = methodDefinition(ppos, pname, entryKind);
                        pname.setJsDocNode(jsdocNode);
                        elems.add(objectProp);
                    }
                } else {
                    pname.setJsDocNode(jsdocNode);
                    elems.add(plainProperty(pname, tt));
                }
            }
        }
        if (this.inUseStrictDirective && propertyName != null) {
            switch(entryKind) {
                case PROP_ENTRY:
                case METHOD_ENTRY:
                    if (getterNames.contains(propertyName) || setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    setterNames.add(propertyName);
                    break;
                case GET_ENTRY:
                    if (getterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    break;
                case SET_ENTRY:
                    if (setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    setterNames.add(propertyName);
                    break;
            }
        }
        // Eat any dangling jsdoc in the property.
        getAndResetJsDoc();
        if (matchToken(Token.COMMA)) {
            afterComma = ts.tokenEnd;
        } else {
            break commaLoop;
        }
    }
    mustMatchToken(Token.RC, "msg.no.brace.prop");
    ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
    if (objJsdocNode != null) {
        pn.setJsDocNode(objJsdocNode);
    }
    pn.setElements(elems);
    pn.setLineno(lineno);
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) Comment(org.mozilla.javascript.ast.Comment) ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) ArrayList(java.util.ArrayList) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 17 with AstNode

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

the class Parser method let.

// have to pass in 'let' kwd position to compute kid offsets properly
private AstNode let(boolean isStatement, int pos) throws IOException {
    LetNode pn = new LetNode(pos);
    pn.setLineno(ts.lineno);
    if (mustMatchToken(Token.LP, "msg.no.paren.after.let"))
        pn.setLp(ts.tokenBeg - pos);
    pushScope(pn);
    try {
        VariableDeclaration vars = variables(Token.LET, ts.tokenBeg, isStatement);
        pn.setVariables(vars);
        if (mustMatchToken(Token.RP, "msg.no.paren.let")) {
            pn.setRp(ts.tokenBeg - pos);
        }
        if (isStatement && peekToken() == Token.LC) {
            // let statement
            consumeToken();
            // position stmt at LC
            int beg = ts.tokenBeg;
            AstNode stmt = statements();
            mustMatchToken(Token.RC, "msg.no.curly.let");
            stmt.setLength(ts.tokenEnd - beg);
            pn.setLength(ts.tokenEnd - pos);
            pn.setBody(stmt);
            pn.setType(Token.LET);
        } else {
            // let expression
            AstNode expr = expr();
            pn.setLength(getNodeEnd(expr) - pos);
            pn.setBody(expr);
            if (isStatement) {
                // let expression in statement context
                ExpressionStatement es = new ExpressionStatement(pn, !insideFunction());
                es.setLineno(pn.getLineno());
                return es;
            }
        }
    } finally {
        popScope();
    }
    return pn;
}
Also used : LetNode(org.mozilla.javascript.ast.LetNode) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Example 18 with AstNode

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

the class Parser method memberExprTail.

/**
 * Parse any number of "(expr)", "[expr]" ".expr", "..expr",
 * or ".(expr)" constructs trailing the passed expression.
 * @param pn the non-null parent node
 * @return the outermost (lexically last occurring) expression,
 * which will have the passed parent node as a descendant
 */
private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOException {
    // we no longer return null for errors, so this won't be null
    if (pn == null)
        codeBug();
    int pos = pn.getPosition();
    int lineno;
    tailLoop: for (; ; ) {
        int tt = peekToken();
        switch(tt) {
            case Token.DOT:
            case Token.DOTDOT:
                lineno = ts.lineno;
                pn = propertyAccess(tt, pn);
                pn.setLineno(lineno);
                break;
            case Token.DOTQUERY:
                consumeToken();
                int opPos = ts.tokenBeg, rp = -1;
                lineno = ts.lineno;
                mustHaveXML();
                setRequiresActivation();
                AstNode filter = expr();
                int end = getNodeEnd(filter);
                if (mustMatchToken(Token.RP, "msg.no.paren")) {
                    rp = ts.tokenBeg;
                    end = ts.tokenEnd;
                }
                XmlDotQuery q = new XmlDotQuery(pos, end - pos);
                q.setLeft(pn);
                q.setRight(filter);
                q.setOperatorPosition(opPos);
                q.setRp(rp - pos);
                q.setLineno(lineno);
                pn = q;
                break;
            case Token.LB:
                consumeToken();
                int lb = ts.tokenBeg, rb = -1;
                lineno = ts.lineno;
                AstNode expr = expr();
                end = getNodeEnd(expr);
                if (mustMatchToken(Token.RB, "msg.no.bracket.index")) {
                    rb = ts.tokenBeg;
                    end = ts.tokenEnd;
                }
                ElementGet g = new ElementGet(pos, end - pos);
                g.setTarget(pn);
                g.setElement(expr);
                g.setParens(lb, rb);
                g.setLineno(lineno);
                pn = g;
                break;
            case Token.LP:
                if (!allowCallSyntax) {
                    break tailLoop;
                }
                lineno = ts.lineno;
                consumeToken();
                checkCallRequiresActivation(pn);
                FunctionCall f = new FunctionCall(pos);
                f.setTarget(pn);
                // Assign the line number for the function call to where
                // the paren appeared, not where the name expression started.
                f.setLineno(lineno);
                f.setLp(ts.tokenBeg - pos);
                List<AstNode> args = argumentList();
                if (args != null && args.size() > ARGC_LIMIT)
                    reportError("msg.too.many.function.args");
                f.setArguments(args);
                f.setRp(ts.tokenBeg - pos);
                f.setLength(ts.tokenEnd - pos);
                pn = f;
                break;
            default:
                break tailLoop;
        }
    }
    return pn;
}
Also used : XmlDotQuery(org.mozilla.javascript.ast.XmlDotQuery) List(java.util.List) ArrayList(java.util.ArrayList) FunctionCall(org.mozilla.javascript.ast.FunctionCall) AstNode(org.mozilla.javascript.ast.AstNode) ElementGet(org.mozilla.javascript.ast.ElementGet)

Example 19 with AstNode

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

the class Parser method arrayLiteral.

/**
 * May return an {@link ArrayLiteral} or {@link ArrayComprehension}.
 */
private AstNode arrayLiteral() throws IOException {
    if (currentToken != Token.LB)
        codeBug();
    int pos = ts.tokenBeg, end = ts.tokenEnd;
    List<AstNode> elements = new ArrayList<AstNode>();
    ArrayLiteral pn = new ArrayLiteral(pos);
    boolean after_lb_or_comma = true;
    int afterComma = -1;
    int skipCount = 0;
    for (; ; ) {
        int tt = peekToken();
        if (tt == Token.COMMA) {
            consumeToken();
            afterComma = ts.tokenEnd;
            if (!after_lb_or_comma) {
                after_lb_or_comma = true;
            } else {
                elements.add(new EmptyExpression(ts.tokenBeg, 1));
                skipCount++;
            }
        } else if (tt == Token.RB) {
            consumeToken();
            // for ([a,] in obj) is legal, but for ([a] in obj) is
            // not since we have both key and value supplied. The
            // trick is that [a,] and [a] are equivalent in other
            // array literal contexts. So we calculate a special
            // length value just for destructuring assignment.
            end = ts.tokenEnd;
            pn.setDestructuringLength(elements.size() + (after_lb_or_comma ? 1 : 0));
            pn.setSkipCount(skipCount);
            if (afterComma != -1)
                warnTrailingComma(pos, elements, afterComma);
            break;
        } else if (tt == Token.FOR && !after_lb_or_comma && elements.size() == 1) {
            return arrayComprehension(elements.get(0), pos);
        } else if (tt == Token.EOF) {
            reportError("msg.no.bracket.arg");
            break;
        } else {
            if (!after_lb_or_comma) {
                reportError("msg.no.bracket.arg");
            }
            elements.add(assignExpr());
            after_lb_or_comma = false;
            afterComma = -1;
        }
    }
    for (AstNode e : elements) {
        pn.addElement(e);
    }
    pn.setLength(end - pos);
    return pn;
}
Also used : ArrayList(java.util.ArrayList) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 20 with AstNode

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

the class Parser method checkBadIncDec.

private void checkBadIncDec(UnaryExpression expr) {
    AstNode op = removeParens(expr.getOperand());
    int tt = op.getType();
    if (!(tt == Token.NAME || tt == Token.GETPROP || tt == Token.GETELEM || tt == Token.GET_REF || tt == Token.CALL))
        reportError(expr.getType() == Token.INC ? "msg.bad.incr" : "msg.bad.decr");
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode)

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