Search in sources :

Example 46 with AstNode

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

the class IRFactory method decompileArrayLiteral.

// used for destructuring forms, since we don't transform() them
void decompileArrayLiteral(ArrayLiteral node) {
    decompiler.addToken(Token.LB);
    List<AstNode> elems = node.getElements();
    int size = elems.size();
    for (int i = 0; i < size; i++) {
        AstNode elem = elems.get(i);
        decompile(elem);
        if (i < size - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RB);
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode)

Example 47 with AstNode

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

the class Parser method assignExpr.

private AstNode assignExpr() throws IOException {
    int tt = peekToken();
    if (tt == Token.YIELD) {
        return returnOrYield(tt, true);
    }
    AstNode pn = condExpr();
    boolean hasEOL = false;
    tt = peekTokenOrEOL();
    if (tt == Token.EOL) {
        hasEOL = true;
        tt = peekToken();
    }
    if (Token.FIRST_ASSIGN <= tt && tt <= Token.LAST_ASSIGN) {
        consumeToken();
        // Pull out JSDoc info and reset it before recursing.
        Comment jsdocNode = getAndResetJsDoc();
        markDestructuring(pn);
        int opPos = ts.tokenBeg;
        pn = new Assignment(tt, pn, assignExpr(), opPos);
        if (jsdocNode != null) {
            pn.setJsDocNode(jsdocNode);
        }
    } else if (tt == Token.SEMI) {
        // For example: /** @type Number */ C.prototype.x;
        if (currentJsDocComment != null) {
            pn.setJsDocNode(getAndResetJsDoc());
        }
    } else if (!hasEOL && tt == Token.ARROW) {
        consumeToken();
        pn = arrowFunction(pn);
    }
    return pn;
}
Also used : Assignment(org.mozilla.javascript.ast.Assignment) Comment(org.mozilla.javascript.ast.Comment) AstNode(org.mozilla.javascript.ast.AstNode)

Example 48 with AstNode

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

the class Parser method condExpr.

private AstNode condExpr() throws IOException {
    AstNode pn = orExpr();
    if (matchToken(Token.HOOK)) {
        int line = ts.lineno;
        int qmarkPos = ts.tokenBeg, colonPos = -1;
        /*
             * Always accept the 'in' operator in the middle clause of a ternary,
             * where it's unambiguous, even if we might be parsing the init of a
             * for statement.
             */
        boolean wasInForInit = inForInit;
        inForInit = false;
        AstNode ifTrue;
        try {
            ifTrue = assignExpr();
        } finally {
            inForInit = wasInForInit;
        }
        if (mustMatchToken(Token.COLON, "msg.no.colon.cond"))
            colonPos = ts.tokenBeg;
        AstNode ifFalse = assignExpr();
        int beg = pn.getPosition(), len = getNodeEnd(ifFalse) - beg;
        ConditionalExpression ce = new ConditionalExpression(beg, len);
        ce.setLineno(line);
        ce.setTestExpression(pn);
        ce.setTrueExpression(ifTrue);
        ce.setFalseExpression(ifFalse);
        ce.setQuestionMarkPosition(qmarkPos - beg);
        ce.setColonPosition(colonPos - beg);
        pn = ce;
    }
    return pn;
}
Also used : ConditionalExpression(org.mozilla.javascript.ast.ConditionalExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 49 with AstNode

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

the class Parser method function.

private FunctionNode function(int type) throws IOException {
    int syntheticType = type;
    // line number where source starts
    int baseLineno = ts.lineno;
    // start of "function" kwd
    int functionSourceStart = ts.tokenBeg;
    Name name = null;
    AstNode memberExprNode = null;
    if (matchToken(Token.NAME)) {
        name = createNameNode(true, Token.NAME);
        if (inUseStrictDirective) {
            String id = name.getIdentifier();
            if ("eval".equals(id) || "arguments".equals(id)) {
                reportError("msg.bad.id.strict", id);
            }
        }
        if (!matchToken(Token.LP)) {
            if (compilerEnv.isAllowMemberExprAsFunctionName()) {
                AstNode memberExprHead = name;
                name = null;
                memberExprNode = memberExprTail(false, memberExprHead);
            }
            mustMatchToken(Token.LP, "msg.no.paren.parms");
        }
    } else if (matchToken(Token.LP)) {
    // Anonymous function:  leave name as null
    } else {
        if (compilerEnv.isAllowMemberExprAsFunctionName()) {
            // Note that memberExpr can not start with '(' like
            // in function (1+2).toString(), because 'function (' already
            // processed as anonymous function
            memberExprNode = memberExpr(false);
        }
        mustMatchToken(Token.LP, "msg.no.paren.parms");
    }
    int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1;
    if (memberExprNode != null) {
        syntheticType = FunctionNode.FUNCTION_EXPRESSION;
    }
    if (syntheticType != FunctionNode.FUNCTION_EXPRESSION && name != null && name.length() > 0) {
        // Function statements define a symbol in the enclosing scope
        defineSymbol(Token.FUNCTION, name.getIdentifier());
    }
    FunctionNode fnNode = new FunctionNode(functionSourceStart, name);
    fnNode.setFunctionType(type);
    if (lpPos != -1)
        fnNode.setLp(lpPos - functionSourceStart);
    fnNode.setJsDocNode(getAndResetJsDoc());
    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        parseFunctionParams(fnNode);
        fnNode.setBody(parseFunctionBody(type, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);
        if (compilerEnv.isStrictMode() && !fnNode.getBody().hasConsistentReturnUsage()) {
            String msg = (name != null && name.length() > 0) ? "msg.no.return.value" : "msg.anon.no.return.value";
            addStrictWarning(msg, name == null ? "" : name.getIdentifier());
        }
    } finally {
        savedVars.restore();
    }
    if (memberExprNode != null) {
        // TODO(stevey): fix missing functionality
        Kit.codeBug();
        // rewrite later
        fnNode.setMemberExprNode(memberExprNode);
    /* old code:
            if (memberExprNode != null) {
                pn = nf.createAssignment(Token.ASSIGN, memberExprNode, pn);
                if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
                    // XXX check JScript behavior: should it be createExprStatement?
                    pn = nf.createExprStatementNoReturn(pn, baseLineno);
                }
            }
            */
    }
    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);
    // at the function boundary when checking for redeclarations.
    if (compilerEnv.isIdeMode()) {
        fnNode.setParentScope(currentScope);
    }
    return fnNode;
}
Also used : FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 50 with AstNode

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

the class Parser method forLoopInit.

private AstNode forLoopInit(int tt) throws IOException {
    try {
        // checked by variables() and relExpr()
        inForInit = true;
        AstNode init = null;
        if (tt == Token.SEMI) {
            init = new EmptyExpression(ts.tokenBeg, 1);
            init.setLineno(ts.lineno);
        } else if (tt == Token.VAR || tt == Token.LET) {
            consumeToken();
            init = variables(tt, ts.tokenBeg, false);
        } else {
            init = expr();
            markDestructuring(init);
        }
        return init;
    } finally {
        inForInit = false;
    }
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

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