Search in sources :

Example 1 with Block

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

the class RhinoJavaScriptBuilder method catchClause.

/**
 * {@inheritDoc}
 */
@Override
public AstNode catchClause(AstNode varName, AstNode body) {
    CatchClause c = new CatchClause();
    c.setVarName(cast(varName, Name.class));
    if (body instanceof Block) {
        c.setBody((Block) body);
    } else {
        Block b = new Block();
        b.addStatement(body);
        c.setBody(b);
    }
    return c;
}
Also used : Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) Name(org.mozilla.javascript.ast.Name)

Example 2 with Block

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

the class RhinoJavaScriptBuilder method addStatementBeginning.

/**
 * {@inheritDoc}
 */
@Override
public AstNode addStatementBeginning(AstNode blockOrStatement, AstNode statement) {
    if (blockOrStatement instanceof Block) {
        if (statement != null) {
            blockOrStatement.addChildrenToFront(statement);
            statement.setParent(blockOrStatement);
        }
        return blockOrStatement;
    }
    Block block = new Block();
    if (statement != null) {
        block.addStatement(statement);
    }
    if (blockOrStatement != null) {
        block.addStatement(blockOrStatement);
    }
    return block;
}
Also used : Block(org.mozilla.javascript.ast.Block)

Example 3 with Block

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

the class Parser method tryStatement.

private TryStatement tryStatement() throws IOException {
    if (currentToken != Token.TRY)
        codeBug();
    consumeToken();
    // Pull out JSDoc info and reset it before recursing.
    Comment jsdocNode = getAndResetJsDoc();
    int tryPos = ts.tokenBeg, lineno = ts.lineno, finallyPos = -1;
    if (peekToken() != Token.LC) {
        reportError("msg.no.brace.try");
    }
    AstNode tryBlock = statement();
    int tryEnd = getNodeEnd(tryBlock);
    List<CatchClause> clauses = null;
    boolean sawDefaultCatch = false;
    int peek = peekToken();
    if (peek == Token.CATCH) {
        while (matchToken(Token.CATCH)) {
            int catchLineNum = ts.lineno;
            if (sawDefaultCatch) {
                reportError("msg.catch.unreachable");
            }
            int catchPos = ts.tokenBeg, lp = -1, rp = -1, guardPos = -1;
            if (mustMatchToken(Token.LP, "msg.no.paren.catch"))
                lp = ts.tokenBeg;
            mustMatchToken(Token.NAME, "msg.bad.catchcond");
            Name varName = createNameNode();
            Comment jsdocNodeForName = getAndResetJsDoc();
            if (jsdocNodeForName != null) {
                varName.setJsDocNode(jsdocNodeForName);
            }
            String varNameString = varName.getIdentifier();
            if (inUseStrictDirective) {
                if ("eval".equals(varNameString) || "arguments".equals(varNameString)) {
                    reportError("msg.bad.id.strict", varNameString);
                }
            }
            AstNode catchCond = null;
            if (matchToken(Token.IF)) {
                guardPos = ts.tokenBeg;
                catchCond = expr();
            } else {
                sawDefaultCatch = true;
            }
            if (mustMatchToken(Token.RP, "msg.bad.catchcond"))
                rp = ts.tokenBeg;
            mustMatchToken(Token.LC, "msg.no.brace.catchblock");
            Block catchBlock = (Block) statements();
            tryEnd = getNodeEnd(catchBlock);
            CatchClause catchNode = new CatchClause(catchPos);
            catchNode.setVarName(varName);
            catchNode.setCatchCondition(catchCond);
            catchNode.setBody(catchBlock);
            if (guardPos != -1) {
                catchNode.setIfPosition(guardPos - catchPos);
            }
            catchNode.setParens(lp, rp);
            catchNode.setLineno(catchLineNum);
            if (mustMatchToken(Token.RC, "msg.no.brace.after.body"))
                tryEnd = ts.tokenEnd;
            catchNode.setLength(tryEnd - catchPos);
            if (clauses == null)
                clauses = new ArrayList<CatchClause>();
            clauses.add(catchNode);
        }
    } else if (peek != Token.FINALLY) {
        mustMatchToken(Token.FINALLY, "msg.try.no.catchfinally");
    }
    AstNode finallyBlock = null;
    if (matchToken(Token.FINALLY)) {
        finallyPos = ts.tokenBeg;
        finallyBlock = statement();
        tryEnd = getNodeEnd(finallyBlock);
    }
    TryStatement pn = new TryStatement(tryPos, tryEnd - tryPos);
    pn.setTryBlock(tryBlock);
    pn.setCatchClauses(clauses);
    pn.setFinallyBlock(finallyBlock);
    if (finallyPos != -1) {
        pn.setFinallyPosition(finallyPos - tryPos);
    }
    pn.setLineno(lineno);
    if (jsdocNode != null) {
        pn.setJsDocNode(jsdocNode);
    }
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) TryStatement(org.mozilla.javascript.ast.TryStatement) ArrayList(java.util.ArrayList) Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 4 with Block

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

