diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go new file mode 100644 index 0000000..3405d26 --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go | |||
@@ -0,0 +1,77 @@ | |||
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) | ||
18 | node.walkChildNodes(func(node Node) Node { | ||
19 | diags = append(diags, VisitAll(node, f)...) | ||
20 | return node | ||
21 | }) | ||
22 | return diags | ||
23 | } | ||
24 | |||
25 | // Walker is an interface used with Walk. | ||
26 | type Walker interface { | ||
27 | Enter(node Node) hcl.Diagnostics | ||
28 | Exit(node Node) hcl.Diagnostics | ||
29 | } | ||
30 | |||
31 | // Walk is a more complex way to traverse the AST starting with a particular | ||
32 | // node, which provides information about the tree structure via separate | ||
33 | // Enter and Exit functions. | ||
34 | func Walk(node Node, w Walker) hcl.Diagnostics { | ||
35 | diags := w.Enter(node) | ||
36 | node.walkChildNodes(func(node Node) Node { | ||
37 | diags = append(diags, Walk(node, w)...) | ||
38 | return node | ||
39 | }) | ||
40 | return diags | ||
41 | } | ||
42 | |||
43 | // Transformer is an interface used with Transform | ||
44 | type Transformer interface { | ||
45 | // Transform accepts a node and returns a replacement node along with | ||
46 | // a flag for whether to also visit child nodes. If the flag is false, | ||
47 | // none of the child nodes will be visited and the TransformExit method | ||
48 | // will not be called for the node. | ||
49 | // | ||
50 | // It is acceptable and appropriate for Transform to return the same node | ||
51 | // it was given, for situations where no transform is needed. | ||
52 | Transform(node Node) (Node, bool, hcl.Diagnostics) | ||
53 | |||
54 | // TransformExit signals the end of transformations of child nodes of the | ||
55 | // given node. If Transform returned a new node, the given node is the | ||
56 | // node that was returned, rather than the node that was originally | ||
57 | // encountered. | ||
58 | TransformExit(node Node) hcl.Diagnostics | ||
59 | } | ||
60 | |||
61 | // Transform allows for in-place transformations of an AST starting with a | ||
62 | // particular node. The provider Transformer implementation drives the | ||
63 | // transformation process. The return value is the node that replaced the | ||
64 | // given top-level node. | ||
65 | func Transform(node Node, t Transformer) (Node, hcl.Diagnostics) { | ||
66 | newNode, descend, diags := t.Transform(node) | ||
67 | if !descend { | ||
68 | return newNode, diags | ||
69 | } | ||
70 | node.walkChildNodes(func(node Node) Node { | ||
71 | newNode, newDiags := Transform(node, t) | ||
72 | diags = append(diags, newDiags...) | ||
73 | return newNode | ||
74 | }) | ||
75 | diags = append(diags, t.TransformExit(newNode)...) | ||
76 | return newNode, diags | ||
77 | } | ||