GoLang ast: generating and printing a tree without position information
Asked Answered
W

1

6

I'm looking to make a tool which generates method stubs from some given input. I've seen the ast package, but it seems to represent an already parsed AST, which has information about where in the source file everything is. Importantly, you need to provide source information

I'm looking at generating a source file programatically, so I have no idea where in the final file my AST nodes will end up.

I'm wondering:

  • Is there a better AST tool which lets you generate code without giving source file position information?
  • If I give dummy information for positions in the ast package, will it pretty-print properly (i.e. ignore the position information)?

I realize I could do this all with text generation, but that seems type-unsafe and harder to deal with.

Wilfredowilfrid answered 1/11, 2016 at 17:24 Comment(7)
Most Go code generators produce text directly and format the text when done. Generating text is much easier than doing anything with the AST.Th
That's... unfortunate.Wilfredowilfrid
See my SO answer on how to build AST prettyprinters: https://mcmap.net/q/83019/-compiling-an-ast-back-to-source-codeVolcano
@IraBaxter That tells me how to build a pretty-printer. My goal is to use someone else's PrettyPrinter, like the one that GoLang has, but which doesn't seem to support constructing ASTs from scratch.Wilfredowilfrid
@jmite: True, most of its focus is on how to build a a prettyprinter, and/or a fidelity printer (that preserves the original layout of the text). It refers to (my company's) tool that implements such prettyprinters quite naturally and easily, and that will build ASTs from scratch given a grammar. It has been tested on many, many grammars; not yet on Go although I don't see why Go would be special.Volcano
Even so, the whole point of the question is that I want to avoid having to model all of Go by hand just to generate some Go code, when there's already AST and PrettyPrint modules in Go. Your tool, if I understand right, would still require me to make a grammar or at least some rules for Go. What's more, I don't need the printing to be too "pretty," just syntactically correct, since I can feed it all through gofmt.Wilfredowilfrid
If you insist on using the Go AST and PrettyPrint modules, then my reference doesn't help you, agreed. You asked if there was a better tool :-} You can't avoid building the part of the AST that is needed; it isn't long before you decide you complete (Go) programs and therefore the complete AST, in my experience. So, yes, you'd have to give DMS what amounts to a complete Go grammar (and lexer).Volcano
B
3

Consider https://github.com/lu4p/astextract which has a nicer AST to work with that can be printed into go code.

I know you've thought about this, but using text/template and goimports on the resulting string is actually quite reasonable. It's way easier to write and it translates much better to writing normal go code. As you note, it's not type-safe (which is fine because running goimports on it later enforces that). The biggest con is actually that it's hard to test (we ended up writing a set of generated tests and manual written tests).

(EDIT: just realized how old of a question this is - going to leave my answer up for others as I'm sure you've found some way to solve this for yourself by now)

Brimstone answered 2/7, 2020 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.