/
expr.go
2311 lines (2166 loc) · 48.5 KB
/
expr.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package expr
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"unicode/utf16"
"unicode/utf8"
"unsafe"
"github.com/tidwall/conv"
)
//lint:file-ignore ST1005 Errors allow for capitalization to match Javascript.
func errUndefined(ident string, chain bool) error {
var err error
if chain {
err = fmt.Errorf("Uncaught TypeError: "+
"Cannot read properties of undefined (reading '%s')", ident)
} else {
err = fmt.Errorf("ReferenceError: %s is not defined", ident)
}
return &errEval{err: err, udef: true}
}
func errOperator(err error) error {
return &errEval{
err: fmt.Errorf("OperatorError: %w", err),
}
}
func errReference(err error) error {
return &errEval{
err: fmt.Errorf("ReferenceError: %w", err),
}
}
func errCall(err error) error {
return &errEval{
err: fmt.Errorf("CallError: %w", err),
}
}
func errSyntax() error {
return &errEval{
err: errors.New("SyntaxError"),
}
}
// ErrStop is used to stop the EvalForEach and ForEachValue
var ErrStop = errors.New("stop")
type errEval struct {
err error
udef bool
}
func (err *errEval) Error() string {
return err.err.Error()
}
// CharPosOfErr returns the character position of where the error occured in
// the Eval function, or -1 if unknown
func CharPosOfErr(err error) int {
return -1
}
var (
Undefined = Value{kind: undefKind}
Null = Value{kind: nullKind}
)
// Op is an operator for Custom values used for the Options.Op function.
type Op string
const (
OpAdd Op = "+"
OpSub Op = "-"
OpMul Op = "*"
OpDiv Op = "/"
OpMod Op = "%"
OpLt Op = "<"
OpStEq Op = "==="
OpAnd Op = "&&"
OpOr Op = "||"
OpBitOr Op = "|"
OpBitXor Op = "^"
OpBitAnd Op = "&"
OpCoal Op = "??"
)
func (op Op) String() string {
return string(op)
}
func closech(open byte) byte {
switch open {
case '(':
return ')'
case '[':
return ']'
case '{':
return '}'
}
return open
}
func parseFloat(s string) (n float64, ok bool) {
var i int
var sign bool
if len(s) > 15 {
// Large numeric strings should be put through the built in converter.
goto notInt
}
if len(s) > 0 && s[0] == '-' {
sign = true
i++
}
if i == len(s) {
goto notInt
}
for ; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
n = n*10 + float64(s[i]-'0')
} else {
goto notInt
}
}
if sign {
return n * -1, true
}
return n, true
notInt:
f, err := strconv.ParseFloat(s, 64)
return f, err == nil
}
func evalAtom(expr string, ctx *evalContext) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
var left Value
var leftReady bool
// first look for non-chainable atoms
switch expr[0] {
case '0':
if len(expr) > 1 && (expr[1] == 'x' || expr[1] == 'X') {
// hexadecimal
x, err := strconv.ParseUint(expr[2:], 16, 64)
if err != nil {
return Undefined, errSyntax()
}
return Float64(float64(x)), nil
}
fallthrough
case '-', '.', '1', '2', '3', '4', '5', '6', '7', '8', '9':
if len(expr) > 3 && strings.HasSuffix(expr, "64") {
if expr[len(expr)-3] == 'u' {
x, err := strconv.ParseUint(expr[:len(expr)-3], 10, 64)
if err != nil {
return Undefined, errSyntax()
}
return Uint64(x), nil
}
if expr[len(expr)-3] == 'i' {
x, err := strconv.ParseInt(expr[:len(expr)-3], 10, 64)
if err != nil {
return Undefined, errSyntax()
}
return Int64(x), nil
}
}
x, ok := parseFloat(expr)
if !ok {
return Undefined, errSyntax()
}
return Float64(x), nil
case '"', '\'':
var s string
var ok bool
s, raw, ok := parseString(expr)
if !ok {
return Undefined, errSyntax()
}
left = String(s)
leftReady = true
expr = expr[len(raw):]
case '(', '{', '[':
g, err := readGroup(expr)
if err != nil {
return Undefined, err
}
if g[0] == '(' {
// paren groups can be evaluated and used as the leading value.
left, err = evalExpr(g[1:len(g)-1], ctx)
if err != nil {
return Undefined, err
}
leftReady = true
expr = expr[len(g):]
} else if g[0] == '[' {
left, err = multiExprsToArray(g[1:len(g)-1], ctx)
if err != nil {
return Undefined, err
}
leftReady = true
expr = expr[len(g):]
} else {
// '{' not currently allowed as a leading value
// Perhaps in the future.
return Undefined, errSyntax()
}
}
var leftIdent string
if !leftReady {
// probably a chainable identifier
ident, ok := readIdent(expr)
if !ok {
return Undefined, errSyntax()
}
switch ident {
case "new", "typeof", "void", "await", "in", "instanceof", "yield":
return Undefined, errSyntax()
case "true":
left = Bool(true)
case "false":
left = Bool(false)
case "NaN":
left = Float64(math.NaN())
case "Infinity":
left = Float64(math.Inf(+1))
case "undefined":
left = Undefined
case "null":
left = Null
default:
var err error
left, err = getRefValue(false, Undefined, ident, false,
ctx.base)
if err != nil {
return Undefined, err
}
}
leftReady = true
expr = expr[len(ident):]
leftIdent = ident
}
var leftLeft Value
var hasLeftLeft bool
// read each chained component
optChain := false
for {
// There are more components to read
expr = trim(expr)
if len(expr) == 0 {
break
}
switch expr[0] {
case '?':
// Optional chaining
if len(expr) == 1 || expr[1] != '.' {
return Undefined, errSyntax()
}
expr = expr[1:]
optChain = true
fallthrough
case '.':
// Member Access
expr = expr[1:]
expr = trim(expr)
ident, ok := readIdent(expr)
if !ok {
return Undefined, errSyntax()
}
val, err := getRefValue(true, left, ident, optChain, ctx.base)
if err != nil {
return Undefined, err
}
leftLeft = left
hasLeftLeft = true
left = val
expr = expr[len(ident):]
leftIdent = ident
case '(', '[':
g, err := readGroup(expr)
if err != nil {
return Undefined, err
}
if g[0] == '(' {
// Function call
if left.kind != funcKind {
return Undefined,
fmt.Errorf("Uncaught TypeError: %s is not a function",
leftIdent)
}
var val Value
if ctx.base != nil && ctx.base.Extender != nil {
var info CallInfo
info.Chain = hasLeftLeft
info.Value = leftLeft
info.Ident = left.funcIdent()
args, err := multiExprsToArray(g[1:len(g)-1], ctx)
if err != nil {
return Undefined, err
}
info.Args = args
val, err = ctx.base.Extender.Call(info, ctx.base)
if err != nil {
return Undefined, errCall(err)
}
}
leftLeft = left
hasLeftLeft = true
left = val
} else {
// Computed Member Access
last, err := evalExpr(g[1:len(g)-1], ctx)
if err != nil {
return Undefined, err
}
ident := last.String()
val, err := getRefValue(true, left, ident, optChain,
ctx.base)
if err != nil {
return Undefined, err
}
leftLeft = left
hasLeftLeft = true
left = val
}
expr = expr[len(g):]
default:
return Undefined, errSyntax()
}
}
return left, nil
}
func multiExprsToArray(expr string, ctx *evalContext) (Value, error) {
var arr []Value
_, err := EvalForEach(expr, func(value Value) error {
arr = append(arr, value)
return nil
}, ctx.base)
if err != nil {
return Undefined, err
}
return Array(arr), nil
}
func getRefValue(chain bool, left Value, ident string, optChain bool,
ctx *Context,
) (Value, error) {
val, err := func() (Value, error) {
if ctx == nil || ctx.Extender == nil {
return Undefined, errUndefined(ident, chain)
}
info := RefInfo{Chain: chain, Value: left, Ident: ident}
val, err := ctx.Extender.Ref(info, ctx)
if err != nil {
return Undefined, errReference(err)
}
if val.kind == undefKind && left.kind == undefKind {
return Undefined, errUndefined(ident, chain)
}
return val, nil
}()
if err != nil {
var skipErr bool
if optChain {
if err, ok := err.(*errEval); ok {
if err.udef {
skipErr = true
}
}
}
if !skipErr {
return Undefined, err
}
}
return val, nil
}
func readIDStart(expr string) (int, bool) {
if len(expr) == 0 {
return 0, true
}
if expr[0] == '$' || expr[0] == '_' ||
(expr[0] >= 'A' && expr[0] <= 'Z') ||
(expr[0] >= 'a' && expr[0] <= 'z') {
return 1, true
}
return 0, false
}
func readIDContinue(expr string) (int, bool) {
if len(expr) == 0 {
return 0, true
}
if expr[0] == '$' || expr[0] == '_' ||
(expr[0] >= 'A' && expr[0] <= 'Z') ||
(expr[0] >= 'a' && expr[0] <= 'z') ||
(expr[0] >= '0' && expr[0] <= '9') {
return 1, true
}
return 0, false
}
func readIdent(expr string) (ident string, ok bool) {
// Only ascii identifiers for now
var i int
var z int
z, ok = readIDStart(expr[i:])
if !ok || z == 0 {
return "", false
}
i += z
for {
z, ok = readIDContinue(expr[i:])
if !ok || z == 0 {
return expr[:i], true
}
i += z
}
}
// parseString parses a Javascript encoded string.
// Adapted from the GJSON project.
func parseString(data string) (out, raw string, ok bool) {
var esc bool
if len(data) < 2 {
return "", "", false
}
qch := data[0]
for i := 1; i < len(data); i++ {
if data[i] < ' ' {
break
}
if data[i] == '\\' {
esc = true
i++
if i == len(data) {
return "", "", false
}
switch data[i] {
case 'u':
if i+1 < len(data) && data[i+1] == '{' {
i += 2
var end bool
for ; i < len(data); i++ {
if data[i] == '}' {
end = true
break
}
if !ishex(data[i]) {
return "", "", false
}
}
if !end {
return "", "", false
}
} else {
for j := 0; j < 4; j++ {
i++
if i >= len(data) || !ishex(data[i]) {
return "", "", false
}
}
}
case 'x':
for j := 0; j < 2; j++ {
i++
if i >= len(data) || !ishex(data[i]) {
return "", "", false
}
}
}
} else if data[i] == qch {
// if i != len(data)-1 {
// return "", "", false
// }
s := data[1:i]
if esc {
s = unescapeString(s)
}
return s, data[:i+1], true
}
}
return "", "", false
}
// runeit returns the rune from the the \uXXXX
func runeit(data string, which byte) (r rune, n int) {
var x uint64
if which == 'x' {
x, _ = strconv.ParseUint(data[:2], 16, 64)
n = 2
} else {
var s, e int
if data[0] == '{' {
s = 1
n = len(data)
for i := 0; i < len(data); i++ {
if data[i] == '}' {
e = i
n = i + 1
break
}
}
} else {
e = 4
n = 4
}
x, _ = strconv.ParseUint(data[s:e], 16, 64)
}
return rune(x), n
}
// unescapeString unescapes a Javascript string.
// Adapted from the GJSON project.
// The input data must be prevalidates for correctness, and must only be called
// from the parseString operation.
func unescapeString(data string) string {
var str = make([]byte, 0, len(data))
for i := 0; i < len(data); i++ {
switch {
default:
str = append(str, data[i])
case data[i] == '\\':
i++
switch data[i] {
case '0':
str = append(str, 0)
case 'b':
str = append(str, '\b')
case 'f':
str = append(str, '\f')
case 'n':
str = append(str, '\n')
case 'r':
str = append(str, '\r')
case 't':
str = append(str, '\t')
case 'v':
str = append(str, '\v')
case 'u':
i++
r, n := runeit(data[i:], 'u')
i += n
if utf16.IsSurrogate(r) {
// need another code
if len(data[i:]) >= 6 && data[i] == '\\' &&
data[i+1] == 'u' {
// we expect it to be correct so just consume it
i += 2
r2, n := runeit(data[i:], 'u')
i += n
r = utf16.DecodeRune(r, r2)
}
}
// provide enough space to encode the largest utf8 possible
str = appendRune(str, r)
i-- // backtrack index by one
case 'x':
i++
r, n := runeit(data[i:], 'x')
i += n
str = appendRune(str, r)
i-- // backtrack index by one
default:
str = append(str, data[i])
}
}
}
return string(str)
}
func appendRune(dst []byte, r rune) []byte {
// provide enough space to encode the largest utf8 possible
dst = append(dst, 0, 0, 0, 0, 0, 0, 0, 0)
n := utf8.EncodeRune(dst[len(dst)-8:], r)
return dst[:len(dst)-8+n]
}
func fact(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
right, err := evalAtom(expr, ctx)
if err != nil {
return Undefined, err
}
switch op {
case '*':
return mul(left, right, ctx)
case '/':
return div(left, right, ctx)
case '%':
return mod(left, right, ctx)
default:
return right, nil
}
}
func evalFacts(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '*', '/', '%':
left, err = fact(left, op, expr[s:i], ctx)
if err != nil {
return Undefined, err
}
op = expr[i]
s = i + 1
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
}
}
return fact(left, op, expr[s:], ctx)
}
func sum(left Value, op byte, expr string, neg, end bool,
ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
// parse factors of expression
right, err := evalAuto(stepSums<<1, expr, ctx)
if err != nil {
return Undefined, err
}
if neg {
// make right negative
right, err = mul(right, Float64(-1), ctx)
if err != nil {
return Undefined, err
}
}
switch op {
case '+':
return add(left, right, ctx)
case '-':
return sub(left, right, ctx)
default:
return right, nil
}
}
func evalSums(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
var fill bool
var neg bool
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '-', '+':
if !fill {
if i > 0 && expr[i-1] == expr[i] {
// -- not allowed
return Undefined, errSyntax()
}
if expr[i] == '-' {
neg = !neg
}
s = i + 1
continue
}
if i > 0 && (expr[i-1] == 'e' || expr[i-1] == 'E') {
// scientific notation
continue
}
if neg {
if s > 0 && s < len(expr) && expr[s-1] == '-' &&
expr[s] >= '0' && expr[s] <= '9' {
s--
neg = false
}
}
left, err = sum(left, op, expr[s:i], neg, false, ctx)
if err != nil {
return Undefined, err
}
op = expr[i]
s = i + 1
fill = false
neg = false
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
fill = true
default:
if !fill && !isspace(expr[i]) {
fill = true
}
}
}
if neg {
if s > 0 && s < len(expr) && expr[s-1] == '-' &&
expr[s] >= '0' && expr[s] <= '9' {
s--
neg = false
}
}
return sum(left, op, expr[s:], neg, true, ctx)
}
func comp(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
// parse next expression
right, err := evalAuto(stepComps<<1, expr, ctx)
if err != nil {
return Undefined, err
}
switch op {
case '<':
return lt(left, right, ctx)
case '<' + 32:
return lte(left, right, ctx)
case '>':
return gt(left, right, ctx)
case '>' + 32:
return gte(left, right, ctx)
default:
return right, nil
}
}
func evalComps(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '<', '>':
opch := expr[i]
opsz := 1
if i < len(expr)-1 && expr[i+1] == '=' {
opch += 32
opsz++
}
left, err = comp(left, op, expr[s:i], ctx)
if err != nil {
return Undefined, err
}
op = opch
i = i + opsz - 1
s = i + 1
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
}
}
return comp(left, op, expr[s:], ctx)
}
func equal(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
var neg bool
var boolit bool
expr = trim(expr)
for {
if len(expr) == 0 {
return Undefined, errSyntax()
}
if expr[0] != '!' {
break
}
neg = !neg
boolit = true
expr = expr[1:]
expr = trim(expr)
}
// parse next expression
right, err := evalAuto(stepEquality<<1, expr, ctx)
if err != nil {
return Undefined, err
}
if boolit {
if right.kind != boolKind {
right = Bool(right.Bool())
}
if neg {
right = Bool(!right.asBool())
}
}
switch op {
case '=':
return eq(left, right, ctx)
case '!':
return neq(left, right, ctx)
case '=' + 32:
return seq(left, right, ctx)
case '!' + 32:
return sneq(left, right, ctx)
default:
return right, nil
}
}
func evalEquality(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '=', '!':
opch := expr[i]
opsz := 1
switch opch {
case '=':
if i > 0 && (expr[i-1] == '>' || expr[i-1] == '<') {
continue
}
if i == len(expr)-1 || expr[i+1] != '=' {
return Undefined, errSyntax()
}
opsz++
case '!':
if i == len(expr)-1 || expr[i+1] != '=' {
continue
}
opsz++
}
if i+2 < len(expr) && expr[i+2] == '=' {
// strict
opch += 32
opsz++
}
left, err = equal(left, op, expr[s:i], ctx)
if err != nil {
return Undefined, err
}
op = opch
i = i + opsz - 1
s = i + 1
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
}
}
return equal(left, op, expr[s:], ctx)
}
func bitwiseXOR(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
right, err := evalAuto(stepBitwiseXOR<<1, expr, ctx)
if err != nil {
return Undefined, err
}
switch op {
case '^':
return xor(left, right, ctx)
default:
return right, nil
}
}
func evalBitwiseXOR(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '^':
left, err = bitwiseXOR(left, op, expr[s:i], ctx)
if err != nil {
return Undefined, err
}
op = expr[i]
s = i + 1
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
}
}
return bitwiseXOR(left, op, expr[s:], ctx)
}
func bitwiseOR(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
right, err := evalAuto(stepBitwiseOR<<1, expr, ctx)
if err != nil {
return Undefined, err
}
switch op {
case '|':
return bor(left, right, ctx)
default:
return right, nil
}
}
func evalBitwiseOR(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '|':
left, err = bitwiseOR(left, op, expr[s:i], ctx)
if err != nil {
return Undefined, err
}
op = expr[i]
s = i + 1
case '(', '[', '{', '"', '\'':
g, err := readGroup(expr[i:])
if err != nil {
return Undefined, err
}
i = i + len(g) - 1
}
}
return bitwiseOR(left, op, expr[s:], ctx)
}
func bitwiseAND(left Value, op byte, expr string, ctx *evalContext,
) (Value, error) {
expr = trim(expr)
if len(expr) == 0 {
return Undefined, errSyntax()
}
right, err := evalAuto(stepBitwiseAND<<1, expr, ctx)
if err != nil {
return Undefined, err
}
switch op {
case '&':
return band(left, right, ctx)
default:
return right, nil
}
}
func evalBitwiseAND(expr string, ctx *evalContext) (Value, error) {
var err error
var s int
var left Value
var op byte
for i := 0; i < len(expr); i++ {
switch expr[i] {
case '&':
left, err = bitwiseAND(left, op, expr[s:i], ctx)
if err != nil {