github.com/franklinhu/terraform@v0.6.9-0.20151202232446-81f7fb1e6f9e/config/lang/ast/ast.go (about) 1 package ast 2 3 import ( 4 "fmt" 5 ) 6 7 // Node is the interface that all AST nodes must implement. 8 type Node interface { 9 // Accept is called to dispatch to the visitors. It must return the 10 // resulting Node (which might be different in an AST transform). 11 Accept(Visitor) Node 12 13 // Pos returns the position of this node in some source. 14 Pos() Pos 15 16 // Type returns the type of this node for the given context. 17 Type(Scope) (Type, error) 18 } 19 20 // Pos is the starting position of an AST node 21 type Pos struct { 22 Column, Line int // Column/Line number, starting at 1 23 } 24 25 func (p Pos) String() string { 26 return fmt.Sprintf("%d:%d", p.Line, p.Column) 27 } 28 29 // Visitors are just implementations of this function. 30 // 31 // The function must return the Node to replace this node with. "nil" is 32 // _not_ a valid return value. If there is no replacement, the original node 33 // should be returned. We build this replacement directly into the visitor 34 // pattern since AST transformations are a common and useful tool and 35 // building it into the AST itself makes it required for future Node 36 // implementations and very easy to do. 37 // 38 // Note that this isn't a true implementation of the visitor pattern, which 39 // generally requires proper type dispatch on the function. However, 40 // implementing this basic visitor pattern style is still very useful even 41 // if you have to type switch. 42 type Visitor func(Node) Node 43 44 //go:generate stringer -type=Type 45 46 // Type is the type of any value. 47 type Type uint32 48 49 const ( 50 TypeInvalid Type = 0 51 TypeAny Type = 1 << iota 52 TypeString 53 TypeInt 54 TypeFloat 55 )