So I want to be able to pretty-print ASTNode and children...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

So I want to be able to pretty-print ASTNode and children...

Andrew Bayer
Yeah, I know, you can visualize the AST nicely in the Groovy console, but I'm dealing with it in the context of Jenkins Pipelines' Groovy CPS transformation/compilation, so...being able to pretty print would be really nice for mapping our weirdness out. =)

I don't think there's anything out there that does this currently, so I'm gonna write up a PR to add this. Now I need to figure out what the right implementation would be. Do I go with adding useful .toString() methods to the various ASTNode children that don't already have them (i.e., most of the *Node classes, some Expressions, some Statements) or do I instead write a helper class that has all the to-string logic for all the ASTNode children in it? Thoughts?

A.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: So I want to be able to pretty-print ASTNode and children...

Jochen Theodorou
On 16.03.2017 17:46, Andrew Bayer wrote:
[...]
> I don't think there's anything out there that does this currently, so
> I'm gonna write up a PR to add this. Now I need to figure out what the
> right implementation would be. Do I go with adding useful .toString()
> methods to the various ASTNode children that don't already have them
> (i.e., most of the *Node classes, some Expressions, some Statements) or
> do I instead write a helper class that has all the to-string logic for
> all the ASTNode children in it? Thoughts?

I would not depend on toString here. You want a pretty-print, that will
require some kind of formatting information across the AST nodes, even
if it is only indentation. toString will not give you that. I would make
an visitor instead, that goes through the AST and produces the pretty-print.

bye Jochen

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: So I want to be able to pretty-print ASTNode and children...

Sergei Egorov

On Thu, Mar 16, 2017 at 11:40 PM Jochen Theodorou <[hidden email]> wrote:
On 16.03.2017 17:46, Andrew Bayer wrote:
[...]
> I don't think there's anything out there that does this currently, so
> I'm gonna write up a PR to add this. Now I need to figure out what the
> right implementation would be. Do I go with adding useful .toString()
> methods to the various ASTNode children that don't already have them
> (i.e., most of the *Node classes, some Expressions, some Statements) or
> do I instead write a helper class that has all the to-string logic for
> all the ASTNode children in it? Thoughts?

I would not depend on toString here. You want a pretty-print, that will
require some kind of formatting information across the AST nodes, even
if it is only indentation. toString will not give you that. I would make
an visitor instead, that goes through the AST and produces the pretty-print.

bye Jochen

Loading...