Search in sources :

Example 71 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class AntlrParserPlugin method dotExpression.

protected Expression dotExpression(AST node) {
    // let's decide if this is a property invocation or a method call
    AST leftNode = node.getFirstChild();
    if (leftNode != null) {
        AST identifierNode = leftNode.getNextSibling();
        if (identifierNode != null) {
            Expression leftExpression = expression(leftNode);
            if (isType(SELECT_SLOT, identifierNode)) {
                Expression field = expression(identifierNode.getFirstChild(), true);
                AttributeExpression attributeExpression = new AttributeExpression(leftExpression, field, node.getType() != DOT);
                if (node.getType() == SPREAD_DOT) {
                    attributeExpression.setSpreadSafe(true);
                }
                configureAST(attributeExpression, node);
                return attributeExpression;
            }
            if (isType(SLIST, identifierNode)) {
                Statement code = statementList(identifierNode);
                ClosureExpression closureExpression = new ClosureExpression(Parameter.EMPTY_ARRAY, code);
                configureAST(closureExpression, identifierNode);
                final PropertyExpression propertyExpression = new PropertyExpression(leftExpression, closureExpression);
                if (node.getType() == SPREAD_DOT) {
                    propertyExpression.setSpreadSafe(true);
                }
                configureAST(propertyExpression, node);
                return propertyExpression;
            }
            Expression property = expression(identifierNode, true);
            // we correct that here into a ConstantExpression
            if (property instanceof VariableExpression) {
                VariableExpression ve = (VariableExpression) property;
                property = new ConstantExpression(ve.getName());
            }
            PropertyExpression propertyExpression = new PropertyExpression(leftExpression, property, node.getType() != DOT);
            if (node.getType() == SPREAD_DOT) {
                propertyExpression.setSpreadSafe(true);
            }
            configureAST(propertyExpression, node);
            return propertyExpression;
        }
    }
    return methodCallExpression(node);
}
Also used : AST(antlr.collections.AST) 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)

Example 72 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class AntlrParserPlugin method synchronizedStatement.

protected Statement synchronizedStatement(AST syncNode) {
    AST node = syncNode.getFirstChild();
    Expression expression = expression(node);
    Statement code = statement(node.getNextSibling());
    SynchronizedStatement synchronizedStatement = new SynchronizedStatement(expression, code);
    configureAST(synchronizedStatement, syncNode);
    return synchronizedStatement;
}
Also used : AST(antlr.collections.AST) 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) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 73 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class AntlrParserPlugin method blockExpression.

protected Expression blockExpression(AST node) {
    AST codeNode = node.getFirstChild();
    if (codeNode == null)
        return ConstantExpression.NULL;
    if (codeNode.getType() == EXPR && codeNode.getNextSibling() == null) {
        // Simplify common case of {expr} to expr.
        return expression(codeNode);
    }
    Parameter[] parameters = Parameter.EMPTY_ARRAY;
    Statement code = statementListNoChild(codeNode, node);
    ClosureExpression closureExpression = new ClosureExpression(parameters, code);
    configureAST(closureExpression, node);
    // Call it immediately.
    String callName = "call";
    Expression noArguments = new ArgumentListExpression();
    MethodCallExpression call = new MethodCallExpression(closureExpression, callName, noArguments);
    configureAST(call, node);
    return call;
}
Also used : AST(antlr.collections.AST) 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) Parameter(org.codehaus.groovy.ast.Parameter)

Example 74 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class AntlrParserPlugin method forStatement.

protected Statement forStatement(AST forNode) {
    AST inNode = forNode.getFirstChild();
    Expression collectionExpression;
    Parameter forParameter;
    if (isType(CLOSURE_LIST, inNode)) {
        forStatementBeingDef = true;
        ClosureListExpression clist = closureListExpression(inNode);
        forStatementBeingDef = false;
        int size = clist.getExpressions().size();
        if (size != 3) {
            throw new ASTRuntimeException(inNode, "3 expressions are required for the classic for loop, you gave " + size);
        }
        collectionExpression = clist;
        forParameter = ForStatement.FOR_LOOP_DUMMY;
    } else {
        AST variableNode = inNode.getFirstChild();
        AST collectionNode = variableNode.getNextSibling();
        ClassNode type = ClassHelper.OBJECT_TYPE;
        if (isType(VARIABLE_DEF, variableNode)) {
            AST node = variableNode.getFirstChild();
            // skip the final modifier if it's present
            if (isType(MODIFIERS, node)) {
                int modifiersMask = modifiers(node, new ArrayList<AnnotationNode>(), 0);
                // only final modifier allowed
                if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
                    throw new ASTRuntimeException(node, "Only the 'final' modifier is allowed in front of the for loop variable.");
                }
                node = node.getNextSibling();
            }
            type = makeTypeWithArguments(node);
            variableNode = node.getNextSibling();
        }
        String variable = identifier(variableNode);
        collectionExpression = expression(collectionNode);
        forParameter = new Parameter(type, variable);
        configureAST(forParameter, variableNode);
    }
    final AST node = inNode.getNextSibling();
    Statement block;
    if (isType(SEMI, node)) {
        block = EmptyStatement.INSTANCE;
    } else {
        block = statement(node);
    }
    ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
    configureAST(forStatement, forNode);
    return forStatement;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) 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) Parameter(org.codehaus.groovy.ast.Parameter) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 75 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class AntlrParserPlugin method switchStatement.

protected Statement switchStatement(AST switchNode) {
    AST node = switchNode.getFirstChild();
    Expression expression = expression(node);
    Statement defaultStatement = EmptyStatement.INSTANCE;
    List list = new ArrayList();
    for (node = node.getNextSibling(); isType(CASE_GROUP, node); node = node.getNextSibling()) {
        Statement tmpDefaultStatement;
        AST child = node.getFirstChild();
        if (isType(LITERAL_case, child)) {
            List cases = new LinkedList();
            // default statement can be grouped with previous case
            tmpDefaultStatement = caseStatements(child, cases);
            list.addAll(cases);
        } else {
            tmpDefaultStatement = statement(child.getNextSibling());
        }
        if (tmpDefaultStatement != EmptyStatement.INSTANCE) {
            if (defaultStatement == EmptyStatement.INSTANCE) {
                defaultStatement = tmpDefaultStatement;
            } else {
                throw new ASTRuntimeException(switchNode, "The default case is already defined.");
            }
        }
    }
    if (node != null) {
        unknownAST(node);
    }
    SwitchStatement switchStatement = new SwitchStatement(expression, list, defaultStatement);
    configureAST(switchStatement, switchNode);
    return switchStatement;
}
Also used : AST(antlr.collections.AST) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) 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) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Aggregations

Statement (org.codehaus.groovy.ast.stmt.Statement)205 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)167 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)121 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)90 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)79 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)71 Expression (org.codehaus.groovy.ast.expr.Expression)63 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)54 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)53 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)53 ClassNode (org.codehaus.groovy.ast.ClassNode)50 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)50 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)40 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)39 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)39 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)38 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)38 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)38