forked from square/grange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
118 lines (97 loc) · 2.47 KB
/
parser.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
package grange
func (r *rangeQuery) popNode() parserNode {
l := len(r.nodeStack)
result := r.nodeStack[l-1]
r.nodeStack = r.nodeStack[:l-1]
return result
}
func (r *rangeQuery) pushNode(node parserNode) {
r.nodeStack = append(r.nodeStack, node)
}
func (r *rangeQuery) addValue(val string) {
r.pushNode(nodeText{val})
}
func (r *rangeQuery) addConstant(val string) {
r.pushNode(nodeConstant{val})
}
func (r *rangeQuery) addNull() {
r.pushNode(nodeNull{})
}
func (r *rangeQuery) addBraceStart() {
r.pushNode(nodeBraceStart{})
}
func (r *rangeQuery) addFuncArg() {
var funcNode parserNode
paramNode := r.popNode()
switch paramNode.(type) {
case nodeFunction:
// No arguments. This is kind of terrible, probably a better way to do
// this.
r.pushNode(paramNode)
default:
funcNode = r.nodeStack[len(r.nodeStack)-1]
fn := funcNode.(nodeFunction)
fn.params = append(fn.params, paramNode)
r.nodeStack[len(r.nodeStack)-1] = fn
}
}
func (r *rangeQuery) addBraces() {
right := r.popNode()
node := r.popNode()
var left parserNode
left = nodeNull{}
// This is kind of bullshit but not sure a better way to do it yet
switch node.(type) {
case nodeBraceStart:
node = nodeNull{}
default:
if len(r.nodeStack) > 0 {
left = r.popNode()
switch left.(type) {
case nodeBraceStart:
left = nodeNull{}
}
}
}
r.pushNode(nodeBraces{node, left, right})
}
func (r *rangeQuery) addGroupLookup() {
exprNode := r.popNode()
r.pushNode(nodeClusterLookup{nodeConstant{"GROUPS"}, exprNode})
}
func (r *rangeQuery) addGroupQuery() {
exprNode := r.popNode()
r.pushNode(nodeGroupQuery{exprNode})
}
func (r *rangeQuery) addLocalClusterLookup(key string) {
r.pushNode(nodeLocalClusterLookup{key})
}
func (r *rangeQuery) addFunction(name string) {
r.pushNode(nodeFunction{name, []parserNode{}})
}
func (r *rangeQuery) addClusterLookup() {
exprNode := r.popNode()
r.pushNode(nodeClusterLookup{exprNode, nodeConstant{"CLUSTER"}})
}
func (r *rangeQuery) addRegex(val string) {
r.pushNode(nodeRegexp{val})
}
func (r *rangeQuery) addKeyLookup() {
keyNode := r.popNode()
// TODO: Error out if no lookup
if len(r.nodeStack) > 0 {
lookupNode := r.popNode()
switch lookupNode.(type) {
case nodeClusterLookup:
n := lookupNode.(nodeClusterLookup)
n.key = keyNode
r.pushNode(n)
// TODO: Error out if wrong node type
}
}
}
func (r *rangeQuery) addOperator(typ operatorType) {
right := r.popNode()
left := r.popNode()
r.pushNode(nodeOperator{typ, left, right})
}