-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.go
69 lines (62 loc) · 1.33 KB
/
dfs.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package eyaml
import (
"github.com/goccy/go-yaml/ast"
)
type addressedLiteral struct{
path string
node ast.Node
}
type YamlLiterals struct {
listByDFS []addressedLiteral
}
func DfsSequence(root ast.Node) *YamlLiterals {
nodes := &YamlLiterals{
listByDFS: make([]addressedLiteral, 0),
}
nodes.DFS(root)
return nodes
}
func (i *YamlLiterals) List() []addressedLiteral {
return i.listByDFS
}
func (i *YamlLiterals) DFS(node ast.Node) {
switch nodeType := node.(type) {
case *ast.MappingNode:
for _, subnode := range nodeType.Values {
i.dfs(subnode)
}
case *ast.MappingValueNode:
i.dfs(nodeType.Value)
case *ast.SequenceNode:
for _, subnode := range nodeType.Values {
i.dfs(subnode)
}
}
return
}
func (i *YamlLiterals) dfs(node ast.Node) {
switch nodeType := node.(type) {
case *ast.MappingValueNode:
i.dfs(nodeType.Value)
case *ast.MappingNode:
for _, subnode := range nodeType.Values {
i.dfs(subnode)
}
case *ast.SequenceNode:
for _, subnode := range nodeType.Values {
i.dfs(subnode)
}
case *ast.LiteralNode:
// LiteralNode.Value points to a StringNode
i.listByDFS = append(i.listByDFS, addressedLiteral{
path: nodeType.GetPath(),
node: nodeType,
})
case *ast.StringNode:
i.listByDFS = append(i.listByDFS, addressedLiteral{
path: nodeType.GetPath(),
node: nodeType,
})
}
return
}