Documentation ¶
Overview ¶
Package dst declares the types used to represent decorated syntax trees for Go packages.
Index ¶
- func Fprint(w io.Writer, 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 Print(x interface{}) error
- func Walk(v Visitor, node Node)
- type ArrayType
- type ArrayTypeDecorations
- type AssignStmt
- type AssignStmtDecorations
- type BadDecl
- type BadDeclDecorations
- type BadExpr
- type BadExprDecorations
- type BadStmt
- type BadStmtDecorations
- type BasicLit
- type BasicLitDecorations
- type BinaryExpr
- type BinaryExprDecorations
- type BlockStmt
- type BlockStmtDecorations
- type BranchStmt
- type BranchStmtDecorations
- type CallExpr
- type CallExprDecorations
- type CaseClause
- type CaseClauseDecorations
- type ChanDir
- type ChanType
- type ChanTypeDecorations
- type CommClause
- type CommClauseDecorations
- type CompositeLit
- type CompositeLitDecorations
- type Decl
- type DeclStmt
- type DeclStmtDecorations
- type Decorations
- type DeferStmt
- type DeferStmtDecorations
- type Ellipsis
- type EllipsisDecorations
- type EmptyStmt
- type EmptyStmtDecorations
- type Expr
- type ExprStmt
- type ExprStmtDecorations
- type Field
- type FieldDecorations
- type FieldFilter
- type FieldList
- type FieldListDecorations
- type File
- type FileDecorations
- type ForStmt
- type ForStmtDecorations
- type FuncDecl
- type FuncDeclDecorations
- type FuncLit
- type FuncLitDecorations
- type FuncType
- type FuncTypeDecorations
- type GenDecl
- type GenDeclDecorations
- type GoStmt
- type GoStmtDecorations
- type Ident
- type IdentDecorations
- type IfStmt
- type IfStmtDecorations
- type ImportSpec
- type ImportSpecDecorations
- type Importer
- type IncDecStmt
- type IncDecStmtDecorations
- type IndexExpr
- type IndexExprDecorations
- type InterfaceType
- type InterfaceTypeDecorations
- type KeyValueExpr
- type KeyValueExprDecorations
- type LabeledStmt
- type LabeledStmtDecorations
- type MapType
- type MapTypeDecorations
- type Node
- type NodeDecs
- type ObjKind
- type Object
- type Package
- type PackageDecorations
- type ParenExpr
- type ParenExprDecorations
- type RangeStmt
- type RangeStmtDecorations
- type ReturnStmt
- type ReturnStmtDecorations
- type Scope
- type SelectStmt
- type SelectStmtDecorations
- type SelectorExpr
- type SelectorExprDecorations
- type SendStmt
- type SendStmtDecorations
- type SliceExpr
- type SliceExprDecorations
- type SpaceType
- type Spec
- type StarExpr
- type StarExprDecorations
- type Stmt
- type StructType
- type StructTypeDecorations
- type SwitchStmt
- type SwitchStmtDecorations
- type TypeAssertExpr
- type TypeAssertExprDecorations
- type TypeSpec
- type TypeSpecDecorations
- type TypeSwitchStmt
- type TypeSwitchStmtDecorations
- type UnaryExpr
- type UnaryExprDecorations
- type ValueSpec
- type ValueSpecDecorations
- type Visitor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fprint ¶
func Fprint(w io.Writer, x interface{}, f FieldFilter) error
Fprint prints the (sub-)tree starting at AST node x to w. If fset != nil, position information is interpreted relative to that file set. Otherwise positions are printed as integer values (file set specific offsets).
A non-nil FieldFilter f may be provided to control the output: struct fields for which f(fieldname, fieldvalue) is true are printed; all others are filtered from the output. Unexported struct fields are never printed.
func Inspect ¶
Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).
Example ¶
This example demonstrates how to inspect the AST of a Go program.
package main import ( "fmt" "go/token" "github.com/dave/dst" "github.com/dave/dst/decorator" ) func main() { // src is the input for which we want to inspect the AST. src := ` package p const c = 1.0 var X = f(3.14)*2 + c ` // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := decorator.ParseFile(fset, "src.go", src, 0) if err != nil { panic(err) } // Inspect the AST and print all identifiers and literals. dst.Inspect(f, func(n dst.Node) bool { var s string switch x := n.(type) { case *dst.BasicLit: s = x.Value case *dst.Ident: s = x.Name } if s != "" { fmt.Println(s) } return true }) }
Output: p c 1.0 X f 3.14 2 c
func IsExported ¶
IsExported reports whether name is an exported Go symbol (that is, whether it begins with an upper-case letter).
func NotNilFilter ¶
NotNilFilter returns true for field values that are not nil; it returns false otherwise.
func Print ¶
func Print(x interface{}) error
Print prints x to standard output, skipping nil fields. Print(fset, x) is the same as Fprint(os.Stdout, fset, x, NotNilFilter).
Types ¶
type ArrayType ¶
type ArrayType struct { Len Expr // Ellipsis node for [...]T array types, nil for slice types Elt Expr // element type Decs ArrayTypeDecorations }
An ArrayType node represents an array or slice type.
func (*ArrayType) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ArrayTypeDecorations ¶
type ArrayTypeDecorations struct { NodeDecs Lbrack Decorations Len Decorations }
ArrayTypeDecorations holds decorations for ArrayType:
type R /*Start*/ [ /*Lbrack*/ 1] /*Len*/ int /*End*/
type AssignStmt ¶
type AssignStmt struct { Lhs []Expr Tok token.Token // assignment token, DEFINE Rhs []Expr Decs AssignStmtDecorations }
An AssignStmt node represents an assignment or a short variable declaration.
func (*AssignStmt) Decorations ¶ added in v0.6.0
func (n *AssignStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type AssignStmtDecorations ¶
type AssignStmtDecorations struct { NodeDecs Tok Decorations }
AssignStmtDecorations holds decorations for AssignStmt:
/*Start*/ i = /*Tok*/ 1 /*End*/
type BadDecl ¶
type BadDecl struct { Length int // position range of bad declaration Decs BadDeclDecorations }
A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.
func (*BadDecl) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BadDeclDecorations ¶
type BadDeclDecorations struct {
NodeDecs
}
BadDeclDecorations holds decorations for BadDecl:
type BadExpr ¶
type BadExpr struct { Length int // position range of bad expression Decs BadExprDecorations }
A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.
func (*BadExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BadExprDecorations ¶
type BadExprDecorations struct {
NodeDecs
}
BadExprDecorations holds decorations for BadExpr:
type BadStmt ¶
type BadStmt struct { Length int // position range of bad statement Decs BadStmtDecorations }
A BadStmt node is a placeholder for statements containing syntax errors for which no correct statement nodes can be created.
func (*BadStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BadStmtDecorations ¶
type BadStmtDecorations struct {
NodeDecs
}
BadStmtDecorations holds decorations for BadStmt:
type BasicLit ¶
type BasicLit struct { 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` Decs BasicLitDecorations }
A BasicLit node represents a literal of basic type.
func (*BasicLit) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BasicLitDecorations ¶
type BasicLitDecorations struct {
NodeDecs
}
BasicLitDecorations holds decorations for BasicLit:
type BinaryExpr ¶
type BinaryExpr struct { X Expr // left operand Op token.Token // operator Y Expr // right operand Decs BinaryExprDecorations }
A BinaryExpr node represents a binary expression.
func (*BinaryExpr) Decorations ¶ added in v0.6.0
func (n *BinaryExpr) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BinaryExprDecorations ¶
type BinaryExprDecorations struct { NodeDecs X Decorations Op Decorations }
BinaryExprDecorations holds decorations for BinaryExpr:
var P = /*Start*/ 1 /*X*/ & /*Op*/ 2 /*End*/
type BlockStmt ¶
type BlockStmt struct { List []Stmt RbraceHasNoPos bool // Sometimes (after a BadExpr?) the rbrace has a zero position, and this causes the brace to render in a different position. We duplicate this in the output for compatibility. Decs BlockStmtDecorations }
A BlockStmt node represents a braced statement list.
func (*BlockStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BlockStmtDecorations ¶
type BlockStmtDecorations struct { NodeDecs Lbrace Decorations }
BlockStmtDecorations holds decorations for BlockStmt:
if true /*Start*/ { /*Lbrace*/ i++ } /*End*/ func() /*Start*/ { /*Lbrace*/ i++ } /*End*/ ()
type BranchStmt ¶
type BranchStmt struct { Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) Label *Ident // label name; or nil Decs BranchStmtDecorations }
A BranchStmt node represents a break, continue, goto, or fallthrough statement.
func (*BranchStmt) Decorations ¶ added in v0.6.0
func (n *BranchStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type BranchStmtDecorations ¶
type BranchStmtDecorations struct { NodeDecs Tok Decorations }
BranchStmtDecorations holds decorations for BranchStmt:
/*Start*/ goto /*Tok*/ A /*End*/
type CallExpr ¶
type CallExpr struct { Fun Expr // function expression Args []Expr // function arguments; or nil Ellipsis bool Decs CallExprDecorations }
A CallExpr node represents an expression followed by an argument list.
func (*CallExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type CallExprDecorations ¶
type CallExprDecorations struct { NodeDecs Fun Decorations Lparen Decorations Ellipsis Decorations }
CallExprDecorations holds decorations for CallExpr:
var L = /*Start*/ C /*Fun*/ ( /*Lparen*/ 0, []int{}... /*Ellipsis*/) /*End*/
type CaseClause ¶
type CaseClause struct { List []Expr // list of expressions or types; nil means default case Body []Stmt // statement list; or nil Decs CaseClauseDecorations }
A CaseClause represents a case of an expression or type switch statement.
func (*CaseClause) Decorations ¶ added in v0.6.0
func (n *CaseClause) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type CaseClauseDecorations ¶
type CaseClauseDecorations struct { NodeDecs Case Decorations Colon Decorations }
CaseClauseDecorations holds decorations for CaseClause:
switch i { /*Start*/ case /*Case*/ 1: /*Colon*/ i++ /*End*/ }
type ChanDir ¶
type ChanDir int
The direction of a channel type is indicated by a bit mask including one or both of the following constants.
type ChanType ¶
type ChanType struct { Dir ChanDir // channel direction Value Expr // value type Decs ChanTypeDecorations }
A ChanType node represents a channel type.
func (*ChanType) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ChanTypeDecorations ¶
type ChanTypeDecorations struct { NodeDecs Begin Decorations Arrow Decorations }
ChanTypeDecorations holds decorations for ChanType:
type W /*Start*/ chan /*Begin*/ int /*End*/ type X /*Start*/ <-chan /*Begin*/ int /*End*/ type Y /*Start*/ chan /*Begin*/ <- /*Arrow*/ int /*End*/
type CommClause ¶
type CommClause struct { Comm Stmt // send or receive statement; nil means default case Body []Stmt // statement list; or nil Decs CommClauseDecorations }
A CommClause node represents a case of a select statement.
func (*CommClause) Decorations ¶ added in v0.6.0
func (n *CommClause) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type CommClauseDecorations ¶
type CommClauseDecorations struct { NodeDecs Case Decorations Comm Decorations Colon Decorations }
CommClauseDecorations holds decorations for CommClause:
select { /*Start*/ case /*Case*/ a := <-c /*Comm*/ : /*Colon*/ print(a) /*End*/ }
type CompositeLit ¶
type CompositeLit struct { Type Expr // literal type; or nil Elts []Expr // list of composite elements; or nil Incomplete bool // true if (source) expressions are missing in the Elts list Decs CompositeLitDecorations }
A CompositeLit node represents a composite literal.
func (*CompositeLit) Decorations ¶ added in v0.6.0
func (n *CompositeLit) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type CompositeLitDecorations ¶
type CompositeLitDecorations struct { NodeDecs Type Decorations Lbrace Decorations }
CompositeLitDecorations holds decorations for CompositeLit:
var D = /*Start*/ A /*Type*/ { /*Lbrace*/ A: 0} /*End*/
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 // *GenDecl with CONST, TYPE, or VAR token Decs DeclStmtDecorations }
A DeclStmt node represents a declaration in a statement list.
func (*DeclStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type DeclStmtDecorations ¶
type DeclStmtDecorations struct {
NodeDecs
}
DeclStmtDecorations holds decorations for DeclStmt:
type Decorations ¶
type Decorations []string
Decorations is a slice of strings which are rendered with the node. Decorations can be comments (starting "//" or "/*") or newlines ("\n").
Example ¶
package main import ( "fmt" "github.com/dave/dst" "github.com/dave/dst/decorator" ) func main() { code := `package main func main() { var a int a++ print(a) }` f, err := decorator.Parse(code) if err != nil { panic(err) } body := f.Decls[0].(*dst.FuncDecl).Body for i, stmt := range body.List { stmt.Decorations().Before = dst.EmptyLine stmt.Decorations().Start.Append(fmt.Sprintf("// foo %d", i)) } call := body.List[2].(*dst.ExprStmt).X.(*dst.CallExpr) call.Args = append(call.Args, dst.NewIdent("b"), dst.NewIdent("c")) for i, expr := range call.Args { expr.Decorations().Before = dst.NewLine expr.Decorations().After = dst.NewLine expr.Decorations().Start.Append(fmt.Sprintf("/* bar %d */", i)) expr.Decorations().End.Append(fmt.Sprintf("// baz %d", i)) } if err := decorator.Print(f); err != nil { panic(err) } }
Output: package main func main() { // foo 0 var a int // foo 1 a++ // foo 2 print( /* bar 0 */ a, // baz 0 /* bar 1 */ b, // baz 1 /* bar 2 */ c, // baz 2 ) }
func (*Decorations) All ¶ added in v0.2.0
func (d *Decorations) All() []string
All returns the decorations as a string slice
func (*Decorations) Append ¶ added in v0.6.0
func (d *Decorations) Append(decs ...string)
Append adds one or more decorations to the end of the list.
func (*Decorations) Clear ¶
func (d *Decorations) Clear()
Clear removes all decorations from this item
func (*Decorations) Prepend ¶ added in v0.6.0
func (d *Decorations) Prepend(decs ...string)
Prepend adds one or more decorations to the start of the list.
func (*Decorations) Replace ¶
func (d *Decorations) Replace(decs ...string)
Replace replaces all decorations with decs.
type DeferStmt ¶
type DeferStmt struct { Call *CallExpr Decs DeferStmtDecorations }
A DeferStmt node represents a defer statement.
func (*DeferStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type DeferStmtDecorations ¶
type DeferStmtDecorations struct { NodeDecs Defer Decorations }
DeferStmtDecorations holds decorations for DeferStmt:
/*Start*/ defer /*Defer*/ func() {}() /*End*/
type Ellipsis ¶
type Ellipsis struct { Elt Expr // ellipsis element type (parameter lists only); or nil Decs EllipsisDecorations }
An Ellipsis node stands for the "..." type in a parameter list or the "..." length in an array type.
func (*Ellipsis) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type EllipsisDecorations ¶
type EllipsisDecorations struct { NodeDecs Ellipsis Decorations }
EllipsisDecorations holds decorations for Ellipsis:
func B(a /*Start*/ ... /*Ellipsis*/ int /*End*/) {}
type EmptyStmt ¶
type EmptyStmt struct { Implicit bool // if set, ";" was omitted in the source Decs EmptyStmtDecorations }
An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately following (explicit or implicit) semicolon.
func (*EmptyStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type EmptyStmtDecorations ¶
type EmptyStmtDecorations struct {
NodeDecs
}
EmptyStmtDecorations holds decorations for EmptyStmt:
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 Decs ExprStmtDecorations }
An ExprStmt node represents a (stand-alone) expression in a statement list.
func (*ExprStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ExprStmtDecorations ¶
type ExprStmtDecorations struct {
NodeDecs
}
ExprStmtDecorations holds decorations for ExprStmt:
type Field ¶
type Field struct { Names []*Ident // field/method/parameter names; or nil Type Expr // field/method/parameter type Tag *BasicLit // field tag; or nil Decs FieldDecorations }
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. Field.Names is nil for unnamed parameters (parameter lists which only contain types) and embedded struct fields. In the latter case, the field name is the type name.
func (*Field) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FieldDecorations ¶
type FieldDecorations struct { NodeDecs Type Decorations }
FieldDecorations holds decorations for Field:
type A struct { /*Start*/ A int /*Type*/ `a:"a"` /*End*/ }
type FieldFilter ¶
A FieldFilter may be provided to Fprint to control the output.
type FieldList ¶
type FieldList struct { Opening bool List []*Field // field list; or nil Closing bool Decs FieldListDecorations }
A FieldList represents a list of Fields, enclosed by parentheses or braces.
func (*FieldList) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FieldListDecorations ¶
type FieldListDecorations struct { NodeDecs Opening Decorations }
FieldListDecorations holds decorations for FieldList:
type A1 struct /*Start*/ { /*Opening*/ a, b int c string } /*End*/
type File ¶
type File struct { Name *Ident // package name Decls []Decl // top-level declarations; or nil Scope *Scope // package scope (this file only) Imports []*ImportSpec // imports in this file Unresolved []*Ident // unresolved identifiers in this file Decs FileDecorations }
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.
For correct printing of source code containing comments (using packages go/format and go/printer), special care must be taken to update comments when a File's syntax tree is modified: For printing, comments are interspersed between tokens based on their position. If syntax tree nodes are removed or moved, relevant comments in their vicinity must also be removed (from the File.Comments list) or moved accordingly (by updating their positions). A CommentMap may be used to facilitate some of these operations.
Whether and how a comment is associated with a node depends on the interpretation of the syntax tree by the manipulating program: Except for Doc and Comment comments directly associated with nodes, the remaining comments are "free-floating" (see also issues #18593, #20744).
func (*File) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FileDecorations ¶
type FileDecorations struct { NodeDecs Package Decorations Name Decorations }
FileDecorations holds decorations for File:
/*Start*/ package /*Package*/ data /*Name*/
type ForStmt ¶
type ForStmt struct { Init Stmt // initialization statement; or nil Cond Expr // condition; or nil Post Stmt // post iteration statement; or nil Body *BlockStmt Decs ForStmtDecorations }
A ForStmt represents a for statement.
func (*ForStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ForStmtDecorations ¶
type ForStmtDecorations struct { NodeDecs For Decorations Init Decorations Cond Decorations Post Decorations }
ForStmtDecorations holds decorations for ForStmt:
/*Start*/ for /*For*/ { i++ } /*End*/ /*Start*/ for /*For*/ i < 1 /*Cond*/ { i++ } /*End*/ /*Start*/ for /*For*/ i = 0; /*Init*/ i < 10; /*Cond*/ i++ /*Post*/ { i++ } /*End*/
type FuncDecl ¶
type FuncDecl struct { Recv *FieldList // receiver (methods); or nil (functions) Name *Ident // function/method name Type *FuncType // function signature: parameters, results, and position of "func" keyword Body *BlockStmt // function body; or nil for external (non-Go) function Decs FuncDeclDecorations }
A FuncDecl node represents a function declaration.
func (*FuncDecl) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FuncDeclDecorations ¶
type FuncDeclDecorations struct { NodeDecs Func Decorations Recv Decorations Name Decorations Params Decorations Results Decorations }
FuncDeclDecorations holds decorations for FuncDecl:
/*Start*/ func /*Func*/ d /*Name*/ (d, e int) /*Params*/ { return } /*End*/ /*Start*/ func /*Func*/ (a *A) /*Recv*/ e /*Name*/ (d, e int) /*Params*/ { return } /*End*/ /*Start*/ func /*Func*/ (a *A) /*Recv*/ f /*Name*/ (d, e int) /*Params*/ (f, g int) /*Results*/ { return } /*End*/
type FuncLit ¶
type FuncLit struct { Type *FuncType // function type Body *BlockStmt // function body Decs FuncLitDecorations }
A FuncLit node represents a function literal.
func (*FuncLit) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FuncLitDecorations ¶
type FuncLitDecorations struct { NodeDecs Type Decorations }
FuncLitDecorations holds decorations for FuncLit:
var C = /*Start*/ func(a int, b ...int) (c int) /*Type*/ { return 0 } /*End*/
type FuncType ¶
type FuncType struct { Func bool Params *FieldList // (incoming) parameters; non-nil Results *FieldList // (outgoing) results; or nil Decs FuncTypeDecorations }
A FuncType node represents a function type.
func (*FuncType) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type FuncTypeDecorations ¶
type FuncTypeDecorations struct { NodeDecs Func Decorations Params Decorations }
FuncTypeDecorations holds decorations for FuncType:
type T /*Start*/ func /*Func*/ (a int) /*Params*/ (b int) /*End*/
type GenDecl ¶
type GenDecl struct { Tok token.Token // IMPORT, CONST, TYPE, VAR Lparen bool Specs []Spec Rparen bool Decs GenDeclDecorations }
A GenDecl node (generic declaration node) represents an import, constant, type or variable declaration. A valid Lparen position (Lparen.IsValid()) indicates a parenthesized declaration.
Relationship between Tok value and Specs element type:
token.IMPORT *ImportSpec token.CONST *ValueSpec token.TYPE *TypeSpec token.VAR *ValueSpec
func (*GenDecl) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type GenDeclDecorations ¶
type GenDeclDecorations struct { NodeDecs Tok Decorations Lparen Decorations }
GenDeclDecorations holds decorations for GenDecl:
/*Start*/ const /*Tok*/ ( /*Lparen*/ a, b = 1, 2 c = 3 ) /*End*/ /*Start*/ const /*Tok*/ d = 1 /*End*/
type GoStmt ¶
type GoStmt struct { Call *CallExpr Decs GoStmtDecorations }
A GoStmt node represents a go statement.
func (*GoStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type GoStmtDecorations ¶
type GoStmtDecorations struct { NodeDecs Go Decorations }
GoStmtDecorations holds decorations for GoStmt:
/*Start*/ go /*Go*/ func() {}() /*End*/
type Ident ¶
type Ident struct { Name string // identifier name Obj *Object // denoted object; or nil Path string // path of the imported package, if this identifier is not local Decs IdentDecorations }
An Ident node represents an identifier.
func NewIdent ¶
NewIdent creates a new Ident without position. Useful for ASTs generated by code other than the Go parser.
func (*Ident) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
func (*Ident) IsExported ¶
IsExported reports whether id is an exported Go symbol (that is, whether it begins with an uppercase letter).
type IdentDecorations ¶
type IdentDecorations struct { NodeDecs X Decorations }
IdentDecorations holds decorations for Ident:
/*Start*/ i /*End*/ ++ /*Start*/ fmt. /*X*/ Print /*End*/ ()
type IfStmt ¶
type IfStmt struct { Init Stmt // initialization statement; or nil Cond Expr // condition Body *BlockStmt Else Stmt // else branch; or nil Decs IfStmtDecorations }
An IfStmt node represents an if statement.
func (*IfStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type IfStmtDecorations ¶
type IfStmtDecorations struct { NodeDecs If Decorations Init Decorations Cond Decorations Else Decorations }
IfStmtDecorations holds decorations for IfStmt:
/*Start*/ if /*If*/ a := b; /*Init*/ a /*Cond*/ { i++ } else /*Else*/ { i++ } /*End*/
type ImportSpec ¶
type ImportSpec struct { Name *Ident // local package name (including "."); or nil Path *BasicLit // import path Decs ImportSpecDecorations }
An ImportSpec node represents a single package import.
func (*ImportSpec) Decorations ¶ added in v0.6.0
func (n *ImportSpec) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ImportSpecDecorations ¶
type ImportSpecDecorations struct { NodeDecs Name Decorations }
ImportSpecDecorations holds decorations for ImportSpec:
import ( /*Start*/ fmt /*Name*/ "fmt" /*End*/ )
type Importer ¶
An Importer resolves import paths to package Objects. The imports map records the packages already imported, indexed by package id (canonical import path). An Importer must determine the canonical import path and check the map to see if it is already present in the imports map. If so, the Importer can return the map entry. Otherwise, the Importer should load the package data for the given path into a new *Object (pkg), record pkg in the imports map, and then return pkg.
type IncDecStmt ¶
type IncDecStmt struct { X Expr Tok token.Token // INC or DEC Decs IncDecStmtDecorations }
An IncDecStmt node represents an increment or decrement statement.
func (*IncDecStmt) Decorations ¶ added in v0.6.0
func (n *IncDecStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type IncDecStmtDecorations ¶
type IncDecStmtDecorations struct { NodeDecs X Decorations }
IncDecStmtDecorations holds decorations for IncDecStmt:
/*Start*/ i /*X*/ ++ /*End*/
type IndexExpr ¶
type IndexExpr struct { X Expr // expression Index Expr // index expression Decs IndexExprDecorations }
An IndexExpr node represents an expression followed by an index.
func (*IndexExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type IndexExprDecorations ¶
type IndexExprDecorations struct { NodeDecs X Decorations Lbrack Decorations Index Decorations }
IndexExprDecorations holds decorations for IndexExpr:
var G = /*Start*/ []int{0} /*X*/ [ /*Lbrack*/ 0 /*Index*/] /*End*/
type InterfaceType ¶
type InterfaceType struct { Methods *FieldList // list of methods Incomplete bool // true if (source) methods are missing in the Methods list Decs InterfaceTypeDecorations }
An InterfaceType node represents an interface type.
func (*InterfaceType) Decorations ¶ added in v0.6.0
func (n *InterfaceType) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type InterfaceTypeDecorations ¶
type InterfaceTypeDecorations struct { NodeDecs Interface Decorations }
InterfaceTypeDecorations holds decorations for InterfaceType:
type U /*Start*/ interface /*Interface*/ { A() } /*End*/
type KeyValueExpr ¶
type KeyValueExpr struct { Key Expr Value Expr Decs KeyValueExprDecorations }
A KeyValueExpr node represents (key : value) pairs in composite literals.
func (*KeyValueExpr) Decorations ¶ added in v0.6.0
func (n *KeyValueExpr) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type KeyValueExprDecorations ¶
type KeyValueExprDecorations struct { NodeDecs Key Decorations Colon Decorations }
KeyValueExprDecorations holds decorations for KeyValueExpr:
var Q = map[string]string{ /*Start*/ "a" /*Key*/ : /*Colon*/ "a", /*End*/ }
type LabeledStmt ¶
type LabeledStmt struct { Label *Ident Stmt Stmt Decs LabeledStmtDecorations }
A LabeledStmt node represents a labeled statement.
func (*LabeledStmt) Decorations ¶ added in v0.6.0
func (n *LabeledStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type LabeledStmtDecorations ¶
type LabeledStmtDecorations struct { NodeDecs Label Decorations Colon Decorations }
LabeledStmtDecorations holds decorations for LabeledStmt:
/*Start*/ A /*Label*/ : /*Colon*/ print("Stmt") /*End*/
type MapType ¶
type MapType struct { Key Expr Value Expr Decs MapTypeDecorations }
A MapType node represents a map type.
func (*MapType) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type MapTypeDecorations ¶
type MapTypeDecorations struct { NodeDecs Map Decorations Key Decorations }
MapTypeDecorations holds decorations for MapType:
type V /*Start*/ map[ /*Map*/ int] /*Key*/ int /*End*/
type Node ¶
type Node interface { // Decorations returns the common Node decorations (Before, After, Start, End). This returns nil for Package nodes. Decorations() *NodeDecs }
Node is satisfied by all nodes types.
func Clone ¶ added in v0.7.0
Clone returns a deep copy of the node, ready to be re-used elsewhere in the tree.
Example ¶
package main import ( "github.com/dave/dst" "github.com/dave/dst/decorator" ) func main() { code := `package main var i /* a */ int` f, err := decorator.Parse(code) if err != nil { panic(err) } cloned := dst.Clone(f.Decls[0]).(*dst.GenDecl) cloned.Decs.Before = dst.NewLine cloned.Specs[0].(*dst.ValueSpec).Names[0].Name = "j" cloned.Specs[0].(*dst.ValueSpec).Names[0].Decs.End.Replace("/* b */") f.Decls = append(f.Decls, cloned) if err := decorator.Print(f); err != nil { panic(err) } }
Output: package main var i /* a */ int var j /* b */ int
type NodeDecs ¶ added in v0.6.0
type NodeDecs struct { Before SpaceType Start Decorations End Decorations After SpaceType }
NodeDecs holds the decorations that are common to all nodes (except Package).
type ObjKind ¶
type ObjKind int
ObjKind describes what an object represents.
type Object ¶
type Object struct { Kind ObjKind Name string // declared name Decl interface{} // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, AssignStmt, Scope; or nil Data interface{} // object-specific data; or nil Type interface{} // placeholder for type information; may be nil }
An Object describes a named language entity such as a package, constant, type, variable, function (incl. methods), or label.
The Data fields contains object-specific data:
Kind Data type Data value Pkg *Scope package scope Con int iota for the respective declaration
func CloneObject ¶ added in v0.7.0
CloneObject returns nil: After cloning a node, it should not be attached to the same object / scope.
type Package ¶
type Package struct { Name string // package name Scope *Scope // package scope across all files Imports map[string]*Object // map of package id -> package object Files map[string]*File // Go source files by filename }
A Package node represents a set of source files collectively building a Go package.
func NewPackage ¶
func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error)
NewPackage creates a new Package node from a set of File nodes. It resolves unresolved identifiers across files and updates each file's Unresolved list accordingly. If a non-nil importer and universe scope are provided, they are used to resolve identifiers not declared in any of the package files. Any remaining unresolved identifiers are reported as undeclared. If the files belong to different packages, one package name is selected and files with different package names are reported and then ignored. The result is a package node and a scanner.ErrorList if there were errors.
func (*Package) Decorations ¶ added in v0.6.0
Decorations is nil for Package nodes.
type PackageDecorations ¶ added in v0.1.0
type PackageDecorations struct {
NodeDecs
}
PackageDecorations holds decorations for Package:
type ParenExpr ¶
type ParenExpr struct { X Expr // parenthesized expression Decs ParenExprDecorations }
A ParenExpr node represents a parenthesized expression.
func (*ParenExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ParenExprDecorations ¶
type ParenExprDecorations struct { NodeDecs Lparen Decorations X Decorations }
ParenExprDecorations holds decorations for ParenExpr:
var E = /*Start*/ ( /*Lparen*/ 1 + 1 /*X*/) /*End*/ / 2
type RangeStmt ¶
type RangeStmt struct {
Key, Value Expr // Key, Value may be nil
Tok token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
X Expr // value to range over
Body *BlockStmt
Decs RangeStmtDecorations
}
A RangeStmt represents a for statement with a range clause.
func (*RangeStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type RangeStmtDecorations ¶
type RangeStmtDecorations struct { NodeDecs For Decorations Key Decorations Value Decorations Range Decorations X Decorations }
RangeStmtDecorations holds decorations for RangeStmt:
/*Start*/ for range /*Range*/ a /*X*/ { } /*End*/ /*Start*/ for /*For*/ k /*Key*/ := range /*Range*/ a /*X*/ { print(k) } /*End*/ /*Start*/ for /*For*/ k /*Key*/, v /*Value*/ := range /*Range*/ a /*X*/ { print(k, v) } /*End*/
type ReturnStmt ¶
type ReturnStmt struct { Results []Expr // result expressions; or nil Decs ReturnStmtDecorations }
A ReturnStmt node represents a return statement.
func (*ReturnStmt) Decorations ¶ added in v0.6.0
func (n *ReturnStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ReturnStmtDecorations ¶
type ReturnStmtDecorations struct { NodeDecs Return Decorations }
ReturnStmtDecorations holds decorations for ReturnStmt:
func() int { /*Start*/ return /*Return*/ 1 /*End*/ }()
type Scope ¶
A Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope.
func CloneScope ¶ added in v0.7.0
CloneScope returns nil: After cloning a node, it should not be attached to the same object / scope.
func (*Scope) Insert ¶
Insert attempts to insert a named object obj into the scope s. If the scope already contains an object alt with the same name, Insert leaves the scope unchanged and returns alt. Otherwise it inserts obj and returns nil.
type SelectStmt ¶
type SelectStmt struct { Body *BlockStmt // CommClauses only Decs SelectStmtDecorations }
An SelectStmt node represents a select statement.
func (*SelectStmt) Decorations ¶ added in v0.6.0
func (n *SelectStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type SelectStmtDecorations ¶
type SelectStmtDecorations struct { NodeDecs Select Decorations }
SelectStmtDecorations holds decorations for SelectStmt:
/*Start*/ select /*Select*/ { } /*End*/
type SelectorExpr ¶
type SelectorExpr struct { X Expr // expression Sel *Ident // field selector Decs SelectorExprDecorations }
A SelectorExpr node represents an expression followed by a selector.
func (*SelectorExpr) Decorations ¶ added in v0.6.0
func (n *SelectorExpr) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type SelectorExprDecorations ¶
type SelectorExprDecorations struct { NodeDecs X Decorations }
SelectorExprDecorations holds decorations for SelectorExpr:
var F = /*Start*/ tt. /*X*/ F /*End*/ ()
type SendStmt ¶
type SendStmt struct { Chan Expr Value Expr Decs SendStmtDecorations }
A SendStmt node represents a send statement.
func (*SendStmt) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type SendStmtDecorations ¶
type SendStmtDecorations struct { NodeDecs Chan Decorations Arrow Decorations }
SendStmtDecorations holds decorations for SendStmt:
/*Start*/ c /*Chan*/ <- /*Arrow*/ 0 /*End*/
type SliceExpr ¶
type SliceExpr struct { X Expr // expression Low Expr // begin of slice range; or nil High Expr // end of slice range; or nil Max Expr // maximum capacity of slice; or nil Slice3 bool // true if 3-index slice (2 colons present) Decs SliceExprDecorations }
An SliceExpr node represents an expression followed by slice indices.
func (*SliceExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type SliceExprDecorations ¶
type SliceExprDecorations struct { NodeDecs X Decorations Lbrack Decorations Low Decorations High Decorations Max Decorations }
SliceExprDecorations holds decorations for SliceExpr:
var H = /*Start*/ []int{0, 1, 2} /*X*/ [ /*Lbrack*/ 1: /*Low*/ 2: /*High*/ 3 /*Max*/] /*End*/ var H1 = /*Start*/ []int{0, 1, 2} /*X*/ [ /*Lbrack*/ 1: /*Low*/ 2 /*High*/] /*End*/ var H2 = /*Start*/ []int{0} /*X*/ [: /*Low*/] /*End*/ var H3 = /*Start*/ []int{0} /*X*/ [ /*Lbrack*/ 1: /*Low*/] /*End*/ var H4 = /*Start*/ []int{0, 1, 2} /*X*/ [: /*Low*/ 2 /*High*/] /*End*/ var H5 = /*Start*/ []int{0, 1, 2} /*X*/ [: /*Low*/ 2: /*High*/ 3 /*Max*/] /*End*/
type SpaceType ¶ added in v0.1.0
type SpaceType int
SpaceType represents the line spacing before or after a node. When the start of one node is adjacent to the end of another node, the SpaceType values are not additive (e.g. two NewLines will render a NewLine and not an EmptyLine).
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 { X Expr // operand Decs StarExprDecorations }
A StarExpr node represents an expression of the form "*" Expression. Semantically it could be a unary "*" expression, or a pointer type.
func (*StarExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type StarExprDecorations ¶
type StarExprDecorations struct { NodeDecs Star Decorations }
StarExprDecorations holds decorations for StarExpr:
var N = /*Start*/ * /*Star*/ p /*End*/
type Stmt ¶
type Stmt interface { Node // contains filtered or unexported methods }
All statement nodes implement the Stmt interface.
type StructType ¶
type StructType struct { Fields *FieldList // list of field declarations Incomplete bool // true if (source) fields are missing in the Fields list Decs StructTypeDecorations }
A StructType node represents a struct type.
func (*StructType) Decorations ¶ added in v0.6.0
func (n *StructType) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type StructTypeDecorations ¶
type StructTypeDecorations struct { NodeDecs Struct Decorations }
StructTypeDecorations holds decorations for StructType:
type S /*Start*/ struct /*Struct*/ { A int } /*End*/
type SwitchStmt ¶
type SwitchStmt struct { Init Stmt // initialization statement; or nil Tag Expr // tag expression; or nil Body *BlockStmt // CaseClauses only Decs SwitchStmtDecorations }
A SwitchStmt node represents an expression switch statement.
func (*SwitchStmt) Decorations ¶ added in v0.6.0
func (n *SwitchStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type SwitchStmtDecorations ¶
type SwitchStmtDecorations struct { NodeDecs Switch Decorations Init Decorations Tag Decorations }
SwitchStmtDecorations holds decorations for SwitchStmt:
/*Start*/ switch /*Switch*/ i /*Tag*/ { } /*End*/ /*Start*/ switch /*Switch*/ a := i; /*Init*/ a /*Tag*/ { } /*End*/
type TypeAssertExpr ¶
type TypeAssertExpr struct { X Expr // expression Type Expr // asserted type; nil means type switch X.(type) Decs TypeAssertExprDecorations }
A TypeAssertExpr node represents an expression followed by a type assertion.
func (*TypeAssertExpr) Decorations ¶ added in v0.6.0
func (n *TypeAssertExpr) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type TypeAssertExprDecorations ¶
type TypeAssertExprDecorations struct { NodeDecs X Decorations Lparen Decorations Type Decorations }
TypeAssertExprDecorations holds decorations for TypeAssertExpr:
var J = /*Start*/ f. /*X*/ ( /*Lparen*/ int /*Type*/) /*End*/
type TypeSpec ¶
type TypeSpec struct { Name *Ident // type name Assign bool // position of '=', if any Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes Decs TypeSpecDecorations }
A TypeSpec node represents a type declaration (TypeSpec production).
func (*TypeSpec) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type TypeSpecDecorations ¶
type TypeSpecDecorations struct { NodeDecs Name Decorations }
TypeSpecDecorations holds decorations for TypeSpec:
type ( /*Start*/ T1 /*Name*/ []int /*End*/ ) type ( /*Start*/ T2 = /*Name*/ T1 /*End*/ )
type TypeSwitchStmt ¶
type TypeSwitchStmt struct { Init Stmt // initialization statement; or nil Assign Stmt // x := y.(type) or y.(type) Body *BlockStmt // CaseClauses only Decs TypeSwitchStmtDecorations }
An TypeSwitchStmt node represents a type switch statement.
func (*TypeSwitchStmt) Decorations ¶ added in v0.6.0
func (n *TypeSwitchStmt) Decorations() *NodeDecs
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type TypeSwitchStmtDecorations ¶
type TypeSwitchStmtDecorations struct { NodeDecs Switch Decorations Init Decorations Assign Decorations }
TypeSwitchStmtDecorations holds decorations for TypeSwitchStmt:
/*Start*/ switch /*Switch*/ f.(type) /*Assign*/ { } /*End*/ /*Start*/ switch /*Switch*/ g := f.(type) /*Assign*/ { case int: print(g) } /*End*/ /*Start*/ switch /*Switch*/ g := f; /*Init*/ g := g.(type) /*Assign*/ { case int: print(g) } /*End*/
type UnaryExpr ¶
type UnaryExpr struct { Op token.Token // operator X Expr // operand Decs UnaryExprDecorations }
A UnaryExpr node represents a unary expression. Unary "*" expressions are represented via StarExpr nodes.
func (*UnaryExpr) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type UnaryExprDecorations ¶
type UnaryExprDecorations struct { NodeDecs Op Decorations }
UnaryExprDecorations holds decorations for UnaryExpr:
var O = /*Start*/ ^ /*Op*/ 1 /*End*/
type ValueSpec ¶
type ValueSpec struct { Names []*Ident // value names (len(Names) > 0) Type Expr // value type; or nil Values []Expr // initial values; or nil Decs ValueSpecDecorations }
A ValueSpec node represents a constant or variable declaration (ConstSpec or VarSpec production).
func (*ValueSpec) Decorations ¶ added in v0.6.0
Decorations returns the decorations that are common to all nodes (Before, Start, End, After).
type ValueSpecDecorations ¶
type ValueSpecDecorations struct { NodeDecs Assign Decorations }
ValueSpecDecorations holds decorations for ValueSpec:
var ( /*Start*/ j = /*Assign*/ 1 /*End*/ ) var ( /*Start*/ k, l = /*Assign*/ 1, 2 /*End*/ ) var ( /*Start*/ m, n int = /*Assign*/ 1, 2 /*End*/ )