Search in sources :

Example 1 with Loop

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

the class IRFactory method transformVariables.

private Node transformVariables(VariableDeclaration node) {
    decompiler.addToken(node.getType());
    transformVariableInitializers(node);
    // Might be most robust to have parser record whether it was
    // a variable declaration statement, possibly as a node property.
    AstNode parent = node.getParent();
    if (!(parent instanceof Loop) && !(parent instanceof LetNode)) {
        decompiler.addEOL(Token.SEMI);
    }
    return node;
}
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) LetNode(org.mozilla.javascript.ast.LetNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with Loop

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

the class Parser method defineSymbol.

void defineSymbol(int declType, String name, boolean ignoreNotInBlock) {
    if (name == null) {
        if (compilerEnv.isIdeMode()) {
            // be robust in IDE-mode
            return;
        } else {
            codeBug();
        }
    }
    Scope definingScope = currentScope.getDefiningScope(name);
    Symbol symbol = definingScope != null ? definingScope.getSymbol(name) : null;
    int symDeclType = symbol != null ? symbol.getDeclType() : -1;
    if (symbol != null && (symDeclType == Token.CONST || declType == Token.CONST || (definingScope == currentScope && symDeclType == Token.LET))) {
        addError(symDeclType == Token.CONST ? "msg.const.redecl" : symDeclType == Token.LET ? "msg.let.redecl" : symDeclType == Token.VAR ? "msg.var.redecl" : symDeclType == Token.FUNCTION ? "msg.fn.redecl" : "msg.parm.redecl", name);
        return;
    }
    switch(declType) {
        case Token.LET:
            if (!ignoreNotInBlock && ((currentScope.getType() == Token.IF) || currentScope instanceof Loop)) {
                addError("msg.let.decl.not.in.block");
                return;
            }
            currentScope.putSymbol(new Symbol(declType, name));
            return;
        case Token.VAR:
        case Token.CONST:
        case Token.FUNCTION:
            if (symbol != null) {
                if (symDeclType == Token.VAR)
                    addStrictWarning("msg.var.redecl", name);
                else if (symDeclType == Token.LP) {
                    addStrictWarning("msg.var.hides.arg", name);
                }
            } else {
                currentScriptOrFn.putSymbol(new Symbol(declType, name));
            }
            return;
        case Token.LP:
            if (symbol != null) {
                // must be duplicate parameter. Second parameter hides the
                // first, so go ahead and add the second parameter
                addWarning("msg.dup.parms", name);
            }
            currentScriptOrFn.putSymbol(new Symbol(declType, name));
            return;
        default:
            throw codeBug();
    }
}
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) Scope(org.mozilla.javascript.ast.Scope) Symbol(org.mozilla.javascript.ast.Symbol)

Example 3 with Loop

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

the class Parser method exitLoop.

private void exitLoop() {
    Loop loop = loopSet.remove(loopSet.size() - 1);
    loopAndSwitchSet.remove(loopAndSwitchSet.size() - 1);
    if (loop.getParent() != null) {
        // see comment in enterLoop
        loop.setRelative(loop.getParent().getPosition());
    }
    popScope();
}
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)

Example 4 with Loop

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

the class Parser method continueStatement.

private ContinueStatement continueStatement() throws IOException {
    if (currentToken != Token.CONTINUE)
        codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;
    Name label = null;
    if (peekTokenOrEOL() == Token.NAME) {
        label = createNameNode();
        end = getNodeEnd(label);
    }
    // matchJumpLabelName only matches if there is one
    LabeledStatement labels = matchJumpLabelName();
    Loop target = null;
    if (labels == null && label == null) {
        if (loopSet == null || loopSet.size() == 0) {
            reportError("msg.continue.outside");
        } else {
            target = loopSet.get(loopSet.size() - 1);
        }
    } else {
        if (labels == null || !(labels.getStatement() instanceof Loop)) {
            reportError("msg.continue.nonloop", pos, end - pos);
        }
        target = labels == null ? null : (Loop) labels.getStatement();
    }
    ContinueStatement pn = new ContinueStatement(pos, end - pos);
    if (// can be null in error-recovery mode
    target != null)
        pn.setTarget(target);
    pn.setLabel(label);
    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) LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) ContinueStatement(org.mozilla.javascript.ast.ContinueStatement) Name(org.mozilla.javascript.ast.Name)

Example 5 with Loop

use of org.mozilla.javascript.ast.Loop 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

ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)5 DoLoop (org.mozilla.javascript.ast.DoLoop)5 ForInLoop (org.mozilla.javascript.ast.ForInLoop)5 ForLoop (org.mozilla.javascript.ast.ForLoop)5 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)5 Loop (org.mozilla.javascript.ast.Loop)5 WhileLoop (org.mozilla.javascript.ast.WhileLoop)5 AstNode (org.mozilla.javascript.ast.AstNode)2 Scope (org.mozilla.javascript.ast.Scope)2 ContinueStatement (org.mozilla.javascript.ast.ContinueStatement)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 LabeledStatement (org.mozilla.javascript.ast.LabeledStatement)1 LetNode (org.mozilla.javascript.ast.LetNode)1 Name (org.mozilla.javascript.ast.Name)1 Symbol (org.mozilla.javascript.ast.Symbol)1 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)1