-
Notifications
You must be signed in to change notification settings - Fork 138
/
type.go
993 lines (840 loc) Β· 22.5 KB
/
type.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
863
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
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* 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 old_parser
import (
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/parser/lexer"
)
const (
typeLeftBindingPowerOptional = 10 * (iota 1)
typeLeftBindingPowerReference
typeLeftBindingPowerRestriction
typeLeftBindingPowerInstantiation
)
type typeNullDenotationFunc func(parser *parser, token lexer.Token) (ast.Type, error)
var typeNullDenotations [lexer.TokenMax]typeNullDenotationFunc
type typeLeftDenotationFunc func(parser *parser, token lexer.Token, left ast.Type) (ast.Type, error)
type typeMetaLeftDenotationFunc func(
p *parser,
rightBindingPower int,
left ast.Type,
) (
result ast.Type,
err error,
done bool,
)
var typeLeftBindingPowers [lexer.TokenMax]int
var typeLeftDenotations [lexer.TokenMax]typeLeftDenotationFunc
var typeMetaLeftDenotations [lexer.TokenMax]typeMetaLeftDenotationFunc
func setTypeNullDenotation(tokenType lexer.TokenType, nullDenotation typeNullDenotationFunc) {
current := typeNullDenotations[tokenType]
if current != nil {
panic(errors.NewUnexpectedError(
"type null denotation for token %s already exists",
tokenType,
))
}
typeNullDenotations[tokenType] = nullDenotation
}
func setTypeLeftBindingPower(tokenType lexer.TokenType, power int) {
current := typeLeftBindingPowers[tokenType]
if current > power {
return
}
typeLeftBindingPowers[tokenType] = power
}
func setTypeLeftDenotation(tokenType lexer.TokenType, leftDenotation typeLeftDenotationFunc) {
current := typeLeftDenotations[tokenType]
if current != nil {
panic(errors.NewUnexpectedError(
"type left denotation for token %s already exists",
tokenType,
))
}
typeLeftDenotations[tokenType] = leftDenotation
}
func setTypeMetaLeftDenotation(tokenType lexer.TokenType, metaLeftDenotation typeMetaLeftDenotationFunc) {
current := typeMetaLeftDenotations[tokenType]
if current != nil {
panic(errors.NewUnexpectedError(
"type meta left denotation for token %s already exists",
tokenType,
))
}
typeMetaLeftDenotations[tokenType] = metaLeftDenotation
}
type prefixTypeFunc func(parser *parser, right ast.Type, tokenRange ast.Range) ast.Type
type postfixTypeFunc func(parser *parser, left ast.Type, tokenRange ast.Range) ast.Type
type literalType struct {
nullDenotation typeNullDenotationFunc
tokenType lexer.TokenType
}
type prefixType struct {
nullDenotation prefixTypeFunc
bindingPower int
tokenType lexer.TokenType
}
type postfixType struct {
leftDenotation postfixTypeFunc
bindingPower int
tokenType lexer.TokenType
}
func defineType(def any) {
switch def := def.(type) {
case prefixType:
tokenType := def.tokenType
setTypeNullDenotation(
tokenType,
func(parser *parser, token lexer.Token) (ast.Type, error) {
right, err := parseType(parser, def.bindingPower)
if err != nil {
return nil, err
}
return def.nullDenotation(parser, right, token.Range), nil
},
)
case postfixType:
tokenType := def.tokenType
setTypeLeftBindingPower(tokenType, def.bindingPower)
setTypeLeftDenotation(
tokenType,
func(p *parser, token lexer.Token, left ast.Type) (ast.Type, error) {
return def.leftDenotation(p, left, token.Range), nil
},
)
case literalType:
tokenType := def.tokenType
setTypeNullDenotation(tokenType, def.nullDenotation)
default:
panic(errors.NewUnreachableError())
}
}
func init() {
defineArrayType()
defineOptionalType()
defineReferenceType()
defineRestrictedOrDictionaryType()
defineFunctionType()
defineInstantiationType()
setTypeNullDenotation(
lexer.TokenIdentifier,
func(p *parser, token lexer.Token) (ast.Type, error) {
switch string(p.tokenSource(token)) {
case keywordAuth:
p.skipSpaceAndComments()
_, err := p.mustOne(lexer.TokenAmpersand)
if err != nil {
return nil, err
}
right, err := parseType(p, typeLeftBindingPowerReference)
if err != nil {
return nil, err
}
refType := ast.NewReferenceType(
p.memoryGauge,
nil,
right,
token.StartPos,
)
refType.LegacyAuthorized = true
return refType, nil
default:
return parseNominalTypeRemainder(p, token)
}
},
)
}
func parseNominalTypeRemainder(p *parser, token lexer.Token) (*ast.NominalType, error) {
var nestedIdentifiers []ast.Identifier
for p.current.Is(lexer.TokenDot) {
// Skip the dot
p.next()
nestedToken := p.current
if !nestedToken.Is(lexer.TokenIdentifier) {
return nil, p.syntaxError(
"expected identifier after %s, got %s",
lexer.TokenDot,
nestedToken.Type,
)
}
nestedIdentifier := p.tokenToIdentifier(nestedToken)
// Skip the identifier
p.next()
nestedIdentifiers = append(
nestedIdentifiers,
nestedIdentifier,
)
}
return ast.NewNominalType(
p.memoryGauge,
p.tokenToIdentifier(token),
nestedIdentifiers,
), nil
}
func defineArrayType() {
setTypeNullDenotation(
lexer.TokenBracketOpen,
func(p *parser, startToken lexer.Token) (ast.Type, error) {
elementType, err := parseType(p, lowestBindingPower)
if err != nil {
return nil, err
}
p.skipSpaceAndComments()
var size *ast.IntegerExpression
if p.current.Is(lexer.TokenSemicolon) {
// Skip the semicolon
p.nextSemanticToken()
if !p.current.Type.IsIntegerLiteral() {
p.reportSyntaxError("expected positive integer size for constant sized type")
// Skip the invalid non-integer literal token
p.next()
} else {
numberExpression, err := parseExpression(p, lowestBindingPower)
if err != nil {
return nil, err
}
integerExpression, ok := numberExpression.(*ast.IntegerExpression)
if !ok || integerExpression.Value.Sign() < 0 {
p.reportSyntaxError("expected positive integer size for constant sized type")
} else {
size = integerExpression
}
}
}
p.skipSpaceAndComments()
endToken, err := p.mustOne(lexer.TokenBracketClose)
if err != nil {
return nil, err
}
typeRange := ast.NewRange(
p.memoryGauge,
startToken.StartPos,
endToken.EndPos,
)
if size != nil {
return ast.NewConstantSizedType(
p.memoryGauge,
elementType,
size,
typeRange,
), nil
} else {
return ast.NewVariableSizedType(
p.memoryGauge,
elementType,
typeRange,
), nil
}
},
)
}
func defineOptionalType() {
defineType(postfixType{
tokenType: lexer.TokenQuestionMark,
bindingPower: typeLeftBindingPowerOptional,
leftDenotation: func(p *parser, left ast.Type, tokenRange ast.Range) ast.Type {
return ast.NewOptionalType(
p.memoryGauge,
left,
tokenRange.EndPos,
)
},
})
defineType(postfixType{
tokenType: lexer.TokenDoubleQuestionMark,
bindingPower: typeLeftBindingPowerOptional,
leftDenotation: func(p *parser, left ast.Type, tokenRange ast.Range) ast.Type {
return ast.NewOptionalType(
p.memoryGauge,
ast.NewOptionalType(
p.memoryGauge,
left,
tokenRange.StartPos,
),
tokenRange.EndPos,
)
},
})
}
func defineReferenceType() {
defineType(prefixType{
tokenType: lexer.TokenAmpersand,
bindingPower: typeLeftBindingPowerReference,
nullDenotation: func(p *parser, right ast.Type, tokenRange ast.Range) ast.Type {
return ast.NewReferenceType(
p.memoryGauge,
nil,
right,
tokenRange.StartPos,
)
},
})
}
func defineRestrictedOrDictionaryType() {
// For the null denotation it is not clear after the start
// if it is a restricted type or a dictionary type.
//
// If a colon is seen it is a dictionary type.
// If no colon is seen it is a restricted type.
setTypeNullDenotation(
lexer.TokenBraceOpen,
func(p *parser, startToken lexer.Token) (ast.Type, error) {
var endPos ast.Position
var dictionaryType *ast.DictionaryType
var restrictedType *ast.IntersectionType
var firstType ast.Type
atEnd := false
expectType := true
for !atEnd {
p.skipSpaceAndComments()
switch p.current.Type {
case lexer.TokenComma:
if dictionaryType != nil {
return nil, p.syntaxError("unexpected comma in dictionary type")
}
if expectType {
return nil, p.syntaxError("unexpected comma in restricted type")
}
if restrictedType == nil {
firstNominalType, ok := firstType.(*ast.NominalType)
if !ok {
return nil, p.syntaxError("non-nominal type in restriction list: %s", firstType)
}
restrictedType = ast.NewIntersectionType(
p.memoryGauge,
[]*ast.NominalType{
firstNominalType,
},
ast.NewRange(
p.memoryGauge,
startToken.StartPos,
ast.EmptyPosition,
),
)
}
// Skip the comma
p.next()
expectType = true
case lexer.TokenColon:
if restrictedType != nil {
return nil, p.syntaxError("unexpected colon in restricted type")
}
if expectType {
return nil, p.syntaxError("unexpected colon in dictionary type")
}
if dictionaryType == nil {
if firstType == nil {
return nil, p.syntaxError("unexpected colon after missing dictionary key type")
}
dictionaryType = ast.NewDictionaryType(
p.memoryGauge,
firstType,
nil,
ast.NewRange(
p.memoryGauge,
startToken.StartPos,
ast.EmptyPosition,
),
)
} else {
return nil, p.syntaxError("unexpected colon in dictionary type")
}
// Skip the colon
p.next()
expectType = true
case lexer.TokenBraceClose:
if expectType {
switch {
case dictionaryType != nil:
p.reportSyntaxError("missing dictionary value type")
case restrictedType != nil:
p.reportSyntaxError("missing type after comma")
}
}
endPos = p.current.EndPos
// Skip the closing brace
p.next()
atEnd = true
case lexer.TokenEOF:
if expectType {
return nil, p.syntaxError("invalid end of input, expected type")
} else {
return nil, p.syntaxError("invalid end of input, expected %s", lexer.TokenBraceClose)
}
default:
if !expectType {
return nil, p.syntaxError("unexpected type")
}
ty, err := parseType(p, lowestBindingPower)
if err != nil {
return nil, err
}
expectType = false
switch {
case dictionaryType != nil:
dictionaryType.ValueType = ty
case restrictedType != nil:
nominalType, ok := ty.(*ast.NominalType)
if !ok {
return nil, p.syntaxError("non-nominal type in restriction list: %s", ty)
}
restrictedType.Types = append(restrictedType.Types, nominalType)
default:
firstType = ty
}
}
}
switch {
case restrictedType != nil:
restrictedType.EndPos = endPos
return restrictedType, nil
case dictionaryType != nil:
dictionaryType.EndPos = endPos
return dictionaryType, nil
default:
restrictedType = ast.NewIntersectionType(
p.memoryGauge,
nil,
ast.NewRange(
p.memoryGauge,
startToken.StartPos,
endPos,
),
)
if firstType != nil {
firstNominalType, ok := firstType.(*ast.NominalType)
if !ok {
return nil, p.syntaxError("non-nominal type in restriction list: %s", firstType)
}
restrictedType.Types = append(restrictedType.Types, firstNominalType)
}
return restrictedType, nil
}
},
)
// For the left denotation we need a meta left denotation:
// We need to look ahead and check if the brace is followed by whitespace or not.
// In case there is a space, the type is *not* considered a restricted type.
// This handles the ambiguous case where a function return type's open brace
// may either be a restricted type (if there is no whitespace)
// or the start of the function body (if there is whitespace).
setTypeMetaLeftDenotation(
lexer.TokenBraceOpen,
func(p *parser, rightBindingPower int, left ast.Type) (result ast.Type, err error, done bool) {
// Perform a lookahead
current := p.current
cursor := p.tokens.Cursor()
// Skip the `{` token.
p.next()
// In case there is a space, the type is *not* considered a restricted type.
// The buffered tokens are replayed to allow them to be re-parsed.
if p.current.Is(lexer.TokenSpace) {
p.current = current
p.tokens.Revert(cursor)
return left, nil, true
}
// It was determined that a restricted type is parsed.
// Still, it should have maybe not been parsed if the right binding power
// was higher. In that case, replay the buffered tokens and stop.
if rightBindingPower >= typeLeftBindingPowerRestriction {
p.current = current
p.tokens.Revert(cursor)
return left, nil, true
}
nominalTypes, endPos, err := parseNominalTypes(p, lexer.TokenBraceClose)
if err != nil {
return nil, err, true
}
// Skip the closing brace
p.next()
intersection := ast.NewIntersectionType(
p.memoryGauge,
nominalTypes,
ast.NewRange(
p.memoryGauge,
left.StartPosition(),
endPos,
),
)
intersection.LegacyRestrictedType = left
result = intersection
return result, err, false
},
)
}
// parseNominalTypes parses zero or more nominal types separated by comma.
func parseNominalTypes(
p *parser,
endTokenType lexer.TokenType,
) (
nominalTypes []*ast.NominalType,
endPos ast.Position,
err error,
) {
expectType := true
atEnd := false
for !atEnd {
p.skipSpaceAndComments()
switch p.current.Type {
case lexer.TokenComma:
if expectType {
return nil, ast.EmptyPosition, p.syntaxError("unexpected comma")
}
// Skip the comma
p.next()
expectType = true
case endTokenType:
if expectType && len(nominalTypes) > 0 {
p.reportSyntaxError("missing type after comma")
}
endPos = p.current.EndPos
atEnd = true
case lexer.TokenEOF:
if expectType {
return nil, ast.EmptyPosition, p.syntaxError("invalid end of input, expected type")
} else {
return nil, ast.EmptyPosition, p.syntaxError("invalid end of input, expected %s", endTokenType)
}
default:
if !expectType {
return nil, ast.EmptyPosition, p.syntaxError(
"unexpected token: got %s, expected %s or %s",
p.current.Type,
lexer.TokenComma,
endTokenType,
)
}
ty, err := parseType(p, lowestBindingPower)
if err != nil {
return nil, ast.EmptyPosition, err
}
expectType = false
nominalType, ok := ty.(*ast.NominalType)
if !ok {
return nil, ast.EmptyPosition, p.syntaxError("unexpected non-nominal type: %s", ty)
}
nominalTypes = append(nominalTypes, nominalType)
}
}
return
}
func defineFunctionType() {
setTypeNullDenotation(
lexer.TokenParenOpen,
func(p *parser, startToken lexer.Token) (ast.Type, error) {
parameterTypeAnnotations, err := parseParameterTypeAnnotations(p)
if err != nil {
return nil, err
}
p.skipSpaceAndComments()
_, err = p.mustOne(lexer.TokenColon)
if err != nil {
return nil, err
}
p.skipSpaceAndComments()
returnTypeAnnotation, err := parseTypeAnnotation(p)
if err != nil {
return nil, err
}
p.skipSpaceAndComments()
endToken, err := p.mustOne(lexer.TokenParenClose)
if err != nil {
return nil, err
}
return ast.NewFunctionType(
p.memoryGauge,
ast.FunctionPurityUnspecified,
parameterTypeAnnotations,
returnTypeAnnotation,
ast.NewRange(
p.memoryGauge,
startToken.StartPos,
endToken.EndPos,
),
), nil
},
)
}
func parseParameterTypeAnnotations(p *parser) (typeAnnotations []*ast.TypeAnnotation, err error) {
p.skipSpaceAndComments()
_, err = p.mustOne(lexer.TokenParenOpen)
if err != nil {
return
}
expectTypeAnnotation := true
atEnd := false
for !atEnd {
p.skipSpaceAndComments()
switch p.current.Type {
case lexer.TokenComma:
if expectTypeAnnotation {
return nil, p.syntaxError(
"expected type annotation or end of list, got %q",
p.current.Type,
)
}
// Skip the comma
p.next()
expectTypeAnnotation = true
case lexer.TokenParenClose:
// Skip the closing paren
p.next()
atEnd = true
case lexer.TokenEOF:
return nil, p.syntaxError(
"missing %q at end of list",
lexer.TokenParenClose,
)
default:
if !expectTypeAnnotation {
return nil, p.syntaxError(
"expected comma or end of list, got %q",
p.current.Type,
)
}
typeAnnotation, err := parseTypeAnnotation(p)
if err != nil {
return nil, err
}
typeAnnotations = append(typeAnnotations, typeAnnotation)
expectTypeAnnotation = false
}
}
return
}
func parseType(p *parser, rightBindingPower int) (ast.Type, error) {
if p.typeDepth == typeDepthLimit {
return nil, TypeDepthLimitReachedError{
Pos: p.current.StartPos,
}
}
p.typeDepth
defer func() {
p.typeDepth--
}()
p.skipSpaceAndComments()
t := p.current
p.next()
left, err := applyTypeNullDenotation(p, t)
if err != nil {
return nil, err
}
for {
var done bool
left, err, done = applyTypeMetaLeftDenotation(p, rightBindingPower, left)
if err != nil {
return nil, err
}
if done {
break
}
}
return left, nil
}
func applyTypeMetaLeftDenotation(
p *parser,
rightBindingPower int,
left ast.Type,
) (
result ast.Type,
err error,
done bool,
) {
// By default, left denotations are applied if the right binding power
// is less than the left binding power of the current token.
//
// Token-specific meta-left denotations allow customizing this,
// e.g. determining the left binding power based on parsing more tokens,
// or performing look-ahead
metaLeftDenotation := typeMetaLeftDenotations[p.current.Type]
if metaLeftDenotation == nil {
metaLeftDenotation = defaultTypeMetaLeftDenotation
}
return metaLeftDenotation(p, rightBindingPower, left)
}
// defaultTypeMetaLeftDenotation is the default type left denotation, which applies
// if the right binding power is less than the left binding power of the current token
func defaultTypeMetaLeftDenotation(
p *parser,
rightBindingPower int,
left ast.Type,
) (
result ast.Type,
err error,
done bool,
) {
if rightBindingPower >= typeLeftBindingPowers[p.current.Type] {
return left, nil, true
}
t := p.current
p.next()
result, err = applyTypeLeftDenotation(p, t, left)
return result, err, false
}
func parseTypeAnnotation(p *parser) (*ast.TypeAnnotation, error) {
p.skipSpaceAndComments()
startPos := p.current.StartPos
isResource := false
if p.current.Is(lexer.TokenAt) {
// Skip the `@`
p.next()
isResource = true
}
ty, err := parseType(p, lowestBindingPower)
if err != nil {
return nil, err
}
return ast.NewTypeAnnotation(
p.memoryGauge,
isResource,
ty,
startPos,
), nil
}
func applyTypeNullDenotation(p *parser, token lexer.Token) (ast.Type, error) {
tokenType := token.Type
nullDenotation := typeNullDenotations[tokenType]
if nullDenotation == nil {
return nil, p.syntaxError("unexpected token in type: %s", tokenType)
}
return nullDenotation(p, token)
}
func applyTypeLeftDenotation(p *parser, token lexer.Token, left ast.Type) (ast.Type, error) {
leftDenotation := typeLeftDenotations[token.Type]
if leftDenotation == nil {
return nil, p.syntaxError("unexpected token in type: %s", token.Type)
}
return leftDenotation(p, token, left)
}
func parseNominalTypeInvocationRemainder(p *parser) (*ast.InvocationExpression, error) {
p.skipSpaceAndComments()
identifier, err := p.mustOne(lexer.TokenIdentifier)
if err != nil {
return nil, err
}
ty, err := parseNominalTypeRemainder(p, identifier)
if err != nil {
return nil, err
}
p.skipSpaceAndComments()
parenOpenToken, err := p.mustOne(lexer.TokenParenOpen)
if err != nil {
return nil, err
}
argumentsStartPos := parenOpenToken.EndPos
arguments, endPos, err := parseArgumentListRemainder(p)
if err != nil {
return nil, err
}
var invokedExpression ast.Expression = ast.NewIdentifierExpression(
p.memoryGauge,
ty.Identifier,
)
for _, nestedIdentifier := range ty.NestedIdentifiers {
invokedExpression = ast.NewMemberExpression(
p.memoryGauge,
invokedExpression,
false,
nestedIdentifier.Pos,
nestedIdentifier,
)
}
return ast.NewInvocationExpression(
p.memoryGauge,
invokedExpression,
nil,
arguments,
argumentsStartPos,
endPos,
), nil
}
// parseCommaSeparatedTypeAnnotations parses zero or more type annotations separated by comma.
func parseCommaSeparatedTypeAnnotations(
p *parser,
endTokenType lexer.TokenType,
) (
typeAnnotations []*ast.TypeAnnotation,
err error,
) {
expectTypeAnnotation := true
atEnd := false
for !atEnd {
p.skipSpaceAndComments()
switch p.current.Type {
case lexer.TokenComma:
if expectTypeAnnotation {
return nil, p.syntaxError("unexpected comma")
}
// Skip the comma
p.next()
expectTypeAnnotation = true
case endTokenType:
if expectTypeAnnotation && len(typeAnnotations) > 0 {
p.reportSyntaxError("missing type annotation after comma")
}
atEnd = true
case lexer.TokenEOF:
if expectTypeAnnotation {
return nil, p.syntaxError("invalid end of input, expected type")
} else {
return nil, p.syntaxError("invalid end of input, expected %s", endTokenType)
}
default:
if !expectTypeAnnotation {
return nil, p.syntaxError(
"unexpected token: got %s, expected %s or %s",
p.current.Type,
lexer.TokenComma,
endTokenType,
)
}
typeAnnotation, err := parseTypeAnnotation(p)
if err != nil {
return nil, err
}
typeAnnotations = append(typeAnnotations, typeAnnotation)
expectTypeAnnotation = false
}
}
return
}
func defineInstantiationType() {
setTypeLeftBindingPower(lexer.TokenLess, typeLeftBindingPowerInstantiation)
setTypeLeftDenotation(
lexer.TokenLess,
func(p *parser, token lexer.Token, left ast.Type) (ast.Type, error) {
typeArgumentsStartPos := token.StartPos
typeArguments, err := parseCommaSeparatedTypeAnnotations(p, lexer.TokenGreater)
if err != nil {
return nil, err
}
endToken, err := p.mustOne(lexer.TokenGreater)
if err != nil {
return nil, err
}
return ast.NewInstantiationType(
p.memoryGauge,
left,
typeArguments,
typeArgumentsStartPos,
endToken.EndPos,
), nil
},
)
}