Documentation ¶
Overview ¶
The AST package declares the types used to represent syntax trees for Go packages.
Index ¶
- func FileExports(src *File) bool
- func IsExported(name string) bool
- func PackageExports(pkg *Package) bool
- func Walk(v Visitor, node interface{})
- type ArrayType
- type AssignStmt
- type BadDecl
- type BadExpr
- type BadStmt
- type BasicLit
- type BinaryExpr
- type BlockStmt
- type BranchStmt
- type CallExpr
- type CaseClause
- type ChanDir
- type ChanType
- type CommClause
- type Comment
- type CommentGroup
- type CompositeLit
- type Decl
- type DeclStmt
- type DeferStmt
- type Ellipsis
- type EmptyStmt
- type Expr
- type ExprStmt
- type Field
- type FieldList
- type File
- type ForStmt
- type FuncDecl
- type FuncLit
- type FuncType
- type GenDecl
- type GoStmt
- type Ident
- type IfStmt
- type ImportSpec
- type IncDecStmt
- type IndexExpr
- type InterfaceType
- type KeyValueExpr
- type LabeledStmt
- type MapType
- type Node
- type ObjKind
- type Object
- type Package
- type ParenExpr
- type RangeStmt
- type ReturnStmt
- type Scope
- type SelectStmt
- type SelectorExpr
- type SliceExpr
- type Spec
- type StarExpr
- type Stmt
- type StructType
- type SwitchStmt
- type TypeAssertExpr
- type TypeCaseClause
- type TypeSpec
- type TypeSwitchStmt
- type UnaryExpr
- type ValueSpec
- type Visitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FileExports ¶
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 ¶
IsExported returns whether name is an exported Go symbol (i.e., whether it begins with an uppercase letter).
func PackageExports ¶
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.
Types ¶
type ArrayType ¶
type ArrayType struct { token.Position // position of "[" Len Expr // Ellipsis node for [...]T array types, nil for slice types Elt Expr // element type }
An ArrayType node represents an array or slice type.
type AssignStmt ¶
type AssignStmt struct { Lhs []Expr TokPos token.Position // position of Tok Tok token.Token // assignment token, DEFINE Rhs []Expr }
An AssignStmt node represents an assignment or a short variable declaration.
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 BadExpr ¶
A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.
type BadStmt ¶
A BadStmt node is a placeholder for statements containing syntax errors for which no correct statement nodes can be created.
type BasicLit ¶
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` }
A BasicLit node represents a literal of basic type.
type BinaryExpr ¶
type BinaryExpr struct { X Expr // left operand OpPos token.Position // position of Op Op token.Token // operator Y Expr // right operand }
A BinaryExpr node represents a binary expression.
func (*BinaryExpr) Pos ¶
func (x *BinaryExpr) Pos() token.Position
type BlockStmt ¶
type BlockStmt struct { token.Position // position of "{" List []Stmt Rbrace token.Position // position of "}" }
A BlockStmt node represents a braced statement list.
type BranchStmt ¶
type BranchStmt struct { token.Position // position of Tok Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) Label *Ident }
A BranchStmt node represents a break, continue, goto, or fallthrough statement.
type CallExpr ¶
type CallExpr struct { Fun Expr // function expression Lparen token.Position // position of "(" Args []Expr // function arguments Rparen token.Position // positions of ")" }
A CallExpr node represents an expression followed by an argument list.
type CaseClause ¶
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 }
A CaseClause represents a case of an expression switch statement.
type ChanDir ¶
type ChanDir int
The direction of a channel type is indicated by one of the following constants.
type ChanType ¶
type ChanType struct { token.Position // position of "chan" keyword or "<-" (whichever comes first) Dir ChanDir // channel direction Value Expr // value type }
A ChanType node represents a channel type.
type CommClause ¶
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 }
A CommClause node represents a case of a select statement.
type Comment ¶
type Comment struct { token.Position // beginning position of the comment Text []byte // comment text (excluding '\n' for //-style comments) }
A Comment node represents a single //-style or /*-style comment.
type CommentGroup ¶
type CommentGroup struct {
List []*Comment
}
A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.
type CompositeLit ¶
type CompositeLit struct { Type Expr // literal type Lbrace token.Position // position of "{" Elts []Expr // list of composite elements Rbrace token.Position // position of "}" }
A CompositeLit node represents a composite literal.
func (*CompositeLit) Pos ¶
func (x *CompositeLit) Pos() token.Position
type Decl ¶
type Decl interface { Node // contains filtered or unexported methods }
All declaration nodes implement the Decl interface.
type DeclStmt ¶
type DeclStmt struct {
Decl Decl
}
A DeclStmt node represents a declaration in a statement list.
type Ellipsis ¶
type Ellipsis struct { token.Position // position of "..." Elt Expr // ellipsis element type (parameter lists only) }
An Ellipsis node stands for the "..." type in a parameter list or the "..." length in an array type.
type EmptyStmt ¶
An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately preceeding semicolon.
type Expr ¶
type Expr interface { Node // contains filtered or unexported methods }
All expression nodes implement the Expr interface.
type ExprStmt ¶
type ExprStmt struct {
X Expr // expression
}
An ExprStmt node represents a (stand-alone) expression in a statement list.
type Field ¶
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 }
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 FieldList ¶
type FieldList struct { Opening token.Position // position of opening parenthesis/brace List []*Field // field list Closing token.Position // position of closing parenthesis/brace }
A FieldList represents a list of Fields, enclosed by parentheses or braces.
type File ¶
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 }
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.
func MergePackageFiles ¶
MergePackageFiles creates a file AST by merging the ASTs of the files belonging to a package. If complete is set, the package files are assumed to contain the complete, unfiltered package information. In this case, MergePackageFiles collects all entities and all comments. Otherwise (complete == false), MergePackageFiles excludes duplicate entries and does not collect comments that are not attached to AST nodes.
type ForStmt ¶
type ForStmt struct { token.Position // position of "for" keyword Init Stmt Cond Expr Post Stmt Body *BlockStmt }
A ForStmt represents a for statement.
type FuncDecl ¶
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) }
A FuncDecl node represents a function declaration.
type FuncType ¶
type FuncType struct { token.Position // position of "func" keyword Params *FieldList // (incoming) parameters Results *FieldList // (outgoing) results }
A FuncType node represents a function type.
type GenDecl ¶
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 }
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 Ident ¶
An Ident node represents an identifier.
func NewIdent ¶
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 ¶
IsExported returns whether id is an exported Go symbol (i.e., whether it begins with an uppercase letter).
type IfStmt ¶
type IfStmt struct { token.Position // position of "if" keyword Init Stmt Cond Expr Body *BlockStmt Else Stmt }
An IfStmt node represents an if statement.
type ImportSpec ¶
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 }
An ImportSpec node represents a single package import.
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.
func (*IncDecStmt) Pos ¶
func (s *IncDecStmt) Pos() token.Position
type InterfaceType ¶
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 }
An InterfaceType node represents an interface type.
type KeyValueExpr ¶
A KeyValueExpr node represents (key : value) pairs in composite literals.
func (*KeyValueExpr) Pos ¶
func (x *KeyValueExpr) Pos() token.Position
type LabeledStmt ¶
A LabeledStmt node represents a labeled statement.
func (*LabeledStmt) Pos ¶
func (s *LabeledStmt) Pos() token.Position
type Object ¶
type Object struct { Kind ObjKind Pos token.Position // declaration position Name string // declared name }
An Object describes a language entity such as a package, constant, type, variable, or function (incl. methods).
func (*Object) IsExported ¶
IsExported returns whether obj is exported.
type Package ¶
type Package struct { Name string // package name Scope *Scope // package scope Files map[string]*File // Go source files by filename }
A Package node represents a set of source files collectively building a Go package.
type ParenExpr ¶
type ParenExpr struct { token.Position // position of "(" X Expr // parenthesized expression Rparen token.Position // position of ")" }
A ParenExpr node represents a parenthesized expression.
type RangeStmt ¶
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 }
A RangeStmt represents a for statement with a range clause.
type ReturnStmt ¶
A ReturnStmt node represents a return statement.
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 SelectStmt ¶
type SelectStmt struct { token.Position // position of "select" keyword Body *BlockStmt // CommClauses only }
An SelectStmt node represents a select statement.
type SelectorExpr ¶
A SelectorExpr node represents an expression followed by a selector.
func (*SelectorExpr) Pos ¶
func (x *SelectorExpr) Pos() token.Position
type SliceExpr ¶
type SliceExpr struct { X Expr // expression Index Expr // beginning of slice range End Expr // end of slice range; or nil }
An SliceExpr node represents an expression followed by slice indices.
type Spec ¶
type Spec interface { Node // contains filtered or unexported methods }
The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
type StarExpr ¶
A StarExpr node represents an expression of the form "*" Expression. Semantically it could be a unary "*" expression, or a pointer type.
type Stmt ¶
type Stmt interface { Node // contains filtered or unexported methods }
All statement nodes implement the Stmt interface.
type StructType ¶
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 }
A StructType node represents a struct type.
type SwitchStmt ¶
type SwitchStmt struct { token.Position // position of "switch" keyword Init Stmt Tag Expr Body *BlockStmt // CaseClauses only }
A SwitchStmt node represents an expression switch statement.
type TypeAssertExpr ¶
type TypeAssertExpr struct { X Expr // expression Type Expr // asserted type; nil means type switch X.(type) }
A TypeAssertExpr node represents an expression followed by a type assertion.
func (*TypeAssertExpr) Pos ¶
func (x *TypeAssertExpr) Pos() token.Position
type TypeCaseClause ¶
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 }
A TypeCaseClause represents a case of a type switch statement.
type TypeSpec ¶
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 }
A TypeSpec node represents a type declaration (TypeSpec production).
type TypeSwitchStmt ¶
type TypeSwitchStmt struct { token.Position // position of "switch" keyword Init Stmt Assign Stmt // x := y.(type) Body *BlockStmt // TypeCaseClauses only }
An TypeSwitchStmt node represents a type switch statement.
type UnaryExpr ¶
type UnaryExpr struct { token.Position // position of Op Op token.Token // operator X Expr // operand }
A UnaryExpr node represents a unary expression. Unary "*" expressions are represented via StarExpr nodes.
type ValueSpec ¶
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 }
A ValueSpec node represents a constant or variable declaration (ConstSpec or VarSpec production).