Search in sources :

Example 1 with TryStatement

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

the class RhinoJavaScriptBuilder method tryStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode tryStatement(AstNode tryBlock, Iterable<AstNode> catchClauses, AstNode finallyBlock) {
    TryStatement t = new TryStatement();
    t.setTryBlock(tryBlock);
    for (AstNode c : catchClauses) {
        t.addCatchClause((CatchClause) c);
    }
    t.setFinallyBlock(finallyBlock);
    return t;
}
Also used : TryStatement(org.mozilla.javascript.ast.TryStatement) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with TryStatement

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

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)2 TryStatement (org.mozilla.javascript.ast.TryStatement)2 ArrayList (java.util.ArrayList)1 Block (org.mozilla.javascript.ast.Block)1 CatchClause (org.mozilla.javascript.ast.CatchClause)1 Comment (org.mozilla.javascript.ast.Comment)1 Name (org.mozilla.javascript.ast.Name)1 XmlString (org.mozilla.javascript.ast.XmlString)1