aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go')
-rw-r--r--vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go44
1 files changed, 4 insertions, 40 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go
index 3405d26..90f81c9 100644
--- a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go
+++ b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go
@@ -15,9 +15,8 @@ type VisitFunc func(node Node) hcl.Diagnostics
15// and returned as a single set. 15// and returned as a single set.
16func VisitAll(node Node, f VisitFunc) hcl.Diagnostics { 16func VisitAll(node Node, f VisitFunc) hcl.Diagnostics {
17 diags := f(node) 17 diags := f(node)
18 node.walkChildNodes(func(node Node) Node { 18 node.walkChildNodes(func(node Node) {
19 diags = append(diags, VisitAll(node, f)...) 19 diags = append(diags, VisitAll(node, f)...)
20 return node
21 }) 20 })
22 return diags 21 return diags
23} 22}
@@ -33,45 +32,10 @@ type Walker interface {
33// Enter and Exit functions. 32// Enter and Exit functions.
34func Walk(node Node, w Walker) hcl.Diagnostics { 33func Walk(node Node, w Walker) hcl.Diagnostics {
35 diags := w.Enter(node) 34 diags := w.Enter(node)
36 node.walkChildNodes(func(node Node) Node { 35 node.walkChildNodes(func(node Node) {
37 diags = append(diags, Walk(node, w)...) 36 diags = append(diags, Walk(node, w)...)
38 return node
39 }) 37 })
38 moreDiags := w.Exit(node)
39 diags = append(diags, moreDiags...)
40 return diags 40 return diags
41} 41}
42
43// Transformer is an interface used with Transform
44type 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.
65func 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}