Package ast
import "go/ast"
The AST package declares the types used to represent syntax trees for Go packages.
Package files
ast.go filter.go scope.go walk.gofunc FileExports
func FileExports(src *File) bool
FileExports trims the AST for a Go source file in place such that only exported nodes remain: all top-level identifiers which are not exported and their associated information (such as type, initial value, or function body) are removed. Non-exported fields and methods of exported types are stripped, and the function bodies of exported functions are set to nil. The File.comments list is not changed.
FileExports returns true if there is an exported declaration; it returns false otherwise.
func IsExported
func IsExported(name string) bool
IsExported returns whether name is an exported Go symbol (i.e., whether it begins with an uppercase letter).
func PackageExports
func PackageExports(pkg *Package) bool
PackageExports trims the AST for a Go package in place such that only exported nodes remain. The pkg.Files list is not changed, so that file names and top-level package comments don't get lost.
PackageExports returns true if there is an exported declaration; it returns false otherwise.
func Walk
func Walk(v Visitor, node interface{})
Walk traverses an AST in depth-first order: If node != nil, it invokes v.Visit(node). If the visitor w returned by v.Visit(node) is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).
Walk may be called with any of the named ast node types. It also accepts arguments of type []*Field, []*Ident, []Expr, []Stmt and []Decl; the respective children are the slice elements.
type ArrayType
An ArrayType node represents an array or slice type.
type ArrayType struct {
token.Position // position of "["
Len Expr // Ellipsis node for [...]T array types, nil for slice types
Elt Expr // element type
}
type AssignStmt
An AssignStmt node represents an assignment or a short variable declaration.
type AssignStmt struct {
Lhs []Expr
TokPos token.Position // position of Tok
Tok token.Token // assignment token, DEFINE
Rhs []Expr
}
func (*AssignStmt) Pos
func (s *AssignStmt) Pos() token.Position
type BadDecl
A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.
type BadDecl struct {
token.Position // beginning position of bad declaration
}
type BadExpr
A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.
type BadExpr struct {
token.Position // beginning position of bad expression
}
type BadStmt
A BadStmt node is a placeholder for statements containing syntax errors for which no correct statement nodes can be created.
type BadStmt struct {
token.Position // beginning position of bad statement
}
type BasicLit
A BasicLit node represents a literal of basic type.
type BasicLit struct {
token.Position // literal position
Kind token.Token // token.INT, token.FLOAT, token.CHAR, or token.STRING
Value []byte // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 'a', '\x7f', "foo" or `\m\n\o`
}
type BinaryExpr
A BinaryExpr node represents a binary expression.
type BinaryExpr struct {
X Expr // left operand
OpPos token.Position // position of Op
Op token.Token // operator
Y Expr // right operand
}
func (*BinaryExpr) Pos
func (x *BinaryExpr) Pos() token.Position
type BlockStmt
A BlockStmt node represents a braced statement list.
type BlockStmt struct {
token.Position // position of "{"
List []Stmt
Rbrace token.Position // position of "}"
}
type BranchStmt
A BranchStmt node represents a break, continue, goto, or fallthrough statement.
type BranchStmt struct {
token.Position // position of Tok
Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label *Ident
}
type CallExpr
A CallExpr node represents an expression followed by an argument list.
type CallExpr struct {
Fun Expr // function expression
Lparen token.Position // position of "("
Args []Expr // function arguments
Rparen token.Position // positions of ")"
}
func (*CallExpr) Pos
func (x *CallExpr) Pos() token.Position
type CaseClause
A CaseClause represents a case of an expression switch statement.
type CaseClause struct {
token.Position // position of "case" or "default" keyword
Values []Expr // nil means default case
Colon token.Position // position of ":"
Body []Stmt // statement list; or nil
}
type ChanDir
The direction of a channel type is indicated by one of the following constants.
type ChanDir int
const (
SEND ChanDir = 1 << iota
RECV
)
type ChanType
A ChanType node represents a channel type.
type ChanType struct {
token.Position // position of "chan" keyword or "<-" (whichever comes first)
Dir ChanDir // channel direction
Value Expr // value type
}
type CommClause
A CommClause node represents a case of a select statement.
type CommClause struct {
token.Position // position of "case" or "default" keyword
Tok token.Token // ASSIGN or DEFINE (valid only if Lhs != nil)
Lhs, Rhs Expr // Rhs == nil means default case
Colon token.Position // position of ":"
Body []Stmt // statement list; or nil
}
type Comment
A Comment node represents a single //-style or /*-style comment.
type Comment struct {
token.Position // beginning position of the comment
Text []byte // comment text (excluding '\n' for //-style comments)
}
type CommentGroup
A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.
type CommentGroup struct {
List []*Comment
}
type CompositeLit
A CompositeLit node represents a composite literal.
type CompositeLit struct {
Type Expr // literal type
Lbrace token.Position // position of "{"
Elts []Expr // list of composite elements
Rbrace token.Position // position of "}"
}
func (*CompositeLit) Pos
func (x *CompositeLit) Pos() token.Position
type Decl
All declaration nodes implement the Decl interface.
type Decl interface {
Node
// contains unexported methods
}
type DeclStmt
A DeclStmt node represents a declaration in a statement list.
type DeclStmt struct {
Decl Decl
}
func (*DeclStmt) Pos
func (s *DeclStmt) Pos() token.Position
Pos() implementations for statement nodes where the position corresponds to the position of a sub-node.
type DeferStmt
A DeferStmt node represents a defer statement.
type DeferStmt struct {
token.Position // position of "defer" keyword
Call *CallExpr
}
type Ellipsis
An Ellipsis node stands for the "..." type in a parameter list or the "..." length in an array type.
type Ellipsis struct {
token.Position // position of "..."
Elt Expr // ellipsis element type (parameter lists only)
}
type EmptyStmt
An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately preceeding semicolon.
type EmptyStmt struct {
token.Position // position of preceeding ";"
}
type Expr
All expression nodes implement the Expr interface.
type Expr interface {
Node
// contains unexported methods
}
type ExprStmt
An ExprStmt node represents a (stand-alone) expression in a statement list.
type ExprStmt struct {
X Expr // expression
}
func (*ExprStmt) Pos
func (s *ExprStmt) Pos() token.Position
type Field
A Field represents a Field declaration list in a struct type, a method list in an interface type, or a parameter/result declaration in a signature.
type Field struct {
Doc *CommentGroup // associated documentation; or nil
Names []*Ident // field/method/parameter names; or nil if anonymous field
Type Expr // field/method/parameter type
Tag *BasicLit // field tag; or nil
Comment *CommentGroup // line comments; or nil
}
func (*Field) Pos
func (f *Field) Pos() token.Position
type FieldList
A FieldList represents a list of Fields, enclosed by parentheses or braces.
type FieldList struct {
Opening token.Position // position of opening parenthesis/brace
List []*Field // field list
Closing token.Position // position of closing parenthesis/brace
}
func (*FieldList) NumFields
func (f *FieldList) NumFields() int
NumFields returns the number of (named and anonymous fields) in a FieldList.
type File
A File node represents a Go source file.
The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other nodes via Doc and Comment fields.
type File struct {
Doc *CommentGroup // associated documentation; or nil
token.Position // position of "package" keyword
Name *Ident // package name
Decls []Decl // top-level declarations
Comments []*CommentGroup // list of all comments in the source file
}
func MergePackageFiles
func MergePackageFiles(pkg *Package) *File
MergePackageFiles creates a file AST by merging the ASTs of the files belonging to a package.
type ForStmt
A ForStmt represents a for statement.
type ForStmt struct {
token.Position // position of "for" keyword
Init Stmt
Cond Expr
Post Stmt
Body *BlockStmt
}
type FuncDecl
A FuncDecl node represents a function declaration.
type FuncDecl struct {
Doc *CommentGroup // associated documentation; or nil
Recv *FieldList // receiver (methods); or nil (functions)
Name *Ident // function/method name
Type *FuncType // position of Func keyword, parameters and results
Body *BlockStmt // function body; or nil (forward declaration)
}
func (*FuncDecl) Pos
func (d *FuncDecl) Pos() token.Position
The position of a FuncDecl node is the position of its function type.
type FuncLit
A FuncLit node represents a function literal.
type FuncLit struct {
Type *FuncType // function type
Body *BlockStmt // function body
}
func (*FuncLit) Pos
func (x *FuncLit) Pos() token.Position
Pos() implementations for expression/type where the position corresponds to the position of a sub-node.
type FuncType
A FuncType node represents a function type.
type FuncType struct {
token.Position // position of "func" keyword
Params *FieldList // (incoming) parameters
Results *FieldList // (outgoing) results
}
type GenDecl
A GenDecl node (generic declaration node) represents an import, constant, type or variable declaration. A valid Lparen position (Lparen.Line > 0) indicates a parenthesized declaration.
Relationship between Tok value and Specs element type:
token.IMPORT *ImportSpec token.CONST *ValueSpec token.TYPE *TypeSpec token.VAR *ValueSpec
type GenDecl struct {
Doc *CommentGroup // associated documentation; or nil
token.Position // position of Tok
Tok token.Token // IMPORT, CONST, TYPE, VAR
Lparen token.Position // position of '(', if any
Specs []Spec
Rparen token.Position // position of ')', if any
}
type GoStmt
A GoStmt node represents a go statement.
type GoStmt struct {
token.Position // position of "go" keyword
Call *CallExpr
}
type Ident
An Ident node represents an identifier.
type Ident struct {
token.Position // identifier position
Obj *Object // denoted object
}
func NewIdent
func NewIdent(name string) *Ident
NewIdent creates a new Ident without position and minimal object information. Useful for ASTs generated by code other than the Go parser.
func (*Ident) IsExported
func (id *Ident) IsExported() bool
IsExported returns whether id is an exported Go symbol (i.e., whether it begins with an uppercase letter).
func (*Ident) Name
func (id *Ident) Name() string
Name returns an identifier's name.
func (*Ident) String
func (id *Ident) String() string
type IfStmt
An IfStmt node represents an if statement.
type IfStmt struct {
token.Position // position of "if" keyword
Init Stmt
Cond Expr
Body *BlockStmt
Else Stmt
}
type ImportSpec
An ImportSpec node represents a single package import.
type ImportSpec struct {
Doc *CommentGroup // associated documentation; or nil
Name *Ident // local package name (including "."); or nil
Path *BasicLit // package path
Comment *CommentGroup // line comments; or nil
}
func (*ImportSpec) Pos
func (s *ImportSpec) Pos() token.Position
Pos() implementations for spec nodes.
type IncDecStmt
An IncDecStmt node represents an increment or decrement statement.
type IncDecStmt struct {
X Expr
Tok token.Token // INC or DEC
}
func (*IncDecStmt) Pos
func (s *IncDecStmt) Pos() token.Position
type IndexExpr
An IndexExpr node represents an expression followed by an index.
type IndexExpr struct {
X Expr // expression
Index Expr // index expression
}
func (*IndexExpr) Pos
func (x *IndexExpr) Pos() token.Position
type InterfaceType
An InterfaceType node represents an interface type.
type InterfaceType struct {
token.Position // position of "interface" keyword
Methods *FieldList // list of methods
Incomplete bool // true if (source) methods are missing in the Methods list
}
type KeyValueExpr
A KeyValueExpr node represents (key : value) pairs in composite literals.
type KeyValueExpr struct {
Key Expr
Colon token.Position // position of ":"
Value Expr
}
func (*KeyValueExpr) Pos
func (x *KeyValueExpr) Pos() token.Position
type LabeledStmt
A LabeledStmt node represents a labeled statement.
type LabeledStmt struct {
Label *Ident
Stmt Stmt
}
func (*LabeledStmt) Pos
func (s *LabeledStmt) Pos() token.Position
type MapType
A MapType node represents a map type.
type MapType struct {
token.Position // position of "map" keyword
Key Expr
Value Expr
}
type Node
All node types implement the Node interface.
type Node interface {
// Pos returns the (beginning) position of the node.
Pos() token.Position
}
type ObjKind
type ObjKind int
The list of possible Object kinds.
const (
Err ObjKind = iota // object kind unknown (forward reference or error)
Pkg // package
Con // constant
Typ // type
Var // variable
Fun // function or method
)
type Object
An Object describes a language entity such as a package, constant, type, variable, or function (incl. methods).
type Object struct {
Kind ObjKind
Pos token.Position // declaration position
Name string // declared name
}
func NewObj
func NewObj(kind ObjKind, pos token.Position, name string) *Object
func (*Object) IsExported
func (obj *Object) IsExported() bool
IsExported returns whether obj is exported.
type Package
A Package node represents a set of source files collectively building a Go package.
type Package struct {
Name string // package name
Scope *Scope // package scope
Files map[string]*File // Go source files by filename
}
type ParenExpr
A ParenExpr node represents a parenthesized expression.
type ParenExpr struct {
token.Position // position of "("
X Expr // parenthesized expression
Rparen token.Position // position of ")"
}
type RangeStmt
A RangeStmt represents a for statement with a range clause.
type RangeStmt struct {
token.Position // position of "for" keyword
Key, Value Expr // Value may be nil
TokPos token.Position // position of Tok
Tok token.Token // ASSIGN, DEFINE
X Expr // value to range over
Body *BlockStmt
}
type ReturnStmt
A ReturnStmt node represents a return statement.
type ReturnStmt struct {
token.Position // position of "return" keyword
Results []Expr
}
type Scope
A Scope maintains the set of named language entities visible in the scope and a link to the immediately surrounding (outer) scope.
type Scope struct {
Outer *Scope
Objects map[string]*Object
}
func NewScope
func NewScope(outer *Scope) *Scope
NewScope creates a new scope nested in the outer scope.
func (*Scope) Declare
func (s *Scope) Declare(obj *Object) *Object
Declare attempts to insert a named object into the scope s. If the scope does not contain an object with that name yet, Declare inserts the object, and returns it. Otherwise, the scope remains unchanged and Declare returns the object found in the scope instead.
func (*Scope) Lookup
func (s *Scope) Lookup(name string) *Object
Lookup looks up an object in the current scope chain. The result is nil if the object is not found.
type SelectStmt
An SelectStmt node represents a select statement.
type SelectStmt struct {
token.Position // position of "select" keyword
Body *BlockStmt // CommClauses only
}
type SelectorExpr
A SelectorExpr node represents an expression followed by a selector.
type SelectorExpr struct {
X Expr // expression
Sel *Ident // field selector
}
func (*SelectorExpr) Pos
func (x *SelectorExpr) Pos() token.Position
type SliceExpr
An SliceExpr node represents an expression followed by slice indices.
type SliceExpr struct {
X Expr // expression
Index Expr // beginning of slice range
End Expr // end of slice range; or nil
}
func (*SliceExpr) Pos
func (x *SliceExpr) Pos() token.Position
type Spec
The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
type Spec interface {
Node
// contains unexported methods
}
type StarExpr
A StarExpr node represents an expression of the form "*" Expression. Semantically it could be a unary "*" expression, or a pointer type.
type StarExpr struct {
token.Position // position of "*"
X Expr // operand
}
type Stmt
All statement nodes implement the Stmt interface.
type Stmt interface {
Node
// contains unexported methods
}
type StructType
A StructType node represents a struct type.
type StructType struct {
token.Position // position of "struct" keyword
Fields *FieldList // list of field declarations
Incomplete bool // true if (source) fields are missing in the Fields list
}
type SwitchStmt
A SwitchStmt node represents an expression switch statement.
type SwitchStmt struct {
token.Position // position of "switch" keyword
Init Stmt
Tag Expr
Body *BlockStmt // CaseClauses only
}
type TypeAssertExpr
A TypeAssertExpr node represents an expression followed by a type assertion.
type TypeAssertExpr struct {
X Expr // expression
Type Expr // asserted type; nil means type switch X.(type)
}
func (*TypeAssertExpr) Pos
func (x *TypeAssertExpr) Pos() token.Position
type TypeCaseClause
A TypeCaseClause represents a case of a type switch statement.
type TypeCaseClause struct {
token.Position // position of "case" or "default" keyword
Types []Expr // nil means default case
Colon token.Position // position of ":"
Body []Stmt // statement list; or nil
}
type TypeSpec
A TypeSpec node represents a type declaration (TypeSpec production).
type TypeSpec struct {
Doc *CommentGroup // associated documentation; or nil
Name *Ident // type name
Type Expr // *ArrayType, *StructType, *FuncType, *InterfaceType, *MapType, *ChanType or *Ident
Comment *CommentGroup // line comments; or nil
}
func (*TypeSpec) Pos
func (s *TypeSpec) Pos() token.Position
type TypeSwitchStmt
An TypeSwitchStmt node represents a type switch statement.
type TypeSwitchStmt struct {
token.Position // position of "switch" keyword
Init Stmt
Assign Stmt // x := y.(type)
Body *BlockStmt // TypeCaseClauses only
}
type UnaryExpr
A UnaryExpr node represents a unary expression. Unary "*" expressions are represented via StarExpr nodes.
type UnaryExpr struct {
token.Position // position of Op
Op token.Token // operator
X Expr // operand
}
type ValueSpec
A ValueSpec node represents a constant or variable declaration (ConstSpec or VarSpec production).
type ValueSpec struct {
Doc *CommentGroup // associated documentation; or nil
Names []*Ident // value names
Type Expr // value type; or nil
Values []Expr // initial values; or nil
Comment *CommentGroup // line comments; or nil
}
func (*ValueSpec) Pos
func (s *ValueSpec) Pos() token.Position
type Visitor
A Visitor's Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).
type Visitor interface {
Visit(node interface{}) (w Visitor)
}
