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