Search in sources :

Example 1 with ForInLoop

use of org.mozilla.javascript.ast.ForInLoop in project st-js by st-js.

the class RhinoJavaScriptBuilder method forInLoop.

/**
 * {@inheritDoc}
 */
@Override
public AstNode forInLoop(AstNode iterator, AstNode iterated, AstNode body) {
    ForInLoop loop = new ForInLoop();
    loop.setIteratedObject(iterated);
    loop.setIterator(iterator);
    loop.setBody(body);
    return loop;
}
Also used : ForInLoop(org.mozilla.javascript.ast.ForInLoop)

Example 2 with ForInLoop

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

the class Parser method forLoop.

private Loop forLoop() throws IOException {
    if (currentToken != Token.FOR)
        codeBug();
    consumeToken();
    int forPos = ts.tokenBeg, lineno = ts.lineno;
    boolean isForEach = false, isForIn = false, isForOf = false;
    int eachPos = -1, inPos = -1, lp = -1, rp = -1;
    // init is also foo in 'foo in object'
    AstNode init = null;
    // cond is also object in 'foo in object'
    AstNode cond = null;
    AstNode incr = null;
    Loop pn = null;
    Scope tempScope = new Scope();
    // decide below what AST class to use
    pushScope(tempScope);
    try {
        // See if this is a for each () instead of just a for ()
        if (matchToken(Token.NAME)) {
            if ("each".equals(ts.getString())) {
                isForEach = true;
                eachPos = ts.tokenBeg - forPos;
            } else {
                reportError("msg.no.paren.for");
            }
        }
        if (mustMatchToken(Token.LP, "msg.no.paren.for"))
            lp = ts.tokenBeg - forPos;
        int tt = peekToken();
        init = forLoopInit(tt);
        if (matchToken(Token.IN)) {
            isForIn = true;
            inPos = ts.tokenBeg - forPos;
            // object over which we're iterating
            cond = expr();
        } else if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6 && matchToken(Token.NAME) && "of".equals(ts.getString())) {
            isForOf = true;
            inPos = ts.tokenBeg - forPos;
            // object over which we're iterating
            cond = expr();
        } else {
            // ordinary for-loop
            mustMatchToken(Token.SEMI, "msg.no.semi.for");
            if (peekToken() == Token.SEMI) {
                // no loop condition
                cond = new EmptyExpression(ts.tokenBeg, 1);
                cond.setLineno(ts.lineno);
            } else {
                cond = expr();
            }
            mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");
            int tmpPos = ts.tokenEnd;
            if (peekToken() == Token.RP) {
                incr = new EmptyExpression(tmpPos, 1);
                incr.setLineno(ts.lineno);
            } else {
                incr = expr();
            }
        }
        if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
            rp = ts.tokenBeg - forPos;
        if (isForIn || isForOf) {
            ForInLoop fis = new ForInLoop(forPos);
            if (init instanceof VariableDeclaration) {
                // check that there was only one variable given
                if (((VariableDeclaration) init).getVariables().size() > 1) {
                    reportError("msg.mult.index");
                }
            }
            if (isForOf && isForEach) {
                reportError("msg.invalid.for.each");
            }
            fis.setIterator(init);
            fis.setIteratedObject(cond);
            fis.setInPosition(inPos);
            fis.setIsForEach(isForEach);
            fis.setEachPosition(eachPos);
            fis.setIsForOf(isForOf);
            pn = fis;
        } else {
            ForLoop fl = new ForLoop(forPos);
            fl.setInitializer(init);
            fl.setCondition(cond);
            fl.setIncrement(incr);
            pn = fl;
        }
        // replace temp scope with the new loop object
        currentScope.replaceWith(pn);
        popScope();
        // We have to parse the body -after- creating the loop node,
        // so that the loop node appears in the loopSet, allowing
        // break/continue statements to find the enclosing loop.
        enterLoop(pn);
        try {
            AstNode body = statement();
            pn.setLength(getNodeEnd(body) - forPos);
            pn.setBody(body);
        } finally {
            exitLoop();
        }
    } finally {
        if (currentScope == tempScope) {
            popScope();
        }
    }
    pn.setParens(lp, rp);
    pn.setLineno(lineno);
    return pn;
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) ForLoop(org.mozilla.javascript.ast.ForLoop) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) Loop(org.mozilla.javascript.ast.Loop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) DoLoop(org.mozilla.javascript.ast.DoLoop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) Scope(org.mozilla.javascript.ast.Scope) ForLoop(org.mozilla.javascript.ast.ForLoop) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Aggregations

ForInLoop (org.mozilla.javascript.ast.ForInLoop)2 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 AstNode (org.mozilla.javascript.ast.AstNode)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 ForLoop (org.mozilla.javascript.ast.ForLoop)1 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)1 Loop (org.mozilla.javascript.ast.Loop)1 Scope (org.mozilla.javascript.ast.Scope)1 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)1 WhileLoop (org.mozilla.javascript.ast.WhileLoop)1