Skip to content

go-toolsmith/astinfo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages