Print AST tree at runtime?

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

Print AST tree at runtime?

JR Smiljanic
Hi All,

I have some context sensitive transforms that I'd like to be able to visualize for debugging.  Because they are context sensitive I cannot (easily) use groovy console.

Does anyone know if there is a way to visualize an AST tree at runtime?  Ideally I'd like something like the source view that is presented by the groovy console AST browser.

JR
Reply | Threaded
Open this post in threaded view
|

Re: Print AST tree at runtime?

ocs@ocs
John,

On Feb 15, 2013, at 12:37 AM, John Smiljanic wrote:

> I have some context sensitive transforms that I'd like to be able to visualize for debugging.  Because they are context sensitive I cannot (easily) use groovy console.
>
> Does anyone know if there is a way to visualize an AST tree at runtime?

Some Groovy Guru might correct me if I am wrong, but far as I can say, there's no AST tree at runtime anymore. The tree exists compile-time, changes in each phase, and is trashed sometimes at OUTPUT phase or something like that, when it is not needed anymore. Later, when  the program is launched, all the ASTs are just a past.

Unless I am much mistaken, Groovy never interprets ASTs -- that would be technically possible, but it's not the way it is. Instead, each script is always translated to the bytecode and run by JVM.

> Ideally I'd like something like the source view that is presented by the groovy console AST browser.

If what you want is to see the AST tree compile-time, and if you happen to use the darned Eclipse thing, greclipse adds a pretty nice AST view -- somewhat less flexible than the Console unless I missed something, but good enough for most needs.

Otherwise, well, you can always write your own AST transform and print out the information yourself. It is a bit tedious, but not much: since the transforms themselves can be written in Groovy exploiting all its grooviness, it does not take much code. And there's a good support in the API.

Depending on the phase in which you need the information you may go with a local transform, or you might need a global one. The difference is negligible and the code would be nearly the same in both cases; local can run from a .class though, whilst for global, far as I know, you need to pack the .class inside a JAR adding a manifest with the transform class name.

There's pretty good documentation for this in the documentation at

http://groovy.codehaus.org/Local+AST+Transformations
http://groovy.codehaus.org/Global+AST+Transformations

All the best,
---
Ondra Čada
OCSoftware:     [hidden email]               http://www.ocs.cz
private         [hidden email]             http://www.ocs.cz/oc




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Print AST tree at runtime?

JR Smiljanic
Thanks for the response.  Clarification:  I didn't mean I wanted to visualize the AST when executng my groovy code.  Instead, I am doing runtime compilation of groovy using GroovyShell from a running application (not eclipse) and I would like to be able to print a visualization of the AST to a diagnostic logger when each of my transforms completes.

Since groovy console (and it sounds like eclipse) already have AST visualization tools there must be an existing solution.  My question to this group is whether that solution is generalized enough to be invoked in any application and with any output channel (or can just return a String).

JR

JR


On Thu, Feb 14, 2013 at 6:36 PM, Ondřej Čada <[hidden email]> wrote:
John,

On Feb 15, 2013, at 12:37 AM, John Smiljanic wrote:

> I have some context sensitive transforms that I'd like to be able to visualize for debugging.  Because they are context sensitive I cannot (easily) use groovy console.
>
> Does anyone know if there is a way to visualize an AST tree at runtime?

Some Groovy Guru might correct me if I am wrong, but far as I can say, there's no AST tree at runtime anymore. The tree exists compile-time, changes in each phase, and is trashed sometimes at OUTPUT phase or something like that, when it is not needed anymore. Later, when  the program is launched, all the ASTs are just a past.

Unless I am much mistaken, Groovy never interprets ASTs -- that would be technically possible, but it's not the way it is. Instead, each script is always translated to the bytecode and run by JVM.

> Ideally I'd like something like the source view that is presented by the groovy console AST browser.

If what you want is to see the AST tree compile-time, and if you happen to use the darned Eclipse thing, greclipse adds a pretty nice AST view -- somewhat less flexible than the Console unless I missed something, but good enough for most needs.

Otherwise, well, you can always write your own AST transform and print out the information yourself. It is a bit tedious, but not much: since the transforms themselves can be written in Groovy exploiting all its grooviness, it does not take much code. And there's a good support in the API.

Depending on the phase in which you need the information you may go with a local transform, or you might need a global one. The difference is negligible and the code would be nearly the same in both cases; local can run from a .class though, whilst for global, far as I know, you need to pack the .class inside a JAR adding a manifest with the transform class name.

There's pretty good documentation for this in the documentation at

http://groovy.codehaus.org/Local+AST+Transformations
http://groovy.codehaus.org/Global+AST+Transformations

All the best,
---
Ondra Čada
OCSoftware:     [hidden email]               http://www.ocs.cz
private         [hidden email]             http://www.ocs.cz/oc




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Print AST tree at runtime?

chiquitinxx
Hello!

At runtime you can generate AST tree from file or String, and then work with that. You can get the AST tree from a code fragment with new AstBuilder().buildFromString(CompilePhase.SEMANTIC_ANALYSIS,"your code here").

Maybe, you can take a look to this class, that take a file, get the AST, and returns back code representation of the AST. https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy

El 15/02/2013, a las 02:08, John Smiljanic escribió:

Thanks for the response.  Clarification:  I didn't mean I wanted to visualize the AST when executng my groovy code.  Instead, I am doing runtime compilation of groovy using GroovyShell from a running application (not eclipse) and I would like to be able to print a visualization of the AST to a diagnostic logger when each of my transforms completes.

