dst

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2018 License: MIT Imports: 10 Imported by: 0

README

Decorated Syntax Tree

The dst package enables manipulation of a Go syntax tree with high fidelity. Decorations (e.g. comments and line spacing) remain attached to the correct nodes as the tree is modified.

Where does go/ast break?

See this golang issue for more information.

Consider this example where we want to reverse the order of the two statements. As you can see the comments don't remain attached to the correct nodes:

code := `package a

func main(){
	var a int    // foo
	var b string // bar
}
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "a.go", code, parser.ParseComments)
if err != nil {
	panic(err)
}

list := f.Decls[0].(*ast.FuncDecl).Body.List
list[0], list[1] = list[1], list[0]

if err := format.Node(os.Stdout, fset, f); err != nil {
	panic(err)
}

//Output:
//package a
//
//func main() {
//	// foo
//	var b string
//	var a int
//	// bar
//}

Here's the same example using dst:

code := `package a

func main(){
	var a int    // foo
	var b string // bar
}
`
f, err := decorator.Parse(code)
if err != nil {
	panic(err)
}

list := f.Decls[0].(*dst.FuncDecl).Body.List
list[0], list[1] = list[1], list[0]

if err := decorator.Print(f); err != nil {
	panic(err)
}

//Output:
//package a
//
//func main() {
//	var b string // bar
//	var a int    // foo
//}
Examples
Line spacing

The Space property marks the node as having a line space (new line or empty line) before the node. These spaces are rendered before any decorations attached to the Start decoration point. The After property is similar but rendered after the node (and after any End decorations).

code := `package main

func main() {
	println(a, b, c)
}`
f, err := decorator.Parse(code)
if err != nil {
	panic(err)
}

call := f.Decls[0].(*dst.FuncDecl).Body.List[0].(*dst.ExprStmt).X.(*dst.CallExpr)

call.Decs.Space = dst.EmptyLine
call.Decs.After = dst.EmptyLine

for _, v := range call.Args {
	v := v.(*dst.Ident)
	v.Decs.Space = dst.NewLine
	v.Decs.After = dst.NewLine
}

if err := decorator.Print(f); err != nil {
	panic(err)
}

//Output:
//package main
//
//func main() {
//
//	println(
//		a,
//		b,
//		c,
//	)
//
//}
Comments

Comments are added at decoration attachment points. See generated-decorations.go for a full list of these points, along with demonstration code of where they are rendered in the output.

The the decoration points have convenience functions Add, Replace, Clear and All to accomplish common tasks. Use the full text of your comment including the // or /**/ markers. When adding a line comment, a newline is automatically rendered.

code := `package main

func main() {
	println("Hello World!")
}`
f, err := decorator.Parse(code)
if err != nil {
	panic(err)
}

call := f.Decls[0].(*dst.FuncDecl).Body.List[0].(*dst.ExprStmt).X.(*dst.CallExpr)

call.Decs.Start.Add("// you can add comments at the start...")
call.Decs.Fun.Add("/* ...in the middle... */")
call.Decs.End.Add("// or at the end.")

if err := decorator.Print(f); err != nil {
	panic(err)
}

//Output:
//package main
//
//func main() {
//	// you can add comments at the start...
//	println /* ...in the middle... */ ("Hello World!") // or at the end.
//}
Common properties

The common decoration properties (Start, End, Space and After) occur on all Expr, Stmt and Decl nodes, so are available on those interfaces:

code := `package main

func main() {
	var i int
	i++
	println(i)
}`
f, err := decorator.Parse(code)
if err != nil {
	panic(err)
}

list := f.Decls[0].(*dst.FuncDecl).Body.List

list[0].SetSpace(dst.EmptyLine)
list[0].End().Add("// the Decorated interface allows access to the common")
list[1].End().Add("// decoration properties (Space, Start, End and After)")
list[2].End().Add("// for all Expr, Stmt and Decl nodes.")
list[2].SetAfter(dst.EmptyLine)

if err := decorator.Print(f); err != nil {
	panic(err)
}

//Output:
//package main
//
//func main() {
//
//	var i int  // the Decorated interface allows access to the common
//	i++        // decoration properties (Space, Start, End and After)
//	println(i) // for all Expr, Stmt and Decl nodes.
//
//}
Newlines as decorations

The Space and After properties cover the vast majority of cases, but occasionally a newline needs to be rendered inside a node. Simply add a \n decoration to accomplish this.

Apply function from astutil

The dstutil package is a fork of golang.org/x/tools/go/ast/astutil, and provides the Apply function with similar semantics.

Integrating with go/types

Forking the go/types package to use a dst tree as input is non-trivial because go/types uses position information in several places. A work-around is to convert ast to dst using a Decorator. After conversion, this exposes the DstNodes and AstNodes properties which map between ast.Node and dst.Node. This way the go/types package can be used:

code := `package main

func main() {
	var i int
	i++
	println(i)
}`

// Parse the code to AST
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, "a.go", code, parser.ParseComments)
if err != nil {
	panic(err)
}

// Invoke the type checker using AST as input
typesInfo := types.Info{
	Defs:	make(map[*ast.Ident]types.Object),
	Uses:	make(map[*ast.Ident]types.Object),
}
conf := &types.Config{}
if _, err := conf.Check("a", fset, []*ast.File{astFile}, &typesInfo); err != nil {
	panic(err)
}

// Decorate the *ast.File to give us a *dst.File
dec := decorator.New()
f := dec.Decorate(fset, astFile).(*dst.File)

// Find the *dst.Ident for the definition of "i"
dstDef := f.Decls[0].(*dst.FuncDecl).Body.List[0].(*dst.DeclStmt).Decl.(*dst.GenDecl).Specs[0].(*dst.ValueSpec).Names[0]

// Find the *ast.Ident using the AstNodes mapping
astDef := dec.AstNodes[dstDef].(*ast.Ident)

// Find the types.Object corresponding to "i"
obj := typesInfo.Defs[astDef]

// Find all the uses of that object
var uses []*dst.Ident
for id, ob := range typesInfo.Uses {
	if ob != obj {
		continue
	}
	uses = append(uses, dec.DstNodes[id].(*dst.Ident))
}

// Change the name of all uses
dstDef.Name = "foo"
for _, id := range uses {
	id.Name = "foo"
}

// Print the DST
if err := decorator.Print(f); err != nil {
	panic(err)
}

//Output:
//package main
//
//func main() {
//	var foo int
//	foo++
//	println(foo)
//}
Status

This is an experimental package under development, so the API and behaviour is expected to change frequently. However I'm now inviting people to try it out and give feedback.

Chat?

Feel free to create an issue or chat in the #dst Gophers Slack channel.

Documentation

Overview

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

Index

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

func Inspect(node Node, f func(Node) bool)

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/ast"
	"go/parser"
	"go/token"
)

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 := parser.ParseFile(fset, "src.go", src, 0)
	if err != nil {
		panic(err)
	}

	// Inspect the AST and print all identifiers and literals.
	ast.Inspect(f, func(n ast.Node) bool {
		var s string
		switch x := n.(type) {
		case *ast.BasicLit:
			s = x.Value
		case *ast.Ident:
			s = x.Name
		}
		if s != "" {
			fmt.Printf("%s:\t%s\n", fset.Position(n.Pos()), s)
		}
		return true
	})

}
Output:

src.go:2:9:	p
src.go:3:7:	c
src.go:3:11:	1.0
src.go:4:5:	X
src.go:4:9:	f
src.go:4:11:	3.14
src.go:4:17:	2
src.go:4:21:	c

func IsExported

func IsExported(name string) bool

IsExported reports whether name is an exported Go symbol (that is, whether it begins with an upper-case letter).

func NotNilFilter

func NotNilFilter(_ string, v reflect.Value) bool

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

Example

This example shows what an AST looks like when printed for debugging.

package main

import (
	"go/ast"
	"go/parser"
	"go/token"
)

func main() {
	// src is the input for which we want to print the AST.
	src := `
