-
Notifications
You must be signed in to change notification settings - Fork 5
/
expression.go
262 lines (228 loc) · 4.83 KB
/
expression.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package veldt
import (
"fmt"
)
type expressionParser struct {
pipeline *Pipeline
}
func newExpressionParser(pipeline *Pipeline) *expressionParser {
return &expressionParser{
pipeline: pipeline,
}
}
func (p *expressionParser) Parse(arg interface{}) (Query, error) {
// parse into correct AST
return parseToken(p.pipeline, arg)
}
func parseToken(pipeline *Pipeline, token interface{}) (Query, error) {
// check if token is an expression
exp, ok := token.([]interface{})
if ok {
// is expression, recursively parse it
return newExpression(pipeline, exp).parse()
}
// is query, parse it directly
query, ok := token.(Query)
if !ok {
return nil, fmt.Errorf("`%v` token is unrecognized", token)
}
return query, nil
}
// expression parses the runtime query expression into it's runtime AST tree.
type expression struct {
pipeline *Pipeline
tokens []interface{}
}
func newExpression(pipeline *Pipeline, arr []interface{}) *expression {
return &expression{
pipeline: pipeline,
tokens: arr,
}
}
func (e *expression) parse() (Query, error) {
lhs, err := e.popOperand()
if err != nil {
return nil, err
}
query, err := e.parseExpression(lhs, 0)
if err != nil {
return nil, err
}
return query, nil
}
func (e *expression) pop() (interface{}, error) {
if len(e.tokens) == 0 {
return nil, fmt.Errorf("expected operand missing")
}
token := e.tokens[0]
e.tokens = e.tokens[1:len(e.tokens)]
return token, nil
}
func (e *expression) popOperand() (Query, error) {
// pops the next operand
// cases to consider:
// - a) unary operator -> expression
// - b) unary operator -> query
// - c) expression
// - d) query
// pop next token
token, err := e.pop()
if err != nil {
return nil, err
}
// see if it is a unary operator
op, ok := token.(string)
isUnary, err := isUnaryOperator(token)
if err != nil {
return nil, err
}
if ok && isUnary {
// get next token
next, err := e.pop()
if err != nil {
return nil, err
}
// parse token
query, err := parseToken(e.pipeline, next)
if err != nil {
return nil, err
}
// get unary expression
unary, err := e.pipeline.GetUnary()
if err != nil {
return nil, err
}
// populate it
err = unary.Parse(map[string]interface{}{
"op": op,
"query": query,
})
if err != nil {
return nil, err
}
return unary, nil
}
// parse token
return parseToken(e.pipeline, token)
}
func (e *expression) peek() interface{} {
if len(e.tokens) == 0 {
return nil
}
return e.tokens[0]
}
func (e *expression) advance() error {
if len(e.tokens) < 2 {
return fmt.Errorf("expected token missing after `%v`", e.tokens[0])
}
e.tokens = e.tokens[1:len(e.tokens)]
return nil
}
func (e *expression) parseExpression(lhs Query, min int) (Query, error) {
var err error
var op string
var rhs Query
var lookahead interface{}
var isBinary, isUnary bool
lookahead = e.peek()
isBinary, err = isBinaryOperator(lookahead)
if err != nil {
return nil, err
}
for isBinary && precedence(lookahead) >= min {
op, err = toOperator(lookahead)
if err != nil {
return nil, err
}
err = e.advance()
if err != nil {
return nil, err
}
rhs, err = e.popOperand()
if err != nil {
return nil, err
}
lookahead = e.peek()
isBinary, err = isBinaryOperator(lookahead)
if err != nil {
return nil, err
}
isUnary, err = isUnaryOperator(lookahead)
if err != nil {
return nil, err
}
for (isBinary && precedence(lookahead) > precedence(op)) ||
(isUnary && precedence(lookahead) == precedence(op)) {
rhs, err = e.parseExpression(rhs, precedence(lookahead))
if err != nil {
return nil, err
}
lookahead = e.peek()
}
// get binary expression
binary, err := e.pipeline.GetBinary()
if err != nil {
return nil, err
}
// populate it
err = binary.Parse(map[string]interface{}{
"left": lhs,
"op": op,
"right": rhs,
})
if err != nil {
return nil, err
}
lhs = binary
}
return lhs, nil
}
func precedence(arg interface{}) int {
op, _ := toOperator(arg)
switch op {
case And:
return 2
case Or:
return 1
case Not:
return 3
}
return 0
}
func toOperator(arg interface{}) (string, error) {
op, ok := arg.(string)
if !ok {
return "", fmt.Errorf("`%v` operator is not of type string", arg)
}
return op, nil
}
func isBinaryOperator(arg interface{}) (bool, error) {
op, ok := arg.(string)
if !ok {
return false, nil
}
switch op {
case And:
return true, nil
case Or:
return true, nil
case Not:
return false, nil
}
return false, fmt.Errorf("`%v` operator not recognized", op)
}
func isUnaryOperator(arg interface{}) (bool, error) {
op, ok := arg.(string)
if !ok {
return false, nil
}
switch op {
case Not:
return true, nil
case And:
return false, nil
case Or:
return false, nil
}
return false, fmt.Errorf("`%v` operator not recognized", op)
}