goblin

package module
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

README

Goblin

Build Status codecov.io

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.

Usage

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.

Format

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 and Expr) are "expression", statements (Statement and Simp) 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.

FAQ's

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).

Licensing

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.

Contributing

Pull requests are enthusiastically accepted!

By participating in this project you agree to follow the Contributor Code of Conduct.

TODO

  • Use JSON Schema to ensure well-formedness of the AST.
  • Pull in github.com/stretchr/testify for assertions and glog for logging.

Known Issues

  • The built-in make and new functions can be shadowed. Since goblin expects make and new 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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var INVALID_POSITION token.Position = token.Position{Filename: "unspecified", Offset: -1, Line: -1, Column: -1}
View Source
var ShouldPanic bool = false
View Source
var TOPLEVEL_POSITION token.Position = token.Position{Filename: "toplevel", Offset: -1, Line: -1, Column: -1}

Functions

func AttemptExprAsType

func AttemptExprAsType(e ast.Expr, fset *token.FileSet) map[string]interface{}

func DumpArray

func DumpArray(a *ast.ArrayType, fset *token.FileSet) map[string]interface{}

func DumpBasicLit

func DumpBasicLit(l *ast.BasicLit, fset *token.FileSet) map[string]interface{}

func DumpBinaryExpr

func DumpBinaryExpr(b *ast.BinaryExpr, fset *token.FileSet) map[string]interface{}

func DumpBlock

func DumpBlock(b *ast.BlockStmt, fset *token.FileSet) []interface{}

func DumpBlockAsStmt

func DumpBlockAsStmt(b *ast.BlockStmt, fset *token.FileSet) map[string]interface{}

func DumpCall

func DumpCall(c *ast.CallExpr, fset *token.FileSet) map[string]interface{}

func DumpChanDir

func DumpChanDir(d ast.ChanDir) string

func DumpCommentGroup

func DumpCommentGroup(g *ast.CommentGroup, fset *token.FileSet) []string

func DumpDecl

func DumpDecl(n ast.Decl, fset *token.FileSet) map[string]interface{}

func DumpExpr

func DumpExpr(e ast.Expr, fset *token.FileSet) map[string]interface{}

func DumpExprAsType

func DumpExprAsType(e ast.Expr, fset *token.FileSet) map[string]interface{}

func DumpExprs

func DumpExprs(exprs []ast.Expr, fset *token.FileSet) []interface{}

func DumpField

func DumpField(f *ast.Field, fset *token.FileSet) map[string]interface{}

func DumpFields

func DumpFields(fs *ast.FieldList, fset *token.FileSet) []map[string]interface{}

func DumpFile

func DumpFile(f *ast.File, fset *token.FileSet) ([]byte, error)

func DumpFuncDecl

func DumpFuncDecl(f *ast.FuncDecl, fset *token.FileSet) map[string]interface{}

func DumpGenDecl

func DumpGenDecl(decl *ast.GenDecl, fset *token.FileSet) map[string]interface{}

func DumpIdent

func DumpIdent(i *ast.Ident, fset *token.FileSet) map[string]interface{}

func DumpImport

func DumpImport(spec *ast.ImportSpec, fset *token.FileSet) map[string]interface{}

func DumpMethodDecl added in v0.1.4

func DumpMethodDecl(f *ast.FuncDecl, fset *token.FileSet) map[string]interface{}

func DumpPosition added in v0.2.2

func DumpPosition(p token.Position) map[string]interface{}

func DumpStmt

func DumpStmt(s ast.Stmt, fset *token.FileSet) interface{}

func DumpTypeAlias

func DumpTypeAlias(t *ast.TypeSpec, fset *token.FileSet) map[string]interface{}

func DumpValue

func DumpValue(kind string, spec *ast.ValueSpec, fset *token.FileSet) map[string]interface{}

func IsImport

func IsImport(d ast.Decl) bool

func Perish added in v0.3.0

func Perish(pos token.Position, typ string, reason string)

func TestExpr

func TestExpr(s string) map[string]interface{}

func TestFile

func TestFile(p string) []byte

func TestStmt

func TestStmt(s string) []byte

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL