You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Andrew Bayer <an...@gmail.com> on 2017/03/16 16:46:17 UTC

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

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.

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

Posted by Sergei Egorov <bs...@gmail.com>.
Hey Andrew,

How about AstNodeToScriptVisitor? Pretty enough? :)

http://stackoverflow.com/questions/35960804/convert-ast-nodes-string-to-groovy-code/35991091#35991091


On Thu, Mar 16, 2017 at 11:40 PM Jochen Theodorou <bl...@gmx.org> 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
>
>

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

Posted by Jochen Theodorou <bl...@gmx.org>.
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