Hacker News new | ask | show | jobs
by edsrzf 3406 days ago
The Go library's standard AST package[1] similarly stores comments and whitespace information. It has to, since it's used by gofmt, and you wouldn't be very happy you lost comments when formatting your code.

[1] https://golang.org/pkg/go/ast/

2 comments

Unfortunately, most people agree that the ast package is a mess. It isn't used by the actual compiler. There is some talk of eventually deprecating it and publishing the internal ast/parser/type-checker packages.
I think calling it a "mess" is hyperbole, but yes, it's true that it has shortcomings and may be deprecated and replaced.
I've used it before for a tool that transforms code that throws away an error code to code that takes the error code and panics if not nil.

https://github.com/placeybordeaux/panic-attack

The package works fine, but I can't say it was fun to work with.

If I remember correctly rust-fmt deals with comments without them being explicitly part of the ast. Sure creates more work though