-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenGroup.go
471 lines (447 loc) · 11.3 KB
/
tokenGroup.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package parser
import (
"errors"
"github.com/pboyd04/godata/filter/lexer"
)
type tokenGroup struct {
children []Operand
}
func (t *tokenGroup) GetData() (interface{}, error) {
return t, nil
}
func newTokenGroup(tokens []*lexer.Token) *tokenGroup {
ret := &tokenGroup{}
ret.children = make([]Operand, len(tokens))
// Not sure why, but append doesn't work in this case
for i, token := range tokens {
ret.children[i] = token
}
return ret
}
func newTokenGroupOperands(ops []Operand) *tokenGroup {
ret := &tokenGroup{}
ret.children = make([]Operand, len(ops))
copy(ret.children, ops)
return ret
}
func (t *tokenGroup) beforeAndAfterOperandProcess(operand Operand, compareFn tokenComparer, i int) error {
switch token := operand.(type) {
case *lexer.Token:
if compareFn(token) {
// Found an operator
err := t.doBeforeAndAfterTokenReplace(token, i)
if err != nil {
return err
}
}
case *tokenGroup:
err := token.beforeAndAfterOpProcess(compareFn)
if err != nil {
return err
}
case *Operation:
err := token.beforeAndAfterOpProcess(compareFn)
if err != nil {
return err
}
case *ObjectOperand, *SliceOperand:
// Don't do anything this is already processed
break
default:
return newParserError("unknown type: %T", token)
}
return nil
}
func (t *tokenGroup) beforeAndAfterOpProcess(compareFn tokenComparer) error {
for i := 0; i < len(t.children); i++ {
err := t.beforeAndAfterOperandProcess(t.children[i], compareFn, i)
if err != nil {
return err
}
}
return nil
}
func (t *tokenGroup) createSlices() {
for i := 0; i < len(t.children); i++ {
switch token := t.children[i].(type) {
case *lexer.Token:
if token.Type == lexer.OpenSquareBracket {
for j := i + 1; j < len(t.children); j++ {
token, ok := t.children[j].(*lexer.Token)
if ok && token.Type == lexer.CloseSquareBracket {
// Found the end of the group
op := newSliceOperand(t.children[i+1 : j])
tmp := t.children[j+1:]
t.children = append(t.children[:i], op)
t.children = append(t.children, tmp...)
break
}
}
}
default:
break
}
}
}
func (t *tokenGroup) doBeforeAndAfterTokenReplace(token *lexer.Token, i int) error {
op := &Operation{Operator: Operator(token.Type)}
// Previous item and next item must both be something... we don't care what...
if i == 0 || i == len(t.children)-1 {
return newParserError("expected item before and after operator")
}
prev := t.children[i-1]
next := t.children[i+1]
op.Operands = []Operand{prev, next}
tmp := t.children[i+2:]
t.children = append(t.children[:i-1], op)
t.children = append(t.children, tmp...)
return nil
}
func (t *tokenGroup) findEndParentheses(start int) (int, bool) {
depth := 1
foundNested := false
for i := start + 1; i < len(t.children); i++ {
token, ok := t.children[i].(*lexer.Token)
if ok && token.Type == lexer.OpenParens {
depth++
foundNested = true
} else if ok && token.Type == lexer.CloseParens {
depth--
if depth == 0 {
return i, foundNested
}
}
}
return -1, false
}
func (t *tokenGroup) flatten(parent *Operation) (*Operation, error) {
if len(t.children) != 1 {
return nil, ErrMoreThanOneChild
}
var op *Operation
switch operand := t.children[0].(type) {
case *Operation:
op = operand
case *tokenGroup:
myOp, err := operand.toOperation(parent)
if err != nil {
return nil, err
}
op = myOp
case *lexer.Token:
return &Operation{Operator: Operator(operand.Type)}, nil
default:
return nil, newParserError("unknown type: %T", t.children[0])
}
err := op.flatten()
if err != nil {
return nil, err
}
return op, nil
}
func (t *tokenGroup) handleFlattenError(o *Operation, err error) error {
if errors.Is(err, ErrMoreThanOneChild) {
if o == nil {
return newParserError("more than one non-operation child at root")
}
o.Operands = append(o.Operands, t.children...)
// Remove all token groups from the parent
for i := 0; i < len(o.Operands); i++ {
_, ok := o.Operands[i].(*tokenGroup)
if ok {
o.Operands = append(o.Operands[:i], o.Operands[i+1:]...)
}
}
return nil
}
return err
}
func (t *tokenGroup) handleInToken(i int) error {
op := &Operation{Operator: lexer.In}
// We need two operands, the first is before the in, the second is after the in
prev := t.children[i-1]
// There are two options, option 1 the next item is a token group, option 2 the next item is in square brackets...
switch next := t.children[i+1].(type) {
case *tokenGroup:
// Found a token group
err := next.in()
if err != nil {
return err
}
op.Operands = []Operand{prev, newSliceOperand(next.children)}
tmp := t.children[i+2:]
t.children = append(t.children[:i-1], op)
t.children = append(t.children, tmp...)
case *lexer.Token:
if next.Type != lexer.OpenSquareBracket {
// Can't find arguments for in
return newParserError("expected open square bracket after in, found %s", next.Text)
}
for j := i + 2; j < len(t.children); j++ {
token, ok := t.children[j].(*lexer.Token)
if ok && token.Type == lexer.CloseSquareBracket {
// Found the end of the group
op.Operands = []Operand{prev, newSliceOperand(t.children[i+2 : j])}
tmp := t.children[j+1:]
t.children = append(t.children[:i-1], op)
t.children = append(t.children, tmp...)
break
}
}
default:
return newParserError("unknown type (inner loop): %T", t.children[i+1])
}
return nil
}
func (t *tokenGroup) handleMethodToken(token *lexer.Token, i int) error {
op := &Operation{Operator: Operator(token.Type)}
// The next item must be a token group otherwise it's an error
next := t.children[i+1]
group, ok := next.(*tokenGroup)
if !ok {
return newParserError("expected token group after method call, found %#v", next)
}
op.Operands = []Operand{group}
tmp := t.children[i+2:]
t.children = append(t.children[:i], op)
t.children = append(t.children, tmp...)
err := group.methodCalls()
if err != nil {
return err
}
group.createSlices()
group.removeCommas()
return nil
}
func (t *tokenGroup) handleObjects() error {
var op *ObjectOperand
initialIndex := -1
for i := 0; i < len(t.children); i++ {
token, ok := t.children[i].(*lexer.Token)
if !ok {
tg, ok := t.children[i].(*tokenGroup)
if !ok {
return newParserError("unknown type: %T", t.children[i])
}
err := tg.handleObjects()
if err != nil {
return err
}
continue
}
if token.Type == lexer.OpenCurlyBrace {
// Found an object
op = &ObjectOperand{Properties: ""}
initialIndex = i
}
if op != nil {
op.Properties += token.Text
}
if token.Type == lexer.CloseCurlyBrace {
// Found the end of the object
t.children[initialIndex] = op
// Remove the tokens that were used to create the object
t.children = append(t.children[:initialIndex+1], t.children[i+1:]...)
op = nil
}
}
return nil
}
func (t *tokenGroup) handleUnaryToken(token *lexer.Token, i int) error {
op := &Operation{Operator: Operator(token.Type)}
// The next item can be whatever...
next := t.children[i+1]
op.Operands = []Operand{next}
tmp := t.children[i+2:]
t.children = append(t.children[:i], op)
switch nextVal := next.(type) {
case *tokenGroup:
err := nextVal.unary()
if err != nil {
return err
}
nextVal.children = append(nextVal.children, tmp...)
case *Operation:
err := nextVal.unary()
if err != nil {
return err
}
nextVal.Operands = append(nextVal.Operands, tmp...)
case *lexer.Token:
group := newTokenGroupOperands([]Operand{next})
group.children = append(group.children, tmp...)
err := group.unary()
if err != nil {
return err
}
// Replace the token with the group
op.Operands = []Operand{group}
}
return nil
}
func (t *tokenGroup) in() error {
// Handle the in operator
for i := 0; i < len(t.children); i++ {
switch token := t.children[i].(type) {
case *lexer.Token:
if token.Type == lexer.In {
// Found an in operator
err := t.handleInToken(i)
if err != nil {
return err
}
break
}
case *tokenGroup:
err := token.in()
if err != nil {
return err
}
default:
// Don't do anything this is already processed
}
}
return nil
}
func (t *tokenGroup) methodCalls() error {
for i := 0; i < len(t.children); i++ {
switch token := t.children[i].(type) {
case *lexer.Token:
if token.HasParameters() {
// Found a method call
err := t.handleMethodToken(token, i)
if err != nil {
return err
}
}
case *tokenGroup:
err := token.methodCalls()
if err != nil {
return err
}
case *Operation:
err := token.methodCalls()
if err != nil {
return err
}
default:
// Don't do anything this is already processed
}
}
return nil
}
func (t *tokenGroup) parentheses() {
// Special case, this is always first so everything in the array is a token
for i := 0; i < len(t.children); i++ {
//nolint:forcetypeassert // We know this is a token
token := t.children[i].(*lexer.Token)
if token.Type == lexer.OpenParens {
end, foundNested := t.findEndParentheses(i)
if end == -1 {
// No matching close parentheses
return
}
newGroup := newTokenGroupOperands(t.children[i+1 : end])
if foundNested {
// Handle the nested parentheses
newGroup.parentheses()
}
tmp := t.children[end+1:]
t.children = append(t.children[:i], newGroup)
t.children = append(t.children, tmp...)
}
}
}
func (t *tokenGroup) processTokenGroupOps() error {
err := t.in()
if err != nil {
return err
}
err = t.unary()
if err != nil {
return err
}
err = t.methodCalls()
if err != nil {
return err
}
err = t.beforeAndAfterOpProcess(multiplicativeCompare)
if err != nil {
return err
}
err = t.beforeAndAfterOpProcess(additiveCompare)
if err != nil {
return err
}
err = t.beforeAndAfterOpProcess(relationalCompare)
if err != nil {
return err
}
err = t.beforeAndAfterOpProcess(equalityCompare)
if err != nil {
return err
}
err = t.beforeAndAfterOpProcess(conjunctionCompare)
if err != nil {
return err
}
return nil
}
func (t *tokenGroup) removeCommas() {
// Remove commas from the token group
for i := 0; i < len(t.children); i++ {
token, ok := t.children[i].(*lexer.Token)
if ok && token.Type == lexer.Comma {
// Remove the comma
t.children = append(t.children[:i], t.children[i+1:]...)
i--
}
}
}
func (t *tokenGroup) toOperation(parent *Operation) (*Operation, error) {
if parent != nil && parent.Operator.hasParameters() {
// This is a method call
parent.Operands = append(parent.Operands, t.children...)
// Remove all token groups from the parent
for i := 0; i < len(parent.Operands); i++ {
_, ok := parent.Operands[i].(*tokenGroup)
if ok {
parent.Operands = append(parent.Operands[:i], parent.Operands[i+1:]...)
}
}
err := parent.flatten()
return nil, err
}
myOp, err := t.flatten(parent)
if err != nil {
return nil, t.handleFlattenError(parent, err)
}
return myOp, nil
}
func (t *tokenGroup) unary() error {
for i := 0; i < len(t.children); i++ {
switch token := t.children[i].(type) {
case *lexer.Token:
if token.IsUnary() {
// Found a unary operator
err := t.handleUnaryToken(token, i)
if err != nil {
return err
}
}
case *tokenGroup:
err := token.unary()
if err != nil {
return err
}
case *Operation:
err := token.unary()
if err != nil {
return err
}
default:
// Don't do anything this is a type that is already processed
}
}
return nil
}