-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary.go
497 lines (453 loc) · 12.9 KB
/
binary.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
package estree
import (
"encoding/json"
"fmt"
)
// BinaryOperator is the operator token of a BinaryExpression, which takes two
// operands and performs operations such as arithmetic or comparisons.
type BinaryOperator string
var (
Equal BinaryOperator = "=="
NotEqual BinaryOperator = "!="
StrictEqual BinaryOperator = "==="
StrictNotEqual BinaryOperator = "!=="
LessThan BinaryOperator = "<"
LessThanOrEqual BinaryOperator = "<="
GreaterThan BinaryOperator = ">"
GreaterThanOrEqual BinaryOperator = ">="
LeftShift BinaryOperator = "<<"
SignedRightShift BinaryOperator = ">>"
UnsignedRightShift BinaryOperator = ">>>"
Add BinaryOperator = "+"
Subtract BinaryOperator = "-"
Multiply BinaryOperator = "*"
Divide BinaryOperator = "/"
Remainder BinaryOperator = "%"
BitwiseOr BinaryOperator = "|"
BitwiseXor BinaryOperator = "^"
BitwiseAnd BinaryOperator = "&"
In BinaryOperator = "in"
InstanceOf BinaryOperator = "instanceof"
)
func (bo BinaryOperator) GoString() string {
switch bo {
case Equal:
return "Equal"
case NotEqual:
return "NotEqual"
case StrictEqual:
return "StrictEqual"
case StrictNotEqual:
return "StrictNotEqual"
case LessThan:
return "LessThan"
case LessThanOrEqual:
return "LessThanOrEqual"
case GreaterThan:
return "GreaterThan"
case GreaterThanOrEqual:
return "GreaterThanOrEqual"
case LeftShift:
return "LeftShift"
case SignedRightShift:
return "SignedRightShift"
case UnsignedRightShift:
return "UnsignedRightShift"
case Add:
return "Add"
case Subtract:
return "Subtract"
case Multiply:
return "Multiply"
case Divide:
return "Divide"
case Remainder:
return "Modulo"
case BitwiseOr:
return "BitwiseOr"
case BitwiseXor:
return "BitwiseXor"
case BitwiseAnd:
return "BitwiseAnd"
case In:
return "In"
case InstanceOf:
return "InstanceOf"
}
return fmt.Sprintf("%q", bo)
}
func (bo BinaryOperator) IsValid() bool {
switch bo {
case Equal, NotEqual, StrictEqual, StrictNotEqual, LessThan,
LessThanOrEqual, GreaterThan, GreaterThanOrEqual, LeftShift,
SignedRightShift, UnsignedRightShift, Add, Subtract, Multiply, Divide,
Remainder, BitwiseOr, BitwiseXor, BitwiseAnd, In, InstanceOf:
return true
}
return false
}
func (bo BinaryOperator) MinVersion() Version { return ES5 }
// BinaryExpression is a binary (two operand) expression.
type BinaryExpression struct {
baseExpression
Loc SourceLocation
Operator BinaryOperator
Left, Right Expression
}
func (BinaryExpression) Type() string { return "BinaryExpression" }
func (be BinaryExpression) Location() SourceLocation { return be.Loc }
func (be BinaryExpression) MinVersion() Version {
return be.Operator.MinVersion()
}
func (be BinaryExpression) IsZero() bool {
return be.Loc.IsZero() &&
be.Operator == "" &&
(be.Left == nil || be.Left.IsZero()) &&
(be.Right == nil || be.Right.IsZero())
}
func (be BinaryExpression) Walk(v Visitor) {
if v = v.Visit(be); v != nil {
defer v.Visit(nil)
if be.Left != nil {
be.Left.Walk(v)
}
if be.Right != nil {
be.Right.Walk(v)
}
}
}
func (be BinaryExpression) Errors() []error {
c := nodeChecker{Node: be}
c.require(be.Left, "left-hand expression")
if !be.Operator.IsValid() {
c.appendf("%w binary operator %q", ErrWrongValue, be.Operator)
}
c.require(be.Right, "right-hand expression")
return c.errors()
}
func (be BinaryExpression) MarshalJSON() ([]byte, error) {
x := nodeToMap(be)
x["operator"] = be.Operator
x["left"] = be.Left
x["right"] = be.Right
return json.Marshal(x)
}
func (be *BinaryExpression) UnmarshalJSON(b []byte) error {
var x struct {
Type string `json:"type"`
Loc SourceLocation `json:"loc"`
Operator BinaryOperator `json:"operator"`
Left json.RawMessage `json:"left"`
Right json.RawMessage `json:"right"`
}
err := json.Unmarshal(b, &x)
if err == nil && x.Type != be.Type() {
err = fmt.Errorf("%w %s, got %q", ErrWrongType, be.Type(), x.Type)
}
if err == nil {
be.Loc = x.Loc
if x.Operator.IsValid() {
be.Operator = x.Operator
} else {
err = fmt.Errorf("%w BinaryExpression.Operator %q", ErrWrongValue, x.Operator)
}
var err2 error
if be.Left, _, err2 = unmarshalExpression(x.Left); err == nil && err2 != nil {
err = err2
}
if be.Right, _, err = unmarshalExpression(x.Right); err == nil && err2 != nil {
err = err2
}
}
return err
}
// AssignmentOperator is the operator token for an AssignmentExpression, which
// assigns a right-hand operand to a left-hand operand.
type AssignmentOperator string
var (
Assign AssignmentOperator = "="
AddAssign AssignmentOperator = "+="
SubtractAssign AssignmentOperator = "-="
MultiplyAssign AssignmentOperator = "*="
DivideAssign AssignmentOperator = "/="
RemainderAssign AssignmentOperator = "%="
LeftShiftAssign AssignmentOperator = "<<="
SignedRightShiftAssign AssignmentOperator = ">>="
UnsignedRightShiftAssign AssignmentOperator = ">>>="
BitwiseOrAssign AssignmentOperator = "|="
BitwiseXorAssign AssignmentOperator = "^="
BitwiseAndAssign AssignmentOperator = "&="
)
func (ao AssignmentOperator) GoString() string {
switch ao {
case Assign:
return "Assign"
case AddAssign:
return "AddAssign"
case SubtractAssign:
return "SubtractAssign"
case MultiplyAssign:
return "MultiplyAssign"
case DivideAssign:
return "DivideAssign"
case RemainderAssign:
return "RemainderAssign"
case LeftShiftAssign:
return "LeftShiftAssign"
case SignedRightShiftAssign:
return "SignedRightShiftAssign"
case UnsignedRightShiftAssign:
return "UnsignedRightShiftAssign"
case BitwiseOrAssign:
return "BitwiseOrAssign"
case BitwiseXorAssign:
return "BitwiseXorAssign"
case BitwiseAndAssign:
return "BitwiseAndAssign"
}
return fmt.Sprintf("%q", ao)
}
func (ao AssignmentOperator) IsValid() bool {
switch ao {
case Assign, AddAssign, SubtractAssign, MultiplyAssign, DivideAssign,
RemainderAssign, LeftShiftAssign, SignedRightShiftAssign,
UnsignedRightShiftAssign, BitwiseOrAssign, BitwiseXorAssign,
BitwiseAndAssign:
return true
}
return false
}
// AssignmentExpression is an expression modifying the Left operand according
// to the Right.
type AssignmentExpression struct {
baseExpression
Loc SourceLocation
Operator AssignmentOperator
Left PatternOrExpression
Right Expression
}
func (AssignmentExpression) Type() string { return "AssignmentExpression" }
func (ae AssignmentExpression) Location() SourceLocation { return ae.Loc }
func (ae AssignmentExpression) IsZero() bool {
return ae.Loc.IsZero() &&
ae.Operator == "" &&
(ae.Left == nil || ae.Left.IsZero()) &&
(ae.Right == nil || ae.Right.IsZero())
}
func (ae AssignmentExpression) Walk(v Visitor) {
if v = v.Visit(ae); v != nil {
defer v.Visit(nil)
if ae.Left != nil {
ae.Left.Walk(v)
}
if ae.Right != nil {
ae.Right.Walk(v)
}
}
}
func (ae AssignmentExpression) Errors() []error {
c := nodeChecker{Node: ae}
c.require(ae.Left, "left-hand expression in assignment")
if !ae.Operator.IsValid() {
c.appendf("%w assignment operator %q", ErrWrongValue, ae.Operator)
}
c.require(ae.Right, "right-hand expression in assignment")
return c.errors()
}
func (ae AssignmentExpression) MarshalJSON() ([]byte, error) {
x := nodeToMap(ae)
x["operator"] = ae.Operator
x["left"] = ae.Left
x["right"] = ae.Right
return json.Marshal(x)
}
func (ae *AssignmentExpression) UnmarshalJSON(b []byte) error {
var x struct {
Type string `json:"type"`
Loc SourceLocation `json:"loc"`
Operator AssignmentOperator `json:"operator"`
Left json.RawMessage `json:"left"`
Right json.RawMessage `json:"right"`
}
err := json.Unmarshal(b, &x)
if err == nil && x.Type != ae.Type() {
err = fmt.Errorf("%w %s, got %q", ErrWrongType, ae.Type(), x.Type)
}
if err == nil {
ae.Loc = x.Loc
if x.Operator.IsValid() {
ae.Operator = x.Operator
} else {
err = fmt.Errorf("%w AssignmentExpression.Operator %q", ErrWrongValue, x.Operator)
}
var err2 error
if ae.Left, err2 = unmarshalPatternOrExpression(x.Left); err == nil && err2 != nil {
err = err2
}
if ae.Right, _, err = unmarshalExpression(x.Right); err == nil && err2 != nil {
err = err2
}
}
return err
}
// LogicalOperator is the operator token for a LogicalExpression, which
// expresses Boolean logic.
type LogicalOperator string
var (
Or LogicalOperator = "||"
And LogicalOperator = "&&"
)
func (lo LogicalOperator) GoString() string {
switch lo {
case Or:
return "Or"
case And:
return "And"
}
return fmt.Sprintf("%q", lo)
}
func (lo LogicalOperator) IsValid() bool {
switch lo {
case Or, And:
return true
}
return false
}
// LogicalExpression is an expression evaluating Boolean logic between two
// operands.
type LogicalExpression struct {
baseExpression
Loc SourceLocation
Operator LogicalOperator
Left, Right Expression
}
func (LogicalExpression) Type() string { return "LogicalExpression" }
func (le LogicalExpression) Location() SourceLocation { return le.Loc }
func (le LogicalExpression) IsZero() bool {
return le.Loc.IsZero() &&
le.Operator == "" &&
(le.Left == nil || le.Left.IsZero()) &&
(le.Right == nil || le.Right.IsZero())
}
func (le LogicalExpression) Walk(v Visitor) {
if v = v.Visit(le); v != nil {
defer v.Visit(nil)
if le.Left != nil {
le.Left.Walk(v)
}
if le.Right != nil {
le.Right.Walk(v)
}
}
}
func (le LogicalExpression) Errors() []error {
c := nodeChecker{Node: le}
c.require(le.Left, "left-hand expression")
if !le.Operator.IsValid() {
c.appendf("%w logical operator %q", ErrWrongValue, le.Operator)
}
c.require(le.Right, "right-hand expression")
return c.errors()
}
func (le LogicalExpression) MarshalJSON() ([]byte, error) {
x := nodeToMap(le)
x["operator"] = le.Operator
x["left"] = le.Left
x["right"] = le.Right
return json.Marshal(x)
}
func (le *LogicalExpression) UnmarshalJSON(b []byte) error {
var x struct {
Type string `json:"type"`
Loc SourceLocation `json:"loc"`
Operator LogicalOperator `json:"operator"`
Left json.RawMessage `json:"left"`
Right json.RawMessage `json:"right"`
}
err := json.Unmarshal(b, &x)
if err == nil && x.Type != le.Type() {
err = fmt.Errorf("%w %s, got %q", ErrWrongType, le.Type(), x.Type)
}
if err == nil {
le.Loc = x.Loc
if x.Operator.IsValid() {
le.Operator = x.Operator
} else {
err = fmt.Errorf("%w LogicalExpression.Operator %q", ErrWrongValue, x.Operator)
}
var err2 error
if le.Left, _, err2 = unmarshalExpression(x.Left); err == nil && err2 != nil {
err = err2
}
if le.Right, _, err = unmarshalExpression(x.Right); err == nil && err2 != nil {
err = err2
}
}
return err
}
// MemberExpression is a member expression, returning a value contained within
// Object, identified by Property.
type MemberExpression struct {
baseExpression
Loc SourceLocation
Object Expression
Property Expression
// Computed indicates the node corresponds to a computed (a[b]) member
// expression, and Property is an Expression. If Computed is false, the
// node corresponds to a static (a.b) member expression and Property is an
// Identifier.
Computed bool
}
func (MemberExpression) Type() string { return "MemberExpression" }
func (me MemberExpression) Location() SourceLocation { return me.Loc }
func (me MemberExpression) IsZero() bool {
return me.Loc.IsZero() &&
(me.Object == nil || me.Object.IsZero()) &&
(me.Property == nil || me.Property.IsZero())
}
func (me MemberExpression) Walk(v Visitor) {
if v = v.Visit(me); v != nil {
defer v.Visit(nil)
if me.Object != nil {
me.Object.Walk(v)
}
if me.Property != nil {
me.Property.Walk(v)
}
}
}
func (me MemberExpression) Errors() []error {
c := nodeChecker{Node: me}
c.require(me.Object, "object in member expression")
c.require(me.Property, "property or index in member expression")
return c.errors()
}
func (me MemberExpression) MarshalJSON() ([]byte, error) {
x := nodeToMap(me)
x["object"] = me.Object
x["property"] = me.Property
x["computed"] = me.Computed
return json.Marshal(x)
}
func (me *MemberExpression) UnmarshalJSON(b []byte) error {
var x struct {
Type string `json:"type"`
Loc SourceLocation `json:"loc"`
Object json.RawMessage `json:"object"`
Property json.RawMessage `json:"property"`
Computed bool `json:"computed"`
}
err := json.Unmarshal(b, &x)
if err == nil && x.Type != me.Type() {
err = fmt.Errorf("%w %s, got %q", ErrWrongType, me.Type(), x.Type)
}
if err == nil {
me.Loc, me.Computed = x.Loc, x.Computed
me.Object, _, err = unmarshalExpression(x.Object)
var err2 error
if me.Property, _, err2 = unmarshalExpression(x.Property); err == nil && err2 != nil {
err = err2
}
}
return err
}