package main
func main() {
	println("Hello, World!")
}
`

	// Create the AST by parsing src.
	fset := token.NewFileSet() // positions are relative to fset
	f, err := parser.ParseFile(fset, "", src, 0)
	if err != nil {
		panic(err)
	}

	// Print the AST.
	ast.Print(fset, f)

}
Output:

     0  *ast.File {
     1  .  Package: 2:1
     2  .  Name: *ast.Ident {
     3  .  .  NamePos: 2:9
     4  .  .  Name: "main"
     5  .  }
     6  .  Decls: []ast.Decl (len = 1) {
     7  .  .  0: *ast.FuncDecl {
     8  .  .  .  Name: *ast.Ident {
     9  .  .  .  .  NamePos: 3:6
    10  .  .  .  .  Name: "main"
    11  .  .  .  .  Obj: *ast.Object {
    12  .  .  .  .  .  Kind: func
    13  .  .  .  .  .  Name: "main"
    14  .  .  .  .  .  Decl: *(obj @ 7)
    15  .  .  .  .  }
    16  .  .  .  }
    17  .  .  .  Type: *ast.FuncType {
    18  .  .  .  .  Func: 3:1
    19  .  .  .  .  Params: *ast.FieldList {
    20  .  .  .  .  .  Opening: 3:10
    21  .  .  .  .  .  Closing: 3:11
    22  .  .  .  .  }
    23  .  .  .  }
    24  .  .  .  Body: *ast.BlockStmt {
    25  .  .  .  .  Lbrace: 3:13
    26  .  .  .  .  List: []ast.Stmt (len = 1) {
    27  .  .  .  .  .  0: *ast.ExprStmt {
    28  .  .  .  .  .  .  X: *ast.CallExpr {
    29  .  .  .  .  .  .  .  Fun: *ast.Ident {
    30  .  .  .  .  .  .  .  .  NamePos: 4:2
    31  .  .  .  .  .  .  .  .  Name: "println"
    32  .  .  .  .  .  .  .  }
    33  .  .  .  .  .  .  .  Lparen: 4:9
    34  .  .  .  .  .  .  .  Args: []ast.Expr (len = 1) {
    35  .  .  .  .  .  .  .  .  0: *ast.BasicLit {
    36  .  .  .  .  .  .  .  .  .  ValuePos: 4:10
    37  .  .  .  .  .  .  .  .  .  Kind: STRING
    38  .  .  .  .  .  .  .  .  .  Value: "\"Hello, World!\""
    39  .  .  .  .  .  .  .  .  }
    40  .  .  .  .  .  .  .  }
    41  .  .  .  .  .  .  .  Ellipsis: -
    42  .  .  .  .  .  .  .  Rparen: 4:25
    43  .  .  .  .  .  .  }
    44  .  .  .  .  .  }
    45  .  .  .  .  }
    46  .  .  .  .  Rbrace: 5:1
    47  .  .  .  }
    48  .  .  }
    49  .  }
    50  .  Scope: *ast.Scope {
    51  .  .  Objects: map[string]*ast.Object (len = 1) {
    52  .  .  .  "main": *(obj @ 11)
    53  .  .  }
    54  .  }
    55  .  Unresolved: []*ast.Ident (len = 1) {
    56  .  .  0: *(obj @ 29)
    57  .  }
    58  }

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

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) After added in v0.2.0

func (v *ArrayType) After() SpaceType

func (*ArrayType) End added in v0.2.0

func (v *ArrayType) End() *Decorations

func (*ArrayType) SetAfter added in v0.2.0

func (v *ArrayType) SetAfter(s SpaceType)

func (*ArrayType) SetSpace added in v0.2.0

func (v *ArrayType) SetSpace(s SpaceType)

func (*ArrayType) Space added in v0.2.0

func (v *ArrayType) Space() SpaceType

func (*ArrayType) Start added in v0.2.0

func (v *ArrayType) Start() *Decorations

type ArrayTypeDecorations

type ArrayTypeDecorations struct {
	Space  SpaceType
	Start  Decorations
	Lbrack Decorations
	Len    Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *AssignStmt) After() SpaceType

func (*AssignStmt) End added in v0.2.0

func (v *AssignStmt) End() *Decorations

func (*AssignStmt) SetAfter added in v0.2.0

func (v *AssignStmt) SetAfter(s SpaceType)

func (*AssignStmt) SetSpace added in v0.2.0

func (v *AssignStmt) SetSpace(s SpaceType)

func (*AssignStmt) Space added in v0.2.0

func (v *AssignStmt) Space() SpaceType

func (*AssignStmt) Start added in v0.2.0

func (v *AssignStmt) Start() *Decorations

type AssignStmtDecorations

type AssignStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Lhs   Decorations
	Tok   Decorations
	End   Decorations
	After SpaceType
}

AssignStmtDecorations holds decorations for AssignStmt:

/*Start*/
i /*Lhs*/ = /*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) After added in v0.2.0

func (v *BadDecl) After() SpaceType

func (*BadDecl) End added in v0.2.0

func (v *BadDecl) End() *Decorations

func (*BadDecl) SetAfter added in v0.2.0

func (v *BadDecl) SetAfter(s SpaceType)

func (*BadDecl) SetSpace added in v0.2.0

func (v *BadDecl) SetSpace(s SpaceType)

func (*BadDecl) Space added in v0.2.0

func (v *BadDecl) Space() SpaceType

func (*BadDecl) Start added in v0.2.0

func (v *BadDecl) Start() *Decorations

type BadDeclDecorations

type BadDeclDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *BadExpr) After() SpaceType

func (*BadExpr) End added in v0.2.0

func (v *BadExpr) End() *Decorations

func (*BadExpr) SetAfter added in v0.2.0

func (v *BadExpr) SetAfter(s SpaceType)

func (*BadExpr) SetSpace added in v0.2.0

func (v *BadExpr) SetSpace(s SpaceType)

func (*BadExpr) Space added in v0.2.0

func (v *BadExpr) Space() SpaceType

func (*BadExpr) Start added in v0.2.0

func (v *BadExpr) Start() *Decorations

type BadExprDecorations

type BadExprDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *BadStmt) After() SpaceType

func (*BadStmt) End added in v0.2.0

func (v *BadStmt) End() *Decorations

func (*BadStmt) SetAfter added in v0.2.0

func (v *BadStmt) SetAfter(s SpaceType)

func (*BadStmt) SetSpace added in v0.2.0

func (v *BadStmt) SetSpace(s SpaceType)

func (*BadStmt) Space added in v0.2.0

func (v *BadStmt) Space() SpaceType

func (*BadStmt) Start added in v0.2.0

func (v *BadStmt) Start() *Decorations

type BadStmtDecorations

type BadStmtDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *BasicLit) After() SpaceType

func (*BasicLit) End added in v0.2.0

func (v *BasicLit) End() *Decorations

func (*BasicLit) SetAfter added in v0.2.0

func (v *BasicLit) SetAfter(s SpaceType)

func (*BasicLit) SetSpace added in v0.2.0

func (v *BasicLit) SetSpace(s SpaceType)

func (*BasicLit) Space added in v0.2.0

func (v *BasicLit) Space() SpaceType

func (*BasicLit) Start added in v0.2.0

func (v *BasicLit) Start() *Decorations

type BasicLitDecorations

type BasicLitDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *BinaryExpr) After() SpaceType

func (*BinaryExpr) End added in v0.2.0

func (v *BinaryExpr) End() *Decorations

func (*BinaryExpr) SetAfter added in v0.2.0

func (v *BinaryExpr) SetAfter(s SpaceType)

func (*BinaryExpr) SetSpace added in v0.2.0

func (v *BinaryExpr) SetSpace(s SpaceType)

func (*BinaryExpr) Space added in v0.2.0

func (v *BinaryExpr) Space() SpaceType

func (*BinaryExpr) Start added in v0.2.0

func (v *BinaryExpr) Start() *Decorations

type BinaryExprDecorations

type BinaryExprDecorations struct {
	Space SpaceType
	Start Decorations
	X     Decorations
	Op    Decorations
	End   Decorations
	After SpaceType
}

BinaryExprDecorations holds decorations for BinaryExpr:

var P = /*Start*/ 1 /*X*/ & /*Op*/ 2 /*End*/

type BlockStmt

type BlockStmt struct {
	List []Stmt
	Decs BlockStmtDecorations
}

A BlockStmt node represents a braced statement list.

func (*BlockStmt) After added in v0.2.0

func (v *BlockStmt) After() SpaceType

func (*BlockStmt) End added in v0.2.0

func (v *BlockStmt) End() *Decorations

func (*BlockStmt) SetAfter added in v0.2.0

func (v *BlockStmt) SetAfter(s SpaceType)

func (*BlockStmt) SetSpace added in v0.2.0

func (v *BlockStmt) SetSpace(s SpaceType)

func (*BlockStmt) Space added in v0.2.0

func (v *BlockStmt) Space() SpaceType

func (*BlockStmt) Start added in v0.2.0

func (v *BlockStmt) Start() *Decorations

type BlockStmtDecorations

type BlockStmtDecorations struct {
	Space  SpaceType
	Start  Decorations
	Lbrace Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *BranchStmt) After() SpaceType

func (*BranchStmt) End added in v0.2.0

func (v *BranchStmt) End() *Decorations

func (*BranchStmt) SetAfter added in v0.2.0

func (v *BranchStmt) SetAfter(s SpaceType)

func (*BranchStmt) SetSpace added in v0.2.0

func (v *BranchStmt) SetSpace(s SpaceType)

func (*BranchStmt) Space added in v0.2.0

func (v *BranchStmt) Space() SpaceType

func (*BranchStmt) Start added in v0.2.0

func (v *BranchStmt) Start() *Decorations

type BranchStmtDecorations

type BranchStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Tok   Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *CallExpr) After() SpaceType

func (*CallExpr) End added in v0.2.0

func (v *CallExpr) End() *Decorations

func (*CallExpr) SetAfter added in v0.2.0

func (v *CallExpr) SetAfter(s SpaceType)

func (*CallExpr) SetSpace added in v0.2.0

func (v *CallExpr) SetSpace(s SpaceType)

func (*CallExpr) Space added in v0.2.0

func (v *CallExpr) Space() SpaceType

func (*CallExpr) Start added in v0.2.0

func (v *CallExpr) Start() *Decorations

type CallExprDecorations

type CallExprDecorations struct {
	Space    SpaceType
	Start    Decorations
	Fun      Decorations
	Lparen   Decorations
	Args     Decorations
	Ellipsis Decorations
	End      Decorations
	After    SpaceType
}

CallExprDecorations holds decorations for CallExpr:

var L = /*Start*/ C /*Fun*/ ( /*Lparen*/ 0, []int{} /*Args*/ ... /*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) After added in v0.2.0

func (v *CaseClause) After() SpaceType

func (*CaseClause) End added in v0.2.0

func (v *CaseClause) End() *Decorations

func (*CaseClause) SetAfter added in v0.2.0

func (v *CaseClause) SetAfter(s SpaceType)

func (*CaseClause) SetSpace added in v0.2.0

func (v *CaseClause) SetSpace(s SpaceType)

func (*CaseClause) Space added in v0.2.0

func (v *CaseClause) Space() SpaceType

func (*CaseClause) Start added in v0.2.0

func (v *CaseClause) Start() *Decorations

type CaseClauseDecorations

type CaseClauseDecorations struct {
	Space SpaceType
	Start Decorations
	Case  Decorations
	List  Decorations
	Colon Decorations
	End   Decorations
	After SpaceType
}

CaseClauseDecorations holds decorations for CaseClause:

switch i {
/*Start*/ case /*Case*/ 1 /*List*/ : /*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.

const (
	SEND ChanDir = 1 << iota
	RECV
)

type ChanType

type ChanType struct {
	Dir   ChanDir // channel direction
	Value Expr    // value type
	Decs  ChanTypeDecorations
}

A ChanType node represents a channel type.

func (*ChanType) After added in v0.2.0

func (v *ChanType) After() SpaceType

func (*ChanType) End added in v0.2.0

func (v *ChanType) End() *Decorations

func (*ChanType) SetAfter added in v0.2.0

func (v *ChanType) SetAfter(s SpaceType)

func (*ChanType) SetSpace added in v0.2.0

func (v *ChanType) SetSpace(s SpaceType)

func (*ChanType) Space added in v0.2.0

func (v *ChanType) Space() SpaceType

func (*ChanType) Start added in v0.2.0

func (v *ChanType) Start() *Decorations

type ChanTypeDecorations

type ChanTypeDecorations struct {
	Space SpaceType
	Start Decorations
	Begin Decorations
	Arrow Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *CommClause) After() SpaceType

func (*CommClause) End added in v0.2.0

func (v *CommClause) End() *Decorations

func (*CommClause) SetAfter added in v0.2.0

func (v *CommClause) SetAfter(s SpaceType)

func (*CommClause) SetSpace added in v0.2.0

func (v *CommClause) SetSpace(s SpaceType)

func (*CommClause) Space added in v0.2.0

func (v *CommClause) Space() SpaceType

func (*CommClause) Start added in v0.2.0

func (v *CommClause) Start() *Decorations

type CommClauseDecorations

type CommClauseDecorations struct {
	Space SpaceType
	Start Decorations
	Case  Decorations
	Comm  Decorations
	Colon Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *CompositeLit) After() SpaceType

func (*CompositeLit) End added in v0.2.0

func (v *CompositeLit) End() *Decorations

func (*CompositeLit) SetAfter added in v0.2.0

func (v *CompositeLit) SetAfter(s SpaceType)

func (*CompositeLit) SetSpace added in v0.2.0

func (v *CompositeLit) SetSpace(s SpaceType)

func (*CompositeLit) Space added in v0.2.0

func (v *CompositeLit) Space() SpaceType

func (*CompositeLit) Start added in v0.2.0

func (v *CompositeLit) Start() *Decorations

type CompositeLitDecorations

type CompositeLitDecorations struct {
	Space  SpaceType
	Start  Decorations
	Type   Decorations
	Lbrace Decorations
	End    Decorations
	After  SpaceType
}

CompositeLitDecorations holds decorations for CompositeLit:

var D = /*Start*/ A /*Type*/ { /*Lbrace*/ A: 0} /*End*/

type Decl

type Decl interface {
	Node
	Decorated
	// 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) After added in v0.2.0

func (v *DeclStmt) After() SpaceType

func (*DeclStmt) End added in v0.2.0

func (v *DeclStmt) End() *Decorations

func (*DeclStmt) SetAfter added in v0.2.0

func (v *DeclStmt) SetAfter(s SpaceType)

func (*DeclStmt) SetSpace added in v0.2.0

func (v *DeclStmt) SetSpace(s SpaceType)

func (*DeclStmt) Space added in v0.2.0

func (v *DeclStmt) Space() SpaceType

func (*DeclStmt) Start added in v0.2.0

func (v *DeclStmt) Start() *Decorations

type DeclStmtDecorations

type DeclStmtDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

DeclStmtDecorations holds decorations for DeclStmt:

type Decorated added in v0.2.0

type Decorated interface {
	Space() SpaceType
	SetSpace(SpaceType)
	Start() *Decorations
	End() *Decorations
	After() SpaceType
	SetAfter(SpaceType)
}
Example
package main

import (
	"github.com/dave/dst"
	"github.com/dave/dst/decorator"
)

func main() {
	code := `package main

	func main() {
		var i int
		i++
		println(i)
	}`
	f, err := decorator.Parse(code)
	if err != nil {
		panic(err)
	}

	list := f.Decls[0].(*dst.FuncDecl).Body.List

	list[0].SetSpace(dst.EmptyLine)
	list[0].End().Add("// the Decorated interface allows access to the common")
	list[1].End().Add("// decoration properties (Space, Start, End and After)")
	list[2].End().Add("// for all Expr, Stmt and Decl nodes.")
	list[2].SetAfter(dst.EmptyLine)

	if err := decorator.Print(f); err != nil {
		panic(err)
	}

}
Output:

package main

func main() {

	var i int  // the Decorated interface allows access to the common
	i++        // decoration properties (Space, Start, End and After)
	println(i) // for all Expr, Stmt and Decl nodes.

}

type Decorations

type Decorations []string
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.SetSpace(dst.EmptyLine)
		stmt.Start().Add(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.SetSpace(dst.NewLine)
		expr.SetAfter(dst.NewLine)
		expr.Start().Add(fmt.Sprintf("/* bar %d */", i))
		expr.End().Add(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) Add

func (d *Decorations) Add(decs ...string)

func (*Decorations) All added in v0.2.0

func (d *Decorations) All() []string

func (*Decorations) Clear

func (d *Decorations) Clear()

func (*Decorations) Replace

func (d *Decorations) Replace(decs ...string)

type DeferStmt

type DeferStmt struct {
	Call *CallExpr
	Decs DeferStmtDecorations
}

A DeferStmt node represents a defer statement.

func (*DeferStmt) After added in v0.2.0

func (v *DeferStmt) After() SpaceType

func (*DeferStmt) End added in v0.2.0

func (v *DeferStmt) End() *Decorations

func (*DeferStmt) SetAfter added in v0.2.0

func (v *DeferStmt) SetAfter(s SpaceType)

func (*DeferStmt) SetSpace added in v0.2.0

func (v *DeferStmt) SetSpace(s SpaceType)

func (*DeferStmt) Space added in v0.2.0

func (v *DeferStmt) Space() SpaceType

func (*DeferStmt) Start added in v0.2.0

func (v *DeferStmt) Start() *Decorations

type DeferStmtDecorations

type DeferStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Defer Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *Ellipsis) After() SpaceType

func (*Ellipsis) End added in v0.2.0

func (v *Ellipsis) End() *Decorations

func (*Ellipsis) SetAfter added in v0.2.0

func (v *Ellipsis) SetAfter(s SpaceType)

func (*Ellipsis) SetSpace added in v0.2.0

func (v *Ellipsis) SetSpace(s SpaceType)

func (*Ellipsis) Space added in v0.2.0

func (v *Ellipsis) Space() SpaceType

func (*Ellipsis) Start added in v0.2.0

func (v *Ellipsis) Start() *Decorations

type EllipsisDecorations

type EllipsisDecorations struct {
	Space    SpaceType
	Start    Decorations
	Ellipsis Decorations
	End      Decorations
	After    SpaceType
}

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) After added in v0.2.0

func (v *EmptyStmt) After() SpaceType

func (*EmptyStmt) End added in v0.2.0

func (v *EmptyStmt) End() *Decorations

func (*EmptyStmt) SetAfter added in v0.2.0

func (v *EmptyStmt) SetAfter(s SpaceType)

func (*EmptyStmt) SetSpace added in v0.2.0

func (v *EmptyStmt) SetSpace(s SpaceType)

func (*EmptyStmt) Space added in v0.2.0

func (v *EmptyStmt) Space() SpaceType

func (*EmptyStmt) Start added in v0.2.0

func (v *EmptyStmt) Start() *Decorations

type EmptyStmtDecorations

type EmptyStmtDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

EmptyStmtDecorations holds decorations for EmptyStmt:

type Expr

type Expr interface {
	Node
	Decorated
	// 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) After added in v0.2.0

func (v *ExprStmt) After() SpaceType

func (*ExprStmt) End added in v0.2.0

func (v *ExprStmt) End() *Decorations

func (*ExprStmt) SetAfter added in v0.2.0

func (v *ExprStmt) SetAfter(s SpaceType)

func (*ExprStmt) SetSpace added in v0.2.0

func (v *ExprStmt) SetSpace(s SpaceType)

func (*ExprStmt) Space added in v0.2.0

func (v *ExprStmt) Space() SpaceType

func (*ExprStmt) Start added in v0.2.0

func (v *ExprStmt) Start() *Decorations

type ExprStmtDecorations

type ExprStmtDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *Field) After() SpaceType

func (*Field) End added in v0.2.0

func (v *Field) End() *Decorations

func (*Field) SetAfter added in v0.2.0

func (v *Field) SetAfter(s SpaceType)

func (*Field) SetSpace added in v0.2.0

func (v *Field) SetSpace(s SpaceType)

func (*Field) Space added in v0.2.0

func (v *Field) Space() SpaceType

func (*Field) Start added in v0.2.0

func (v *Field) Start() *Decorations

type FieldDecorations

type FieldDecorations struct {
	Space SpaceType
	Start Decorations
	Names Decorations
	Type  Decorations
	End   Decorations
	After SpaceType
}

FieldDecorations holds decorations for Field:

type A struct {
	/*Start*/ A /*Names*/ int /*Type*/ `a:"a"` /*End*/
}

type FieldFilter

type FieldFilter func(name string, value reflect.Value) bool

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) After added in v0.2.0

func (v *FieldList) After() SpaceType

func (*FieldList) End added in v0.2.0

func (v *FieldList) End() *Decorations

func (*FieldList) NumFields

func (f *FieldList) NumFields() int

NumFields returns the number of parameters or struct fields represented by a FieldList.

func (*FieldList) SetAfter added in v0.2.0

func (v *FieldList) SetAfter(s SpaceType)

func (*FieldList) SetSpace added in v0.2.0

func (v *FieldList) SetSpace(s SpaceType)

func (*FieldList) Space added in v0.2.0

func (v *FieldList) Space() SpaceType

func (*FieldList) Start added in v0.2.0

func (v *FieldList) Start() *Decorations

type FieldListDecorations

type FieldListDecorations struct {
	Space   SpaceType
	Start   Decorations
	Opening Decorations
	End     Decorations
	After   SpaceType
}

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) After added in v0.2.0

func (v *File) After() SpaceType

func (*File) SetAfter added in v0.2.0

func (v *File) SetAfter(s SpaceType)

func (*File) SetSpace added in v0.2.0

func (v *File) SetSpace(s SpaceType)

func (*File) Space added in v0.2.0

func (v *File) Space() SpaceType

func (*File) Start added in v0.2.0

func (v *File) Start() *Decorations

type FileDecorations

type FileDecorations struct {
	Space   SpaceType
	Start   Decorations
	Package Decorations
	Name    Decorations
	After   SpaceType
}

FileDecorations holds decorations for File:

/*Start*/ package /*Package*/ postests /*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) After added in v0.2.0

func (v *ForStmt) After() SpaceType

func (*ForStmt) End added in v0.2.0

func (v *ForStmt) End() *Decorations

func (*ForStmt) SetAfter added in v0.2.0

func (v *ForStmt) SetAfter(s SpaceType)

func (*ForStmt) SetSpace added in v0.2.0

func (v *ForStmt) SetSpace(s SpaceType)

func (*ForStmt) Space added in v0.2.0

func (v *ForStmt) Space() SpaceType

func (*ForStmt) Start added in v0.2.0

func (v *ForStmt) Start() *Decorations

type ForStmtDecorations

type ForStmtDecorations struct {
	Space SpaceType
	Start Decorations
	For   Decorations
	Init  Decorations
	Cond  Decorations
	Post  Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *FuncDecl) After() SpaceType

func (*FuncDecl) End added in v0.2.0

func (v *FuncDecl) End() *Decorations

func (*FuncDecl) SetAfter added in v0.2.0

func (v *FuncDecl) SetAfter(s SpaceType)

func (*FuncDecl) SetSpace added in v0.2.0

func (v *FuncDecl) SetSpace(s SpaceType)

func (*FuncDecl) Space added in v0.2.0

func (v *FuncDecl) Space() SpaceType

func (*FuncDecl) Start added in v0.2.0

func (v *FuncDecl) Start() *Decorations

type FuncDeclDecorations

type FuncDeclDecorations struct {
	Space   SpaceType
	Start   Decorations
	Func    Decorations
	Recv    Decorations
	Name    Decorations
	Params  Decorations
	Results Decorations
	End     Decorations
	After   SpaceType
}

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
}

type FuncLit

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

A FuncLit node represents a function literal.

func (*FuncLit) After added in v0.2.0

func (v *FuncLit) After() SpaceType

func (*FuncLit) End added in v0.2.0

func (v *FuncLit) End() *Decorations

func (*FuncLit) SetAfter added in v0.2.0

func (v *FuncLit) SetAfter(s SpaceType)

func (*FuncLit) SetSpace added in v0.2.0

func (v *FuncLit) SetSpace(s SpaceType)

func (*FuncLit) Space added in v0.2.0

func (v *FuncLit) Space() SpaceType

func (*FuncLit) Start added in v0.2.0

func (v *FuncLit) Start() *Decorations

type FuncLitDecorations

type FuncLitDecorations struct {
	Space SpaceType
	Start Decorations
	Type  Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *FuncType) After() SpaceType

func (*FuncType) End added in v0.2.0

func (v *FuncType) End() *Decorations

func (*FuncType) SetAfter added in v0.2.0

func (v *FuncType) SetAfter(s SpaceType)

func (*FuncType) SetSpace added in v0.2.0

func (v *FuncType) SetSpace(s SpaceType)

func (*FuncType) Space added in v0.2.0

func (v *FuncType) Space() SpaceType

func (*FuncType) Start added in v0.2.0

func (v *FuncType) Start() *Decorations

type FuncTypeDecorations

type FuncTypeDecorations struct {
	Space  SpaceType
	Start  Decorations
	Func   Decorations
	Params Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *GenDecl) After() SpaceType

func (*GenDecl) End added in v0.2.0

func (v *GenDecl) End() *Decorations

func (*GenDecl) SetAfter added in v0.2.0

func (v *GenDecl) SetAfter(s SpaceType)

func (*GenDecl) SetSpace added in v0.2.0

func (v *GenDecl) SetSpace(s SpaceType)

func (*GenDecl) Space added in v0.2.0

func (v *GenDecl) Space() SpaceType

func (*GenDecl) Start added in v0.2.0

func (v *GenDecl) Start() *Decorations

type GenDeclDecorations

type GenDeclDecorations struct {
	Space  SpaceType
	Start  Decorations
	Tok    Decorations
	Lparen Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *GoStmt) After() SpaceType

func (*GoStmt) End added in v0.2.0

func (v *GoStmt) End() *Decorations

func (*GoStmt) SetAfter added in v0.2.0

func (v *GoStmt) SetAfter(s SpaceType)

func (*GoStmt) SetSpace added in v0.2.0

func (v *GoStmt) SetSpace(s SpaceType)

func (*GoStmt) Space added in v0.2.0

func (v *GoStmt) Space() SpaceType

func (*GoStmt) Start added in v0.2.0

func (v *GoStmt) Start() *Decorations

type GoStmtDecorations

type GoStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Go    Decorations
	End   Decorations
	After SpaceType
}

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
	Decs IdentDecorations
}

An Ident node represents an identifier.

func NewIdent

func NewIdent(name string) *Ident

NewIdent creates a new Ident without position. Useful for ASTs generated by code other than the Go parser.

func (*Ident) After added in v0.2.0

func (v *Ident) After() SpaceType

func (*Ident) End added in v0.2.0

func (v *Ident) End() *Decorations

func (*Ident) IsExported

func (id *Ident) IsExported() bool

IsExported reports whether id is an exported Go symbol (that is, whether it begins with an uppercase letter).

func (*Ident) SetAfter added in v0.2.0

func (v *Ident) SetAfter(s SpaceType)

func (*Ident) SetSpace added in v0.2.0

func (v *Ident) SetSpace(s SpaceType)

func (*Ident) Space added in v0.2.0

func (v *Ident) Space() SpaceType

func (*Ident) Start added in v0.2.0

func (v *Ident) Start() *Decorations

func (*Ident) String

func (id *Ident) String() string

type IdentDecorations

type IdentDecorations struct {
	Space SpaceType
	Start Decorations
	End   Decorations
	After SpaceType
}

IdentDecorations holds decorations for Ident:

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) After added in v0.2.0

func (v *IfStmt) After() SpaceType

func (*IfStmt) End added in v0.2.0

func (v *IfStmt) End() *Decorations

func (*IfStmt) SetAfter added in v0.2.0

func (v *IfStmt) SetAfter(s SpaceType)

func (*IfStmt) SetSpace added in v0.2.0

func (v *IfStmt) SetSpace(s SpaceType)

func (*IfStmt) Space added in v0.2.0

func (v *IfStmt) Space() SpaceType

func (*IfStmt) Start added in v0.2.0

func (v *IfStmt) Start() *Decorations

type IfStmtDecorations

type IfStmtDecorations struct {
	Space SpaceType
	Start Decorations
	If    Decorations
	Init  Decorations
	Cond  Decorations
	Else  Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *ImportSpec) After() SpaceType

func (*ImportSpec) End added in v0.2.0

func (v *ImportSpec) End() *Decorations

func (*ImportSpec) SetAfter added in v0.2.0

func (v *ImportSpec) SetAfter(s SpaceType)

func (*ImportSpec) SetSpace added in v0.2.0

func (v *ImportSpec) SetSpace(s SpaceType)

func (*ImportSpec) Space added in v0.2.0

func (v *ImportSpec) Space() SpaceType

func (*ImportSpec) Start added in v0.2.0

func (v *ImportSpec) Start() *Decorations

type ImportSpecDecorations

type ImportSpecDecorations struct {
	Space SpaceType
	Start Decorations
	Name  Decorations
	End   Decorations
	After SpaceType
}

ImportSpecDecorations holds decorations for ImportSpec:

import (
	/*Start*/ fmt /*Name*/ "fmt" /*End*/
)

type Importer

type Importer func(imports map[string]*Object, path string) (pkg *Object, err error)

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) After added in v0.2.0

func (v *IncDecStmt) After() SpaceType

func (*IncDecStmt) End added in v0.2.0

func (v *IncDecStmt) End() *Decorations

func (*IncDecStmt) SetAfter added in v0.2.0

func (v *IncDecStmt) SetAfter(s SpaceType)

func (*IncDecStmt) SetSpace added in v0.2.0

func (v *IncDecStmt) SetSpace(s SpaceType)

func (*IncDecStmt) Space added in v0.2.0

func (v *IncDecStmt) Space() SpaceType

func (*IncDecStmt) Start added in v0.2.0

func (v *IncDecStmt) Start() *Decorations

type IncDecStmtDecorations

type IncDecStmtDecorations struct {
	Space SpaceType
	Start Decorations
	X     Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *IndexExpr) After() SpaceType

func (*IndexExpr) End added in v0.2.0

func (v *IndexExpr) End() *Decorations

func (*IndexExpr) SetAfter added in v0.2.0

func (v *IndexExpr) SetAfter(s SpaceType)

func (*IndexExpr) SetSpace added in v0.2.0

func (v *IndexExpr) SetSpace(s SpaceType)

func (*IndexExpr) Space added in v0.2.0

func (v *IndexExpr) Space() SpaceType

func (*IndexExpr) Start added in v0.2.0

func (v *IndexExpr) Start() *Decorations

type IndexExprDecorations

type IndexExprDecorations struct {
	Space  SpaceType
	Start  Decorations
	X      Decorations
	Lbrack Decorations
	Index  Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *InterfaceType) After() SpaceType

func (*InterfaceType) End added in v0.2.0

func (v *InterfaceType) End() *Decorations

func (*InterfaceType) SetAfter added in v0.2.0

func (v *InterfaceType) SetAfter(s SpaceType)

func (*InterfaceType) SetSpace added in v0.2.0

func (v *InterfaceType) SetSpace(s SpaceType)

func (*InterfaceType) Space added in v0.2.0

func (v *InterfaceType) Space() SpaceType

func (*InterfaceType) Start added in v0.2.0

func (v *InterfaceType) Start() *Decorations

type InterfaceTypeDecorations

type InterfaceTypeDecorations struct {
	Space     SpaceType
	Start     Decorations
	Interface Decorations
	End       Decorations
	After     SpaceType
}

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) After added in v0.2.0

func (v *KeyValueExpr) After() SpaceType

func (*KeyValueExpr) End added in v0.2.0

func (v *KeyValueExpr) End() *Decorations

func (*KeyValueExpr) SetAfter added in v0.2.0

func (v *KeyValueExpr) SetAfter(s SpaceType)

func (*KeyValueExpr) SetSpace added in v0.2.0

func (v *KeyValueExpr) SetSpace(s SpaceType)

func (*KeyValueExpr) Space added in v0.2.0

func (v *KeyValueExpr) Space() SpaceType

func (*KeyValueExpr) Start added in v0.2.0

func (v *KeyValueExpr) Start() *Decorations

type KeyValueExprDecorations

type KeyValueExprDecorations struct {
	Space SpaceType
	Start Decorations
	Key   Decorations
	Colon Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *LabeledStmt) After() SpaceType

func (*LabeledStmt) End added in v0.2.0

func (v *LabeledStmt) End() *Decorations

func (*LabeledStmt) SetAfter added in v0.2.0

func (v *LabeledStmt) SetAfter(s SpaceType)

func (*LabeledStmt) SetSpace added in v0.2.0

func (v *LabeledStmt) SetSpace(s SpaceType)

func (*LabeledStmt) Space added in v0.2.0

func (v *LabeledStmt) Space() SpaceType

func (*LabeledStmt) Start added in v0.2.0

func (v *LabeledStmt) Start() *Decorations

type LabeledStmtDecorations

type LabeledStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Label Decorations
	Colon Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *MapType) After() SpaceType

func (*MapType) End added in v0.2.0

func (v *MapType) End() *Decorations

func (*MapType) SetAfter added in v0.2.0

func (v *MapType) SetAfter(s SpaceType)

func (*MapType) SetSpace added in v0.2.0

func (v *MapType) SetSpace(s SpaceType)

func (*MapType) Space added in v0.2.0

func (v *MapType) Space() SpaceType

func (*MapType) Start added in v0.2.0

func (v *MapType) Start() *Decorations

type MapTypeDecorations

type MapTypeDecorations struct {
	Space SpaceType
	Start Decorations
	Map   Decorations
	Key   Decorations
	End   Decorations
	After SpaceType
}

MapTypeDecorations holds decorations for MapType:

type V /*Start*/ map[ /*Map*/ int] /*Key*/ int /*End*/

type Node

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

All node types implement the Node interface.

type ObjKind

type ObjKind int

ObjKind describes what an object represents.

const (
	Bad ObjKind = iota // for error handling
	Pkg                // package
	Con                // constant
	Typ                // type
	Var                // variable
	Fun                // function or method
	Lbl                // label
)

The list of possible Object kinds.

func (ObjKind) String

func (kind ObjKind) String() string

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 NewObj

func NewObj(kind ObjKind, name string) *Object

NewObj creates a new object of a given kind and name.

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.

type PackageDecorations added in v0.1.0

type PackageDecorations struct {
	Space SpaceType
	After SpaceType
}

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) After added in v0.2.0

func (v *ParenExpr) After() SpaceType

func (*ParenExpr) End added in v0.2.0

func (v *ParenExpr) End() *Decorations

func (*ParenExpr) SetAfter added in v0.2.0

func (v *ParenExpr) SetAfter(s SpaceType)

func (*ParenExpr) SetSpace added in v0.2.0

func (v *ParenExpr) SetSpace(s SpaceType)

func (*ParenExpr) Space added in v0.2.0

func (v *ParenExpr) Space() SpaceType

func (*ParenExpr) Start added in v0.2.0

func (v *ParenExpr) Start() *Decorations

type ParenExprDecorations

type ParenExprDecorations struct {
	Space  SpaceType
	Start  Decorations
	Lparen Decorations
	X      Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *RangeStmt) After() SpaceType

func (*RangeStmt) End added in v0.2.0

func (v *RangeStmt) End() *Decorations

func (*RangeStmt) SetAfter added in v0.2.0

func (v *RangeStmt) SetAfter(s SpaceType)

func (*RangeStmt) SetSpace added in v0.2.0

func (v *RangeStmt) SetSpace(s SpaceType)

func (*RangeStmt) Space added in v0.2.0

func (v *RangeStmt) Space() SpaceType

func (*RangeStmt) Start added in v0.2.0

func (v *RangeStmt) Start() *Decorations

type RangeStmtDecorations

type RangeStmtDecorations struct {
	Space SpaceType
	Start Decorations
	For   Decorations
	Key   Decorations
	Value Decorations
	Range Decorations
	X     Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *ReturnStmt) After() SpaceType

func (*ReturnStmt) End added in v0.2.0

func (v *ReturnStmt) End() *Decorations

func (*ReturnStmt) SetAfter added in v0.2.0

func (v *ReturnStmt) SetAfter(s SpaceType)

func (*ReturnStmt) SetSpace added in v0.2.0

func (v *ReturnStmt) SetSpace(s SpaceType)

func (*ReturnStmt) Space added in v0.2.0

func (v *ReturnStmt) Space() SpaceType

func (*ReturnStmt) Start added in v0.2.0

func (v *ReturnStmt) Start() *Decorations

type ReturnStmtDecorations

type ReturnStmtDecorations struct {
	Space  SpaceType
	Start  Decorations
	Return Decorations
	End    Decorations
	After  SpaceType
}

ReturnStmtDecorations holds decorations for ReturnStmt:

func() int {
	/*Start*/ return /*Return*/ 1 /*End*/
}()

type Scope

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

A Scope maintains the set of named language entities declared 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) Insert

func (s *Scope) Insert(obj *Object) (alt *Object)

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.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) *Object

Lookup returns the object with the given name if it is found in scope s, otherwise it returns nil. Outer scopes are ignored.

func (*Scope) String

func (s *Scope) String() string

Debugging support

type SelectStmt

type SelectStmt struct {
	Body *BlockStmt // CommClauses only
	Decs SelectStmtDecorations
}

An SelectStmt node represents a select statement.

func (*SelectStmt) After added in v0.2.0

func (v *SelectStmt) After() SpaceType

func (*SelectStmt) End added in v0.2.0

func (v *SelectStmt) End() *Decorations

func (*SelectStmt) SetAfter added in v0.2.0

func (v *SelectStmt) SetAfter(s SpaceType)

func (*SelectStmt) SetSpace added in v0.2.0

func (v *SelectStmt) SetSpace(s SpaceType)

func (*SelectStmt) Space added in v0.2.0

func (v *SelectStmt) Space() SpaceType

func (*SelectStmt) Start added in v0.2.0

func (v *SelectStmt) Start() *Decorations

type SelectStmtDecorations

type SelectStmtDecorations struct {
	Space  SpaceType
	Start  Decorations
	Select Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *SelectorExpr) After() SpaceType

func (*SelectorExpr) End added in v0.2.0

func (v *SelectorExpr) End() *Decorations

func (*SelectorExpr) SetAfter added in v0.2.0

func (v *SelectorExpr) SetAfter(s SpaceType)

func (*SelectorExpr) SetSpace added in v0.2.0

func (v *SelectorExpr) SetSpace(s SpaceType)

func (*SelectorExpr) Space added in v0.2.0

func (v *SelectorExpr) Space() SpaceType

func (*SelectorExpr) Start added in v0.2.0

func (v *SelectorExpr) Start() *Decorations

type SelectorExprDecorations

type SelectorExprDecorations struct {
	Space SpaceType
	Start Decorations
	X     Decorations
	End   Decorations
	After SpaceType
}

SelectorExprDecorations holds decorations for SelectorExpr:

var F = /*Start*/ fmt. /*X*/ Sprint /*End*/ (0)

type SendStmt

type SendStmt struct {
	Chan  Expr
	Value Expr
	Decs  SendStmtDecorations
}

A SendStmt node represents a send statement.

func (*SendStmt) After added in v0.2.0

func (v *SendStmt) After() SpaceType

func (*SendStmt) End added in v0.2.0

func (v *SendStmt) End() *Decorations

func (*SendStmt) SetAfter added in v0.2.0

func (v *SendStmt) SetAfter(s SpaceType)

func (*SendStmt) SetSpace added in v0.2.0

func (v *SendStmt) SetSpace(s SpaceType)

func (*SendStmt) Space added in v0.2.0

func (v *SendStmt) Space() SpaceType

func (*SendStmt) Start added in v0.2.0

func (v *SendStmt) Start() *Decorations

type SendStmtDecorations

type SendStmtDecorations struct {
	Space SpaceType
	Start Decorations
	Chan  Decorations
	Arrow Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *SliceExpr) After() SpaceType

func (*SliceExpr) End added in v0.2.0

func (v *SliceExpr) End() *Decorations

func (*SliceExpr) SetAfter added in v0.2.0

func (v *SliceExpr) SetAfter(s SpaceType)

func (*SliceExpr) SetSpace added in v0.2.0

func (v *SliceExpr) SetSpace(s SpaceType)

func (*SliceExpr) Space added in v0.2.0

func (v *SliceExpr) Space() SpaceType

func (*SliceExpr) Start added in v0.2.0

func (v *SliceExpr) Start() *Decorations

type SliceExprDecorations

type SliceExprDecorations struct {
	Space  SpaceType
	Start  Decorations
	X      Decorations
	Lbrack Decorations
	Low    Decorations
	High   Decorations
	Max    Decorations
	End    Decorations
	After  SpaceType
}

SliceExprDecorations holds decorations for SliceExpr:

var H = /*Start*/ []int{0} /*X*/ [ /*Lbrack*/ 1: /*Low*/ 2: /*High*/ 3 /*Max*/] /*End*/

var H1 = /*Start*/ []int{0} /*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} /*X*/ [: /*Low*/ 2 /*High*/] /*End*/

var H5 = /*Start*/ []int{0} /*X*/ [: /*Low*/ 2: /*High*/ 3 /*Max*/] /*End*/

type SpaceType added in v0.1.0

type SpaceType int
const (
	None      SpaceType = 0
	NewLine   SpaceType = 1
	EmptyLine SpaceType = 2
)

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) After added in v0.2.0

func (v *StarExpr) After() SpaceType

func (*StarExpr) End added in v0.2.0

func (v *StarExpr) End() *Decorations

func (*StarExpr) SetAfter added in v0.2.0

func (v *StarExpr) SetAfter(s SpaceType)

func (*StarExpr) SetSpace added in v0.2.0

func (v *StarExpr) SetSpace(s SpaceType)

func (*StarExpr) Space added in v0.2.0

func (v *StarExpr) Space() SpaceType

func (*StarExpr) Start added in v0.2.0

func (v *StarExpr) Start() *Decorations

type StarExprDecorations

type StarExprDecorations struct {
	Space SpaceType
	Start Decorations
	Star  Decorations
	End   Decorations
	After SpaceType
}

StarExprDecorations holds decorations for StarExpr:

var N = /*Start*/ * /*Star*/ p /*End*/

type Stmt

type Stmt interface {
	Node
	Decorated
	// 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) After added in v0.2.0

func (v *StructType) After() SpaceType

func (*StructType) End added in v0.2.0

func (v *StructType) End() *Decorations

func (*StructType) SetAfter added in v0.2.0

func (v *StructType) SetAfter(s SpaceType)

func (*StructType) SetSpace added in v0.2.0

func (v *StructType) SetSpace(s SpaceType)

func (*StructType) Space added in v0.2.0

func (v *StructType) Space() SpaceType

func (*StructType) Start added in v0.2.0

func (v *StructType) Start() *Decorations

type StructTypeDecorations

type StructTypeDecorations struct {
	Space  SpaceType
	Start  Decorations
	Struct Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *SwitchStmt) After() SpaceType

func (*SwitchStmt) End added in v0.2.0

func (v *SwitchStmt) End() *Decorations

func (*SwitchStmt) SetAfter added in v0.2.0

func (v *SwitchStmt) SetAfter(s SpaceType)

func (*SwitchStmt) SetSpace added in v0.2.0

func (v *SwitchStmt) SetSpace(s SpaceType)

func (*SwitchStmt) Space added in v0.2.0

func (v *SwitchStmt) Space() SpaceType

func (*SwitchStmt) Start added in v0.2.0

func (v *SwitchStmt) Start() *Decorations

type SwitchStmtDecorations

type SwitchStmtDecorations struct {
	Space  SpaceType
	Start  Decorations
	Switch Decorations
	Init   Decorations
	Tag    Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *TypeAssertExpr) After() SpaceType

func (*TypeAssertExpr) End added in v0.2.0

func (v *TypeAssertExpr) End() *Decorations

func (*TypeAssertExpr) SetAfter added in v0.2.0

func (v *TypeAssertExpr) SetAfter(s SpaceType)

func (*TypeAssertExpr) SetSpace added in v0.2.0

func (v *TypeAssertExpr) SetSpace(s SpaceType)

func (*TypeAssertExpr) Space added in v0.2.0

func (v *TypeAssertExpr) Space() SpaceType

func (*TypeAssertExpr) Start added in v0.2.0

func (v *TypeAssertExpr) Start() *Decorations

type TypeAssertExprDecorations

type TypeAssertExprDecorations struct {
	Space  SpaceType
	Start  Decorations
	X      Decorations
	Lparen Decorations
	Type   Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *TypeSpec) After() SpaceType

func (*TypeSpec) End added in v0.2.0

func (v *TypeSpec) End() *Decorations

func (*TypeSpec) SetAfter added in v0.2.0

func (v *TypeSpec) SetAfter(s SpaceType)

func (*TypeSpec) SetSpace added in v0.2.0

func (v *TypeSpec) SetSpace(s SpaceType)

func (*TypeSpec) Space added in v0.2.0

func (v *TypeSpec) Space() SpaceType

func (*TypeSpec) Start added in v0.2.0

func (v *TypeSpec) Start() *Decorations

type TypeSpecDecorations

type TypeSpecDecorations struct {
	Space SpaceType
	Start Decorations
	Name  Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *TypeSwitchStmt) After() SpaceType

func (*TypeSwitchStmt) End added in v0.2.0

func (v *TypeSwitchStmt) End() *Decorations

func (*TypeSwitchStmt) SetAfter added in v0.2.0

func (v *TypeSwitchStmt) SetAfter(s SpaceType)

func (*TypeSwitchStmt) SetSpace added in v0.2.0

func (v *TypeSwitchStmt) SetSpace(s SpaceType)

func (*TypeSwitchStmt) Space added in v0.2.0

func (v *TypeSwitchStmt) Space() SpaceType

func (*TypeSwitchStmt) Start added in v0.2.0

func (v *TypeSwitchStmt) Start() *Decorations

type TypeSwitchStmtDecorations

type TypeSwitchStmtDecorations struct {
	Space  SpaceType
	Start  Decorations
	Switch Decorations
	Init   Decorations
	Assign Decorations
	End    Decorations
	After  SpaceType
}

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) After added in v0.2.0

func (v *UnaryExpr) After() SpaceType

func (*UnaryExpr) End added in v0.2.0

func (v *UnaryExpr) End() *Decorations

func (*UnaryExpr) SetAfter added in v0.2.0

func (v *UnaryExpr) SetAfter(s SpaceType)

func (*UnaryExpr) SetSpace added in v0.2.0

func (v *UnaryExpr) SetSpace(s SpaceType)

func (*UnaryExpr) Space added in v0.2.0

func (v *UnaryExpr) Space() SpaceType

func (*UnaryExpr) Start added in v0.2.0

func (v *UnaryExpr) Start() *Decorations

type UnaryExprDecorations

type UnaryExprDecorations struct {
	Space SpaceType
	Start Decorations
	Op    Decorations
	End   Decorations
	After SpaceType
}

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) After added in v0.2.0

func (v *ValueSpec) After() SpaceType

func (*ValueSpec) End added in v0.2.0

func (v *ValueSpec) End() *Decorations

func (*ValueSpec) SetAfter added in v0.2.0

func (v *ValueSpec) SetAfter(s SpaceType)

func (*ValueSpec) SetSpace added in v0.2.0

func (v *ValueSpec) SetSpace(s SpaceType)

func (*ValueSpec) Space added in v0.2.0

func (v *ValueSpec) Space() SpaceType

func (*ValueSpec) Start added in v0.2.0

func (v *ValueSpec) Start() *Decorations

type ValueSpecDecorations

type ValueSpecDecorations struct {
	Space  SpaceType
	Start  Decorations
	Names  Decorations
	Assign Decorations
	End    Decorations
	After  SpaceType
}

ValueSpecDecorations holds decorations for ValueSpec:

var (
	/*Start*/ j = /*Assign*/ 1 /*End*/
)

var (
	/*Start*/ k, l = /*Assign*/ 1, 2 /*End*/
)

var (
	/*Start*/ m, n /*Names*/ int = /*Assign*/ 1, 2 /*End*/
)

type Visitor

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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