the class RhinoJavaScriptBuilder method function.

/**
 * {@inheritDoc}
 */
@Override
public AstNode function(String name, Iterable<AstNode> params, AstNode body) {
    FunctionNode func = new FunctionNode();
    if (name != null) {
        func.setFunctionName((Name) name(name));
    }
    func.setParams(list(params));
    if (body == null) {
        func.setBody(new Block());
    } else if (body instanceof Block) {
        func.setBody(body);
    } else {
        func.setBody(addStatement(null, body));
    }
    return func;
}
Also used : FunctionNode(org.mozilla.javascript.ast.FunctionNode) Block(org.mozilla.javascript.ast.Block)

Example 5 with Block

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

the class IRFactory method transformTry.

private Node transformTry(TryStatement node) {
    decompiler.addToken(Token.TRY);
    decompiler.addEOL(Token.LC);
    Node tryBlock = transform(node.getTryBlock());
    decompiler.addEOL(Token.RC);
    Node catchBlocks = new Block();
    for (CatchClause cc : node.getCatchClauses()) {
        decompiler.addToken(Token.CATCH);
        decompiler.addToken(Token.LP);
        String varName = cc.getVarName().getIdentifier();
        decompiler.addName(varName);
        Node catchCond = null;
        AstNode ccc = cc.getCatchCondition();
        if (ccc != null) {
            decompiler.addName(" ");
            decompiler.addToken(Token.IF);
            catchCond = transform(ccc);
        } else {
            catchCond = new EmptyExpression();
        }
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(cc.getBody());
        decompiler.addEOL(Token.RC);
        catchBlocks.addChildToBack(createCatch(varName, catchCond, body, cc.getLineno()));
    }
    Node finallyBlock = null;
    if (node.getFinallyBlock() != null) {
        decompiler.addToken(Token.FINALLY);
        decompiler.addEOL(Token.LC);
        finallyBlock = transform(node.getFinallyBlock());
        decompiler.addEOL(Token.RC);
    }
    return createTryCatchFinally(tryBlock, catchBlocks, finallyBlock, node.getLineno());
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Aggregations

Block (org.mozilla.javascript.ast.Block)8 AstNode (org.mozilla.javascript.ast.AstNode)5 CatchClause (org.mozilla.javascript.ast.CatchClause)3 FunctionNode (org.mozilla.javascript.ast.FunctionNode)3 XmlString (org.mozilla.javascript.ast.XmlString)3 LetNode (org.mozilla.javascript.ast.LetNode)2 Name (org.mozilla.javascript.ast.Name)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 ArrayList (java.util.ArrayList)1 Comment (org.mozilla.javascript.ast.Comment)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 ReturnStatement (org.mozilla.javascript.ast.ReturnStatement)1 SwitchCase (org.mozilla.javascript.ast.SwitchCase)1 TryStatement (org.mozilla.javascript.ast.TryStatement)1