2017-09-11 23:40:37 +00:00
|
|
|
package hclsyntax
|
2017-05-23 15:05:44 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-05-23 15:05:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// VisitFunc is the callback signature for VisitAll.
|
2017-09-11 23:40:37 +00:00
|
|
|
type VisitFunc func(node Node) hcl.Diagnostics
|
2017-05-23 15:05:44 +00:00
|
|
|
|
|
|
|
// 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.
|
2017-09-11 23:40:37 +00:00
|
|
|
func VisitAll(node Node, f VisitFunc) hcl.Diagnostics {
|
2017-05-23 15:05:44 +00:00
|
|
|
diags := f(node)
|
2018-09-26 14:38:43 +00:00
|
|
|
node.walkChildNodes(func(node Node) {
|
2017-05-23 15:05:44 +00:00
|
|
|
diags = append(diags, VisitAll(node, f)...)
|
|
|
|
})
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walker is an interface used with Walk.
|
|
|
|
type Walker interface {
|
2017-09-11 23:40:37 +00:00
|
|
|
Enter(node Node) hcl.Diagnostics
|
|
|
|
Exit(node Node) hcl.Diagnostics
|
2017-05-23 15:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-09-11 23:40:37 +00:00
|
|
|
func Walk(node Node, w Walker) hcl.Diagnostics {
|
2017-05-23 15:05:44 +00:00
|
|
|
diags := w.Enter(node)
|
2018-09-26 14:38:43 +00:00
|
|
|
node.walkChildNodes(func(node Node) {
|
2017-05-23 15:05:44 +00:00
|
|
|
diags = append(diags, Walk(node, w)...)
|
|
|
|
})
|
2018-09-25 15:23:11 +00:00
|
|
|
moreDiags := w.Exit(node)
|
|
|
|
diags = append(diags, moreDiags...)
|
2017-05-23 15:05:44 +00:00
|
|
|
return diags
|
|
|
|
}
|