ast

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IdentNil    = "nil"
	IdentTrue   = "true"
	IdentFalse  = "false"
	IdentIgnore = "_"
)

Variables

This section is empty.

Functions

func AddImportForcely

func AddImportForcely(root *dst.File, paths ...string) *dst.GenDecl

func AddStructField

func AddStructField(decl dst.Decl, name string, typ string)

func AddressOf

func AddressOf(name string) *dst.UnaryExpr

func ArrayType

func ArrayType(elem dst.Expr) *dst.ArrayType

func AssignStmt

func AssignStmt(lhs, rhs dst.Expr) *dst.AssignStmt

func Block

func Block(stmt dst.Stmt) *dst.BlockStmt

func BlockStmts

func BlockStmts(stmts ...dst.Stmt) *dst.BlockStmt

func BoolFalse

func BoolFalse() *dst.BasicLit

func BoolTrue

func BoolTrue() *dst.BasicLit

func CallTo

func CallTo(name string, typeArgs *dst.FieldList, args []dst.Expr) *dst.CallExpr

CallTo creates a call expression to a function with optional type arguments for generics. For non-generic functions (typeArgs is nil or empty), creates a simple call: Foo(args...) For generic functions with type arguments, creates: Foo[T1, T2](args...)

func CloneTypeParams

func CloneTypeParams(typeParams *dst.FieldList) *dst.FieldList

CloneTypeParams safely clones a type parameter field list for generic functions. Returns nil if the input is nil.

func CompositeLit

func CompositeLit(t dst.Expr, elts []dst.Expr) *dst.CompositeLit

func DeferStmt

func DeferStmt(call *dst.CallExpr) *dst.DeferStmt

func DefineStmts

func DefineStmts(lhs, rhs []dst.Expr) *dst.AssignStmt

func DereferenceOf

func DereferenceOf(expr dst.Expr) dst.Expr

func Ellipsis

func Ellipsis(elt dst.Expr) *dst.Ellipsis

func EmptyStmt

func EmptyStmt() *dst.EmptyStmt

func ExprStmt

func ExprStmt(expr dst.Expr) *dst.ExprStmt

func Exprs

func Exprs(exprs ...dst.Expr) []dst.Expr

func FindFuncDecl

func FindFuncDecl(root *dst.File, function string, receiverType string) []*dst.FuncDecl

func FindFuncDeclWithoutRecv

func FindFuncDeclWithoutRecv(root *dst.File, funcName string) *dst.FuncDecl

func FindImport

func FindImport(root *dst.File, path string) *dst.ImportSpec

func FindStructDecl

func FindStructDecl(root *dst.File, structName string) *dst.GenDecl

func HasReceiver

func HasReceiver(fn *dst.FuncDecl) bool

func Ident

func Ident(name string) *dst.Ident

func IfNotNilStmt

func IfNotNilStmt(cond dst.Expr, body, elseBody *dst.BlockStmt) *dst.IfStmt

func IfStmt

func IfStmt(init dst.Stmt, cond dst.Expr,
	body, elseBody *dst.BlockStmt) *dst.IfStmt

func IndexExpr

func IndexExpr(x dst.Expr, index dst.Expr) *dst.IndexExpr

func IndexListExpr

func IndexListExpr(x dst.Expr, indices []dst.Expr) *dst.IndexListExpr

func IntLit

func IntLit(value int) *dst.BasicLit

func InterfaceType

func InterfaceType() *dst.InterfaceType

func IsEllipsis

func IsEllipsis(typ dst.Expr) bool

func IsInterfaceType

func IsInterfaceType(typ dst.Expr) bool

func IsStringLit

func IsStringLit(expr dst.Expr, val string) bool

func IsUnusedIdent

func IsUnusedIdent(ident *dst.Ident) bool

func KeyValueExpr

func KeyValueExpr(key string, value dst.Expr) *dst.KeyValueExpr

func ListFuncDecls

func ListFuncDecls(root *dst.File) []*dst.FuncDecl

func MakeUnusedIdent

func MakeUnusedIdent(ident *dst.Ident) *dst.Ident

func NewField

func NewField(name string, typ dst.Expr) *dst.Field

func NewVarDecl

func NewVarDecl(name string, paramTypes *dst.FieldList) *dst.GenDecl

func Nil

func Nil() dst.Expr

func ParenExpr

func ParenExpr(x dst.Expr) *dst.ParenExpr

func ParseFile

func ParseFile(filePath string) (*dst.File, error)

ParseFile parses the AST from complete source file.

func ParseFileFast

func ParseFileFast(filePath string) (*dst.File, error)

func ParseFileOnlyPackage

func ParseFileOnlyPackage(filePath string) (*dst.File, error)

func RemoveImport

func RemoveImport(root *dst.File, path string) *dst.ImportSpec

func ReturnStmt

func ReturnStmt(results []dst.Expr) *dst.ReturnStmt

func SelectorExpr

func SelectorExpr(x dst.Expr, sel string) *dst.SelectorExpr

func SplitMultiNameFields

func SplitMultiNameFields(fieldList *dst.FieldList) *dst.FieldList

SplitMultiNameFields splits fields that have multiple names into separate fields. For example, a field like "a, b int" becomes two fields: "a int" and "b int".

func Stmts

func Stmts(stmts ...dst.Stmt) []dst.Stmt

func StringLit

func StringLit(value string) *dst.BasicLit

func StructLit

func StructLit(typeName string, fields ...*dst.KeyValueExpr) dst.Expr

func SwitchCase

func SwitchCase(list []dst.Expr, stmts []dst.Stmt) *dst.CaseClause

func TypeAssertExpr

func TypeAssertExpr(x dst.Expr, typ dst.Expr) *dst.TypeAssertExpr

func WriteFile

func WriteFile(astRoot *dst.File, filePath string) (string, error)

WriteFile writes the AST to source file.

Types

type AstParser

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

func NewAstParser

func NewAstParser() *AstParser

func (*AstParser) FindPosition

func (ap *AstParser) FindPosition(node dst.Node) token.Position

func (*AstParser) ParseFile

func (ap *AstParser) ParseFile(filePath string, mode parser.Mode) (*dst.File, error)

func (*AstParser) ParseSnippet

func (ap *AstParser) ParseSnippet(codeSnippet string) ([]dst.Stmt, error)

ParseSnippet parses the AST from incomplete source code snippet.

func (*AstParser) ParseSource

func (ap *AstParser) ParseSource(source string) (*dst.File, error)

ParseSource parses the AST from complete source code.

Jump to

Keyboard shortcuts

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