trav

package module
v0.0.0-...-fda3f9f Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2018 License: MIT Imports: 3 Imported by: 0

README

trav

This package is an extension to the native ast.Walk function. It traverses the Go AST and calls a hander function for each node. It includes the whole path that leads to the current node as a slice.

Installation

This package is go-gettable.

$ go get -u github.com/noxer/trav

Usage

Start by defining your handler function, it is called once for each node in the AST.

func handler(p trav.Path) {
    // do stuff
}

trav.Path is a slice of ast.Nodes, you can range over it. Call p.Current() to get the current node (the last one in the slice). If you want to continue using the slice once the handler has returned, you can call p.Copy to create a copy of the slice. The original slice will be reused in the next iteration. Now you can call one of the trav.Traverse*() functions to traverse the AST.

trav.TraverseFile("code.go", handler)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Break

func Break()

Break terminates the traversal of the AST. Must not be called outside a traversal function.

func TraverseFile

func TraverseFile(name string, f func(Path)) error

TraverseFile traverses the AST of a Go file, calling f with a slice containing the full path of nodes leading to the current node. The last element is the current node.

func TraverseNode

func TraverseNode(node ast.Node, f func(path Path))

TraverseNode traverses the subtree of a ast.Node, calling f with a slice containing the full path of nodes leading to the current node. The last element is the current node.

func TraverseSource

func TraverseSource(src string, f func(Path)) error

TraverseSource traverses the AST of a Go source string, calling f with a slice containing the full path of nodes leading to the current node. The last element is the current node.

Types

type Path

type Path []ast.Node

Path represents a path of nodes in the AST.

func (Path) Copy

func (p Path) Copy() Path

Copy creates a copy of the path which can safely be used beyond the scope of the traversal function.

func (Path) Current

func (p Path) Current() ast.Node

Current returns the last element in the path, which is the current node.

Jump to

Keyboard shortcuts

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