/
include.go
123 lines (109 loc) · 2.83 KB
/
include.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package expr
import (
"fmt"
"github.com/lytics/qlbridge/lex"
)
const (
maxIncludeDepth = 100
)
var (
// If we hit max depth
ErrMaxDepth = fmt.Errorf("Recursive Evaluation Error")
)
// FindIncludes recursively descend down a node looking for all Include identities
func FindIncludes(node Node) []string {
return findAllIncludes(node, nil)
}
// InlineIncludes take an expression and resolve any includes so that
// the included expression is "Inline"
func InlineIncludes(ctx Includer, n Node) (Node, error) {
return doInlineIncludes(ctx, n, 0)
}
func doInlineIncludes(ctx Includer, n Node, depth int) (Node, error) {
// We need to make a copy, so we lazily use the To/From pb
// We need the copy because we are going to mutate this node
// but AST is assumed to be immuteable, and shared, since we are breaking
// this contract we copy
npb := n.NodePb()
newNode := NodeFromNodePb(npb)
return inlineIncludesDepth(ctx, newNode, depth)
}
func inlineIncludesDepth(ctx Includer, arg Node, depth int) (Node, error) {
if depth > maxIncludeDepth {
return nil, ErrMaxDepth
}
switch n := arg.(type) {
// FuncNode, BinaryNode, BooleanNode, TriNode, UnaryNode, ArrayNode
case NodeArgs:
args := n.ChildrenArgs()
for i, narg := range args {
newNode, err := inlineIncludesDepth(ctx, narg, depth+1)
if err != nil {
return nil, err
}
if newNode != nil {
args[i] = newNode
}
}
return arg, nil
case *IncludeNode:
return resolveInclude(ctx, n, depth+1)
default:
//*NumberNode, *IdentityNode, *StringNode, nil,
//*ValueNode, *NullNode:
return arg, nil
}
}
func resolveInclude(ctx Includer, inc *IncludeNode, depth int) (Node, error) {
// if inc.inlineExpr != nil {
// return inc.inlineExpr, nil
// }
n, err := ctx.Include(inc.Identity.Text)
if err != nil {
return nil, err
}
if n == nil {
return nil, ErrIncludeNotFound
}
// Now inline, the inlines
n, err = doInlineIncludes(ctx, n, depth)
if err != nil {
return nil, err
}
if inc.Negated() {
inc.inlineExpr = NewUnary(lex.Token{T: lex.TokenNegate, V: "NOT"}, n)
} else {
inc.inlineExpr = n
}
inc.ExprNode = inc.inlineExpr
return inc.inlineExpr, nil
}
func findAllIncludes(node Node, current []string) []string {
switch n := node.(type) {
case *IncludeNode:
current = append(current, n.Identity.Text)
case *BinaryNode:
for _, arg := range n.Args {
current = findAllIncludes(arg, current)
}
case *BooleanNode:
for _, arg := range n.Args {
current = findAllIncludes(arg, current)
}
case *UnaryNode:
current = findAllIncludes(n.Arg, current)
case *TriNode:
for _, arg := range n.Args {
current = findAllIncludes(arg, current)
}
case *ArrayNode:
for _, arg := range n.Args {
current = findAllIncludes(arg, current)
}
case *FuncNode:
for _, arg := range n.Args {
current = findAllIncludes(arg, current)
}
}
return current
}