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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations