aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/walk.go
blob: 90f81c9c1c1333af6227b1e2f2ddd5276fd8b3e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package hclsyntax

import (
	"github.com/hashicorp/hcl2/hcl"
)

// VisitFunc is the callback signature for VisitAll.
type VisitFunc func(node Node) hcl.Diagnostics

// VisitAll is a basic way to traverse the AST beginning with a particular
// node. The given function will be called once for each AST node in
// depth-first order, but no context is provided about the shape of the tree.
//
// The VisitFunc may return diagnostics, in which case they will be accumulated
// and returned as a single set.
func VisitAll(node Node, f VisitFunc) hcl.Diagnostics {
	diags := f(node)
	node.walkChildNodes(func(node Node) {
		diags = append(diags, VisitAll(node, f)...)
	})
	return diags
}

// Walker is an interface used with Walk.
type Walker interface {
	Enter(node Node) hcl.Diagnostics
	Exit(node Node) hcl.Diagnostics
}

// Walk is a more complex way to traverse the AST starting with a particular
// node, which provides information about the tree structure via separate
// Enter and Exit functions.
func Walk(node Node, w Walker) hcl.Diagnostics {
	diags := w.Enter(node)
	node.walkChildNodes(func(node Node) {
		diags = append(diags, Walk(node, w)...)
	})
	moreDiags := w.Exit(node)
	diags = append(diags, moreDiags...)
	return diags
}