-
Notifications
You must be signed in to change notification settings - Fork 153
/
ast.go
1301 lines (1095 loc) · 29.8 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
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
// Package ast declares the types used to represent the syntax tree for Flux source code.
package ast
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"
)
// Position represents a specific location in the source
type Position struct {
Line int `json:"line"` // Line is the line in the source marked by this position
Column int `json:"column"` // Column is the column in the source marked by this position
}
func (p Position) String() string {
return fmt.Sprintf("%d:%d", p.Line, p.Column)
}
func (p Position) Less(o Position) bool {
if p.Line == o.Line {
return p.Column < o.Column
}
return p.Line < o.Line
}
func (p Position) IsValid() bool {
return p.Line > 0 && p.Column > 0
}
// SourceLocation represents the location of a node in the AST
type SourceLocation struct {
File string `json:"file,omitempty"`
Start Position `json:"start"` // Start is the location in the source the node starts
End Position `json:"end"` // End is the location in the source the node ends
Source string `json:"source,omitempty"` // Source is optional raw source
}
func (l SourceLocation) String() string {
if l.File != "" {
return fmt.Sprintf("%s|%v-%v", l.File, l.Start, l.End)
}
return fmt.Sprintf("%v-%v", l.Start, l.End)
}
func (l SourceLocation) Less(o SourceLocation) bool {
if l.Start == o.Start {
return l.End.Less(o.End)
}
return l.Start.Less(o.Start)
}
func (l SourceLocation) IsValid() bool {
return l.Start.IsValid() && l.End.IsValid()
}
func (l *SourceLocation) Copy() *SourceLocation {
if l == nil {
return nil
}
nl := *l
return &nl
}
// Node represents a node in the InfluxDB abstract syntax tree.
type Node interface {
node()
Type() string // Type property is a string that contains the variant type of the node
Location() SourceLocation
Errs() []Error
Copy() Node
// All node must support json marshalling
json.Marshaler
}
func (*Package) node() {}
func (*File) node() {}
func (*PackageClause) node() {}
func (*ImportDeclaration) node() {}
func (*Block) node() {}
func (*BadStatement) node() {}
func (*ExpressionStatement) node() {}
func (*ReturnStatement) node() {}
func (*OptionStatement) node() {}
func (*BuiltinStatement) node() {}
func (*TestStatement) node() {}
func (*VariableAssignment) node() {}
func (*MemberAssignment) node() {}
func (*ArrayExpression) node() {}
func (*FunctionExpression) node() {}
func (*BinaryExpression) node() {}
func (*CallExpression) node() {}
func (*ConditionalExpression) node() {}
func (*LogicalExpression) node() {}
func (*MemberExpression) node() {}
func (*IndexExpression) node() {}
func (*PipeExpression) node() {}
func (*ObjectExpression) node() {}
func (*UnaryExpression) node() {}
func (*Property) node() {}
func (*Identifier) node() {}
func (*BooleanLiteral) node() {}
func (*DateTimeLiteral) node() {}
func (*DurationLiteral) node() {}
func (*FloatLiteral) node() {}
func (*IntegerLiteral) node() {}
func (*PipeLiteral) node() {}
func (*RegexpLiteral) node() {}
func (*StringLiteral) node() {}
func (*UnsignedIntegerLiteral) node() {}
// BaseNode holds the attributes every expression or statement should have
type BaseNode struct {
Loc *SourceLocation `json:"location,omitempty"`
Errors []Error `json:"errors,omitempty"`
}
// Location is the source location of the Node
func (b BaseNode) Location() SourceLocation {
if b.Loc == nil {
return SourceLocation{}
}
return *b.Loc
}
func (b BaseNode) Errs() []Error {
return b.Errors
}
func (b BaseNode) Copy() BaseNode {
// Note b is already shallow copy because of the non pointer receiver
b.Loc = b.Loc.Copy()
if len(b.Errors) > 0 {
cpy := make([]Error, len(b.Errors))
copy(cpy, b.Errors)
b.Errors = cpy
}
return b
}
// Error represents an error in the AST construction.
// The node that this is attached to is not valid.
type Error struct {
Msg string `json:"msg"`
}
func (e Error) Error() string {
return e.Msg
}
// Package represents a complete package source tree
type Package struct {
BaseNode
Path string `json:"path,omitempty"`
Package string `json:"package"`
Files []*File `json:"files"`
}
// Type is the abstract type
func (*Package) Type() string { return "Package" }
func (p *Package) Copy() Node {
if p == nil {
return p
}
np := new(Package)
*np = *p
np.BaseNode = p.BaseNode.Copy()
if len(p.Files) > 0 {
np.Files = make([]*File, len(p.Files))
for i, f := range p.Files {
np.Files[i] = f.Copy().(*File)
}
}
return np
}
// File represents a source from a single file
type File struct {
BaseNode
Name string `json:"name,omitempty"` // name of the file
Package *PackageClause `json:"package"`
Imports []*ImportDeclaration `json:"imports"`
Body []Statement `json:"body"`
}
// Type is the abstract type
func (*File) Type() string { return "File" }
func (f *File) Copy() Node {
if f == nil {
return f
}
nf := new(File)
*nf = *f
nf.BaseNode = f.BaseNode.Copy()
nf.Package = f.Package.Copy().(*PackageClause)
if len(f.Imports) > 0 {
nf.Imports = make([]*ImportDeclaration, len(f.Imports))
for i, s := range f.Imports {
nf.Imports[i] = s.Copy().(*ImportDeclaration)
}
}
if len(f.Body) > 0 {
nf.Body = make([]Statement, len(f.Body))
for i, s := range f.Body {
nf.Body[i] = s.Copy().(Statement)
}
}
return nf
}
// PackageClause defines the current package identifier.
type PackageClause struct {
BaseNode
Name *Identifier `json:"name"`
}
// Type is the abstract type
func (*PackageClause) Type() string { return "PackageClause" }
func (c *PackageClause) Copy() Node {
if c == nil {
return c
}
nc := new(PackageClause)
*nc = *c
nc.BaseNode = c.BaseNode.Copy()
nc.Name = c.Name.Copy().(*Identifier)
return nc
}
// ImportDeclaration declares a single import
type ImportDeclaration struct {
BaseNode
As *Identifier `json:"as"`
Path *StringLiteral `json:"path"`
}
// Type is the abstract type
func (*ImportDeclaration) Type() string { return "ImportDeclaration" }
func (d *ImportDeclaration) Copy() Node {
if d == nil {
return d
}
nd := new(ImportDeclaration)
*nd = *d
nd.BaseNode = d.BaseNode.Copy()
return nd
}
// Block is a set of statements
type Block struct {
BaseNode
Body []Statement `json:"body"`
}
// Type is the abstract type
func (*Block) Type() string { return "Block" }
func (s *Block) Copy() Node {
if s == nil {
return s
}
ns := new(Block)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
if len(s.Body) > 0 {
ns.Body = make([]Statement, len(s.Body))
for i, stmt := range s.Body {
ns.Body[i] = stmt.Copy().(Statement)
}
}
return ns
}
// Statement Perhaps we don't even want statements nor expression statements
type Statement interface {
Node
stmt()
}
func (*BadStatement) stmt() {}
func (*VariableAssignment) stmt() {}
func (*MemberAssignment) stmt() {}
func (*ExpressionStatement) stmt() {}
func (*ReturnStatement) stmt() {}
func (*OptionStatement) stmt() {}
func (*BuiltinStatement) stmt() {}
func (*TestStatement) stmt() {}
type Assignment interface {
Statement
assignment()
}
func (*VariableAssignment) assignment() {}
func (*MemberAssignment) assignment() {}
// BadStatement is a placeholder for statements for which no correct statement nodes
// can be created.
type BadStatement struct {
BaseNode
Text string `json:"text"`
}
// Type is the abstract type.
func (*BadStatement) Type() string { return "BadStatement" }
func (s *BadStatement) Copy() Node {
if s == nil {
return s
}
ns := *s
ns.BaseNode = s.BaseNode.Copy()
return &ns
}
// ExpressionStatement may consist of an expression that does not return a value and is executed solely for its side-effects.
type ExpressionStatement struct {
BaseNode
Expression Expression `json:"expression"`
}
// Type is the abstract type
func (*ExpressionStatement) Type() string { return "ExpressionStatement" }
func (s *ExpressionStatement) Copy() Node {
if s == nil {
return s
}
ns := new(ExpressionStatement)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
if s.Expression != nil {
ns.Expression = s.Expression.Copy().(Expression)
}
return ns
}
// ReturnStatement defines an Expression to return
type ReturnStatement struct {
BaseNode
Argument Expression `json:"argument"`
}
// Type is the abstract type
func (*ReturnStatement) Type() string { return "ReturnStatement" }
func (s *ReturnStatement) Copy() Node {
if s == nil {
return s
}
ns := new(ReturnStatement)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
if s.Argument != nil {
ns.Argument = s.Argument.Copy().(Expression)
}
return ns
}
// OptionStatement syntactically is a single variable declaration
type OptionStatement struct {
BaseNode
Assignment Assignment `json:"assignment"`
}
// Type is the abstract type
func (*OptionStatement) Type() string { return "OptionStatement" }
// Copy returns a deep copy of an OptionStatement Node
func (s *OptionStatement) Copy() Node {
if s == nil {
return s
}
ns := new(OptionStatement)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
if s.Assignment != nil {
ns.Assignment = s.Assignment.Copy().(Assignment)
}
return ns
}
// BuiltinStatement declares a builtin identifier and its type
type BuiltinStatement struct {
BaseNode
ID *Identifier `json:"id"`
// TODO(nathanielc): Add type expression here
// Type TypeExpression
}
// Type is the abstract type
func (*BuiltinStatement) Type() string { return "BuiltinStatement" }
// Copy returns a deep copy of an BuiltinStatement Node
func (s *BuiltinStatement) Copy() Node {
if s == nil {
return s
}
ns := new(BuiltinStatement)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
ns.ID = s.ID.Copy().(*Identifier)
return ns
}
// TestStatement declares a Flux test case
type TestStatement struct {
BaseNode
Assignment *VariableAssignment `json:"assignment"`
}
// Type is the abstract type
func (*TestStatement) Type() string { return "TestStatement" }
// Copy returns a deep copy of a TestStatement Node
func (s *TestStatement) Copy() Node {
if s == nil {
return s
}
ns := new(TestStatement)
*ns = *s
ns.BaseNode = s.BaseNode.Copy()
if s.Assignment != nil {
ns.Assignment = s.Assignment.Copy().(*VariableAssignment)
}
return ns
}
// VariableAssignment represents the declaration of a variable
type VariableAssignment struct {
BaseNode
ID *Identifier `json:"id"`
Init Expression `json:"init"`
}
// Type is the abstract type
func (*VariableAssignment) Type() string { return "VariableAssignment" }
func (d *VariableAssignment) Copy() Node {
if d == nil {
return d
}
nd := new(VariableAssignment)
*nd = *d
nd.BaseNode = d.BaseNode.Copy()
if d.Init != nil {
nd.Init = d.Init.Copy().(Expression)
}
return nd
}
type MemberAssignment struct {
BaseNode
Member *MemberExpression `json:"member"`
Init Expression `json:"init"`
}
func (*MemberAssignment) Type() string { return "MemberAssignment" }
func (a *MemberAssignment) Copy() Node {
if a == nil {
return a
}
na := new(MemberAssignment)
*na = *a
na.BaseNode = a.BaseNode.Copy()
if a.Member != nil {
na.Member = a.Member.Copy().(*MemberExpression)
}
if a.Init != nil {
na.Init = a.Init.Copy().(Expression)
}
return na
}
// Expression represents an action that can be performed by InfluxDB that can be evaluated to a value.
type Expression interface {
Node
expression()
}
func (*ArrayExpression) expression() {}
func (*FunctionExpression) expression() {}
func (*BinaryExpression) expression() {}
func (*BooleanLiteral) expression() {}
func (*CallExpression) expression() {}
func (*ConditionalExpression) expression() {}
func (*DateTimeLiteral) expression() {}
func (*DurationLiteral) expression() {}
func (*FloatLiteral) expression() {}
func (*Identifier) expression() {}
func (*IntegerLiteral) expression() {}
func (*LogicalExpression) expression() {}
func (*MemberExpression) expression() {}
func (*IndexExpression) expression() {}
func (*ObjectExpression) expression() {}
func (*PipeExpression) expression() {}
func (*PipeLiteral) expression() {}
func (*RegexpLiteral) expression() {}
func (*StringLiteral) expression() {}
func (*UnaryExpression) expression() {}
func (*UnsignedIntegerLiteral) expression() {}
// CallExpression represents a function call
type CallExpression struct {
BaseNode
Callee Expression `json:"callee"`
Arguments []Expression `json:"arguments,omitempty"`
}
// Type is the abstract type
func (*CallExpression) Type() string { return "CallExpression" }
func (e *CallExpression) Copy() Node {
if e == nil {
return e
}
ne := new(CallExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Callee != nil {
ne.Callee = e.Callee.Copy().(Expression)
}
if len(e.Arguments) > 0 {
ne.Arguments = make([]Expression, len(e.Arguments))
for i, arg := range e.Arguments {
ne.Arguments[i] = arg.Copy().(Expression)
}
}
return ne
}
type PipeExpression struct {
BaseNode
Argument Expression `json:"argument"`
Call *CallExpression `json:"call"`
}
// Type is the abstract type
func (*PipeExpression) Type() string { return "PipeExpression" }
func (e *PipeExpression) Copy() Node {
if e == nil {
return e
}
ne := new(PipeExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Argument != nil {
ne.Argument = e.Argument.Copy().(Expression)
}
ne.Call = e.Call.Copy().(*CallExpression)
return ne
}
// MemberExpression represents calling a property of a CallExpression
type MemberExpression struct {
BaseNode
Object Expression `json:"object"`
Property PropertyKey `json:"property"`
}
// Type is the abstract type
func (*MemberExpression) Type() string { return "MemberExpression" }
func (e *MemberExpression) Copy() Node {
if e == nil {
return e
}
ne := new(MemberExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Object != nil {
ne.Object = e.Object.Copy().(Expression)
}
if e.Property != nil {
ne.Property = e.Property.Copy().(PropertyKey)
}
return ne
}
// IndexExpression represents indexing into an array
type IndexExpression struct {
BaseNode
Array Expression `json:"array"`
Index Expression `json:"index"`
}
func (*IndexExpression) Type() string { return "IndexExpression" }
func (e *IndexExpression) Copy() Node {
if e == nil {
return e
}
ne := new(IndexExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Array != nil {
ne.Array = e.Array.Copy().(Expression)
}
if e.Index != nil {
ne.Index = e.Index.Copy().(Expression)
}
return ne
}
type FunctionExpression struct {
BaseNode
Params []*Property `json:"params"`
Body Node `json:"body"`
}
// Type is the abstract type
func (*FunctionExpression) Type() string { return "FunctionExpression" }
func (e *FunctionExpression) Copy() Node {
if e == nil {
return e
}
ne := new(FunctionExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if len(e.Params) > 0 {
ne.Params = make([]*Property, len(e.Params))
for i, param := range e.Params {
ne.Params[i] = param.Copy().(*Property)
}
}
if e.Body != nil {
ne.Body = e.Body.Copy()
}
return ne
}
// OperatorKind are Equality and Arithmatic operators.
// Result of evaluating an equality operator is always of type Boolean based on whether the
// comparison is true
// Arithmetic operators take numerical values (either literals or variables) as their operands
// and return a single numerical value.
type OperatorKind int
const (
opBegin OperatorKind = iota
MultiplicationOperator
DivisionOperator
AdditionOperator
SubtractionOperator
LessThanEqualOperator
LessThanOperator
GreaterThanEqualOperator
GreaterThanOperator
StartsWithOperator
InOperator
NotOperator
NotEmptyOperator
EmptyOperator
EqualOperator
NotEqualOperator
RegexpMatchOperator
NotRegexpMatchOperator
opEnd
)
func (o OperatorKind) String() string {
return OperatorTokens[o]
}
// OperatorLookup converts the operators to OperatorKind
func OperatorLookup(op string) OperatorKind {
return operators[op]
}
func (o OperatorKind) MarshalText() ([]byte, error) {
text, ok := OperatorTokens[o]
if !ok {
return nil, fmt.Errorf("unknown operator %d", int(o))
}
return []byte(text), nil
}
func (o *OperatorKind) UnmarshalText(data []byte) error {
var ok bool
*o, ok = operators[string(data)]
if !ok {
return fmt.Errorf("unknown operator %q", string(data))
}
return nil
}
// BinaryExpression use binary operators act on two operands in an expression.
// BinaryExpression includes relational and arithmatic operators
type BinaryExpression struct {
BaseNode
Operator OperatorKind `json:"operator"`
Left Expression `json:"left"`
Right Expression `json:"right"`
}
// Type is the abstract type
func (*BinaryExpression) Type() string { return "BinaryExpression" }
func (e *BinaryExpression) Copy() Node {
if e == nil {
return e
}
ne := new(BinaryExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Left != nil {
ne.Left = e.Left.Copy().(Expression)
}
if e.Right != nil {
ne.Right = e.Right.Copy().(Expression)
}
return ne
}
// UnaryExpression use operators act on a single operand in an expression.
type UnaryExpression struct {
BaseNode
Operator OperatorKind `json:"operator"`
Argument Expression `json:"argument"`
}
// Type is the abstract type
func (*UnaryExpression) Type() string { return "UnaryExpression" }
func (e *UnaryExpression) Copy() Node {
if e == nil {
return e
}
ne := new(UnaryExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Argument != nil {
ne.Argument = e.Argument.Copy().(Expression)
}
return ne
}
// LogicalOperatorKind are used with boolean (logical) values
type LogicalOperatorKind int
const (
logOpBegin LogicalOperatorKind = iota
AndOperator
OrOperator
logOpEnd
)
func (o LogicalOperatorKind) String() string {
return LogicalOperatorTokens[o]
}
// LogicalOperatorLookup converts the operators to LogicalOperatorKind
func LogicalOperatorLookup(op string) LogicalOperatorKind {
return logOperators[op]
}
func (o LogicalOperatorKind) MarshalText() ([]byte, error) {
text, ok := LogicalOperatorTokens[o]
if !ok {
return nil, fmt.Errorf("unknown logical operator %d", int(o))
}
return []byte(text), nil
}
func (o *LogicalOperatorKind) UnmarshalText(data []byte) error {
var ok bool
*o, ok = logOperators[string(data)]
if !ok {
return fmt.Errorf("unknown logical operator %q", string(data))
}
return nil
}
// LogicalExpression represent the rule conditions that collectively evaluate to either true or false.
// `or` expressions compute the disjunction of two boolean expressions and return boolean values.
// `and`` expressions compute the conjunction of two boolean expressions and return boolean values.
type LogicalExpression struct {
BaseNode
Operator LogicalOperatorKind `json:"operator"`
Left Expression `json:"left"`
Right Expression `json:"right"`
}
// Type is the abstract type
func (*LogicalExpression) Type() string { return "LogicalExpression" }
func (e *LogicalExpression) Copy() Node {
if e == nil {
return e
}
ne := new(LogicalExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Left != nil {
ne.Left = e.Left.Copy().(Expression)
}
if e.Right != nil {
ne.Right = e.Right.Copy().(Expression)
}
return ne
}
// ArrayExpression is used to create and directly specify the elements of an array object
type ArrayExpression struct {
BaseNode
Elements []Expression `json:"elements"`
}
// Type is the abstract type
func (*ArrayExpression) Type() string { return "ArrayExpression" }
func (e *ArrayExpression) Copy() Node {
if e == nil {
return e
}
ne := new(ArrayExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if len(e.Elements) > 0 {
ne.Elements = make([]Expression, len(e.Elements))
for i, el := range e.Elements {
ne.Elements[i] = el.Copy().(Expression)
}
}
return ne
}
// ObjectExpression allows the declaration of an anonymous object within a declaration.
type ObjectExpression struct {
BaseNode
Properties []*Property `json:"properties"`
}
// Type is the abstract type
func (*ObjectExpression) Type() string { return "ObjectExpression" }
func (e *ObjectExpression) Copy() Node {
if e == nil {
return e
}
ne := new(ObjectExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if len(e.Properties) > 0 {
ne.Properties = make([]*Property, len(e.Properties))
for i, p := range e.Properties {
ne.Properties[i] = p.Copy().(*Property)
}
}
return ne
}
// ConditionalExpression selects one of two expressions, `Alternate` or `Consequent`
// depending on a third, boolean, expression, `Test`.
type ConditionalExpression struct {
BaseNode
Test Expression `json:"test"`
Consequent Expression `json:"consequent"`
Alternate Expression `json:"alternate"`
}
// Type is the abstract type
func (*ConditionalExpression) Type() string { return "ConditionalExpression" }
func (e *ConditionalExpression) Copy() Node {
if e == nil {
return e
}
ne := new(ConditionalExpression)
*ne = *e
ne.BaseNode = e.BaseNode.Copy()
if e.Test != nil {
ne.Test = e.Test.Copy().(Expression)
}
if e.Alternate != nil {
ne.Alternate = e.Alternate.Copy().(Expression)
}
if e.Consequent != nil {
ne.Consequent = e.Consequent.Copy().(Expression)
}
return ne
}
// PropertyKey represents an object key
type PropertyKey interface {
Node
Key() string
}
// Property is the value associated with a key.
// A property's key can be either an identifier or string literal.
type Property struct {
BaseNode
Key PropertyKey `json:"key"`
Value Expression `json:"value"`
}
func (p *Property) Copy() Node {
if p == nil {
return p
}
np := new(Property)
*np = *p
np.BaseNode = p.BaseNode.Copy()
if p.Value != nil {
np.Value = p.Value.Copy().(Expression)
}
return np
}
// Type is the abstract type
func (*Property) Type() string { return "Property" }
// Identifier represents a name that identifies a unique Node
type Identifier struct {
BaseNode
Name string `json:"name"`
}
// Identifiers are valid object keys
func (i *Identifier) Key() string {
return i.Name
}
// Type is the abstract type
func (*Identifier) Type() string { return "Identifier" }
func (i *Identifier) Copy() Node {
if i == nil {
return i
}
ni := new(Identifier)
*ni = *i
ni.BaseNode = i.BaseNode.Copy()
return ni
}
// Literal is the lexical form for a literal expression which defines
// boolean, string, integer, number, duration, datetime or field values.
// Literals must be coerced explicitly.
type Literal interface {
Expression
literal() //lint:ignore U1000 Yes, this function is unused, but it's here to limit the implementers of the Literal interface.