/
parserUtils.go
558 lines (474 loc) · 11.7 KB
/
parserUtils.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package internal
import (
"fmt"
"math"
"reflect"
"runtime"
"strconv"
)
func init() {
yyDebug = 0 // can be increased for debugging the generated parser
yyErrorVerbose = true // make sure to get better errors than just "syntax error"
}
// ExpressionFunction can be called from within expressions.
// The returned object needs to have one of the following types: `nil`, `bool`, `int`, `float64`, `string`, `[]interface{}` or `map[string]interface{}`.
type ExpressionFunction = func(args ...interface{}) (interface{}, error)
func typeOf(val interface{}) string {
if val == nil {
return "nil"
}
kind := reflect.TypeOf(val).Kind()
switch kind {
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Float64:
return "number"
case reflect.String:
return "string"
}
if _, ok := val.([]interface{}); ok {
return "array"
}
if _, ok := val.(map[string]interface{}); ok {
return "object"
}
return "<unknown type>"
}
func asBool(val interface{}) bool {
b, ok := val.(bool)
if !ok {
panic(fmt.Errorf("type error: required bool, but was %s", typeOf(val)))
}
return b
}
func asInteger(val interface{}) int {
i, ok := val.(int)
if ok {
return i
}
f, ok := val.(float64)
if !ok {
panic(fmt.Errorf("type error: required number of type integer, but was %s", typeOf(val)))
}
i = int(f)
if float64(i) != f {
panic(fmt.Errorf("type error: cannot cast floating point number to integer without losing precision"))
}
return i
}
func add(val1 interface{}, val2 interface{}) interface{} {
str1, str1OK := val1.(string)
str2, str2OK := val2.(string)
if str1OK && str2OK { // string + string = string
return str1 + str2
}
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK { // int + int = int
return int1 + int2
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return float1 + float2
}
if str1OK && float2OK {
return str1 + strconv.FormatFloat(float2, 'f', -1, 64)
}
if float1OK && str2OK {
return strconv.FormatFloat(float1, 'f', -1, 64) + str2
}
if str1OK && val2 == nil {
return str1 + "nil"
}
if val1 == nil && str2OK {
return "nil" + str2
}
bool1, bool1OK := val1.(bool)
bool2, bool2OK := val2.(bool)
if str1OK && bool2OK {
return str1 + strconv.FormatBool(bool2)
}
if bool1OK && str2OK {
return strconv.FormatBool(bool1) + str2
}
arr1, arr1OK := val1.([]interface{})
arr2, arr2OK := val2.([]interface{})
if arr1OK && arr2OK {
return append(arr1, arr2...)
}
obj1, obj1OK := val1.(map[string]interface{})
obj2, obj2OK := val2.(map[string]interface{})
if obj1OK && obj2OK {
sum := make(map[string]interface{})
for k, v := range obj1 {
sum[k] = v
}
for k, v := range obj2 {
sum[k] = v
}
return sum
}
panic(fmt.Errorf("type error: cannot add or concatenate type %s and %s", typeOf(val1), typeOf(val2)))
}
func sub(val1 interface{}, val2 interface{}) interface{} {
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK {
return int1 - int2
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return float1 - float2
}
panic(fmt.Errorf("type error: cannot subtract type %s and %s", typeOf(val1), typeOf(val2)))
}
func mul(val1 interface{}, val2 interface{}) interface{} {
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK {
return int1 * int2
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return float1 * float2
}
panic(fmt.Errorf("type error: cannot multiply type %s and %s", typeOf(val1), typeOf(val2)))
}
func div(val1 interface{}, val2 interface{}) interface{} {
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK {
return int1 / int2
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return float1 / float2
}
panic(fmt.Errorf("type error: cannot divide type %s and %s", typeOf(val1), typeOf(val2)))
}
func pow(val1 interface{}, val2 interface{}) interface{} {
var float1, float2 float64
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
var ok bool
if int1OK {
float1 = float64(int1)
} else {
if float1, ok = val1.(float64); !ok {
panic(fmt.Errorf("type error: cannot multiply type %s and %s", typeOf(val1), typeOf(val2)))
}
}
if int2OK {
float2 = float64(int2)
} else {
if float2, ok = val2.(float64); !ok {
panic(fmt.Errorf("type error: cannot multiply type %s and %s", typeOf(val1), typeOf(val2)))
}
}
res := math.Pow(float1, float2)
if int1OK && int2OK { // If both inputs are integers, also return an integer, unless the result is not representable as such
intRes := int(res)
if float64(intRes) == res {
return intRes
}
}
return res
}
func mod(val1 interface{}, val2 interface{}) interface{} {
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK {
return int1 % int2
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return math.Mod(float1, float2)
}
panic(fmt.Errorf("type error: cannot perform modulo on type %s and %s", typeOf(val1), typeOf(val2)))
}
func unaryMinus(val interface{}) interface{} {
intVal, ok := val.(int)
if ok {
return -intVal
}
floatVal, ok := val.(float64)
if ok {
return -floatVal
}
panic(fmt.Errorf("type error: unary minus requires number, but was %s", typeOf(val)))
}
func deepEqual(val1 interface{}, val2 interface{}) bool {
switch typ1 := val1.(type) {
case []interface{}:
typ2, ok := val2.([]interface{})
if !ok || len(typ1) != len(typ2) {
return false
}
for idx := range typ1 {
if !deepEqual(typ1[idx], typ2[idx]) {
return false
}
}
return true
case map[string]interface{}:
typ2, ok := val2.(map[string]interface{})
if !ok || len(typ1) != len(typ2) {
return false
}
for idx := range typ1 {
if !deepEqual(typ1[idx], typ2[idx]) {
return false
}
}
return true
case int:
int2, ok := val2.(int)
if ok {
return typ1 == int2
}
float2, ok := val2.(float64)
if ok {
return float64(typ1) == float2
}
return false
case float64:
float2, ok := val2.(float64)
if ok {
return typ1 == float2
}
int2, ok := val2.(int)
if ok {
return typ1 == float64(int2)
}
return false
}
return val1 == val2
}
func compare(val1 interface{}, val2 interface{}, operation string) bool {
int1, int1OK := val1.(int)
int2, int2OK := val2.(int)
if int1OK && int2OK {
return compareInt(int1, int2, operation)
}
float1, float1OK := val1.(float64)
float2, float2OK := val2.(float64)
if int1OK {
float1 = float64(int1)
float1OK = true
}
if int2OK {
float2 = float64(int2)
float2OK = true
}
if float1OK && float2OK {
return compareFloat(float1, float2, operation)
}
panic(fmt.Errorf("type error: cannot compare type %s and %s", typeOf(val1), typeOf(val2)))
}
func compareInt(val1 int, val2 int, operation string) bool {
switch operation {
case "<":
return val1 < val2
case "<=":
return val1 <= val2
case ">":
return val1 > val2
case ">=":
return val1 >= val2
}
panic(fmt.Errorf("syntax error: unsupported operation %q", operation))
}
func compareFloat(val1 float64, val2 float64, operation string) bool {
switch operation {
case "<":
return val1 < val2
case "<=":
return val1 <= val2
case ">":
return val1 > val2
case ">=":
return val1 >= val2
}
panic(fmt.Errorf("syntax error: unsupported operation %q", operation))
}
func asObjectKey(key interface{}) string {
s, ok := key.(string)
if !ok {
panic(fmt.Errorf("type error: object key must be string, but was %s", typeOf(key)))
}
return s
}
func addObjectMember(obj map[string]interface{}, key, val interface{}) map[string]interface{} {
s := asObjectKey(key)
_, ok := obj[s]
if ok {
panic(fmt.Errorf("syntax error: duplicate object key %q", s))
}
obj[s] = val
return obj
}
func accessVar(variables map[string]interface{}, varName string) interface{} {
val, ok := variables[varName]
if !ok {
panic(fmt.Errorf("var error: variable %q does not exist", varName))
}
return val
}
func accessField(s interface{}, field interface{}) interface{} {
obj, ok := s.(map[string]interface{})
if ok {
key, ok := field.(string)
if !ok {
panic(fmt.Errorf("syntax error: object key must be string, but was %s", typeOf(field)))
}
val, ok := obj[key]
if !ok {
panic(fmt.Errorf("var error: object has no member %q", field))
}
return val
}
arrVar, ok := s.([]interface{})
if ok {
intIdx, ok := field.(int)
if !ok {
floatIdx, ok := field.(float64)
if !ok {
panic(fmt.Errorf("syntax error: array index must be number, but was %s", typeOf(field)))
}
intIdx = int(floatIdx)
if float64(intIdx) != floatIdx {
panic(fmt.Errorf("eval error: array index must be whole number, but was %f", floatIdx))
}
}
if intIdx < 0 || intIdx >= len(arrVar) {
panic(fmt.Errorf("var error: array index %d is out of range [%d, %d]", intIdx, 0, len(arrVar)))
}
return arrVar[intIdx]
}
panic(fmt.Errorf("syntax error: cannot access fields on type %s", typeOf(s)))
}
func slice(v interface{}, from, to interface{}) interface{} {
str, isStr := v.(string)
arr, isArr := v.([]interface{})
if !isStr && !isArr {
panic(fmt.Errorf("syntax error: slicing requires an array or string, but was %s", typeOf(v)))
}
var fromInt, toInt int
if from == nil {
fromInt = 0
} else {
fromInt = asInteger(from)
}
if to == nil && isStr {
toInt = len(str)
} else if to == nil && isArr {
toInt = len(arr)
} else {
toInt = asInteger(to)
}
if fromInt < 0 {
panic(fmt.Errorf("range error: start-index %d is negative", fromInt))
}
if isStr {
if toInt < 0 || toInt > len(str) {
panic(fmt.Errorf("range error: end-index %d is out of range [0, %d]", toInt, len(str)))
}
if fromInt > toInt {
panic(fmt.Errorf("range error: start-index %d is greater than end-index %d", fromInt, toInt))
}
return str[fromInt:toInt]
}
if toInt < 0 || toInt > len(arr) {
panic(fmt.Errorf("range error: end-index %d is out of range [0, %d]", toInt, len(arr)))
}
if fromInt > toInt {
panic(fmt.Errorf("range error: start-index %d is greater than end-index %d", fromInt, toInt))
}
return arr[fromInt:toInt]
}
func arrayContains(arr interface{}, val interface{}) bool {
a, ok := arr.([]interface{})
if !ok {
panic(fmt.Errorf("syntax error: in-operator requires array, but was %s", typeOf(arr)))
}
for _, v := range a {
if deepEqual(v, val) {
return true
}
}
return false
}
func callFunction(functions map[string]ExpressionFunction, name string, args []interface{}) interface{} {
f, ok := functions[name]
if !ok {
panic(fmt.Errorf("syntax error: no such function %q", name))
}
res, err := callAndRecover(f, args)
if err != nil {
panic(fmt.Errorf("function error: %q - %w", name, err))
}
return res
}
func callAndRecover(f ExpressionFunction, args []interface{}) (_ interface{}, retErr error) {
defer func() {
r := recover()
if r == nil {
return
}
_, ok := r.(runtime.Error)
if ok {
panic(r)
}
if err, ok := r.(error); ok {
retErr = err
} else {
retErr = fmt.Errorf("panic: %v", r)
}
}()
return f(args...)
}