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.

jmite
  • 8,171
  • 6
  • 40
  • 81
  • 2
    Most Go code generators produce text directly and [format](https://godoc.org/go/format#Source) the text when done. Generating text is much easier than doing anything with the AST. – Charlie Tumahai Nov 01 '16 at 17:58
  • 1
    That's... unfortunate. – jmite Nov 01 '16 at 18:19
  • See my SO answer on how to build AST prettyprinters: http://stackoverflow.com/a/5834775/120163 – Ira Baxter Nov 01 '16 at 19:59
  • @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. – jmite Nov 01 '16 at 20:04
  • @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. – Ira Baxter Nov 01 '16 at 20:08
  • 1
    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. – jmite Nov 01 '16 at 20:11
  • 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). – Ira Baxter Nov 01 '16 at 20:29

1 Answers1

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)

Liyan Chang
  • 7,721
  • 3
  • 39
  • 59