Search in sources :

Example 1 with LabeledStatement

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

the class RhinoJavaScriptBuilder method labeledStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode labeledStatement(AstNode label, AstNode statement) {
    LabeledStatement s = new LabeledStatement();
    s.addLabel(cast(label, Label.class));
    s.setStatement(statement);
    return s;
}
Also used : LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) Label(org.mozilla.javascript.ast.Label)

Example 2 with LabeledStatement

use of org.mozilla.javascript.ast.LabeledStatement in project HL4A by HL4A.

the class Parser method breakStatement.

private BreakStatement breakStatement() throws IOException {
    if (currentToken != Token.BREAK)
        codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;
    Name breakLabel = null;
    if (peekTokenOrEOL() == Token.NAME) {
        breakLabel = createNameNode();
        end = getNodeEnd(breakLabel);
    }
    // matchJumpLabelName only matches if there is one
    LabeledStatement labels = matchJumpLabelName();
    // always use first label as target
    Jump breakTarget = labels == null ? null : labels.getFirstLabel();
    if (breakTarget == null && breakLabel == null) {
        if (loopAndSwitchSet == null || loopAndSwitchSet.size() == 0) {
            if (breakLabel == null) {
                reportError("msg.bad.break", pos, end - pos);
            }
        } else {
            breakTarget = loopAndSwitchSet.get(loopAndSwitchSet.size() - 1);
        }
    }
    BreakStatement pn = new BreakStatement(pos, end - pos);
    pn.setBreakLabel(breakLabel);
    // can be null if it's a bad break in error-recovery mode
    if (breakTarget != null)
        pn.setBreakTarget(breakTarget);
    pn.setLineno(lineno);
    return pn;
}
Also used : BreakStatement(org.mozilla.javascript.ast.BreakStatement) LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) Jump(org.mozilla.javascript.ast.Jump) Name(org.mozilla.javascript.ast.Name)

Example 3 with LabeledStatement

use of org.mozilla.javascript.ast.LabeledStatement in project HL4A by HL4A.

the class Parser method continueStatement.

private ContinueStatement continueStatement() throws IOException {
    if (currentToken != Token.CONTINUE)
        codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;
    Name label = null;
    if (peekTokenOrEOL() == Token.NAME) {
        label = createNameNode();
        end = getNodeEnd(label);
    }
    // matchJumpLabelName only matches if there is one
    LabeledStatement labels = matchJumpLabelName();
    Loop target = null;
    if (labels == null && label == null) {
        if (loopSet == null || loopSet.size() == 0) {
            reportError("msg.continue.outside");
        } else {
            target = loopSet.get(loopSet.size() - 1);
        }
    } else {
        if (labels == null || !(labels.getStatement() instanceof Loop)) {
            reportError("msg.continue.nonloop", pos, end - pos);
        }
        target = labels == null ? null : (Loop) labels.getStatement();
    }
    ContinueStatement pn = new ContinueStatement(pos, end - pos);
    if (// can be null in error-recovery mode
    target != null)
        pn.setTarget(target);
    pn.setLabel(label);
    pn.setLineno(lineno);
    return pn;
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) ForLoop(org.mozilla.javascript.ast.ForLoop) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) Loop(org.mozilla.javascript.ast.Loop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) DoLoop(org.mozilla.javascript.ast.DoLoop) LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) ContinueStatement(org.mozilla.javascript.ast.ContinueStatement) Name(org.mozilla.javascript.ast.Name)

Example 4 with LabeledStatement

use of org.mozilla.javascript.ast.LabeledStatement in project HL4A by HL4A.

the class Parser method nameOrLabel.

/**
 * Found a name in a statement context.  If it's a label, we gather
 * up any following labels and the next non-label statement into a
 * {@link LabeledStatement} "bundle" and return that.  Otherwise we parse
 * an expression and return it wrapped in an {@link ExpressionStatement}.
 */
private AstNode nameOrLabel() throws IOException {
    if (currentToken != Token.NAME)
        throw codeBug();
    int pos = ts.tokenBeg;
    // set check for label and call down to primaryExpr
    currentFlaggedToken |= TI_CHECK_LABEL;
    AstNode expr = expr();
    if (expr.getType() != Token.LABEL) {
        AstNode n = new ExpressionStatement(expr, !insideFunction());
        n.lineno = expr.lineno;
        return n;
    }
    LabeledStatement bundle = new LabeledStatement(pos);
    recordLabel((Label) expr, bundle);
    bundle.setLineno(ts.lineno);
    // look for more labels
    AstNode stmt = null;
    while (peekToken() == Token.NAME) {
        currentFlaggedToken |= TI_CHECK_LABEL;
        expr = expr();
        if (expr.getType() != Token.LABEL) {
            stmt = new ExpressionStatement(expr, !insideFunction());
            autoInsertSemicolon(stmt);
            break;
        }
        recordLabel((Label) expr, bundle);
    }
    // no more labels; now parse the labeled statement
    try {
        currentLabel = bundle;
        if (stmt == null) {
            stmt = statementHelper();
        }
    } finally {
        currentLabel = null;
        // remove the labels for this statement from the global set
        for (Label lb : bundle.getLabels()) {
            labelSet.remove(lb.getName());
        }
    }
    // If stmt has parent assigned its position already is relative
    // (See bug #710225)
    bundle.setLength(stmt.getParent() == null ? getNodeEnd(stmt) - pos : getNodeEnd(stmt));
    bundle.setStatement(stmt);
    return bundle;
}
Also used : LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) Label(org.mozilla.javascript.ast.Label) AstNode(org.mozilla.javascript.ast.AstNode)

Example 5 with LabeledStatement

use of org.mozilla.javascript.ast.LabeledStatement in project HL4A by HL4A.

the class Parser method recordLabel.

private void recordLabel(Label label, LabeledStatement bundle) throws IOException {
    // current token should be colon that primaryExpr left untouched
    if (peekToken() != Token.COLON)
        codeBug();
    consumeToken();
    String name = label.getName();
    if (labelSet == null) {
        labelSet = new HashMap<String, LabeledStatement>();
    } else {
        LabeledStatement ls = labelSet.get(name);
        if (ls != null) {
            if (compilerEnv.isIdeMode()) {
                Label dup = ls.getLabelByName(name);
                reportError("msg.dup.label", dup.getAbsolutePosition(), dup.getLength());
            }
            reportError("msg.dup.label", label.getPosition(), label.getLength());
        }
    }
    bundle.addLabel(label);
    labelSet.put(name, bundle);
}
Also used : LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) Label(org.mozilla.javascript.ast.Label) XmlString(org.mozilla.javascript.ast.XmlString)

Aggregations

LabeledStatement (org.mozilla.javascript.ast.LabeledStatement)5 Label (org.mozilla.javascript.ast.Label)3 Name (org.mozilla.javascript.ast.Name)2 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 AstNode (org.mozilla.javascript.ast.AstNode)1 BreakStatement (org.mozilla.javascript.ast.BreakStatement)1 ContinueStatement (org.mozilla.javascript.ast.ContinueStatement)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)1 ForInLoop (org.mozilla.javascript.ast.ForInLoop)1 ForLoop (org.mozilla.javascript.ast.ForLoop)1 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)1 Jump (org.mozilla.javascript.ast.Jump)1 Loop (org.mozilla.javascript.ast.Loop)1 WhileLoop (org.mozilla.javascript.ast.WhileLoop)1 XmlString (org.mozilla.javascript.ast.XmlString)1