Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 1.36 KB

README.md

File metadata and controls

65 lines (49 loc) · 1.36 KB

astinfo

build-img pkg-img reportcard-img version-img

Package astinfo records useful AST information like node parents and such.

Installation:

Go version 1.16+

go get github.com/go-toolsmith/astinfo

Example

package main

import (
	"fmt"
	"go/ast"

	"github.com/go-toolsmith/astinfo"
)

func main() {
	innermost := &ast.Ident{}
	root := &ast.ExprStmt{
		X: &ast.BinaryExpr{
			X: &ast.BasicLit{},
			Y: &ast.UnaryExpr{X: innermost},
		},
	}

	info := astinfo.Info{Parents: make(map[ast.Node]ast.Node)}
	info.Origin = root
	info.Resolve()

	for p := info.Parents[innermost]; p != nil; p = info.Parents[p] {
		fmt.Printf("%T\n", p)
	}

	// Output:
	// *ast.UnaryExpr
	// *ast.BinaryExpr
	// *ast.ExprStmt
}

License

MIT License.