Since groovy console (and it sounds like eclipse) already have AST visualization tools there must be an existing solution.  My question to this group is whether that solution is generalized enough to be invoked in any application and with any output channel (or can just return a String).

JR

JR


On Thu, Feb 14, 2013 at 6:36 PM, Ondřej Čada <[hidden email]> wrote:
John,

On Feb 15, 2013, at 12:37 AM, John Smiljanic wrote:

> I have some context sensitive transforms that I'd like to be able to visualize for debugging.  Because they are context sensitive I cannot (easily) use groovy console.
>
> Does anyone know if there is a way to visualize an AST tree at runtime?

Some Groovy Guru might correct me if I am wrong, but far as I can say, there's no AST tree at runtime anymore. The tree exists compile-time, changes in each phase, and is trashed sometimes at OUTPUT phase or something like that, when it is not needed anymore. Later, when  the program is launched, all the ASTs are just a past.

Unless I am much mistaken, Groovy never interprets ASTs -- that would be technically possible, but it's not the way it is. Instead, each script is always translated to the bytecode and run by JVM.

> Ideally I'd like something like the source view that is presented by the groovy console AST browser.

If what you want is to see the AST tree compile-time, and if you happen to use the darned Eclipse thing, greclipse adds a pretty nice AST view -- somewhat less flexible than the Console unless I missed something, but good enough for most needs.

Otherwise, well, you can always write your own AST transform and print out the information yourself. It is a bit tedious, but not much: since the transforms themselves can be written in Groovy exploiting all its grooviness, it does not take much code. And there's a good support in the API.

Depending on the phase in which you need the information you may go with a local transform, or you might need a global one. The difference is negligible and the code would be nearly the same in both cases; local can run from a .class though, whilst for global, far as I know, you need to pack the .class inside a JAR adding a manifest with the transform class name.

There's pretty good documentation for this in the documentation at

http://groovy.codehaus.org/Local+AST+Transformations
http://groovy.codehaus.org/Global+AST+Transformations

All the best,
---
Ondra Čada
OCSoftware:     [hidden email]               http://www.ocs.cz
private         [hidden email]             http://www.ocs.cz/oc




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|

Re: Print AST tree at runtime?

JR Smiljanic
Thanks Jorge.  Something like what AstNodeToScriptAdapter.groovy is doing is exactly what I was looking for!  Now, if only the inner ASTNodeToScriptVisitor were public so that I could use the visitor directly with my exisiting AST.  I'll figure something out...

JR


On Fri, Feb 15, 2013 at 12:37 AM, Jorge Franco Leza <[hidden email]> wrote:
Hello!

At runtime you can generate AST tree from file or String, and then work with that. You can get the AST tree from a code fragment with new AstBuilder().buildFromString(CompilePhase.SEMANTIC_ANALYSIS,"your code here").

Maybe, you can take a look to this class, that take a file, get the AST, and returns back code representation of the AST. https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy

El 15/02/2013, a las 02:08, John Smiljanic escribió:

Thanks for the response.  Clarification:  I didn't mean I wanted to visualize the AST when executng my groovy code.  Instead, I am doing runtime compilation of groovy using GroovyShell from a running application (not eclipse) and I would like to be able to print a visualization of the AST to a diagnostic logger when each of my transforms completes.

Since groovy console (and it sounds like eclipse) already have AST visualization tools there must be an existing solution.  My question to this group is whether that solution is generalized enough to be invoked in any application and with any output channel (or can just return a String).

JR

JR


On Thu, Feb 14, 2013 at 6:36 PM, Ondřej Čada <[hidden email]> wrote:
John,

On Feb 15, 2013, at 12:37 AM, John Smiljanic wrote:

> I have some context sensitive transforms that I'd like to be able to visualize for debugging.  Because they are context sensitive I cannot (easily) use groovy console.
>
> Does anyone know if there is a way to visualize an AST tree at runtime?

Some Groovy Guru might correct me if I am wrong, but far as I can say, there's no AST tree at runtime anymore. The tree exists compile-time, changes in each phase, and is trashed sometimes at OUTPUT phase or something like that, when it is not needed anymore. Later, when  the program is launched, all the ASTs are just a past.

Unless I am much mistaken, Groovy never interprets ASTs -- that would be technically possible, but it's not the way it is. Instead, each script is always translated to the bytecode and run by JVM.

> Ideally I'd like something like the source view that is presented by the groovy console AST browser.

If what you want is to see the AST tree compile-time, and if you happen to use the darned Eclipse thing, greclipse adds a pretty nice AST view -- somewhat less flexible than the Console unless I missed something, but good enough for most needs.

Otherwise, well, you can always write your own AST transform and print out the information yourself. It is a bit tedious, but not much: since the transforms themselves can be written in Groovy exploiting all its grooviness, it does not take much code. And there's a good support in the API.

Depending on the phase in which you need the information you may go with a local transform, or you might need a global one. The difference is negligible and the code would be nearly the same in both cases; local can run from a .class though, whilst for global, far as I know, you need to pack the .class inside a JAR adding a manifest with the transform class name.

There's pretty good documentation for this in the documentation at

http://groovy.codehaus.org/Local+AST+Transformations
http://groovy.codehaus.org/Global+AST+Transformations

All the best,
---
Ondra Čada
OCSoftware:     [hidden email]               http://www.ocs.cz
private         [hidden email]             http://www.ocs.cz/oc




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email