Search in sources :

Example 1 with SwitchCase

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;
}
Also used : SwitchCase(org.mozilla.javascript.ast.SwitchCase) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with SwitchCase

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("}");
}
Also used : SwitchCase(org.mozilla.javascript.ast.SwitchCase)

Example 3 with SwitchCase

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;
}
Also used : SwitchCase(org.mozilla.javascript.ast.SwitchCase) 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) AstNode(org.mozilla.javascript.ast.AstNode)

Example 4 with SwitchCase

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;
}
Also used : SwitchStatement(org.mozilla.javascript.ast.SwitchStatement) SwitchCase(org.mozilla.javascript.ast.SwitchCase) AstNode(org.mozilla.javascript.ast.AstNode)

Aggregations

SwitchCase (org.mozilla.javascript.ast.SwitchCase)4 AstNode (org.mozilla.javascript.ast.AstNode)3 Block (org.mozilla.javascript.ast.Block)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 LetNode (org.mozilla.javascript.ast.LetNode)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 SwitchStatement (org.mozilla.javascript.ast.SwitchStatement)1