]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package hclsyntax |
2 | ||
3 | import ( | |
4 | "github.com/hashicorp/hcl2/hcl" | |
5 | ) | |
6 | ||
7 | // VisitFunc is the callback signature for VisitAll. | |
8 | type 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. | |
16 | func VisitAll(node Node, f VisitFunc) hcl.Diagnostics { | |
17 | diags := f(node) | |
107c1cdb | 18 | node.walkChildNodes(func(node Node) { |
15c0b25d | 19 | diags = append(diags, VisitAll(node, f)...) |
15c0b25d AP |
20 | }) |
21 | return diags | |
22 | } | |
23 | ||
24 | // Walker is an interface used with Walk. | |
25 | type 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. | |
33 | func Walk(node Node, w Walker) hcl.Diagnostics { | |
34 | diags := w.Enter(node) | |
107c1cdb | 35 | node.walkChildNodes(func(node Node) { |
15c0b25d | 36 | diags = append(diags, Walk(node, w)...) |
15c0b25d | 37 | }) |
107c1cdb ND |
38 | moreDiags := w.Exit(node) |
39 | diags = append(diags, moreDiags...) | |
15c0b25d AP |
40 | return diags |
41 | } |