use of org.mozilla.javascript.ast.SwitchCase in project st-js by st-js.
the class RhinoJavaScriptBuilder method caseStatement.
/**
* {@inheritDoc}
*/
@Override
public AstNode caseStatement(AstNode expression, Iterable<AstNode> statements) {
SwitchCase s = new SwitchCase();
s.setExpression(expression);
for (AstNode stmt : statements) {
if (stmt != null) {
s.addStatement(stmt);
}
}
return s;
}
use of org.mozilla.javascript.ast.SwitchCase in project st-js by st-js.
the class RhinoJavaScriptWriter method visitSwitchStatement.
/**
* {@inheritDoc}
*/
@Override
public void visitSwitchStatement(SwitchStatement s, Boolean param) {
startPosition(s);
print("switch (");
visitorSupport.accept(s.getExpression(), this, param);
println(") {");
indent();
for (SwitchCase sc : s.getCases()) {
visitorSupport.accept(sc, this, param);
}
unindent();
println("}");
}
use of org.mozilla.javascript.ast.SwitchCase in project HL4A by HL4A.
the class IRFactory method transformSwitch.
private Node transformSwitch(SwitchStatement node) {
// The switch will be rewritten from:
//
// switch (expr) {
// case test1: statements1;
// ...
// default: statementsDefault;
// ...
// case testN: statementsN;
// }
//
// to:
//
// {
// switch (expr) {
// case test1: goto label1;
// ...
// case testN: goto labelN;
// }
// goto labelDefault;
// label1:
// statements1;
// ...
// labelDefault:
// statementsDefault;
// ...
// labelN:
// statementsN;
// breakLabel:
// }
//
// where inside switch each "break;" without label will be replaced
// by "goto breakLabel".
//
// If the original switch does not have the default label, then
// after the switch he transformed code would contain this goto:
// goto breakLabel;
// instead of:
// goto labelDefault;
decompiler.addToken(Token.SWITCH);
decompiler.addToken(Token.LP);
Node switchExpr = transform(node.getExpression());
decompiler.addToken(Token.RP);
node.addChildToBack(switchExpr);
Node block = new Node(Token.BLOCK, node, node.getLineno());
decompiler.addEOL(Token.LC);
for (SwitchCase sc : node.getCases()) {
AstNode expr = sc.getExpression();
Node caseExpr = null;
if (expr != null) {
decompiler.addToken(Token.CASE);
caseExpr = transform(expr);
} else {
decompiler.addToken(Token.DEFAULT);
}
decompiler.addEOL(Token.COLON);
List<AstNode> stmts = sc.getStatements();
Node body = new Block();
if (stmts != null) {
for (AstNode kid : stmts) {
body.addChildToBack(transform(kid));
}
}
addSwitchCase(block, caseExpr, body);
}
decompiler.addEOL(Token.RC);
closeSwitch(block);
return block;
}
use of org.mozilla.javascript.ast.SwitchCase in project HL4A by HL4A.
the class Parser method switchStatement.
private SwitchStatement switchStatement() throws IOException {
if (currentToken != Token.SWITCH)
codeBug();
consumeToken();
int pos = ts.tokenBeg;
SwitchStatement pn = new SwitchStatement(pos);
if (mustMatchToken(Token.LP, "msg.no.paren.switch"))
pn.setLp(ts.tokenBeg - pos);
pn.setLineno(ts.lineno);
AstNode discriminant = expr();
pn.setExpression(discriminant);
enterSwitch(pn);
try {
if (mustMatchToken(Token.RP, "msg.no.paren.after.switch"))
pn.setRp(ts.tokenBeg - pos);
mustMatchToken(Token.LC, "msg.no.brace.switch");
boolean hasDefault = false;
int tt;
switchLoop: for (; ; ) {
tt = nextToken();
int casePos = ts.tokenBeg;
int caseLineno = ts.lineno;
AstNode caseExpression = null;
switch(tt) {
case Token.RC:
pn.setLength(ts.tokenEnd - pos);
break switchLoop;
case Token.CASE:
caseExpression = expr();
mustMatchToken(Token.COLON, "msg.no.colon.case");
break;
case Token.DEFAULT:
if (hasDefault) {
reportError("msg.double.switch.default");
}
hasDefault = true;
caseExpression = null;
mustMatchToken(Token.COLON, "msg.no.colon.case");
break;
default:
reportError("msg.bad.switch");
break switchLoop;
}
SwitchCase caseNode = new SwitchCase(casePos);
caseNode.setExpression(caseExpression);
// include colon
caseNode.setLength(ts.tokenEnd - pos);
caseNode.setLineno(caseLineno);
while ((tt = peekToken()) != Token.RC && tt != Token.CASE && tt != Token.DEFAULT && tt != Token.EOF) {
// updates length
caseNode.addStatement(statement());
}
pn.addCase(caseNode);
}
} finally {
exitSwitch();
}
return pn;
}
Aggregations