use of org.mozilla.javascript.ast.CatchClause 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;
}
use of org.mozilla.javascript.ast.CatchClause in project st-js by st-js.
the class RhinoJavaScriptWriter method visitTryStatement.
/**
* {@inheritDoc}
*/
@Override
public void visitTryStatement(TryStatement t, Boolean param) {
startPosition(t);
print("try ");
visitorSupport.accept(t.getTryBlock(), this, param);
for (CatchClause cc : t.getCatchClauses()) {
visitorSupport.accept(cc, this, param);
}
if (t.getFinallyBlock() != null) {
print(" finally ");
visitorSupport.accept(t.getFinallyBlock(), this, param);
}
println();
}
use of org.mozilla.javascript.ast.CatchClause 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;
}
use of org.mozilla.javascript.ast.CatchClause 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());
}
Aggregations