use of org.codehaus.groovy.ast.stmt.BreakStatement in project groovy by apache.
the class FinalVariableAnalyzer method fallsThrough.
/**
* @return true if the block falls through, i.e. no break/return
*/
private boolean fallsThrough(Statement statement) {
if (statement instanceof EmptyStatement) {
return true;
}
if (statement instanceof ReturnStatement) {
// from ReturnAdder
return false;
}
// currently only possibility
BlockStatement block = (BlockStatement) statement;
if (block.getStatements().size() == 0) {
return true;
}
Statement last = DefaultGroovyMethods.last(block.getStatements());
boolean completesAbruptly = last instanceof ReturnStatement || last instanceof BreakStatement || last instanceof ThrowStatement || last instanceof ContinueStatement;
return !completesAbruptly;
}
use of org.codehaus.groovy.ast.stmt.BreakStatement in project groovy by apache.
the class AntlrParserPlugin method statement.
// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
Statement statement = null;
int type = node.getType();
switch(type) {
case SLIST:
case LITERAL_finally:
statement = statementList(node);
break;
case METHOD_CALL:
statement = methodCall(node);
break;
case VARIABLE_DEF:
statement = variableDef(node);
break;
case LABELED_STAT:
return labelledStatement(node);
case LITERAL_assert:
statement = assertStatement(node);
break;
case LITERAL_break:
statement = breakStatement(node);
break;
case LITERAL_continue:
statement = continueStatement(node);
break;
case LITERAL_if:
statement = ifStatement(node);
break;
case LITERAL_for:
statement = forStatement(node);
break;
case LITERAL_return:
statement = returnStatement(node);
break;
case LITERAL_synchronized:
statement = synchronizedStatement(node);
break;
case LITERAL_switch:
statement = switchStatement(node);
break;
case LITERAL_try:
statement = tryStatement(node);
break;
case LITERAL_throw:
statement = throwStatement(node);
break;
case LITERAL_while:
statement = whileStatement(node);
break;
default:
statement = new ExpressionStatement(expression(node));
}
if (statement != null) {
configureAST(statement, node);
}
return statement;
}
use of org.codehaus.groovy.ast.stmt.BreakStatement in project groovy by apache.
the class AntlrParserPlugin method breakStatement.
protected Statement breakStatement(AST node) {
BreakStatement breakStatement = new BreakStatement(label(node));
configureAST(breakStatement, node);
return breakStatement;
}
use of org.codehaus.groovy.ast.stmt.BreakStatement in project groovy by apache.
the class ReturnAdder method adjustSwitchCaseCode.
private Statement adjustSwitchCaseCode(final Statement statement, final VariableScope scope, final boolean lastCase) {
if (!statement.isEmpty() && statement instanceof BlockStatement) {
BlockStatement block = (BlockStatement) statement;
int breakIndex = block.getStatements().size() - 1;
if (block.getStatements().get(breakIndex) instanceof BreakStatement) {
if (doAdd) {
Statement breakStatement = block.getStatements().remove(breakIndex);
if (breakIndex == 0)
block.addStatement(EmptyStatement.INSTANCE);
addReturnsIfNeeded(block, scope);
// GROOVY-9880: some code structures will fall through
Statement lastStatement = last(block.getStatements());
if (!(lastStatement instanceof ReturnStatement || lastStatement instanceof ThrowStatement)) {
block.addStatement(breakStatement);
}
} else {
addReturnsIfNeeded(new BlockStatement(block.getStatements().subList(0, breakIndex), null), scope);
}
} else if (lastCase) {
return addReturnsIfNeeded(statement, scope);
}
}
return statement;
}
use of org.codehaus.groovy.ast.stmt.BreakStatement in project groovy by apache.
the class AstBuilder method visitBreakStatement.
@Override
public BreakStatement visitBreakStatement(final BreakStatementContext ctx) {
if (0 == visitingLoopStatementCount && 0 == visitingSwitchStatementCount) {
throw createParsingFailedException("break statement is only allowed inside loops or switches", ctx);
}
GroovyParserRuleContext gprc = switchExpressionRuleContextStack.peek();
if (gprc instanceof SwitchExpressionContext) {
throw createParsingFailedException("switch expression does not support `break`", ctx);
}
String label = asBoolean(ctx.identifier()) ? this.visitIdentifier(ctx.identifier()) : null;
return configureAST(new BreakStatement(label), ctx);
}
Aggregations