1package hclsyntax 2 3import ( 4 "github.com/hashicorp/hcl2/hcl" 5) 6 7// VisitFunc is the callback signature for VisitAll. 8type VisitFunc func(node Node) hcl.Diagnostics 9 10// VisitAll is a basic way to traverse the AST beginning with a particular 11// node. The given function will be called once for each AST node in 12// depth-first order, but no context is provided about the shape of the tree. 13// 14// The VisitFunc may return diagnostics, in which case they will be accumulated 15// and returned as a single set. 16func VisitAll(node Node, f VisitFunc) hcl.Diagnostics { 17 diags := f(node) 18 node.walkChildNodes(func(node Node) { 19 diags = append(diags, VisitAll(node, f)...) 20 }) 21 return diags 22} 23 24// Walker is an interface used with Walk. 25type Walker interface { 26 Enter(node Node) hcl.Diagnostics 27 Exit(node Node) hcl.Diagnostics 28} 29 30// Walk is a more complex way to traverse the AST starting with a particular 31// node, which provides information about the tree structure via separate 32// Enter and Exit functions. 33func Walk(node Node, w Walker) hcl.Diagnostics { 34 diags := w.Enter(node) 35 node.walkChildNodes(func(node Node) { 36 diags = append(diags, Walk(node, w)...) 37 }) 38 moreDiags := w.Exit(node) 39 diags = append(diags, moreDiags...) 40 return diags 41} 42