-
Notifications
You must be signed in to change notification settings - Fork 226
/
ast.go
767 lines (636 loc) · 18.9 KB
/
ast.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
/*
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package ast provides AST nodes and ancillary structures and algorithms.
package ast
import (
"fmt"
)
// Identifier represents a variable / parameter / field name.
// +gen set
type Identifier string
// Identifiers represents an Identifier slice.
type Identifiers []Identifier
// TODO(jbeda) implement interning of identifiers if necessary. The C++
// version does so.
// ---------------------------------------------------------------------------
// Context represents the surrounding context of a node (e.g. a function it's in)
type Context *string
// Node represents a node in the AST.
type Node interface {
Context() Context
Loc() *LocationRange
FreeVariables() Identifiers
SetFreeVariables(Identifiers)
SetContext(Context)
// OpenFodder returns the fodder before the first token of an AST node.
// Since every AST node has opening fodder, it is defined here.
// If the AST node is left recursive (e.g. BinaryOp) then it is ambiguous
// where the fodder should be stored. This is resolved by storing it as
// far inside the tree as possible. OpenFodder returns a pointer to allow
// the caller to modify the fodder.
OpenFodder() *Fodder
}
// Nodes represents a Node slice.
type Nodes []Node
// ---------------------------------------------------------------------------
// NodeBase holds fields common to all node types.
type NodeBase struct {
// This is the fodder that precedes the first token of the node.
// If the node is left-recursive, i.e. the first token is actually
// a token of a sub-expression, then Fodder is nil.
Fodder Fodder
Ctx Context
FreeVars Identifiers
LocRange LocationRange
}
// NewNodeBase creates a new NodeBase from initial LocationRange and
// Identifiers.
func NewNodeBase(loc LocationRange, fodder Fodder, freeVariables Identifiers) NodeBase {
return NodeBase{
LocRange: loc,
Fodder: fodder,
FreeVars: freeVariables,
}
}
// NewNodeBaseLoc creates a new NodeBase from an initial LocationRange.
func NewNodeBaseLoc(loc LocationRange, fodder Fodder) NodeBase {
return NewNodeBase(loc, fodder, []Identifier{})
}
// Loc returns a NodeBase's loc.
func (n *NodeBase) Loc() *LocationRange {
return &n.LocRange
}
// OpenFodder returns a NodeBase's opening fodder.
func (n *NodeBase) OpenFodder() *Fodder {
return &n.Fodder
}
// FreeVariables returns a NodeBase's freeVariables.
func (n *NodeBase) FreeVariables() Identifiers {
return n.FreeVars
}
// SetFreeVariables sets a NodeBase's freeVariables.
func (n *NodeBase) SetFreeVariables(idents Identifiers) {
n.FreeVars = idents
}
// Context returns a NodeBase's context.
func (n *NodeBase) Context() Context {
return n.Ctx
}
// SetContext sets a NodeBase's context.
func (n *NodeBase) SetContext(context Context) {
n.Ctx = context
}
// ---------------------------------------------------------------------------
// IfSpec represents an if-specification in a comprehension.
type IfSpec struct {
Expr Node
IfFodder Fodder
}
// ForSpec represents a for-specification in a comprehension.
// Example:
// expr for x in arr1 for y in arr2 for z in arr3
// The order is the same as in python, i.e. the leftmost is the outermost.
//
// Our internal representation reflects how they are semantically nested:
// ForSpec(z, outer=ForSpec(y, outer=ForSpec(x, outer=nil)))
// Any ifspecs are attached to the relevant ForSpec.
//
// Ifs are attached to the one on the left, for example:
// expr for x in arr1 for y in arr2 if x % 2 == 0 for z in arr3
// The if is attached to the y forspec.
//
// It desugares to:
//
// flatMap(\x ->
// flatMap(\y ->
// flatMap(\z -> [expr], arr3)
// arr2)
// arr3)
type ForSpec struct {
ForFodder Fodder
VarFodder Fodder
Conditions []IfSpec
Outer *ForSpec
Expr Node
VarName Identifier
InFodder Fodder
}
// ---------------------------------------------------------------------------
// Apply represents a function call
type Apply struct {
Target Node
FodderLeft Fodder
Arguments Arguments
FodderRight Fodder
TailStrictFodder Fodder
NodeBase
// Always false if there were no arguments.
TrailingComma bool
TailStrict bool
}
// NamedArgument represents a named argument to function call x=1.
type NamedArgument struct {
NameFodder Fodder
Name Identifier
EqFodder Fodder
Arg Node
CommaFodder Fodder
}
// CommaSeparatedExpr represents an expression that is an element of a
// comma-separated list of expressions (e.g. in an array or the arguments of a
// call)
type CommaSeparatedExpr struct {
Expr Node
CommaFodder Fodder
}
// Arguments represents positional and named arguments to a function call
// f(x, y, z=1).
type Arguments struct {
Positional []CommaSeparatedExpr
Named []NamedArgument
}
// ---------------------------------------------------------------------------
// ApplyBrace represents e { }. Desugared to e + { }.
type ApplyBrace struct {
Left Node
Right Node
NodeBase
}
// ---------------------------------------------------------------------------
// Array represents array constructors [1, 2, 3].
type Array struct {
Elements []CommaSeparatedExpr
CloseFodder Fodder
NodeBase
// Always false if there were no elements.
TrailingComma bool
}
// ---------------------------------------------------------------------------
// ArrayComp represents array comprehensions (which are like Python list
// comprehensions)
type ArrayComp struct {
Body Node
TrailingCommaFodder Fodder
Spec ForSpec
CloseFodder Fodder
NodeBase
TrailingComma bool
}
// ---------------------------------------------------------------------------
// Assert represents an assert expression (not an object-level assert).
//
// After parsing, message can be nil indicating that no message was
// specified. This AST is elimiated by desugaring.
type Assert struct {
Cond Node
Message Node
Rest Node
ColonFodder Fodder
SemicolonFodder Fodder
NodeBase
}
// ---------------------------------------------------------------------------
// BinaryOp represents a binary operator.
type BinaryOp int
// Binary operators
const (
BopMult BinaryOp = iota
BopDiv
BopPercent
BopPlus
BopMinus
BopShiftL
BopShiftR
BopGreater
BopGreaterEq
BopLess
BopLessEq
BopIn
BopManifestEqual
BopManifestUnequal
BopBitwiseAnd
BopBitwiseXor
BopBitwiseOr
BopAnd
BopOr
)
var bopStrings = []string{
BopMult: "*",
BopDiv: "/",
BopPercent: "%",
BopPlus: "+",
BopMinus: "-",
BopShiftL: "<<",
BopShiftR: ">>",
BopGreater: ">",
BopGreaterEq: ">=",
BopLess: "<",
BopLessEq: "<=",
BopIn: "in",
BopManifestEqual: "==",
BopManifestUnequal: "!=",
BopBitwiseAnd: "&",
BopBitwiseXor: "^",
BopBitwiseOr: "|",
BopAnd: "&&",
BopOr: "||",
}
// BopMap is a map from binary operator token strings to BinaryOp values.
var BopMap = map[string]BinaryOp{
"*": BopMult,
"/": BopDiv,
"%": BopPercent,
"+": BopPlus,
"-": BopMinus,
"<<": BopShiftL,
">>": BopShiftR,
">": BopGreater,
">=": BopGreaterEq,
"<": BopLess,
"<=": BopLessEq,
"in": BopIn,
"==": BopManifestEqual,
"!=": BopManifestUnequal,
"&": BopBitwiseAnd,
"^": BopBitwiseXor,
"|": BopBitwiseOr,
"&&": BopAnd,
"||": BopOr,
}
func (b BinaryOp) String() string {
if b < 0 || int(b) >= len(bopStrings) {
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised binary operator: %d", b))
}
return bopStrings[b]
}
// Binary represents binary operators.
type Binary struct {
Right Node
Left Node
OpFodder Fodder
NodeBase
Op BinaryOp
}
// ---------------------------------------------------------------------------
// Conditional represents if/then/else.
//
// After parsing, branchFalse can be nil indicating that no else branch
// was specified. The desugarer fills this in with a LiteralNull
type Conditional struct {
Cond Node
BranchTrue Node
BranchFalse Node
ThenFodder Fodder
ElseFodder Fodder
NodeBase
}
// ---------------------------------------------------------------------------
// Dollar represents the $ keyword
type Dollar struct{ NodeBase }
// ---------------------------------------------------------------------------
// Error represents the error e.
type Error struct {
Expr Node
NodeBase
}
// ---------------------------------------------------------------------------
// Function represents a function definition
type Function struct {
ParenLeftFodder Fodder
ParenRightFodder Fodder
Body Node
Parameters []Parameter
NodeBase
// Always false if there were no parameters.
TrailingComma bool
}
// Parameter represents a parameter of function.
// If DefaultArg is set, it's an optional named parameter.
// Otherwise, it's a positional parameter and EqFodder is not used.
type Parameter struct {
NameFodder Fodder
Name Identifier
CommaFodder Fodder
EqFodder Fodder
DefaultArg Node
LocRange LocationRange
}
// CommaSeparatedID represents an expression that is an element of a
// comma-separated list of identifiers (e.g. an array of parameters)
type CommaSeparatedID struct {
NameFodder Fodder
Name Identifier
CommaFodder Fodder
}
// ---------------------------------------------------------------------------
// Import represents import "file".
type Import struct {
File *LiteralString
NodeBase
}
// ---------------------------------------------------------------------------
// ImportStr represents importstr "file".
type ImportStr struct {
File *LiteralString
NodeBase
}
// ---------------------------------------------------------------------------
// ImportBin represents importbin "file".
type ImportBin struct {
File *LiteralString
NodeBase
}
// ---------------------------------------------------------------------------
// Index represents both e[e] and the syntax sugar e.f.
//
// One of index and id will be nil before desugaring. After desugaring id
// will be nil.
type Index struct {
Target Node
Index Node
// When Index is being used, this is the fodder before the ']'.
// When Id is being used, this is the fodder before the id.
RightBracketFodder Fodder
// When Index is being used, this is the fodder before the '['.
// When Id is being used, this is the fodder before the '.'.
LeftBracketFodder Fodder
//nolint: golint,stylecheck // keeping Id instead of ID for now to avoid breaking 3rd parties
Id *Identifier
NodeBase
}
// Slice represents an array slice a[begin:end:step].
type Slice struct {
Target Node
LeftBracketFodder Fodder
// Each of these can be nil
BeginIndex Node
EndColonFodder Fodder
EndIndex Node
StepColonFodder Fodder
Step Node
RightBracketFodder Fodder
NodeBase
}
// ---------------------------------------------------------------------------
// LocalBind is a helper struct for astLocal
type LocalBind struct {
VarFodder Fodder
// If Fun is set then its body == Body.
Body Node
EqFodder Fodder
Variable Identifier
// The fodder before the closing ',' or ';' (whichever it is)
CloseFodder Fodder
// There is no base fodder in Fun because there was no `function` keyword.
Fun *Function
LocRange LocationRange
}
// LocalBinds represents a LocalBind slice.
type LocalBinds []LocalBind
// Local represents local x = e; e. After desugaring, functionSugar is false.
type Local struct {
Binds LocalBinds
Body Node
NodeBase
}
// ---------------------------------------------------------------------------
// LiteralBoolean represents true and false
type LiteralBoolean struct {
NodeBase
Value bool
}
// ---------------------------------------------------------------------------
// LiteralNull represents the null keyword
type LiteralNull struct{ NodeBase }
// ---------------------------------------------------------------------------
// LiteralNumber represents a JSON number
type LiteralNumber struct {
OriginalString string
NodeBase
}
// ---------------------------------------------------------------------------
// LiteralStringKind represents the kind of a literal string.
type LiteralStringKind int
// Literal string kinds
const (
StringSingle LiteralStringKind = iota
StringDouble
StringBlock
VerbatimStringDouble
VerbatimStringSingle
)
// FullyEscaped returns true iff the literal string kind may contain escape
// sequences that require unescaping.
func (k LiteralStringKind) FullyEscaped() bool {
switch k {
case StringSingle, StringDouble:
return true
case StringBlock, VerbatimStringDouble, VerbatimStringSingle:
return false
}
panic(fmt.Sprintf("Unknown string kind: %v", k))
}
// LiteralString represents a JSON string
type LiteralString struct {
Value string
BlockIndent string
BlockTermIndent string
NodeBase
Kind LiteralStringKind
}
// ---------------------------------------------------------------------------
// ObjectFieldKind represents the kind of an object field.
type ObjectFieldKind int
// Kinds of object fields
const (
// In the following:
// <colon> is a short-hand for
// <opF> ( ':' | '::' | ':::' | '+:' | '+::' | '+:::' )
// f1, f2, f3, opF and commaF refer to the various Fodder fields.
// For brevity, we omit the syntax for method sugar, which applies to all
// but ObjectAssert below.
// <f1> 'assert' <expr2> '[' <opF> ':' <expr3> ']' <commaF>
// where expr3 can be nil
ObjectAssert ObjectFieldKind = iota
// <f1> <id> <colon> <expr2> <commaF>
ObjectFieldID
// <f1> '[' <expr1> <f2> ']' <colon> <expr2> <commaF>
ObjectFieldExpr
// <expr1> <colon> <expr2> <commaF>
ObjectFieldStr
// <f1> 'local' <f2> <id> '=' <expr2> <commaF>
ObjectLocal
)
// ObjectFieldHide represents the visibility of an object field.
type ObjectFieldHide int
// Object field visibilities
const (
ObjectFieldHidden ObjectFieldHide = iota // f:: e
ObjectFieldInherit // f: e
ObjectFieldVisible // f::: e
)
// ObjectField represents a field of an object or object comprehension.
// TODO(sbarzowski) consider having separate types for various kinds
type ObjectField struct {
// f(x, y, z): ... (ignore if kind == astObjectAssert)
// If Method is set then Expr2 == Method.Body.
// There is no base fodder in Method because there was no `function`
// keyword.
Method *Function
//nolint: golint,stylecheck // keeping Id instead of ID for now to avoid breaking 3rd parties
Id *Identifier
Fodder2 Fodder
Fodder1 Fodder
OpFodder Fodder
CommaFodder Fodder
Expr1 Node // Not in scope of the object
Expr2, Expr3 Node // In scope of the object (can see self).
LocRange LocationRange
Kind ObjectFieldKind
Hide ObjectFieldHide // (ignore if kind != astObjectFieldID/Expr/Str)
SuperSugar bool // +: (ignore if kind != astObjectFieldID/Expr/Str)
}
// ObjectFieldLocalNoMethod creates a non-method local object field.
func ObjectFieldLocalNoMethod(id *Identifier, body Node, loc LocationRange) ObjectField {
return ObjectField{
Kind: ObjectLocal,
Hide: ObjectFieldVisible,
Id: id,
Expr2: body,
LocRange: loc,
}
}
// ObjectFields represents an ObjectField slice.
type ObjectFields []ObjectField
// Object represents object constructors { f: e ... }.
//
// The trailing comma is only allowed if len(fields) > 0. Converted to
// DesugaredObject during desugaring.
type Object struct {
Fields ObjectFields
CloseFodder Fodder
NodeBase
TrailingComma bool
}
// ---------------------------------------------------------------------------
// DesugaredObjectField represents a desugared object field.
type DesugaredObjectField struct {
Name Node
Body Node
LocRange LocationRange
Hide ObjectFieldHide
PlusSuper bool
}
// DesugaredObjectFields represents a DesugaredObjectField slice.
type DesugaredObjectFields []DesugaredObjectField
// DesugaredObject represents object constructors { f: e ... } after
// desugaring.
//
// The assertions either return true or raise an error.
type DesugaredObject struct {
Asserts Nodes
Fields DesugaredObjectFields
Locals LocalBinds
NodeBase
}
// ---------------------------------------------------------------------------
// ObjectComp represents object comprehension
//
// { [e]: e for x in e for.. if... }.
type ObjectComp struct {
Fields ObjectFields
TrailingCommaFodder Fodder
CloseFodder Fodder
Spec ForSpec
NodeBase
TrailingComma bool
}
// ---------------------------------------------------------------------------
// Parens represents parentheses
//
// ( e )
type Parens struct {
Inner Node
CloseFodder Fodder
NodeBase
}
// ---------------------------------------------------------------------------
// Self represents the self keyword.
type Self struct{ NodeBase }
// ---------------------------------------------------------------------------
// SuperIndex represents the super[e] and super.f constructs.
//
// Either index or identifier will be set before desugaring. After desugaring, id will be
// nil.
type SuperIndex struct {
// If super.f, the fodder before the 'f'
// If super[e], the fodder before the ']'.
IDFodder Fodder
Index Node
// If super.f, the fodder before the '.'
// If super[e], the fodder before the '['.
DotFodder Fodder
//nolint: golint,stylecheck // keeping Id instead of ID for now to avoid breaking 3rd parties
Id *Identifier
NodeBase
}
// InSuper represents the e in super construct.
type InSuper struct {
Index Node
InFodder Fodder
SuperFodder Fodder
NodeBase
}
// ---------------------------------------------------------------------------
// UnaryOp represents a unary operator.
type UnaryOp int
// Unary operators
const (
UopNot UnaryOp = iota
UopBitwiseNot
UopPlus
UopMinus
)
var uopStrings = []string{
UopNot: "!",
UopBitwiseNot: "~",
UopPlus: "+",
UopMinus: "-",
}
// UopMap is a map from unary operator token strings to UnaryOp values.
var UopMap = map[string]UnaryOp{
"!": UopNot,
"~": UopBitwiseNot,
"+": UopPlus,
"-": UopMinus,
}
func (u UnaryOp) String() string {
if u < 0 || int(u) >= len(uopStrings) {
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised unary operator: %d", u))
}
return uopStrings[u]
}
// Unary represents unary operators.
type Unary struct {
Expr Node
NodeBase
Op UnaryOp
}
// ---------------------------------------------------------------------------
// Var represents variables.
type Var struct {
//nolint: golint,stylecheck // keeping Id instead of ID for now to avoid breaking 3rd parties
Id Identifier
NodeBase
}
// ---------------------------------------------------------------------------