-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
parse.go
42 lines (38 loc) · 998 Bytes
/
parse.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
package interpolate
import (
"fmt"
"text/template"
"text/template/parse"
)
// functionsCalled returns a map (to be used as a set) of the functions
// that are called from the given text template.
func functionsCalled(t *template.Template) map[string]struct{} {
result := make(map[string]struct{})
functionsCalledWalk(t.Tree.Root, result)
return result
}
func functionsCalledWalk(raw parse.Node, r map[string]struct{}) {
switch node := raw.(type) {
case *parse.ActionNode:
functionsCalledWalk(node.Pipe, r)
case *parse.CommandNode:
if in, ok := node.Args[0].(*parse.IdentifierNode); ok {
r[in.Ident] = struct{}{}
}
for _, n := range node.Args[1:] {
functionsCalledWalk(n, r)
}
case *parse.ListNode:
for _, n := range node.Nodes {
functionsCalledWalk(n, r)
}
case *parse.PipeNode:
for _, n := range node.Cmds {
functionsCalledWalk(n, r)
}
case *parse.StringNode, *parse.TextNode:
// Ignore
default:
panic(fmt.Sprintf("unknown type: %T", node))
}
}