?
? ????? PHP ??? ???? ??? ?? ??
import "go/ast"
概述
索引
示例
ast包聲明 了用于表示 Go 包的語法樹的類型。
func FileExports(src *File) bool
func FilterDecl(decl Decl, f Filter) bool
func FilterFile(src *File, f Filter) bool
func FilterPackage(pkg *Package, f Filter) bool
func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error
func Inspect(node Node, f func(Node) bool)
func IsExported(name string) bool
func NotNilFilter(_ string, v reflect.Value) bool
func PackageExports(pkg *Package) bool
func Print(fset *token.FileSet, x interface{}) error
func SortImports(fset *token.FileSet, f *File)
func Walk(v Visitor, node Node)
type ArrayType
func (x *ArrayType) End() token.Pos
func (x *ArrayType) Pos() token.Pos
type AssignStmt
func (s *AssignStmt) End() token.Pos
func (s *AssignStmt) Pos() token.Pos
type BadDecl
func (d *BadDecl) End() token.Pos
func (d *BadDecl) Pos() token.Pos
type BadExpr
func (x *BadExpr) End() token.Pos
func (x *BadExpr) Pos() token.Pos
type BadStmt
func (s *BadStmt) End() token.Pos
func (s *BadStmt) Pos() token.Pos
type BasicLit
func (x *BasicLit) End() token.Pos
func (x *BasicLit) Pos() token.Pos
type BinaryExpr
func (x *BinaryExpr) End() token.Pos
func (x *BinaryExpr) Pos() token.Pos
type BlockStmt
func (s *BlockStmt) End() token.Pos
func (s *BlockStmt) Pos() token.Pos
type BranchStmt
func (s *BranchStmt) End() token.Pos
func (s *BranchStmt) Pos() token.Pos
type CallExpr
func (x *CallExpr) End() token.Pos
func (x *CallExpr) Pos() token.Pos
type CaseClause
func (s *CaseClause) End() token.Pos
func (s *CaseClause) Pos() token.Pos
type ChanDir
type ChanType
func (x *ChanType) End() token.Pos
func (x *ChanType) Pos() token.Pos
type CommClause
func (s *CommClause) End() token.Pos
func (s *CommClause) Pos() token.Pos
type Comment
func (c *Comment) End() token.Pos
func (c *Comment) Pos() token.Pos
type CommentGroup
func (g *CommentGroup) End() token.Pos
func (g *CommentGroup) Pos() token.Pos
func (g *CommentGroup) Text() string
type CommentMap
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
func (cmap CommentMap) Comments() []*CommentGroup
func (cmap CommentMap) Filter(node Node) CommentMap
func (cmap CommentMap) String() string
func (cmap CommentMap) Update(old, new Node) Node
type CompositeLit
func (x *CompositeLit) End() token.Pos
func (x *CompositeLit) Pos() token.Pos
type Decl
type DeclStmt
func (s *DeclStmt) End() token.Pos
func (s *DeclStmt) Pos() token.Pos
type DeferStmt
func (s *DeferStmt) End() token.Pos
func (s *DeferStmt) Pos() token.Pos
type Ellipsis
func (x *Ellipsis) End() token.Pos
func (x *Ellipsis) Pos() token.Pos
type EmptyStmt
func (s *EmptyStmt) End() token.Pos
func (s *EmptyStmt) Pos() token.Pos
type Expr
type ExprStmt
func (s *ExprStmt) End() token.Pos
func (s *ExprStmt) Pos() token.Pos
type Field
func (f *Field) End() token.Pos
func (f *Field) Pos() token.Pos
type FieldFilter
type FieldList
func (f *FieldList) End() token.Pos
func (f *FieldList) NumFields() int
func (f *FieldList) Pos() token.Pos
type File
func MergePackageFiles(pkg *Package, mode MergeMode) *File
func (f *File) End() token.Pos
func (f *File) Pos() token.Pos
type Filter
type ForStmt
func (s *ForStmt) End() token.Pos
func (s *ForStmt) Pos() token.Pos
type FuncDecl
func (d *FuncDecl) End() token.Pos
func (d *FuncDecl) Pos() token.Pos
type FuncLit
func (x *FuncLit) End() token.Pos
func (x *FuncLit) Pos() token.Pos
type FuncType
func (x *FuncType) End() token.Pos
func (x *FuncType) Pos() token.Pos
type GenDecl
func (d *GenDecl) End() token.Pos
func (d *GenDecl) Pos() token.Pos
type GoStmt
func (s *GoStmt) End() token.Pos
func (s *GoStmt) Pos() token.Pos
type Ident
func NewIdent(name string) *Ident
func (x *Ident) End() token.Pos
func (id *Ident) IsExported() bool
func (x *Ident) Pos() token.Pos
func (id *Ident) String() string
type IfStmt
func (s *IfStmt) End() token.Pos
func (s *IfStmt) Pos() token.Pos
type ImportSpec
func (s *ImportSpec) End() token.Pos
func (s *ImportSpec) Pos() token.Pos
type Importer
type IncDecStmt
func (s *IncDecStmt) End() token.Pos
func (s *IncDecStmt) Pos() token.Pos
type IndexExpr
func (x *IndexExpr) End() token.Pos
func (x *IndexExpr) Pos() token.Pos
type InterfaceType
func (x *InterfaceType) End() token.Pos
func (x *InterfaceType) Pos() token.Pos
type KeyValueExpr
func (x *KeyValueExpr) End() token.Pos
func (x *KeyValueExpr) Pos() token.Pos
type LabeledStmt
func (s *LabeledStmt) End() token.Pos
func (s *LabeledStmt) Pos() token.Pos
type MapType
func (x *MapType) End() token.Pos
func (x *MapType) Pos() token.Pos
type MergeMode
type Node
type ObjKind
func (kind ObjKind) String() string
type Object
func NewObj(kind ObjKind, name string) *Object
func (obj *Object) Pos() token.Pos
type Package
func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
func (p *Package) End() token.Pos
func (p *Package) Pos() token.Pos
type ParenExpr
func (x *ParenExpr) End() token.Pos
func (x *ParenExpr) Pos() token.Pos
type RangeStmt
func (s *RangeStmt) End() token.Pos
func (s *RangeStmt) Pos() token.Pos
type ReturnStmt
func (s *ReturnStmt) End() token.Pos
func (s *ReturnStmt) Pos() token.Pos
type Scope
func NewScope(outer *Scope) *Scope
func (s *Scope) Insert(obj *Object) (alt *Object)
func (s *Scope) Lookup(name string) *Object
func (s *Scope) String() string
type SelectStmt
func (s *SelectStmt) End() token.Pos
func (s *SelectStmt) Pos() token.Pos
type SelectorExpr
func (x *SelectorExpr) End() token.Pos
func (x *SelectorExpr) Pos() token.Pos
type SendStmt
func (s *SendStmt) End() token.Pos
func (s *SendStmt) Pos() token.Pos
type SliceExpr
func (x *SliceExpr) End() token.Pos
func (x *SliceExpr) Pos() token.Pos
type Spec
type StarExpr
func (x *StarExpr) End() token.Pos
func (x *StarExpr) Pos() token.Pos
type Stmt
type StructType
func (x *StructType) End() token.Pos
func (x *StructType) Pos() token.Pos
type SwitchStmt
func (s *SwitchStmt) End() token.Pos
func (s *SwitchStmt) Pos() token.Pos
type TypeAssertExpr
func (x *TypeAssertExpr) End() token.Pos
func (x *TypeAssertExpr) Pos() token.Pos
type TypeSpec
func (s *TypeSpec) End() token.Pos
func (s *TypeSpec) Pos() token.Pos
type TypeSwitchStmt
func (s *TypeSwitchStmt) End() token.Pos
func (s *TypeSwitchStmt) Pos() token.Pos
type UnaryExpr
func (x *UnaryExpr) End() token.Pos
func (x *UnaryExpr) Pos() token.Pos
type ValueSpec
func (s *ValueSpec) End() token.Pos
func (s *ValueSpec) Pos() token.Pos
type Visitor
CommentMap Inspect Print
ast.go commentmap.go filter.go import.go print.go resolve.go scope.go walk.go
func FileExports(src *File) bool
FileExports 將 AST 文件修剪為適當?shù)?Go 源文件,以便只保留導出的節(jié)點:刪除所有未導出的頂級標識符及其關聯(lián)信息(如類型,初始值或函數(shù)體)。非導出字段和導出類型的方法被刪除。File.Comments 列表不會更改。
FileExports 報告是否有導出聲明。
func FilterDecl(decl Decl, f Filter) bool
FilterDecl 通過刪除不通過過濾器f的所有名稱(包括struct 字段和接口方法名稱,但不包括參數(shù)列表)來修飾 AST 的 Go 聲明。
FilterDecl 報告過濾后是否有任何聲明的名稱。
func FilterFile(src *File, f Filter) bool
FilterFile 通過刪除不通過過濾器f的頂級聲明(包括 struct字段和接口方法名稱,但不包含參數(shù)列表)中的所有名稱來修剪 AST 文件。如果后面的聲明為空,則聲明將從 AST 中刪除。導入聲明總是被刪除。File.Comments 列表不會更改。
FilterFile 報告過濾后是否還有任何頂級聲明。
func FilterPackage(pkg *Package, f Filter) bool
FilterPackage 通過刪除不通過過濾器f的頂級聲明(包括struct 字段和接口方法名稱,但不包括參數(shù)列表)中的所有名稱來修剪適用于 Go 包的 AST。如果后面的聲明為空,則聲明將從 AST 中刪除。pkg.Files 列表未更改,因此文件名和頂級包注釋不會丟失。
FilterPackage 報告過濾后是否有任何頂級聲明。
func Fprint(w io.Writer, fset *token.FileSet, x interface{}, f FieldFilter) error
Fprint 將從 AST 節(jié)點x開始的(sub-)樹打印到 w。如果 fset!= nil,則相對于該文件集解釋位置信息。否則,位置將被打印為整數(shù)值(文件集特定的偏移量)。
可以提供非零 FieldFilter f 來控制輸出:打印f(fieldname,fieldvalue)為 true 的 struct 字段;所有其他的都從輸出中過濾掉。未導出的結構字段從不打印。
func Inspect(node Node, f func(Node) bool)
檢查以深度優(yōu)先順序遍歷AST:它通過調(diào)用f(node) 開始;節(jié)點不能為零。如果 f 返回 true,Inspect 會為節(jié)點的每個非零子節(jié)點遞歸調(diào)用f,然后調(diào)用 f(nil)。
這個例子演示了如何檢查 Go 程序的 AST。
package mainimport ("fmt""go/ast""go/parser""go/token")func main() {// src是我們要檢查AST的輸入。 src := ` package p const c = 1.0 var X = f(3.14)*2 + c `// 通過解析src來創(chuàng)建AST。 fset := token.NewFileSet() // 職位相對于fset f, err := parser.ParseFile(fset, "src.go", src, 0)if err != nil {panic(err)}// 檢查AST并打印所有標識符和文字。 ast.Inspect(f, func(n ast.Node) bool {var s stringswitch x := n.(type) {case *ast.BasicLit: s = x.Valuecase *ast.Ident: s = x.Name}if s != "" { fmt.Printf("%s:\t%s\n", fset.Position(n.Pos()), s)}return true})}
func IsExported(name string) bool
IsExported 報告名稱是否為導出的 Go 符號(即,它是否以大寫字母開頭)。
func NotNilFilter(_ string, v reflect.Value) bool
NotNilFilter 對不為零的字段值返回 true;否則返回 false。
func PackageExports(pkg *Package) bool
PackageExports 將適用于 Go 包的 AST 修剪為僅保留導出的節(jié)點。pkg.Files 列表未更改,因此文件名和頂級包注釋不會丟失。
PackageExports 報告是否有導出聲明;否則返回 false。
func Print(fset *token.FileSet, x interface{}) error
Print打印x到標準輸出,跳過零字段。打?。╢set,x)與Fprint(os.Stdout,fset,x,NotNilFilter)相同。
這個例子顯示了打印進行調(diào)試時 AST 的外觀。
package mainimport ("go/ast""go/parser""go/token")func main() {// src是我們要打印AST的輸入。 src := ` package main func main() { println("Hello, World!") } `// 通過解析src來創(chuàng)建AST。 fset := token.NewFileSet() // 職位相對于fset f, err := parser.ParseFile(fset, "", src, 0)if err != nil {panic(err)}// 打印AST。 ast.Print(fset, f)}
func SortImports(fset *token.FileSet, f *File)
SortImports 對f中的導入塊中的連續(xù)導入行進行排序。它還可以在不丟失數(shù)據(jù)的情況下刪除重復導入。
func Walk(v Visitor, node Node)
以深度優(yōu)先順序遍歷 AST:它通過調(diào)用 v.Visit(節(jié)點)開始; 節(jié)點不能為零。如果由 v.Visit(節(jié)點)返回的訪問者 w 不為零,則對于節(jié)點的每個非零子節(jié)點,訪問者 w 遞歸地調(diào)用 Walk,隨后調(diào)用 w.Visit(nil)。
ArrayType 節(jié)點表示一個數(shù)組或切片類型。
type ArrayType struct { Lbrack token.Pos // “[”的位置 Len Expr // [...] T數(shù)組類型的省略號節(jié)點,切片類型為零 Elt Expr // 元素類型}
func (x *ArrayType) End() token.Pos
func (x *ArrayType) Pos() token.Pos
AssignStmt 節(jié)點表示賦值或短變量聲明。
type AssignStmt struct { Lhs []Expr TokPos token.Pos // Tok的位置 Tok token.Token // 賦值令牌,DEFINE Rhs []Expr}
func (s *AssignStmt) End() token.Pos
func (s *AssignStmt) Pos() token.Pos
BadDecl 節(jié)點是包含語法錯誤的聲明的占位符,其中不能創(chuàng)建正確的聲明節(jié)點。
type BadDecl struct { From, To token.Pos // 不良聲明的位置范圍}
func (d *BadDecl) End() token.Pos
func (d *BadDecl) Pos() token.Pos
BadExpr 節(jié)點是包含語法錯誤的表達式的占位符,不能創(chuàng)建正確的表達式節(jié)點。
type BadExpr struct { From, To token.Pos // 不良表達的位置范圍}
func (x *BadExpr) End() token.Pos
func (x *BadExpr) Pos() token.Pos
BadStmt 節(jié)點是包含語法錯誤的語句的占位符,其中不能創(chuàng)建正確的語句節(jié)點。
type BadStmt struct { From, To token.Pos // 不良陳述的位置范圍}
func (s *BadStmt) End() token.Pos
func (s *BadStmt) Pos() token.Pos
BasicLit 節(jié)點表示基本類型的文字。
type BasicLit struct { ValuePos token.Pos // literal position Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`}
func (x *BasicLit) End() token.Pos
func (x *BasicLit) Pos() token.Pos
BinaryExpr 節(jié)點表示一個二進制表達式。
type BinaryExpr struct { X Expr // 左操作數(shù) OpPos token.Pos // Op的位置 Op token.Token // 操作者 Y Expr // 右操作數(shù)}
func (x *BinaryExpr) End() token.Pos
func (x *BinaryExpr) Pos() token.Pos
BlockStmt 節(jié)點表示一個支撐語句列表。
type BlockStmt struct { Lbrace token.Pos // “{”的位置 List []Stmt Rbrace token.Pos // “}”的位置}
func (s *BlockStmt) End() token.Pos
func (s *BlockStmt) Pos() token.Pos
BranchStmt 節(jié)點表示中斷,繼續(xù),轉(zhuǎn)到或直通語句。
type BranchStmt struct { TokPos token.Pos // Tok的位置 Tok token.Token // 關鍵字令牌(BREAK,CONTINUE,GOTO,F(xiàn)ALLTHROUGH) Label *Ident // 標簽名稱; 或者nil}
func (s *BranchStmt) End() token.Pos
func (s *BranchStmt) Pos() token.Pos
CallExpr 節(jié)點代表一個表達式,后跟一個參數(shù)列表。
type CallExpr struct { Fun Expr // 函數(shù)表達 Lparen token.Pos // “(”的位置 Args []Expr // 函數(shù)參數(shù); 或者沒有 Ellipsis token.Pos // “......”的位置(如果沒有“......”則為token.NoPos) Rparen token.Pos // “)的位置}
func (x *CallExpr) End() token.Pos
func (x *CallExpr) Pos() token.Pos
CaseClause表示表達式或類型切換語句的一種情況。
type CaseClause struct { Case token.Pos // “case”或“default”關鍵字的位置 List []Expr // 表達式或類型列表; nil表示默認情況 Colon token.Pos // “:”的位置 Body []Stmt // 聲明清單; 或者沒有}
func (s *CaseClause) End() token.Pos
func (s *CaseClause) Pos() token.Pos
通道類型的方向由以下常量之一表示。
type ChanDir int
const ( SEND ChanDir = 1 << iota RECV)
ChanType 節(jié)點代表一個通道類型。
type ChanType struct { Begin token.Pos // “chan”關鍵字或“< - ”的位置(以先到者為準) Arrow token.Pos // “< - ”的位置(如果沒有“< - ”則為token.NoPos) Dir ChanDir // 渠道方向 Value Expr // 值類型}
func (x *ChanType) End() token.Pos
func (x *ChanType) Pos() token.Pos
CommClause 節(jié)點表示選擇語句的情況。
type CommClause struct { Case token.Pos // “case”或“default”關鍵字的位置 Comm Stmt // 發(fā)送或接收聲明; nil表示默認情況 Colon token.Pos // “:”的位置 Body []Stmt // 聲明清單; 或者沒有}
func (s *CommClause) End() token.Pos
func (s *CommClause) Pos() token.Pos
Comment 節(jié)點表示一個 //style 或 /*- style 的注釋。
type Comment struct { Slash token.Pos // “/”開頭注釋的位置 Text string // 注釋文本(不包括用于//style 注釋的'\n')}
func (c *Comment) End() token.Pos
func (c *Comment) Pos() token.Pos
CommentGroup 表示一系列注釋,沒有其他標記,也沒有空行。
type CommentGroup struct { List []*Comment // len(List) > 0}
func (g *CommentGroup) End() token.Pos
func (g *CommentGroup) Pos() token.Pos
func (g *CommentGroup) Text() string
文本返回注釋的文本。注釋標記(//,/* 和 */),行注釋的第一個空格,以及前導空行和尾行空行被刪除。多個空行減少為一行,行尾的空格被修剪。除非結果是空的,否則它是換行符終止的。
CommentMap 將 AST 節(jié)點映射到與其關聯(lián)的評論組列表。請參閱 NewCommentMap 以獲取關聯(lián)的描述。
type CommentMap map[Node][]*CommentGroup
這個例子說明了如何使用 ast.CommentMap 去除 Go 程序中的變量聲明,同時保持正確的評論關聯(lián)。
package mainimport ("bytes""fmt""go/ast""go/format""go/parser""go/token")func main() {// src是我們創(chuàng)建AST的輸入// 將要操縱。 src := ` // This is the package comment. package main // This comment is associated with the hello constant. const hello = "Hello, World!" // line comment 1 // This comment is associated with the foo variable. var foo = hello // line comment 2 // This comment is associated with the main function. func main() { fmt.Println(hello) // line comment 3 } `// 通過解析src來創(chuàng)建AST。 fset := token.NewFileSet() // 職位相對于fset f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)if err != nil {panic(err)}// 從ast.File的注釋中創(chuàng)建一個ast.CommentMap。// 這有助于保持注釋之間的關聯(lián)// 和AST節(jié)點。 cmap := ast.NewCommentMap(fset, f, f.Comments)// 從聲明列表中刪除第一個變量聲明。for i, decl := range f.Decls {if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR {copy(f.Decls[i:], f.Decls[i+1:]) f.Decls = f.Decls[:len(f.Decls)-1]}}// 使用注釋地圖過濾不再屬于的注釋// (與變量聲明相關的注釋),并創(chuàng)建// 新注釋列表。 f.Comments = cmap.Filter(f).Comments()// 打印修改后的AST。var buf bytes.Bufferif err := format.Node(&buf, fset, f); err != nil {panic(err)} fmt.Printf("%s", buf.Bytes())}
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
NewCommentMap 通過將注釋列表的注釋組與節(jié)點指定的 AST 的節(jié)點關聯(lián)來創(chuàng)建新的注釋圖。
如果滿足以下條件,則注釋組 g 與節(jié)點 n 相關聯(lián):
- g與n結束時在同一行開始- g緊跟在n之后的行上開始,并且有 g之后和下一個節(jié)點之前至少有一個空行- g在n之前開始,并且不與n之前的節(jié)點相關聯(lián) 通過以前的規(guī)則
NewCommentMap 嘗試將注釋組關聯(lián)到可能的“l(fā)argest”節(jié)點:例如,如果注釋是一個跟隨分配的行注釋,則注釋與整個分配相關聯(lián),而不僅僅是分配中的最后一個操作數(shù)。
func (cmap CommentMap) Comments() []*CommentGroup
評論返回評論地圖中評論組的列表。結果按來源順序排序。
func (cmap CommentMap) Filter(node Node) CommentMap
篩選器返回一個新的注釋映射,其中只包含由節(jié)點指定的 AST 中存在相應節(jié)點的 cmap 的條目。
func (cmap CommentMap) String() string
func (cmap CommentMap) Update(old, new Node) Node
更新用新節(jié)點替換評論映射中的舊節(jié)點并返回新節(jié)點。與舊節(jié)點相關聯(lián)的注釋與新節(jié)點相關聯(lián)。
CompositeLit 節(jié)點表示復合文字。
type CompositeLit struct { Type Expr // 字面類型; 或者nil Lbrace token.Pos // “{”的位置 Elts []Expr // 復合元素列表; 或者nil Rbrace token.Pos // “}”的位置}
func (x *CompositeLit) End() token.Pos
func (x *CompositeLit) Pos() token.Pos
所有聲明節(jié)點都實現(xiàn) Decl 接口。
type Decl interface { Node // 包含過濾或未導出的方法}
DeclStmt節(jié)點表示聲明列表中的聲明。
type DeclStmt struct { Decl Decl // * GenDecl與CONST,TYPE或VAR令牌}
func (s *DeclStmt) End() token.Pos
func (s *DeclStmt) Pos() token.Pos
DeferStmt 節(jié)點表示推遲語句。
type DeferStmt struct { Defer token.Pos // “defer”關鍵字的位置 Call *CallExpr}
func (s *DeferStmt) End() token.Pos
func (s *DeferStmt) Pos() token.Pos
省略號節(jié)點代表參數(shù)列表中的“...”類型或數(shù)組類型中的“...”長度。
type Ellipsis struct { Ellipsis token.Pos // “...”的位置 Elt Expr // 省略號元素類型(僅限參數(shù)列表); 或者沒有
func (x *Ellipsis) End() token.Pos
func (x *Ellipsis) Pos() token.Pos
EmptyStmt 節(jié)點表示一個空語句。空語句的“position”是緊隨其后的(顯式或隱式)分號的位置。
type EmptyStmt struct { Semicolon token.Pos // 以下“;”的位置 Implicit bool // 如果設置,“;” 在源中省略了}
func (s *EmptyStmt) End() token.Pos
func (s *EmptyStmt) Pos() token.Pos
所有表達式節(jié)點都實現(xiàn) Expr 接口。
type Expr interface { Node // 包含過濾或未導出的方法}
ExprStmt節(jié)點表示語句列表中的(stand-alone)表達式。
type ExprStmt struct { X Expr // 表達式}
func (s *ExprStmt) End() token.Pos
func (s *ExprStmt) Pos() token.Pos
字段表示結構類型中的字段聲明列表,接口類型中的方法列表或簽名中的參數(shù)/結果聲明。
type Field struct { Doc *CommentGroup // 相關文件; 或者沒有 Names []*Ident // 字段/方法/參數(shù)名稱;或者如果匿名字段為零 Type Expr // 字段/方法/參數(shù)類型 Tag *BasicLit // 字段標簽; 或者沒有 Comment *CommentGroup // 行注釋; 或者沒有}
func (f *Field) End() token.Pos
func (f *Field) Pos() token.Pos
FieldFilter可以提供給Fprint來控制輸出。
type FieldFilter func(name string, value reflect.Value) bool
FieldList表示一個由圓括號或大括號包圍的字段列表。
type FieldList struct { Opening token.Pos // 開括號/括號的位置,如果有的話 List []*Field // 字段列表; 或者沒有 Closing token.Pos // 右括號/括號的位置,如果有的話}
func (f *FieldList) End() token.Pos
func (f *FieldList) NumFields() int
NumFields 返回 FieldList 中的(命名和匿名字段)的數(shù)量。
func (f *FieldList) Pos() token.Pos
一個文件節(jié)點代表一個 Go 源文件。
注釋列表按照外觀順序包含源文件中的所有注釋,包括通過 Doc 和注釋字段從其他節(jié)點指向的注釋。
為了正確打印包含注釋的源代碼(使用軟件包 go/format 和 go/printer),必須特別小心在修改文件語法樹時更新注釋:對于打印,注釋根據(jù)其位置散布在令牌之間。如果語法樹節(jié)點被刪除或移動,則其附近的相關注釋也必須從 File.Comments 列表中移除或相應移動(通過更新其位置)。一個 CommentMap 可以用來方便這些操作。
注釋是否以及如何與節(jié)點相關取決于操作程序?qū)φZ法樹的解釋:除了與節(jié)點直接相關的文檔和評論評論之外,其余注釋是“自由浮動的(free-floating)”(另見issues #18593, #20744)。
type File struct { Doc *CommentGroup // 相關文件; 或者沒有 Package token.Pos // “package”關鍵字的位置 Name *Ident // 包名稱 Decls []Decl // 頂級聲明; 或者沒有 Scope *Scope // 包范圍(僅限此文件) Imports []*ImportSpec // 在此文件中導入 Unresolved []*Ident // 此文件中未解析的標識符 Comments []*CommentGroup // 源文件中所有注釋的列表}
func MergePackageFiles(pkg *Package, mode MergeMode) *File
MergePackageFiles 通過合并屬于包的文件的 AST 來創(chuàng)建文件 AST。模式標志控制合并行為。
func (f *File) End() token.Pos
func (f *File) Pos() token.Pos
type Filter func(string) bool
ForStmt 表示 for 語句。
type ForStmt struct { For token.Pos // “for”關鍵字的位置 Init Stmt // 初始化語句; 或者沒有 Cond Expr // 條件; 或者沒有 Post Stmt // post iteration語句; 或者沒有 Body *BlockStmt}
func (s *ForStmt) End() token.Pos
func (s *ForStmt) Pos() token.Pos
FuncDecl 節(jié)點表示一個函數(shù)聲明。
type FuncDecl struct { Doc *CommentGroup // 相關文件; 或者沒有 Recv *FieldList // 接收器(方法); 或零(函數(shù)) Name *Ident // 函數(shù)/方法名稱 Type *FuncType // 函數(shù)簽名:“func”關鍵字的參數(shù),結果和位置 Body *BlockStmt // 功能體; 或者對于外部(非Go)功能為零
func (d *FuncDecl) End() token.Pos
func (d *FuncDecl) Pos() token.Pos
一個FuncLit節(jié)點代表一個函數(shù)文字。
type FuncLit struct { Type *FuncType // 函數(shù)類型 Body *BlockStmt // 函數(shù)體}
func (x *FuncLit) End() token.Pos
func (x *FuncLit) Pos() token.Pos
FuncType 節(jié)點表示一個函數(shù)類型。
type FuncType struct { Func token.Pos // “func”關鍵字的位置(如果沒有“func”,則為token.NoPos) Params *FieldList // (傳入)參數(shù);非空 Results *FieldList // (外向)結果;或者沒有}
func (x *FuncType) End() token.Pos
func (x *FuncType) Pos() token.Pos
GenDecl節(jié)點(通用聲明節(jié)點)表示一個導入,常量,類型或變量聲明。有效的 Lparen 位置(Lparen.IsValid())表示括號聲明。
Tok 值與 Specs 元素類型之間的關系:
token.IMPORT *ImportSpec token.CONST *ValueSpec token.TYPE *TypeSpec token.VAR *ValueSpec
type GenDecl struct { Doc *CommentGroup // 相關文件; 或者沒有 TokPos token.Pos // Tok 的位置 Tok token.Token // IMPORT, CONST, TYPE, VAR Lparen token.Pos // “(',如果有的話)的位置 Specs []Spec Rparen token.Pos // ')'的位置,如果有的話}
func (d *GenDecl) End() token.Pos
func (d *GenDecl) Pos() token.Pos
GoStmt 節(jié)點表示 go 語句。
type GoStmt struct { Go token.Pos // “go” 關鍵字的位置 Call *CallExpr}
func (s *GoStmt) End() token.Pos
func (s *GoStmt) Pos() token.Pos
一個 Ident 節(jié)點表示一個標識符。
type Ident struct { NamePos token.Pos // 確定位置 Name string // 標識符名稱 Obj *Object // 表示對象; 或者沒有}
func NewIdent(name string) *Ident
NewIdent 創(chuàng)建一個沒有位置的新的 Ident。對于由 Go 解析器以外的代碼生成的 AST 非常有用。
func (x *Ident) End() token.Pos
func (id *Ident) IsExported() bool
IsExported 報告 id 是否是導出的 Go 符號(即,它是否以大寫字母開頭)。
func (x *Ident) Pos() token.Pos
func (id *Ident) String() string
IfStmt 節(jié)點表示一個 if 語句。
type IfStmt struct { If token.Pos // “if”關鍵字的位置 Init Stmt // 初始化語句; 或者沒有 Cond Expr // 條件 Body *BlockStmt Else Stmt // 別的分支; 或者沒有}
func (s *IfStmt) End() token.Pos
func (s *IfStmt) Pos() token.Pos
ImportSpec 節(jié)點表示單個包導入。
type ImportSpec struct { Doc *CommentGroup // 相關文件; 或者沒有 Name *Ident // 本地包名(包括“.”); 或者沒有 Path *BasicLit // 導入路徑 Comment *CommentGroup // 行注釋;或者沒有 EndPos token.Pos // 規(guī)范結束(如果非零則覆蓋Path.Pos)}
func (s *ImportSpec) End() token.Pos
func (s *ImportSpec) Pos() token.Pos
導入程序解析導入路徑以打包對象。導入映射記錄已導入的包,并按包ID(規(guī)范導入路徑)進行索引。導入程序必須確定規(guī)范導入路徑并檢查導圖,看它是否已經(jīng)存在于導入導圖中。如果是這樣,導入器可以返回地圖條目。否則,導入程序應將給定路徑的包數(shù)據(jù)加載到新的 *Object(pkg)中,在導入映射中記錄 pkg,然后返回 pkg。
type Importer func(imports map[string]*Object, path string) (pkg *Object, err error)
IncDecStmt 節(jié)點表示增量或減量語句。
type IncDecStmt struct { X Expr TokPos token.Pos // Tok的位置 Tok token.Token // INC或DEC}
func (s *IncDecStmt) End() token.Pos
func (s *IncDecStmt) Pos() token.Pos
IndexExpr 節(jié)點表示后跟一個索引的表達式。
type IndexExpr struct { X Expr // 表達式 Lbrack token.Pos // “[”的位置 Index Expr // 索引表達式 Rbrack token.Pos // “]的位置}
func (x *IndexExpr) End() token.Pos
func (x *IndexExpr) Pos() token.Pos
InterfaceType 節(jié)點表示接口類型。
type InterfaceType struct { Interface token.Pos // “interface”關鍵字的位置 Methods *FieldList // 方法列表 Incomplete bool // 如果“方法”列表中缺少(源)方法,則為true}
func (x *InterfaceType) End() token.Pos
func (x *InterfaceType) Pos() token.Pos
KeyValueExpr節(jié)點表示復合文字中的(鍵:值)對。
type KeyValueExpr struct { Key Expr Colon token.Pos // “:”的位置 Value Expr}
func (x *KeyValueExpr) End() token.Pos
func (x *KeyValueExpr) Pos() token.Pos
LabeledStmt 節(jié)點表示帶標簽的語句。
type LabeledStmt struct { Label *Ident Colon token.Pos // “:”的位置 Stmt Stmt}
func (s *LabeledStmt) End() token.Pos
func (s *LabeledStmt) Pos() token.Pos
MapType節(jié)點表示地圖類型。
type MapType struct { Map token.Pos // “map”關鍵字的位置 Key Expr Value Expr}
func (x *MapType) End() token.Pos
func (x *MapType) Pos() token.Pos
MergeMode 標志控制 MergePackageFiles 的行為。
type MergeMode uint
const ( // 如果設置,則排除重復的函數(shù)聲明。 FilterFuncDuplicates MergeMode = 1 << iota // 如果設置,則與特定的注釋無關 // AST節(jié)點(作為Doc或Comment)被排除在外。 FilterUnassociatedComments // 如果設置,則排除重復的導入聲明。 FilterImportDuplicates)
所有節(jié)點類型都實現(xiàn) Node 接口。
type Node interface { Pos() token.Pos // 屬于該節(jié)點的第一個字符的位置 End() token.Pos // 緊接在節(jié)點之后的第一個字符的位置}
ObjKind 描述了一個對象代表什么。
type ObjKind int
可能的對象種類的列表。
const ( Bad ObjKind = iota // 用于錯誤處理 Pkg // 包 Con // 常量 Typ // 類型 Var // 變量 Fun // 函數(shù)或方法 Lbl // 標簽)
func (kind ObjKind) String() string
一個對象描述了一個命名的語言實體,如包,常量,類型,變量,函數(shù)(包括方法)或標簽。
數(shù)據(jù)字段包含對象特定的數(shù)據(jù):
Kind Data type Data value Pkg *Scope package scope Con int iota for the respective declaration
type Object struct { Kind ObjKind Name string // 聲明的名稱 Decl interface{} // 對應的Field,XxxSpec,F(xiàn)uncDecl,LabeledStmt,AssignStmt,Scope; 或者沒有 Data interface{} // 特定對象數(shù)據(jù);或者沒有 Type interface{} // 占位符的類型信息; 可能是零}
func NewObj(kind ObjKind, name string) *Object
NewObj 創(chuàng)建一個給定種類和名稱的新對象。
func (obj *Object) Pos() token.Pos
Pos 計算對象名稱聲明的源位置。如果無法計算結果可能是無效位置(obj.Decl 可能為零或不正確)。
Package 節(jié)點表示一組源文件,共同構建 Go 包。
type Package struct { Name string // 包名稱 Scope *Scope // 所有文件的包范圍 Imports map[string]*Object // 包id的地圖 - >包對象 Files map[string]*File // 按文件名轉(zhuǎn)到源文件
func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
NewPackage 從一組 File 節(jié)點創(chuàng)建一個新的 Package 節(jié)點。它解決跨文件的未解析標識符,并相應地更新每個文件的未解析列表。如果提供了非零導入器和 Universe 范圍,則它們將用于解析未在任何包文件中聲明的標識符。任何剩余的未解析標識符都會報告為未聲明。如果這些文件屬于不同的軟件包,則會選擇一個軟件包名稱,并報告具有不同軟件包名稱的文件,然后將其忽略。結果是包節(jié)點和 scanner.ErrorList 是否有錯誤。
func (p *Package) End() token.Pos
func (p *Package) Pos() token.Pos
一個 ParenExpr 節(jié)點表示一個帶括號的表達式。
type ParenExpr struct { Lparen token.Pos // “(”的位置 X Expr // 括號表達式 Rparen token.Pos // “)”的位置}
func (x *ParenExpr) End() token.Pos
func (x *ParenExpr) Pos() token.Pos
RangeStmt 表示帶有范圍子句的 for 語句。
type RangeStmt struct { For token.Pos // “for”關鍵字的位置 Key, Value Expr // 關鍵,值可能是零 TokPos token.Pos // Tok的位置; 如果Key == nil則無效 Tok token.Token // 如果Key == nil,ASSIGN,DEFINE,則為ILLEGAL X Expr // 值范圍超過 Body *BlockStmt}
func (s *RangeStmt) End() token.Pos
func (s *RangeStmt) Pos() token.Pos
ReturnStmt 節(jié)點表示一個返回語句。
type ReturnStmt struct { Return token.Pos // “return”關鍵字的位置 Results []Expr // 結果表達式;或者沒有}
func (s *ReturnStmt) End() token.Pos
func (s *ReturnStmt) Pos() token.Pos
Scope 維護在范圍中聲明的一組指定語言實體以及到緊鄰的(外部)范圍的鏈接。
type Scope struct { Outer *Scope Objects map[string]*Object}
func NewScope(outer *Scope) *Scope
NewScope 創(chuàng)建一個嵌套在外部作用域中的新作用域。
func (s *Scope) Insert(obj *Object) (alt *Object)
Insert 嘗試將命名對象 obj 插入到作用域中。如果范圍已經(jīng)包含名稱相同的對象 alt,則 Insert 會保持范圍不變并返回 alt。否則,它插入 obj 并返回 nil。
func (s *Scope) Lookup(name string) *Object
如果在范圍s中找到,Lookup 將返回具有給定名稱的對象,否則返回 nil。外部作用域被忽略。
func (s *Scope) String() string
調(diào)試支持
SelectStmt 節(jié)點表示一個 select 語句。
type SelectStmt struct { Select token.Pos // “select”關鍵字的位置 Body *BlockStmt // 僅限CommClauses}
func (s *SelectStmt) End() token.Pos
func (s *SelectStmt) Pos() token.Pos
SelectorExpr 節(jié)點表示一個表達式,后跟一個選擇器。
type SelectorExpr struct { X Expr // 表達式 Sel *Ident // 場選擇器}
func (x *SelectorExpr) End() token.Pos
func (x *SelectorExpr) Pos() token.Pos
SendStmt 節(jié)點表示發(fā)送語句。
type SendStmt struct { Chan Expr Arrow token.Pos // “<- ”的位置 Value Expr}
func (s *SendStmt) End() token.Pos
func (s *SendStmt) Pos() token.Pos
SliceExpr 節(jié)點表示一個表達式,后跟切片索引。
type SliceExpr struct { X Expr // 表達式 Lbrack token.Pos // “[”的位置 Low Expr // 切片范圍的開始;或者沒有 High Expr // 切片范圍的結束; 或者沒有 Max Expr // 最大切片容量; 或者沒有 Slice3 bool // 如果3-index切片(存在2個冒號),則為true Rbrack token.Pos // “]"的位置}
func (x *SliceExpr) End() token.Pos
func (x *SliceExpr) Pos() token.Pos
Spec類型代表 *ImportSpec,*ValueSpec 和 *TypeSpec 中的任何一個。
type Spec interface { Node // 包含過濾或未導出的方法}
StarExpr 節(jié)點表示形式為“*”表達式的表達式。在語義上它可以是一個一元“*”表達式或指針類型。
type StarExpr struct { Star token.Pos // “*”的位置 X Expr // 操作數(shù)}
func (x *StarExpr) End() token.Pos
func (x *StarExpr) Pos() token.Pos
所有語句節(jié)點都實現(xiàn) Stmt 接口。
type Stmt interface { Node // 包含過濾或未導出的方法}
StructType 節(jié)點表示一個結構類型。
type StructType struct { Struct token.Pos // “struct”關鍵字的位置 Fields *FieldList // 列表聲明清單 Incomplete bool // 如果“字段”列表中缺少(源)字段,則為true}
func (x *StructType) End() token.Pos
func (x *StructType) Pos() token.Pos
一個 SwitchStmt 節(jié)點表示一個表達式 switch 語句。
type SwitchStmt struct { Switch token.Pos // “switch”關鍵字的位置 Init Stmt // 初始化語句; 或者沒有 Tag Expr // 標簽表達; 或者沒有 Body *BlockStmt // 僅限CaseClauses}
func (s *SwitchStmt) End() token.Pos
func (s *SwitchStmt) Pos() token.Pos
TypeAssertExpr 節(jié)點表示一個表達式,后跟一個類型斷言。
type TypeAssertExpr struct { X Expr // 表達式 Lparen token.Pos // “(”的位置 Type Expr // 斷言類型; nil表示類型開關X.(type) Rparen token.Pos // “)"的位置}
func (x *TypeAssertExpr) End() token.Pos
func (x *TypeAssertExpr) Pos() token.Pos
TypeSpec 節(jié)點代表一個類型聲明(TypeSpec 生成)。
type TypeSpec struct { Doc *CommentGroup // 相關文件;或者沒有 Name *Ident // 類型名稱 Assign token.Pos // '='的位置,如果有的話 Type Expr // *Ident,*ParenExpr,*SelectorExpr,*StarExpr或任何*XxxTypes Comment *CommentGroup // 行注釋;或者沒有}
func (s *TypeSpec) End() token.Pos
func (s *TypeSpec) Pos() token.Pos
TypeSwitchStmt 節(jié)點表示類型切換語句。
type TypeSwitchStmt struct { Switch token.Pos // “switch”關鍵字的位置 Init Stmt // 初始化語句; 或者沒有 Assign Stmt // x := y.(type)或y.(type) Body *BlockStmt // 僅限CaseClauses}
func (s *TypeSwitchStmt) End() token.Pos
func (s *TypeSwitchStmt) Pos() token.Pos
UnaryExpr 節(jié)點表示一個一元表達式。一元“*”表達式通過 StarExpr 節(jié)點表示。
type UnaryExpr struct { OpPos token.Pos // Op 的位置 Op token.Token // 操作者 X Expr // 操作數(shù)}
func (x *UnaryExpr) End() token.Pos
func (x *UnaryExpr) Pos() token.Pos
ValueSpec 節(jié)點表示常量或變量聲明(ConstSpec 或 VarSpec 生成)。
type ValueSpec struct { Doc *CommentGroup // 相關文件; 或者沒有 Names []*Ident // 值名稱(len(Names) > 0) Type Expr // 值類型; 或者沒有 Values []Expr // 初始值; 或者沒有 Comment *CommentGroup // 行注釋 或者沒有}
func (s *ValueSpec) End() token.Pos
func (s *ValueSpec) Pos() token.Pos
為 Walk 遇到的每個節(jié)點調(diào)用訪問者訪問方法。如果結果訪問者w不為零,則 Walk 訪問w的每個子節(jié)點,然后調(diào)用 w.Visit(nil)。
type Visitor interface { Visit(node Node) (w Visitor)}