ast

package
v0.0.0-...-90c9d3a Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2010 License: BSD-3-Clause, GooglePatentClause Imports: 4 Imported by: 0

Documentation

Overview

The AST package declares the types used to represent syntax trees for Go packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func 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.

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

type BadDecl struct {
	token.Position // beginning position of bad declaration
}

A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.

type BadExpr

type BadExpr struct {
	token.Position // beginning position of bad expression
}

A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.

type BadStmt

type BadStmt struct {
	token.Position // beginning position of bad statement
}

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.

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Position

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.

const (
	SEND ChanDir = 1 << iota
	RECV
)

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.

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

type DeferStmt struct {
	token.Position // position of "defer" keyword
	Call           *CallExpr
}

A DeferStmt node represents a defer statement.

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

type EmptyStmt struct {
	token.Position // position of preceeding ";"
}

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.

func (*ExprStmt) Pos

func (s *ExprStmt) Pos() token.Position

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.

func (*Field) Pos

func (f *Field) Pos() token.Position

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.

func (*FieldList) NumFields

func (f *FieldList) NumFields() int

NumFields returns the number of (named and anonymous fields) in a FieldList.

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

func MergePackageFiles(pkg *Package, complete bool) *File

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.

func (*FuncDecl) Pos

func (d *FuncDecl) Pos() token.Position

The position of a FuncDecl node is the position of its function type.

type FuncLit

type FuncLit struct {
	Type *FuncType  // function type
	Body *BlockStmt // function body
}

A FuncLit node represents a function literal.

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

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 GoStmt

type GoStmt struct {
	token.Position // position of "go" keyword
	Call           *CallExpr
}

A GoStmt node represents a go statement.

type Ident

type Ident struct {
	token.Position         // identifier position
	Obj            *Object // denoted object
}

An Ident node represents an identifier.

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

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

type IncDecStmt struct {
	X   Expr
	Tok token.Token // INC or DEC
}

An IncDecStmt node represents an increment or decrement statement.

func (*IncDecStmt) Pos

func (s *IncDecStmt) Pos() token.Position

type IndexExpr

type IndexExpr struct {
	X     Expr // expression
	Index Expr // index expression
}

An IndexExpr node represents an expression followed by an index.

func (*IndexExpr) Pos

func (x *IndexExpr) 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

type KeyValueExpr struct {
	Key   Expr
	Colon token.Position // position of ":"
	Value Expr
}

A KeyValueExpr node represents (key : value) pairs in composite literals.

func (*KeyValueExpr) Pos

func (x *KeyValueExpr) Pos() token.Position

type LabeledStmt

type LabeledStmt struct {
	Label *Ident
	Stmt  Stmt
}

A LabeledStmt node represents a labeled statement.

func (*LabeledStmt) Pos

func (s *LabeledStmt) Pos() token.Position

type MapType

type MapType struct {
	token.Position // position of "map" keyword
	Key            Expr
	Value          Expr
}

A MapType node represents a map type.

type Node

type Node interface {
	// Pos returns the (beginning) position of the node.
	Pos() token.Position
}

All node types implement the Node interface.

type ObjKind

type ObjKind int
const (
	Err ObjKind = iota // object kind unknown (forward reference or error)
	Pkg                // package
	Con                // constant
	Typ                // type
	Var                // variable
	Fun                // function or method
)

The list of possible Object kinds.

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 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

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

type ReturnStmt struct {
	token.Position // position of "return" keyword
	Results        []Expr
}

A ReturnStmt node represents a return statement.

type Scope

type Scope struct {
	Outer   *Scope
	Objects map[string]*Object
}

A Scope maintains the set of named language entities visible in the scope and a link to the immediately surrounding (outer) scope.

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

type SelectStmt struct {
	token.Position            // position of "select" keyword
	Body           *BlockStmt // CommClauses only
}

An SelectStmt node represents a select statement.

type SelectorExpr

type SelectorExpr struct {
	X   Expr   // expression
	Sel *Ident // field selector
}

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.

func (*SliceExpr) Pos

func (x *SliceExpr) Pos() token.Position

type Spec

type Spec interface {
	Node
	// contains filtered or unexported methods
}

The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.

type StarExpr

type StarExpr struct {
	token.Position      // position of "*"
	X              Expr // operand
}

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).

func (*TypeSpec) Pos

func (s *TypeSpec) Pos() token.Position

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).

func (*ValueSpec) Pos

func (s *ValueSpec) Pos() token.Position

type Visitor

type Visitor interface {
	Visit(node interface{}) (w 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).

Jump to

Keyboard shortcuts

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