Search in sources :

Example 51 with ExpressionStatement

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

the class JavaAwareResolveVisitor method getConstructorCall.

private static Expression getConstructorCall(Statement code) {
    if (code == null)
        return null;
    if (code instanceof BlockStatement) {
        BlockStatement bs = (BlockStatement) code;
        if (bs.isEmpty())
            return null;
        return getConstructorCall(bs.getStatements().get(0));
    }
    if (!(code instanceof ExpressionStatement))
        return null;
    ExpressionStatement es = (ExpressionStatement) code;
    Expression exp = es.getExpression();
    if (!(exp instanceof ConstructorCallExpression))
        return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) exp;
    if (!cce.isSpecialCall())
        return null;
    return cce;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 52 with ExpressionStatement

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

the class AntlrParserPlugin method variableDef.

protected Statement variableDef(AST variableDef) {
    ExpressionStatement expressionStatement = new ExpressionStatement(declarationExpression(variableDef));
    configureAST(expressionStatement, variableDef);
    return expressionStatement;
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 53 with ExpressionStatement

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

the class AntlrParserPlugin method declarationExpression.

protected Expression declarationExpression(AST variableDef) {
    AST node = variableDef.getFirstChild();
    ClassNode type = null;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        // force check of modifier conflicts
        modifiers = modifiers(node, annotations, 0);
        node = node.getNextSibling();
    }
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    Expression leftExpression;
    Expression rightExpression = EmptyExpression.INSTANCE;
    AST right;
    if (isType(ASSIGN, node)) {
        node = node.getFirstChild();
        AST left = node.getFirstChild();
        ArgumentListExpression alist = new ArgumentListExpression();
        for (AST varDef = left; varDef != null; varDef = varDef.getNextSibling()) {
            assertNodeType(VARIABLE_DEF, varDef);
            DeclarationExpression de = (DeclarationExpression) declarationExpression(varDef);
            alist.addExpression(de.getVariableExpression());
        }
        leftExpression = alist;
        right = node.getNextSibling();
        if (right != null)
            rightExpression = expression(right);
    } else {
        String name = identifier(node);
        VariableExpression ve = new VariableExpression(name, type);
        ve.setModifiers(modifiers);
        leftExpression = ve;
        right = node.getNextSibling();
        if (right != null) {
            assertNodeType(ASSIGN, right);
            rightExpression = expression(right.getFirstChild());
        }
    }
    configureAST(leftExpression, node);
    Token token = makeToken(Types.ASSIGN, variableDef);
    DeclarationExpression expression = new DeclarationExpression(leftExpression, token, rightExpression);
    expression.addAnnotations(annotations);
    configureAST(expression, variableDef);
    ExpressionStatement expressionStatement = new ExpressionStatement(expression);
    configureAST(expressionStatement, variableDef);
    return expression;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) ArrayList(java.util.ArrayList) Token(org.codehaus.groovy.syntax.Token) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 54 with ExpressionStatement

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

the class GeneralUtils method copyStatementsWithSuperAdjustment.

public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body) {
    Statement preCode = pre.getCode();
    boolean changed = false;
    if (preCode instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) preCode;
        List<Statement> statements = block.getStatements();
        for (int i = 0; i < statements.size(); i++) {
            Statement statement = statements.get(i);
            // adjust the first statement if it's a super call
            if (i == 0 && statement instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) statement;
                Expression preExp = es.getExpression();
                if (preExp instanceof MethodCallExpression) {
                    MethodCallExpression mce = (MethodCallExpression) preExp;
                    String name = mce.getMethodAsString();
                    if ("super".equals(name)) {
                        es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
                        changed = true;
                    }
                }
            }
            body.addStatement(statement);
        }
    }
    return changed;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 55 with ExpressionStatement

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

the class AntlrParserPlugin method methodCall.

protected Statement methodCall(AST code) {
    Expression expression = methodCallExpression(code);
    ExpressionStatement expressionStatement = new ExpressionStatement(expression);
    configureAST(expressionStatement, code);
    return expressionStatement;
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)122 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)78 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)58 Expression (org.codehaus.groovy.ast.expr.Expression)54 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)52 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)51 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)49 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)44 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)39 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)35 Parameter (org.codehaus.groovy.ast.Parameter)30 MethodNode (org.codehaus.groovy.ast.MethodNode)29 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)29 ClassNode (org.codehaus.groovy.ast.ClassNode)27 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArrayList (java.util.ArrayList)25 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)24