org.codehaus.groovy.ast.expr.ClosureListExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.ClosureListExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticCompilationVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitForLoop(final ForStatement statement) {
    super.visitForLoop(statement);
    Expression collectionExpression = statement.getCollectionExpression();
    if (!(collectionExpression instanceof ClosureListExpression)) {
        ClassNode forLoopVariableType = statement.getVariableType();
        ClassNode collectionType = getType(collectionExpression);
        ClassNode componentType;
        if (Character_TYPE.equals(ClassHelper.getWrapper(forLoopVariableType)) && STRING_TYPE.equals(collectionType)) {
            // we allow auto-coercion here
            componentType = forLoopVariableType;
        } else {
            componentType = inferLoopElementType(collectionType);
        }
        statement.getVariable().setType(componentType);
    }
}
 
Example #2
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitClosureListExpression(ClosureListExpression node) {
	pushASTNode(node);
	try {
		super.visitClosureListExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #3
Source File: VariableFinderVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClosureListExpression(ClosureListExpression cle) {
    // FIXME whole tree allowed ?
    if (!blocks.remove(cle)) {
        return;
    }

    super.visitClosureListExpression(cle);
}
 
Example #4
Source File: StatementWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void visitExpressionOfLoopStatement(final Expression expression) {
    Consumer<Expression> visit = expr -> {
        if (expr instanceof EmptyExpression) return;
        int mark = controller.getOperandStack().getStackLength();
        expr.visit(controller.getAcg());
        controller.getOperandStack().popDownTo(mark);
    };

    if (expression instanceof ClosureListExpression) {
        ((ClosureListExpression) expression).getExpressions().forEach(visit);
    } else {
        visit.accept(expression);
    }
}
 
Example #5
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(ClosureListExpression node) {
    if (isInside(node, line, column)) {
        super.visitClosureListExpression(node);
    }
}
 
Example #6
Source File: VariableFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void collect() {
    TokenSequence<GroovyTokenId> ts = LexUtilities.getPositionedSequence(doc, cursorOffset);
    if (ts == null) {
        return;
    }
    Token<GroovyTokenId> token = ts.token();
    if (token == null) {
        return;
    }

    ASTNode last = null;

    blocks.clear();
    variables.clear();

    // We are going through the path up marking all the declaration
    // blocks and the top (last) one.
    for (Iterator<ASTNode> it = path.iterator(); it.hasNext();) {
        ASTNode scope = it.next();
        if ((scope instanceof ClosureExpression) || (scope instanceof MethodNode)
                || (scope instanceof ConstructorNode) || (scope instanceof ForStatement)
                || (scope instanceof BlockStatement) || (scope instanceof ClosureListExpression)
                || (scope instanceof CatchStatement)) {

            last = scope;
            blocks.add(scope);

            // In for loop we have to allow visitor to visit ClosureListExpression
            if ((scope instanceof ForStatement)
                    && (((ForStatement) scope).getCollectionExpression() instanceof ClosureListExpression)) {
                blocks.add(((ForStatement) scope).getCollectionExpression());
            }
        }
    }

    // Lets visit the code from top. We visit only allowed blocks
    // to avoid visiting subtrees declared before offset, but not usable.
    // ie
    // def clos = {
    //     def x = {
    //         String str
    //     }
    //     ^ // we are here and we dont want to get str as possibility
    // }
    if (last instanceof ClosureExpression) {
        visitClosureExpression((ClosureExpression) last);
    } else if (last instanceof MethodNode) {
        visitMethod((MethodNode) last);
    } else if (last instanceof ConstructorNode) {
        visitConstructor((ConstructorNode) last);
    } else if (last instanceof ForStatement) {
        visitForLoop((ForStatement) last);
    } else if (last instanceof BlockStatement) {
        visitBlockStatement((BlockStatement) last);
    } else if (last instanceof ClosureListExpression) {
        visitClosureListExpression((ClosureListExpression) last);
    } else if (last instanceof CatchStatement) {
        visitCatchStatement((CatchStatement) last);
    }

}
 
Example #7
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(final ClosureListExpression cle) {
    super.visitClosureListExpression(cle);
    tryFind(ClosureListExpression.class, cle);
}
 
Example #8
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(final ClosureListExpression cle) {
    pushContext(cle);
    super.visitClosureListExpression(cle);
    popContext();
}
 
Example #9
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(final ClosureListExpression closureListExpression) {
    assertExpressionAuthorized(closureListExpression);
    if (!isClosuresAllowed) throw new SecurityException("Closures are not allowed");
    visitListOfExpressions(closureListExpression.getExpressions());
}
 
Example #10
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(final ClosureListExpression cle) {
    super.visitClosureListExpression(cle);
    trn.visitClosureListExpression(cle);
}
 
Example #11
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClosureListExpression(ClosureListExpression expression) {
    visitListOfExpressions(expression.getExpressions());
}
 
Example #12
Source File: StatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void writeForLoopWithClosureList(final ForStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitForLoop");
    writeStatementLabel(statement);

    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(statement.getVariableScope(), statement.getStatementLabels());

    ClosureListExpression clExpr = (ClosureListExpression) statement.getCollectionExpression();
    controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());

    List<Expression> expressions = clExpr.getExpressions();
    int size = expressions.size();

    // middle element is condition, lower half is init, higher half is increment
    int condIndex = (size - 1) / 2;

    // visit init
    for (int i = 0; i < condIndex; i += 1) {
        visitExpressionOfLoopStatement(expressions.get(i));
    }

    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();

    Label cond = new Label();
    mv.visitLabel(cond);
    // visit condition leave boolean on stack
    {
        int mark = controller.getOperandStack().getStackLength();
        Expression condExpr = expressions.get(condIndex);
        condExpr.visit(controller.getAcg());
        controller.getOperandStack().castToBool(mark, true);
    }
    // jump if we don't want to continue
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    controller.getOperandStack().jump(IFEQ, breakLabel);

    // Generate the loop body
    statement.getLoopBlock().visit(controller.getAcg());

    // visit increment
    mv.visitLabel(continueLabel);
    // fix for being on the wrong line when debugging for loop
    controller.getAcg().onLineNumber(statement, "increment condition");
    for (int i = condIndex + 1; i < size; i += 1) {
        visitExpressionOfLoopStatement(expressions.get(i));
    }

    // jump to test the condition again
    mv.visitJumpInsn(GOTO, cond);

    // loop end
    mv.visitLabel(breakLabel);

    controller.getCompileStack().pop();
    controller.getCompileStack().pop();
}
 
Example #13
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitClosureListExpression(ClosureListExpression expression);