astinfo

package module
v0.0.0-...-9809ff7 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2018 License: MIT Imports: 1 Imported by: 1

README

Go Report Card GoDoc Build Status

astinfo

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

Installation:

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
}

Documentation

Overview

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

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	// Origin is a node that was used to collect all the
	// information stored inside this object.
	Origin ast.Node

	// Parents maps child AST not to its parent.
	//
	// Does not contain top-level nodes, so the lookup will
	// return nil for those.
	Parents map[ast.Node]ast.Node
}

Info holds AST metadata collected during Origin field traversal.

func (*Info) Resolve

func (info *Info) Resolve()

Resolve fills AST info collected during info.Origin traversal.

Jump to

Keyboard shortcuts

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