astutils

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(root ast.Node, pre, post ApplyFunc) (result ast.Node)

Apply traverses a syntax tree recursively, starting with root, and calling pre and post for each node as described below. Apply returns the syntax tree, possibly modified.

If pre is not nil, it is called for each node before the node's children are traversed (pre-order). If pre returns false, no children are traversed, and post is not called for that node.

If post is not nil, and a prior call of pre didn't return false, post is called for each node after its children are traversed (post-order). If post returns false, traversal is terminated and Apply returns immediately.

Only fields that refer to AST nodes are considered children; i.e., token.Pos, Scopes, Objects, and fields of basic types (strings, etc.) are ignored.

Children are traversed in the order in which they appear in the respective node's struct definition. A package's files are traversed in the filenames' alphabetical order.

func Join

func Join(list *ast.List, sep string) string
func Search(root ast.Node, f func(ast.Node) bool) *ast.List

func Walk

func Walk(f Visitor, node ast.Node)

Types

type ApplyFunc

type ApplyFunc func(*Cursor) bool

An ApplyFunc is invoked by Apply for each node n, even if n is nil, before and/or after the node's children, using a Cursor describing the current node and providing operations on it.

The return value of ApplyFunc controls the syntax tree traversal. See Apply for details.

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

A Cursor describes a node encountered during Apply. Information about the node and its parent is available from the Node, Parent, Name, and Index methods.

If p is a variable of type and value of the current parent node c.Parent(), and f is the field identifier with name c.Name(), the following invariants hold:

p.f            == c.Node()  if c.Index() <  0
p.f[c.Index()] == c.Node()  if c.Index() >= 0

The methods Replace, Delete, InsertBefore, and InsertAfter can be used to change the AST without disrupting Apply.

func (*Cursor) Index

func (c *Cursor) Index() int

Index reports the index >= 0 of the current Node in the slice of Nodes that contains it, or a value < 0 if the current Node is not part of a slice. The index of the current node changes if InsertBefore is called while processing the current node.

func (*Cursor) Name

func (c *Cursor) Name() string

Name returns the name of the parent Node field that contains the current Node. If the parent is a *ast.Package and the current Node is a *ast.File, Name returns the filename for the current Node.

func (*Cursor) Node

func (c *Cursor) Node() ast.Node

Node returns the current Node.

func (*Cursor) Parent

func (c *Cursor) Parent() ast.Node

Parent returns the parent of the current Node.

func (*Cursor) Replace

func (c *Cursor) Replace(n ast.Node)

Replace replaces the current Node with n. The replacement node is not walked by Apply.

type Visitor

type Visitor interface {
	Visit(ast.Node) Visitor
}

type VisitorFunc

type VisitorFunc func(ast.Node)

func (VisitorFunc) Visit

func (vf VisitorFunc) Visit(node ast.Node) Visitor

Jump to

Keyboard shortcuts

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