This is a fork of goblin
extended to include type information with the emitted ASTs.
Differences from the original:
- DumpFile takes two more arguments: a file path string and a *types.Info. The latter is optional (pass nil for no type information).
- The "kind" and "type" fields of binary and unary expression nodes are swapped.
- Bugfixes.
Known issues:
- Can't handle recursive types yet (causes stack overflow).
goblin
is an executable that uses Go's ast
, parser
, and token
modules to dump a Go expression, statement, or file to JSON. It is small, fast, self-contained, and incurs no dependencies.
goblin --file [FILENAME]
dumps a given file.
goblin --expr EXPR
dumps an expression.
goblin --stmt STMT
dumps a statement—due to a quirk in the Go AST API, this statement will be surrounded by a dummy function.
Every node is a JSON object containing at least two guaranteed keys:
kind
(string): this corresponds to the data type of the given node. Expressions (Prim
andExpr
) are"expression"
, statements (Statement
andSimp
) are"statement"
, binary and unary expressions are"unary"
and"binary"
respectively.type
(string): this corresponds to the data constructor associated with the node. Casts have kind"expression""
and type"cast"
. Floats have kind"literal"
and type"FLOAT"
. Pointer types have kind"type"
and type"pointer"
.
I apologize for the semantic overlap associated with the vagueness of the words "kind" and "type". Suggestions as to better nomenclature are welcomed.
Why not use the ast.Visitor
interface instead of recursing manually into every node? Because Visitor
is inherently side-effectual: it declares no return type, so it is not possible to use it to express an algebra (which is all this program really is).
goblin
is open-source software © Reconfigure.io, released to the public under the terms of the Apache 2.0 license. A copy can be found under the LICENSE file in the project root.
Pull requests are enthusiastically accepted!
By participating in this project you agree to follow the Contributor Code of Conduct.
- Use JSON Schema to ensure well-formedness of the AST.
- Pull in github.com/stretchr/testify for assertions and glog for logging.
- The built-in
make
andnew
functions can be shadowed. Since goblin expectsmake
andnew
to take types as arguments, it will reject a shadowing as a syntax error. The chances of this happening in real code are pretty low, as shadowing built-in functions is discouraged in real-world code.