astutil

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneNode

func CloneNode[T ast.Node](n T) T

CloneNode returns a deep copy of a Node. It omits pointers to ast.{Scope,Object} variables.

func Deprecation added in v0.30.0

func Deprecation(doc *ast.CommentGroup) string

Deprecation returns the paragraph of the doc comment that starts with the conventional "Deprecation: " marker, as defined by https://go.dev/wiki/Deprecated, or "" if the documented symbol is not deprecated.

func PosInStringLiteral added in v0.30.0

func PosInStringLiteral(lit *ast.BasicLit, offset int) (token.Pos, error)

PosInStringLiteral returns the position within a string literal corresponding to the specified byte offset within the logical string that it denotes.

func PreorderStack added in v0.33.0

func PreorderStack(root ast.Node, stack []ast.Node, f func(n ast.Node, stack []ast.Node) bool)

PreorderStack traverses the tree rooted at root, calling f before visiting each node.

Each call to f provides the current node and traversal stack, consisting of the original value of stack appended with all nodes from root to n, excluding n itself. (This design allows calls to PreorderStack to be nested without double counting.)

If f returns false, the traversal skips over that subtree. Unlike ast.Inspect, no second call to f is made after visiting node n. In practice, the second call is nearly always used only to pop the stack, and it is surprisingly tricky to do this correctly; see https://go.dev/issue/73319.

TODO(adonovan): replace with ast.PreorderStack when go1.25 is assured.

func RangeInStringLiteral added in v0.30.0

func RangeInStringLiteral(lit *ast.BasicLit, start, end int) (token.Pos, token.Pos, error)

RangeInStringLiteral calculates the positional range within a string literal corresponding to the specified start and end byte offsets within the logical string.

Types

type Directive added in v0.31.0

type Directive struct {
	Pos  token.Pos // of preceding "//"
	Tool string
	Name string
	Args string // may contain internal spaces
}

A directive is a comment line with special meaning to the Go toolchain or another tool. It has the form:

//tool:name args

The "tool:" portion is missing for the three directives named line, extern, and export.

See https://go.dev/doc/comment#Syntax for details of Go comment syntax and https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives for details of directives used by the Go compiler.

func Directives added in v0.31.0

func Directives(g *ast.CommentGroup) (res []*Directive)

Directives returns the directives within the comment.