-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression_visitor.go
609 lines (586 loc) · 16.6 KB
/
expression_visitor.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package eval
import (
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"github.com/gammazero/deque"
"github.com/stirante/go-simple-eval/eval/functions"
"github.com/stirante/go-simple-eval/eval/utils"
"github.com/stirante/go-simple-eval/parser"
"reflect"
)
const NaN = "NaN"
var majorAliases = []string{
"major",
"a",
"x",
}
var minorAliases = []string{
"minor",
"b",
"y",
}
var patchAliases = []string{
"patch",
"c",
"z",
}
type ExpressionVisitor struct {
parser.BaseSimpleQueryLanguageVisitor
scope deque.Deque[interface{}]
path *string
}
func (v *ExpressionVisitor) Visit(tree antlr.ParseTree) interface{} {
switch val := tree.(type) {
case *parser.FieldContext:
return v.VisitField(val)
case *parser.ArrayContext:
return v.VisitArray(val)
case *parser.ObjectContext:
return v.VisitObject(val)
case *parser.Object_fieldContext:
return v.VisitObject_field(val)
case *parser.Function_paramContext:
return v.VisitFunction_param(val)
case *parser.NameContext:
return v.VisitName(val)
case *parser.IndexContext:
return v.VisitIndex(val)
case *parser.ExpressionContext:
return v.VisitExpression(val)
}
panic("Unknown tree type " + reflect.TypeOf(tree).String())
}
// pushScope pushes a new scope to the stack
func (v *ExpressionVisitor) pushScope(scope map[string]interface{}) {
v.scope.PushBack(scope)
}
// pushScopePair pushes a new entry to the stack
func (v *ExpressionVisitor) pushScopePair(key string, value interface{}) {
v.scope.PushBack(map[string]interface{}{key: value})
}
// popScope pops the last scope from the stack
func (v *ExpressionVisitor) popScope() {
v.scope.PopBack()
}
// negate negates a value
func negate(value interface{}) interface{} {
if value == nil {
return nil
}
if b, ok := value.(utils.JsonNumber); ok {
return utils.JsonNumber{
Value: -b.Value,
Decimal: b.Decimal,
}
}
if b, ok := value.(bool); ok {
return !b
}
if b, ok := value.(int); ok {
return utils.ToNumber(-b)
}
if b, ok := value.(float64); ok {
return utils.ToNumber(-b)
}
if b, ok := value.(float32); ok {
return utils.ToNumber(-b)
}
if utils.IsArray(value) {
result := make([]interface{}, len(value.([]interface{})))
for i, v := range value.([]interface{}) {
result[i] = negate(v)
}
return result
}
return NaN
}
func isError(v interface{}) bool {
_, err := v.(error)
return err
}
func getError(v interface{}) error {
if err, ok := v.(error); ok {
return err
}
return nil
}
// resolveScope resolves a value from the scope by name
func (v *ExpressionVisitor) resolveScope(name string) interface{} {
for i := v.scope.Len() - 1; i >= 0; i-- {
m := v.scope.At(i)
if c, ok := m.(map[string]interface{}); ok {
if b, ok := c[name]; ok {
return b
}
}
// Seems like sometimes above cast fails, so we need to check for map[string]interface{} as well
if c, ok := m.(map[string]interface{}); ok {
if v, ok := c[name]; ok {
return v
}
}
}
return nil
}
func (v *ExpressionVisitor) VisitExpression(context *parser.ExpressionContext) interface{} {
return v.Visit(context.Field())
}
func (v *ExpressionVisitor) VisitField(context *parser.FieldContext) interface{} {
if context.Null() != nil {
return nil
}
if context.True() != nil {
return true
}
if context.False() != nil {
return false
}
if context.Not() != nil {
return !utils.ToBoolean(v.Visit(context.Field(0)))
}
// process field composed of two other fields
if len(context.AllField()) == 2 {
f1 := v.Visit(context.Field(0))
if isError(f1) {
return f1
}
// Move AND and OR here to make those operators short-circuiting
if context.And() != nil {
if utils.ToBoolean(f1) {
f2 := v.Visit(context.Field(1))
if isError(f2) {
return f2
}
return utils.ToBoolean(f2)
} else {
return false
}
} else if context.Or() != nil {
b := utils.ToBoolean(f1)
if !b {
f2 := v.Visit(context.Field(1))
if isError(f2) {
return f2
}
return utils.ToBoolean(f2)
} else {
return b
}
} else if context.Question() != nil {
if utils.ToBoolean(f1) {
f2 := v.Visit(context.Field(1))
if isError(f2) {
return f2
}
return f2
} else {
return nil
}
}
f2 := v.Visit(context.Field(1))
if isError(f2) {
return f2
}
if context.NullCoalescing() != nil {
if f1 == nil {
return f2
} else {
return f1
}
} else if context.Add() != nil {
if (utils.IsNumber(f1) && utils.IsNumber(f2)) || (utils.IsNumber(f1) && f2 == nil) || (f1 == nil && utils.IsNumber(f2)) {
n1 := utils.ToNumber(f1)
n2 := utils.ToNumber(f2)
return utils.JsonNumber{
Value: n1.FloatValue() + n2.FloatValue(),
Decimal: n1.Decimal || n2.Decimal,
}
} else if utils.IsArray(f1) && utils.IsArray(f2) {
array := utils.MergeArray(f1.([]interface{}), f2.([]interface{}))
return array
} else if utils.IsObject(f1) && utils.IsObject(f2) {
var result []interface{} = nil
result = append(result, f1.([]interface{})...)
return append(result, f2.([]interface{})...)
} else {
return utils.ToString(f1) + utils.ToString(f2)
}
} else if context.Equal() != nil {
return utils.IsEqual(f1, f2)
} else if context.NotEqual() != nil {
return !utils.IsEqual(f1, f2)
} else if utils.IsNumber(f1) && utils.IsNumber(f2) {
n1 := utils.ToNumber(f1)
n2 := utils.ToNumber(f2)
if len(context.AllDot()) == 2 {
return utils.CreateRange(n1.IntValue(), n2.IntValue())
} else if context.Greater() != nil {
return n1.FloatValue() > n2.FloatValue()
} else if context.Less() != nil {
return n1.FloatValue() < n2.FloatValue()
} else if context.GreaterOrEqual() != nil {
return n1.FloatValue() >= n2.FloatValue()
} else if context.LessOrEqual() != nil {
return n1.FloatValue() <= n2.FloatValue()
} else if n1.Decimal || n2.Decimal {
if context.Subtract() != nil {
return utils.JsonNumber{
Value: n1.FloatValue() - n2.FloatValue(),
Decimal: true,
}
} else if context.Divide() != nil {
return utils.JsonNumber{
Value: n1.FloatValue() / n2.FloatValue(),
Decimal: true,
}
} else if context.Multiply() != nil {
return utils.JsonNumber{
Value: n1.FloatValue() * n2.FloatValue(),
Decimal: true,
}
}
} else {
if context.Subtract() != nil {
return utils.JsonNumber{
Value: float64(n1.IntValue() - n2.IntValue()),
Decimal: false,
}
} else if context.Divide() != nil {
return utils.JsonNumber{
Value: float64(n1.IntValue() / n2.IntValue()),
Decimal: false,
}
} else if context.Multiply() != nil {
return utils.JsonNumber{
Value: float64(n1.IntValue() * n2.IntValue()),
Decimal: false,
}
}
}
} else if utils.IsSemver(f1) && utils.IsSemver(f2) {
s1 := utils.ToSemver(f1)
s2 := utils.ToSemver(f2)
if context.Greater() != nil {
return s1.CompareTo(s2) > 0
} else if context.Less() != nil {
return s1.CompareTo(s2) < 0
} else if context.GreaterOrEqual() != nil {
return s1.CompareTo(s2) >= 0
} else if context.LessOrEqual() != nil {
return s1.CompareTo(s2) <= 0
}
} else if context.Greater() != nil || context.Less() != nil || context.GreaterOrEqual() != nil || context.LessOrEqual() != nil {
return false
} else {
return NaN
}
} else if len(context.AllField()) == 3 {
// handle ternary operator
if context.Question() != nil {
f1 := v.Visit(context.Field(0))
if isError(f1) {
return f1
}
if utils.ToBoolean(f1) {
return v.Visit(context.Field(1))
} else {
return v.Visit(context.Field(2))
}
}
}
// if the field is another field in parentheses, return the value of that field
// we need to also check if the first element is not a field, because if it is, it will be a function call
if context.LeftParen() != nil && len(context.AllField()) == 1 && context.GetChild(0) != context.Field(0) {
return v.Visit(context.Field(0))
} else if context.LeftParen() != nil && len(context.AllField()) == 1 && context.GetChild(0) == context.Field(0) {
lambda := v.Visit(context.Field(0))
if isError(lambda) {
return lambda
}
params := make([]interface{}, len(context.AllFunction_param()))
for i, param := range context.AllFunction_param() {
params[i] = v.Visit(param)
if isError(params[i]) {
return params[i]
}
}
if _, ok := lambda.(utils.JsonFunction); ok {
i, err := lambda.(utils.JsonFunction)(params)
if err != nil {
return err
}
return i
} else {
var methodName *string = nil
for _, child := range context.Field(0).GetChildren() {
if b, ok := child.(parser.INameContext); ok {
text := b.GetText()
methodName = &text
break
}
}
if methodName == nil || !functions.HasFunction(*methodName) {
return burrito.WrappedErrorf("Function '%s' not found!", context.Field(0).GetText())
}
function, err := functions.CallFunction(*methodName, params)
if err != nil {
return burrito.WrapErrorf(err, "Error calling function '%s'", *methodName)
}
return function
}
}
// handle accessing a property of an object or calling an instance function
if context.Name() != nil && len(context.AllField()) == 1 {
text := context.Name().GetText()
object := v.Visit(context.Field(0))
if isError(object) {
return object
}
var newScope interface{} = nil
if object == nil {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Cannot access %s because %s is %s", context.GetText(), context.Field(0).GetText(), utils.ToString(object))
}
}
if utils.IsSemver(object) {
u := utils.ToSemver(object)
if utils.IndexOf(majorAliases, text) != -1 {
return utils.ToNumber(u.Major)
}
if utils.IndexOf(minorAliases, text) != -1 {
return utils.ToNumber(u.Minor)
}
if utils.IndexOf(patchAliases, text) != -1 {
return utils.ToNumber(u.Patch)
}
return burrito.WrappedErrorf("Cannot access %s because %s is not a valid semver field", context.GetText(), text)
} else if utils.IsObject(object) {
u := object.(map[string]interface{})
if c, ok := u[text]; ok {
newScope = c
}
} else if functions.HasInstanceFunction(reflect.TypeOf(object), text) {
return utils.JsonFunction(
func(o []interface{}) (interface{}, error) {
function, err := functions.CallInstanceFunction(text, object, o)
if err != nil {
return nil, burrito.WrapErrorf(err, "Error calling function '%s' on %s", text, utils.ToString(object))
}
return function, nil
},
)
}
if newScope == nil {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Failed to resolve field '%s' (%s) in %s", text, context.GetText(), utils.ToString(object))
}
}
return newScope
}
if context.Name() != nil {
return v.Visit(context.Name())
}
// handle indexed access
if context.Index() != nil && len(context.AllField()) == 1 {
i := v.Visit(context.Index())
if isError(i) {
return i
}
object := v.Visit(context.Field(0))
if isError(object) {
return object
}
if utils.IsArray(object) {
// in case of an array, we need an integer index
if utils.IsNumber(i) {
value := utils.ToNumber(i).IntValue()
if value < 0 || value >= int32(len(object.([]interface{}))) {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Index out of bounds: %d (%s)", value, context.GetText())
}
}
return object.([]interface{})[value]
} else {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Index must be a number: %s (%s)", utils.ToString(i), context.GetText())
}
}
} else if utils.IsSemver(object) {
// in case of an object, we need a string index
value := utils.ToString(i)
u := utils.ToSemver(object)
if utils.IndexOf(majorAliases, value) != -1 {
return utils.ToNumber(u.Major)
}
if utils.IndexOf(minorAliases, value) != -1 {
return utils.ToNumber(u.Minor)
}
if utils.IndexOf(patchAliases, value) != -1 {
return utils.ToNumber(u.Patch)
}
return burrito.WrappedErrorf("Cannot access %s because %s is not a valid semver field", context.GetText(), value)
} else if utils.IsObject(object) {
// in case of an object, we need a string index
value := utils.ToString(i)
u := object.(map[string]interface{})
if _, ok := u[value]; !ok {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Property '%s' (%s) not found in %s", value, context.GetText(), utils.ToString(object))
}
} else {
return u[value]
}
} else if str, ok := object.(string); ok {
// in case of a string, we need an integer index
if utils.IsNumber(i) {
value := utils.ToNumber(i).IntValue()
if value < 0 || value >= int32(len(str)) {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Index out of bounds: %d (%s)", value, context.GetText())
}
}
return string(rune(str[value]))
} else {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Index must be a number: %s (%s)", utils.ToString(object), context.GetText())
}
}
} else {
// handle null-forgiving operator
if context.Question() != nil {
return nil
} else {
return burrito.WrappedErrorf("Cannot access %s because %s is %s", context.GetText(), context.Field(0).GetText(), utils.ToString(object))
}
}
}
if context.NUMBER() != nil {
return utils.ToNumber(context.NUMBER().GetText())
}
if context.ESCAPED_STRING() != nil {
return utils.UnescapeString(utils.ToString(context.ESCAPED_STRING().GetText()))
}
// literal array notation
if context.Array() != nil {
return v.Visit(context.Array())
}
// literal object notation
if context.Object() != nil {
return v.Visit(context.Object())
}
// negation
if context.Subtract() != nil && len(context.AllField()) == 1 {
f := v.Visit(context.Field(0))
if isError(f) {
return f
}
return negate(f)
}
return burrito.WrappedErrorf("Failed to resolve '%s'", context.GetText())
}
func (v *ExpressionVisitor) VisitName(context *parser.NameContext) interface{} {
text := context.GetText()
// "this" is a special case that always refers to the current full scope
if text == "this" {
scope := map[string]interface{}{}
for i := 0; i < v.scope.Len(); i++ {
s := v.scope.At(i)
if c, ok := s.(map[string]interface{}); ok {
for key, _ := range c {
scope[key] = c[key]
}
}
}
return scope
}
newScope := v.resolveScope(text)
back := v.scope.Len() - 1
for newScope == nil && back >= 0 {
m := v.scope.At(back)
if c, ok := m.(map[string]interface{}); ok {
if v, ok := c[text]; ok {
newScope = v
}
}
back--
}
return newScope
}
func (v *ExpressionVisitor) VisitIndex(context *parser.IndexContext) interface{} {
if context.NUMBER() != nil {
return utils.ToNumber(context.NUMBER().GetText())
}
if context.ESCAPED_STRING() != nil {
return utils.UnescapeString(utils.ToString(context.ESCAPED_STRING().GetText()))
}
if context.Field() != nil {
return v.Visit(context.Field())
}
return burrito.WrappedErrorf("Invalid index: %s", context.GetText())
}
func (v *ExpressionVisitor) VisitArray(context *parser.ArrayContext) interface{} {
result := make([]interface{}, len(context.AllField()))
for i, f := range context.AllField() {
result[i] = v.Visit(f)
if isError(result[i]) {
return result[i]
}
}
return result
}
func (v *ExpressionVisitor) VisitObject(context *parser.ObjectContext) interface{} {
result := map[string]interface{}{}
for _, f := range context.AllObject_field() {
obj := v.Visit(f)
if isError(obj) {
return obj
}
u := obj.(map[string]interface{})
for key, v := range u {
result[key] = v
}
}
return result
}
func (v *ExpressionVisitor) VisitObject_field(context *parser.Object_fieldContext) interface{} {
name := ""
if context.ESCAPED_STRING() != nil {
name = utils.UnescapeString(utils.ToString(context.ESCAPED_STRING().GetText()))
} else {
name = context.Name().GetText()
}
field := v.Visit(context.Field())
if isError(field) {
return field
}
n := map[string]interface{}{}
n[name] = field
return n
}
func (v *ExpressionVisitor) VisitFunction_param(ctx *parser.Function_paramContext) interface{} {
if ctx.Field() != nil {
return v.Visit(ctx.Field())
}
return burrito.WrappedErrorf("Invalid function parameter: %s", ctx.GetText())
}