4 "github.com/hashicorp/hcl2/hcl"
7 // VisitFunc is the callback signature for VisitAll.
8 type VisitFunc func(node Node) hcl.Diagnostics
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.
14 // The VisitFunc may return diagnostics, in which case they will be accumulated
15 // and returned as a single set.
16 func VisitAll(node Node, f VisitFunc) hcl.Diagnostics {
18 node.walkChildNodes(func(node Node) {
19 diags = append(diags, VisitAll(node, f)...)
24 // Walker is an interface used with Walk.
25 type Walker interface {
26 Enter(node Node) hcl.Diagnostics
27 Exit(node Node) hcl.Diagnostics
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.
33 func 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)...)
38 moreDiags := w.Exit(node)
39 diags = append(diags, moreDiags...)