Search in sources :

Example 1 with SwitchStatement

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

the class RhinoJavaScriptBuilder method switchStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode switchStatement(AstNode expr, Iterable<AstNode> cases) {
    SwitchStatement s = new SwitchStatement();
    s.setExpression(expr);
    for (AstNode c : cases) {
        // the user must make sure it sends the correct types. TODO the code can check and build the correct type if
        // needed
        s.addCase((SwitchCase) c);
    }
    return s;
}
Also used : SwitchStatement(org.mozilla.javascript.ast.SwitchStatement) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with SwitchStatement

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

AstNode (org.mozilla.javascript.ast.AstNode)2 SwitchStatement (org.mozilla.javascript.ast.SwitchStatement)2 SwitchCase (org.mozilla.javascript.ast.SwitchCase)1