Search in sources :

Example 1 with BreakStatement

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;
}
Also used : BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement)

Example 2 with BreakStatement

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;
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 3 with BreakStatement

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;
}
Also used : BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement)

Example 4 with 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;
}
Also used : BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 5 with BreakStatement

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);
}
Also used : BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) GroovyParserRuleContext(org.apache.groovy.parser.antlr4.GroovyParser.GroovyParserRuleContext) SwitchExpressionContext(org.apache.groovy.parser.antlr4.GroovyLangParser.SwitchExpressionContext)

Aggregations

BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)5 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)3 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)3 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)3 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)3 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)3 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)3 Statement (org.codehaus.groovy.ast.stmt.Statement)3 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)3 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)3 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)3 ContinueStatement (org.codehaus.groovy.ast.stmt.ContinueStatement)2 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)2 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)2 SwitchExpressionContext (org.apache.groovy.parser.antlr4.GroovyLangParser.SwitchExpressionContext)1 GroovyParserRuleContext (org.apache.groovy.parser.antlr4.GroovyParser.GroovyParserRuleContext)1 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)1 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)1 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)1