You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Paolo Di Tommaso <pa...@gmail.com> on 2018/09/24 17:34:58 UTC

Clousure expression transformation

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

Re: Clousure expression transformation

Posted by Paolo Di Tommaso <pa...@gmail.com>.
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 <pa...@gmail.com>
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
>
>