Clousure expression transformation

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Clousure expression transformation

Paolo Di Tommaso
Dear all, 


I'm trying to write a global transformation for my DSL to apply a custom logic on binary expression. For example: 

@GroovyASTTransformation(phase = CompilePhase.CONVERSION)
class FooXformImpl implements ASTTransformation {

    SourceUnit unit

    @Override
    void visit(ASTNode[] nodes, SourceUnit source) {
        this.unit = unit
        createVisitor().visitClass((ClassNode)nodes[1])
    }

    protected ClassCodeExpressionTransformer createVisitor() {

        new ClassCodeExpressionTransformer() {

            protected SourceUnit getSourceUnit() { unit }

            Expression transform(Expression expr) {
                if( expr.class == BinaryExpression ) {
                    return .. // my replacement where
                }
                super.transform(expr)
            }
        }
    }
}


The transformation is correctly applied, however it turns out the expressions nested in a closure definition are *not* visited. Therefore the above transformation is not applied to binary expressions inside a closure. 

Is that expected? How to manage this use case ? 


Cheers,
Paolo
 
Reply | Threaded
Open this post in threaded view
|

Re: Clousure expression transformation

Paolo Di Tommaso
Replying to myself, for some reason the closure expression needs to be explicitly visited. The following transformer do the trick: 

       new ClassCodeExpressionTransformer() {

            protected SourceUnit getSourceUnit() { unit }

            Expression transform(Expression expr) {
                if( expr.class == BinaryExpression ) {
                    return .. // my replacement where
                }
                else if( expr instanceof ClosureExpression) {
                    // explicitly visit the closure 
                    visitClosureExpression(expr)
                }
                super.transform(expr)
            }
        } 



p

On Mon, Sep 24, 2018 at 7:34 PM Paolo Di Tommaso <[hidden email]> wrote:
Dear all, 


I'm trying to write a global transformation for my DSL to apply a custom logic on binary expression. For example: 

@GroovyASTTransformation(phase = CompilePhase.CONVERSION)
class FooXformImpl implements ASTTransformation {

    SourceUnit unit

    @Override
    void visit(ASTNode[] nodes, SourceUnit source) {
        this.unit = unit
        createVisitor().visitClass((ClassNode)nodes[1])
    }

    protected ClassCodeExpressionTransformer createVisitor() {

        new ClassCodeExpressionTransformer() {

            protected SourceUnit getSourceUnit() { unit }

            Expression transform(Expression expr) {
                if( expr.class == BinaryExpression ) {
                    return .. // my replacement where
                }
                super.transform(expr)
            }
        }
    }
}


The transformation is correctly applied, however it turns out the expressions nested in a closure definition are *not* visited. Therefore the above transformation is not applied to binary expressions inside a closure. 

Is that expected? How to manage this use case ? 


Cheers,
Paolo