forked from ceylon/ceylon-spec
/
Ceylon.g
3416 lines (3094 loc) · 99.9 KB
/
Ceylon.g
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
grammar Ceylon;
options {
memoize=false;
}
@parser::header { package com.redhat.ceylon.compiler.typechecker.parser;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import static com.redhat.ceylon.compiler.typechecker.tree.CustomTree.*; }
@lexer::header { package com.redhat.ceylon.compiler.typechecker.parser; }
@members {
private java.util.List<ParseError> errors
= new java.util.ArrayList<ParseError>();
@Override public void displayRecognitionError(String[] tn,
RecognitionException re) {
errors.add(new ParseError(this, re, tn));
}
public void displayRecognitionError(String[] tn, RecognitionException re, int code) {
errors.add(new ParseError(this, re, tn, code));
}
public java.util.List<ParseError> getErrors() {
return errors;
}
int expecting=-1;
}
@lexer::members {
private java.util.List<LexError> errors
= new java.util.ArrayList<LexError>();
@Override public void displayRecognitionError(String[] tn,
RecognitionException re) {
errors.add(new LexError(this, re, tn));
}
public java.util.List<LexError> getErrors() {
return errors;
}
}
compilationUnit returns [CompilationUnit compilationUnit]
: { $compilationUnit = new CompilationUnit(null); }
(
ca1=compilerAnnotations
SEMICOLON
{ $compilationUnit.getCompilerAnnotations().addAll($ca1.annotations); }
)?
(
(compilerAnnotations annotations MODULE)=>
moduleDescriptor
{ $compilationUnit.setModuleDescriptor($moduleDescriptor.moduleDescriptor); }
|
(compilerAnnotations annotations PACKAGE)=>
packageDescriptor
{ $compilationUnit.setPackageDescriptor($packageDescriptor.packageDescriptor); }
|
importList
{ $compilationUnit.setImportList($importList.importList); }
(
ca2=compilerAnnotations declaration
{ if ($declaration.declaration!=null)
$compilationUnit.addDeclaration($declaration.declaration);
if ($declaration.declaration!=null)
$declaration.declaration.getCompilerAnnotations().addAll($ca2.annotations); }
)*
)
EOF
;
moduleDescriptor returns [ModuleDescriptor moduleDescriptor]
: compilerAnnotations annotations
MODULE
{ $moduleDescriptor = new ModuleDescriptor($MODULE);
$moduleDescriptor.setAnnotationList($annotations.annotationList);
$moduleDescriptor.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
packagePath
{ $moduleDescriptor.setImportPath($packagePath.importPath); }
QUOTED_LITERAL
{ $moduleDescriptor.setVersion(new QuotedLiteral($QUOTED_LITERAL)); }
importModuleList
{ $moduleDescriptor.setImportModuleList($importModuleList.importModuleList); }
;
importModuleList returns [ImportModuleList importModuleList]
: LBRACE
{ $importModuleList = new ImportModuleList($LBRACE); }
(
compilerAnnotations annotations
importModule
{ if ($importModule.importModule!=null)
$importModuleList.addImportModule($importModule.importModule);
if ($importModule.importModule!=null)
$importModule.importModule.setAnnotationList($annotations.annotationList);
if ($importModule.importModule!=null)
$importModule.importModule.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
)*
RBRACE
{ $importModuleList.setEndToken($RBRACE); }
;
packageDescriptor returns [PackageDescriptor packageDescriptor]
: compilerAnnotations annotations
PACKAGE
{ $packageDescriptor = new PackageDescriptor($PACKAGE);
$packageDescriptor.setAnnotationList($annotations.annotationList);
$packageDescriptor.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
packagePath
{ $packageDescriptor.setImportPath($packagePath.importPath);
expecting=SEMICOLON; }
SEMICOLON
{ $packageDescriptor.setEndToken($SEMICOLON);
expecting=-1; }
;
importModule returns [ImportModule importModule]
: IMPORT
{ $importModule = new ImportModule($IMPORT); }
(
q1=QUOTED_LITERAL
{ $importModule.setQuotedLiteral(new QuotedLiteral($q1)); }
|
packagePath
{ $importModule.setImportPath($packagePath.importPath); }
)
q2=QUOTED_LITERAL
{ $importModule.setVersion(new QuotedLiteral($q2));
expecting=SEMICOLON; }
SEMICOLON
{ $importModule.setEndToken($SEMICOLON);
expecting=-1; }
;
importList returns [ImportList importList]
: { $importList = new ImportList(null); }
( importDeclaration { $importList.addImport($importDeclaration.importDeclaration); } )*
;
importDeclaration returns [Import importDeclaration]
: IMPORT
{ $importDeclaration = new Import($IMPORT); }
(
packagePath
{ $importDeclaration.setImportPath($packagePath.importPath); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LIDENTIFIER, input)); }
)
importElementList
{ $importDeclaration.setImportMemberOrTypeList($importElementList.importMemberOrTypeList); }
;
importElementList returns [ImportMemberOrTypeList importMemberOrTypeList]
@init { ImportMemberOrTypeList il=null;
boolean wildcarded = false; }
:
LBRACE
{ il = new ImportMemberOrTypeList($LBRACE);
$importMemberOrTypeList = il; }
(
ie1=importElement
{ if ($ie1.importMemberOrType!=null)
il.addImportMemberOrType($ie1.importMemberOrType); }
(
c1=COMMA
{ il.setEndToken($c1);
if (wildcarded)
displayRecognitionError(getTokenNames(),
new MismatchedTokenException(RBRACE, input)); }
(
ie2=importElement
{ if ($ie2.importMemberOrType!=null)
il.addImportMemberOrType($ie2.importMemberOrType);
if ($ie2.importMemberOrType!=null)
il.setEndToken(null); }
| iw=importWildcard
{ wildcarded = true;
if ($iw.importWildcard!=null)
il.setImportWildcard($iw.importWildcard);
if ($iw.importWildcard!=null)
il.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(ELLIPSIS, input)); }
)
)*
| iw=importWildcard { il.setImportWildcard($iw.importWildcard); }
)?
RBRACE
{ il.setEndToken($RBRACE); }
;
importElement returns [ImportMemberOrType importMemberOrType]
@init { Alias alias = null; }
: compilerAnnotations
( in1=importName
{ $importMemberOrType = new ImportMember(null);
$importMemberOrType.setIdentifier($in1.identifier); }
( SPECIFY
{ alias = new Alias($SPECIFY);
alias.setIdentifier($in1.identifier);
$importMemberOrType.setAlias(alias);
$importMemberOrType.setIdentifier(null); }
(
in2=importName
{ $importMemberOrType.setIdentifier($in2.identifier); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException($in1.identifier.getToken().getType(), input)); }
)
)?
(
iel2=importElementList
{ $importMemberOrType.setImportMemberOrTypeList($iel2.importMemberOrTypeList); }
)?
)
{ if ($importMemberOrType!=null)
$importMemberOrType.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
;
importWildcard returns [ImportWildcard importWildcard]
: ELLIPSIS
{ $importWildcard = new ImportWildcard($ELLIPSIS); }
;
importName returns [Identifier identifier]
: memberName { $identifier=$memberName.identifier; }
| typeName { $identifier=$typeName.identifier; }
;
packagePath returns [ImportPath importPath]
@init { $importPath = new ImportPath(null); }
: pn1=packageName
{ if ($pn1.identifier!=null)
$importPath.addIdentifier($pn1.identifier); }
(
m=MEMBER_OP
{ $importPath.setEndToken($m); }
(
pn2=packageName
{ $importPath.addIdentifier($pn2.identifier);
$importPath.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LIDENTIFIER, input)); }
)
)*
;
packageName returns [Identifier identifier]
: LIDENTIFIER
{ $identifier = new Identifier($LIDENTIFIER);
$LIDENTIFIER.setType(PIDENTIFIER);}
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LIDENTIFIER, input), 5001); }
UIDENTIFIER
{ $identifier = new Identifier($UIDENTIFIER);
$UIDENTIFIER.setType(PIDENTIFIER);}
;
typeName returns [Identifier identifier]
: UIDENTIFIER
{ $identifier = new Identifier($UIDENTIFIER); }
;
annotationName returns [Identifier identifier]
: LIDENTIFIER
{ $identifier = new Identifier($LIDENTIFIER);
$LIDENTIFIER.setType(AIDENTIFIER); }
;
memberName returns [Identifier identifier]
: LIDENTIFIER
{ $identifier = new Identifier($LIDENTIFIER); }
;
memberNameDeclaration returns [Identifier identifier]
: memberName { $identifier = $memberName.identifier; }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LIDENTIFIER, input), 5001); }
typeName { $identifier = $typeName.identifier; }
;
typeNameDeclaration returns [Identifier identifier]
: typeName { $identifier = $typeName.identifier; }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input), 5002); }
memberName { $identifier = $memberName.identifier; }
;
objectDeclaration returns [ObjectDefinition declaration]
: OBJECT_DEFINITION
{ $declaration = new ObjectDefinition($OBJECT_DEFINITION);
$declaration.setType(new ValueModifier(null)); }
memberNameDeclaration
{ $declaration.setIdentifier($memberNameDeclaration.identifier); }
(
extendedType
{ $declaration.setExtendedType($extendedType.extendedType); }
)?
(
satisfiedTypes
{ $declaration.setSatisfiedTypes($satisfiedTypes.satisfiedTypes); }
)?
(
classBody
{ $declaration.setClassBody($classBody.classBody); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LBRACE, input)); }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON); }
)
//-> ^(OBJECT_DEFINITION VALUE_MODIFIER memberName extendedType? satisfiedTypes? classBody?)
;
voidOrInferredMethodDeclaration returns [AnyMethod declaration]
@init { MethodDefinition def=null;
MethodDeclaration dec=null; }
: (
VOID_MODIFIER
{ VoidModifier vm = new VoidModifier($VOID_MODIFIER);
def = new MethodDefinition($VOID_MODIFIER);
dec = new MethodDeclaration($VOID_MODIFIER);
def.setType(vm);
dec.setType(vm);
$declaration = dec; }
| FUNCTION_MODIFIER
{ FunctionModifier fm = new FunctionModifier($FUNCTION_MODIFIER);
def = new MethodDefinition($FUNCTION_MODIFIER);
dec = new MethodDeclaration($FUNCTION_MODIFIER);
def.setType(fm);
dec.setType(fm);
$declaration = dec; }
)
memberNameDeclaration
{ dec.setIdentifier($memberNameDeclaration.identifier);
def.setIdentifier($memberNameDeclaration.identifier); }
(
typeParameters
{ def.setTypeParameterList($typeParameters.typeParameterList);
dec.setTypeParameterList($typeParameters.typeParameterList); }
)?
(
parameters
{ def.addParameterList($parameters.parameterList);
dec.addParameterList($parameters.parameterList); }
)*
//metatypes?
(
typeConstraints
{ def.setTypeConstraintList($typeConstraints.typeConstraintList);
dec.setTypeConstraintList($typeConstraints.typeConstraintList); }
)?
(
{ $declaration = def; }
block
{ def.setBlock($block.block); }
//-> ^(METHOD_DEFINITION VOID_MODIFIER memberName methodParameters? block)
|
(
specifier
{ dec.setSpecifierExpression($specifier.specifierExpression); }
)?
{ expecting=SEMICOLON; }
SEMICOLON
{ expecting=-1; }
{ $declaration.setEndToken($SEMICOLON); }
//-> ^(METHOD_DECLARATION VOID_MODIFIER memberName methodParameters? specifier?)
)
;
setterDeclaration returns [AttributeSetterDefinition declaration]
: ASSIGN
{ $declaration = new AttributeSetterDefinition($ASSIGN);
$declaration.setType( new VoidModifier(null) ); }
memberNameDeclaration
{ $declaration.setIdentifier($memberNameDeclaration.identifier); }
(
block
{ $declaration.setBlock($block.block); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LBRACE, input)); }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON); }
)
//-> ^(ATTRIBUTE_SETTER_DEFINITION[$ASSIGN] VOID_MODIFIER memberName block)
;
inferredAttributeDeclaration returns [AnyAttribute declaration]
@init { AttributeGetterDefinition def=null;
AttributeDeclaration dec=null; }
: VALUE_MODIFIER
{ ValueModifier fm = new ValueModifier($VALUE_MODIFIER);
def = new AttributeGetterDefinition($VALUE_MODIFIER);
dec = new AttributeDeclaration($VALUE_MODIFIER);
def.setType(fm);
dec.setType(fm);
$declaration = dec; }
memberNameDeclaration
{ dec.setIdentifier($memberNameDeclaration.identifier);
def.setIdentifier($memberNameDeclaration.identifier); }
(
(
specifier
{ dec.setSpecifierOrInitializerExpression($specifier.specifierExpression); }
|
initializer
{ dec.setSpecifierOrInitializerExpression($initializer.initializerExpression); }
)?
{ expecting=SEMICOLON; }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON);
expecting=-1; }
//-> ^(ATTRIBUTE_DECLARATION VALUE_MODIFIER memberName specifier? initializer?)
|
{ $declaration = def; }
block
{ def.setBlock($block.block); }
//-> ^(ATTRIBUTE_GETTER_DEFINITION VALUE_MODIFIER memberName block)
)
;
typedMethodOrAttributeDeclaration returns [TypedDeclaration declaration]
@init { AttributeGetterDefinition adef=new AttributeGetterDefinition(null);
AttributeDeclaration adec=new AttributeDeclaration(null);
MethodDefinition mdef=new MethodDefinition(null);
MethodDeclaration mdec=new MethodDeclaration(null);
$declaration = adec; }
: type
{ adef.setType($type.type);
adec.setType($type.type);
mdef.setType($type.type);
mdec.setType($type.type); }
memberNameDeclaration
{ adef.setIdentifier($memberNameDeclaration.identifier);
adec.setIdentifier($memberNameDeclaration.identifier);
mdef.setIdentifier($memberNameDeclaration.identifier);
mdec.setIdentifier($memberNameDeclaration.identifier); }
(
{ $declaration = mdec; }
(
typeParameters
{ mdef.setTypeParameterList($typeParameters.typeParameterList);
mdec.setTypeParameterList($typeParameters.typeParameterList); }
)?
(
parameters
{ mdef.addParameterList($parameters.parameterList);
mdec.addParameterList($parameters.parameterList); }
)+
//metatypes?
(
typeConstraints
{ mdef.setTypeConstraintList($typeConstraints.typeConstraintList);
mdec.setTypeConstraintList($typeConstraints.typeConstraintList); }
)?
(
{ $declaration = mdef; }
b1=block
{ mdef.setBlock($b1.block); }
//-> ^(METHOD_DEFINITION unionType memberName methodParameters memberBody)
|
(
ms=specifier
{ mdec.setSpecifierExpression($ms.specifierExpression); }
)?
{ expecting=SEMICOLON; }
s1=SEMICOLON
{ $declaration.setEndToken($s1);
expecting=-1; }
//-> ^(METHOD_DECLARATION unionType memberName methodParameters specifier?)
)
|
(
as=specifier
{ adec.setSpecifierOrInitializerExpression($as.specifierExpression); }
|
initializer
{ adec.setSpecifierOrInitializerExpression($initializer.initializerExpression); }
)?
{ expecting=SEMICOLON; }
s2=SEMICOLON
{ $declaration.setEndToken($s2);
expecting=-1; }
//-> ^(ATTRIBUTE_DECLARATION unionType memberName specifier? initializer?)
|
{ $declaration = adef; }
b2=block
{ adef.setBlock($b2.block); }
//-> ^(ATTRIBUTE_GETTER_DEFINITION unionType memberName memberBody)
)
;
interfaceDeclaration returns [AnyInterface declaration]
@init { InterfaceDefinition def=null;
InterfaceDeclaration dec=null; }
: INTERFACE_DEFINITION
{ def = new InterfaceDefinition($INTERFACE_DEFINITION);
dec = new InterfaceDeclaration($INTERFACE_DEFINITION);
$declaration = dec; }
typeNameDeclaration
{ dec.setIdentifier($typeNameDeclaration.identifier);
def.setIdentifier($typeNameDeclaration.identifier); }
(
typeParameters
{ def.setTypeParameterList($typeParameters.typeParameterList);
dec.setTypeParameterList($typeParameters.typeParameterList); }
)?
(
caseTypes
{ def.setCaseTypes($caseTypes.caseTypes);
dec.setCaseTypes($caseTypes.caseTypes); }
)?
/*metatypes?*/
(
adaptedTypes
{ def.setAdaptedTypes($adaptedTypes.adaptedTypes); }
)?
(
satisfiedTypes
{ def.setSatisfiedTypes($satisfiedTypes.satisfiedTypes);
dec.setSatisfiedTypes($satisfiedTypes.satisfiedTypes); }
)?
(
typeConstraints
{ def.setTypeConstraintList($typeConstraints.typeConstraintList);
dec.setTypeConstraintList($typeConstraints.typeConstraintList); }
)?
(
{ $declaration = def; }
interfaceBody
{ def.setInterfaceBody($interfaceBody.interfaceBody); }
//-> ^(INTERFACE_DEFINITION typeName interfaceParameters? interfaceBody)
|
(
typeSpecifier
{ dec.setTypeSpecifier($typeSpecifier.typeSpecifier); }
)?
{ expecting=SEMICOLON; }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON);
expecting=-1; }
//-> ^(INTERFACE_DECLARATION[$INTERFACE_DEFINITION] typeName interfaceParameters? typeSpecifier?)
)
;
classDeclaration returns [AnyClass declaration]
@init { ClassDefinition def=null;
ClassDeclaration dec=null; }
: CLASS_DEFINITION
{ def = new ClassDefinition($CLASS_DEFINITION);
dec = new ClassDeclaration($CLASS_DEFINITION);
$declaration = dec; }
typeNameDeclaration
{ dec.setIdentifier($typeNameDeclaration.identifier);
def.setIdentifier($typeNameDeclaration.identifier); }
(
typeParameters
{ def.setTypeParameterList($typeParameters.typeParameterList);
dec.setTypeParameterList($typeParameters.typeParameterList); }
)?
(
parameters
{ def.setParameterList($parameters.parameterList);
dec.setParameterList($parameters.parameterList); }
)?
(
caseTypes
{ def.setCaseTypes($caseTypes.caseTypes);
dec.setCaseTypes($caseTypes.caseTypes); }
)?
/*metatypes?*/
(
extendedType
{ def.setExtendedType($extendedType.extendedType);
dec.setExtendedType($extendedType.extendedType); }
)?
(
satisfiedTypes
{ def.setSatisfiedTypes($satisfiedTypes.satisfiedTypes);
dec.setSatisfiedTypes($satisfiedTypes.satisfiedTypes); }
)?
(
typeConstraints
{ def.setTypeConstraintList($typeConstraints.typeConstraintList);
dec.setTypeConstraintList($typeConstraints.typeConstraintList); }
)?
(
{ $declaration = def; }
classBody
{ def.setClassBody($classBody.classBody); }
//-> ^(CLASS_DEFINITION typeName classParameters? classBody)
|
(
typeSpecifier
{ dec.setTypeSpecifier($typeSpecifier.typeSpecifier); }
)?
{ expecting=SEMICOLON; }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON);
expecting=-1; }
//-> ^(CLASS_DECLARATION[$CLASS_DEFINITION] typeName classParameters? typeSpecifier?)
)
;
assertion returns [Assertion assertion]
: annotations
ASSERT
{ $assertion = new Assertion($ASSERT);
$assertion.setAnnotationList($annotations.annotationList); }
condition
{ $assertion.setCondition($condition.condition); }
{ expecting=SEMICOLON; }
SEMICOLON
{ $assertion.setEndToken($SEMICOLON);
expecting=-1; }
/*{ ValueModifier fm = new ValueModifier($ASSERT);
dec = new AttributeDeclaration($ASSERT);
dec.setType(fm);
$declaration = dec; }
memberNameDeclaration
{ dec.setIdentifier($memberNameDeclaration.identifier);
def.setIdentifier($memberNameDeclaration.identifier); }
specifier
{ dec.setSpecifierOrInitializerExpression($specifier.specifierExpression); }
{ expecting=SEMICOLON; }
SEMICOLON
{ $declaration.setEndToken($SEMICOLON);
expecting=-1; }
//-> ^(ATTRIBUTE_DECLARATION VALUE_MODIFIER memberName specifier? initializer?)*/
;
block returns [Block block]
: LBRACE
{ $block = new Block($LBRACE); }
(
declarationOrStatement
{ if ($declarationOrStatement.statement!=null)
$block.addStatement($declarationOrStatement.statement); }
)*
RBRACE
{ $block.setEndToken($RBRACE); }
//-> ^(BLOCK[$LBRACE] annotatedDeclarationOrStatement*)
;
//Note: interface bodies can't really contain
// statements, but error recovery works
// much better if we validate that later
// on, instead of doing it in the parser.
interfaceBody returns [InterfaceBody interfaceBody]
: LBRACE
{ $interfaceBody = new InterfaceBody($LBRACE); }
(
declarationOrStatement
{ if ($declarationOrStatement.statement!=null)
$interfaceBody.addStatement($declarationOrStatement.statement); }
)*
RBRACE
{ $interfaceBody.setEndToken($RBRACE); }
//-> ^(INTERFACE_BODY[$LBRACE] annotatedDeclarationOrStatement2*)
;
classBody returns [ClassBody classBody]
: LBRACE
{ $classBody = new ClassBody($LBRACE); }
(
declarationOrStatement
{ if ($declarationOrStatement.statement!=null)
$classBody.addStatement($declarationOrStatement.statement); }
)*
RBRACE
{ $classBody.setEndToken($RBRACE); }
//-> ^(CLASS_BODY[$LBRACE] annotatedDeclarationOrStatement2*)
;
extendedType returns [ExtendedType extendedType]
: EXTENDS
{ $extendedType = new ExtendedType($EXTENDS); }
(
qualifiedType
{ $extendedType.setType($qualifiedType.type); }
//-> ^(EXTENDED_TYPE[$EXTENDS] type ^(INVOCATION_EXPRESSION ^(EXTENDED_TYPE_EXPRESSION) positionalArguments))
| SUPER MEMBER_OP
typeReference
{ QualifiedType qt=new QualifiedType(null);
SuperType st = new SuperType($SUPER);
qt.setOuterType(st);
qt.setIdentifier($typeReference.identifier);
if ($typeReference.typeArgumentList!=null)
qt.setTypeArgumentList($typeReference.typeArgumentList);
$extendedType.setType(qt); }
//-> ^(EXTENDED_TYPE[$EXTENDS] ^(QUALIFIED_TYPE SUPER_TYPE[$SUPER] typeReference) ^(INVOCATION_EXPRESSION ^(EXTENDED_TYPE_EXPRESSION) positionalArguments))
)
(
positionalArguments
{ InvocationExpression ie = new InvocationExpression(null);
ExtendedTypeExpression ete = new ExtendedTypeExpression(null);
ie.setPrimary(ete);
ete.setExtendedType($extendedType.getType());
ie.setPositionalArgumentList($positionalArguments.positionalArgumentList);
$extendedType.setInvocationExpression(ie); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(LPAREN, input)); }
)
;
satisfiedTypes returns [SatisfiedTypes satisfiedTypes]
: SATISFIES
{ $satisfiedTypes = new SatisfiedTypes($SATISFIES); }
t1=qualifiedType
{ $satisfiedTypes.addType($t1.type); }
(
i=INTERSECTION_OP
{ $satisfiedTypes.setEndToken($i); }
(
t2=qualifiedType
{ $satisfiedTypes.addType($t2.type);
$satisfiedTypes.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input)); }
)
)*
//-> ^(SATISFIED_TYPES[$SATISFIES] type+)
;
abstractedType returns [AbstractedType abstractedType]
: ABSTRACTED_TYPE
{ $abstractedType = new AbstractedType($ABSTRACTED_TYPE); }
qualifiedType
{ $abstractedType.setType($qualifiedType.type); }
;
adaptedTypes returns [AdaptedTypes adaptedTypes]
: ADAPTED_TYPES
{ $adaptedTypes = new AdaptedTypes($ADAPTED_TYPES); }
t1=qualifiedType
{ $adaptedTypes.addType($t1.type); }
(
i=INTERSECTION_OP
{ $adaptedTypes.setEndToken($i); }
(
t2=qualifiedType
{ $adaptedTypes.addType($t2.type);
$adaptedTypes.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input)); }
)
)*
;
caseTypes returns [CaseTypes caseTypes]
: CASE_TYPES
{ $caseTypes = new CaseTypes($CASE_TYPES); }
ct1=caseType
{ if ($ct1.type!=null) $caseTypes.addType($ct1.type);
if ($ct1.instance!=null) $caseTypes.addBaseMemberExpression($ct1.instance); }
(
u=UNION_OP
{ $caseTypes.setEndToken($u); }
(
ct2=caseType
{ if ($ct2.type!=null) $caseTypes.addType($ct2.type);
if ($ct2.instance!=null) $caseTypes.addBaseMemberExpression($ct2.instance);
$caseTypes.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input)); }
)
)*
;
caseType returns [SimpleType type, BaseMemberExpression instance]
: t=qualifiedType
{ $type=$t.type;}
| memberName //-> ^(BASE_MEMBER_EXPRESSION memberName)
{ $instance = new BaseMemberExpression(null);
$instance.setIdentifier($memberName.identifier);
$instance.setTypeArguments( new InferredTypeArguments(null) ); }
;
//Support for metatypes
//Note that we don't need this for now
/*metatypes
: IS_OP type ('&' type)*
-> ^(METATYPES[$IS_OP] type*)
;*/
parameters returns [ParameterList parameterList]
: LPAREN
{ $parameterList=new ParameterList($LPAREN); }
(
ap1=parameterDeclaration
{ if ($ap1.parameter!=null)
$parameterList.addParameter($ap1.parameter); }
(
c=COMMA
{ $parameterList.setEndToken($c); }
(
(~(COMPILER_ANNOTATION | LIDENTIFIER | UIDENTIFIER)) =>
{ displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input)); }
|
ap2=parameterDeclaration
{ $parameterList.addParameter($ap2.parameter);
$parameterList.setEndToken(null); }
)
)*
)?
RPAREN
{ $parameterList.setEndToken($RPAREN); }
//-> ^(PARAMETER_LIST[$LPAREN] annotatedParameter*)
;
//special rule for syntactic predicate
//to distinguish between a formal
//parameter list and a parenthesized body
//of an inline callable argument
//be careful with this one, since it
//matches "()", which can also be an
//argument list
parametersStart
: LPAREN ( annotatedDeclarationStart | RPAREN )
;
// FIXME: This accepts more than the language spec: named arguments
// and varargs arguments can appear in any order. We'll have to
// enforce the rule that the ... appears at the end of the parapmeter
// list in a later pass of the compiler.
parameter returns [Parameter parameter]
@init { ValueParameterDeclaration vp = new ValueParameterDeclaration(null);
FunctionalParameterDeclaration fp = new FunctionalParameterDeclaration(null);
$parameter = vp; }
: parameterType
{ vp.setType($parameterType.type);
fp.setType($parameterType.type); }
memberName
{ vp.setIdentifier($memberName.identifier);
fp.setIdentifier($memberName.identifier); }
(
(
valueParameter
)?
//-> ^(VALUE_PARAMETER_DECLARATION parameterType memberName specifier?)
|
(
parameters
{ fp.addParameterList($parameters.parameterList); }
)+
{ $parameter=fp; }
//for callable parameters
//-> ^(FUNCTIONAL_PARAMETER_DECLARATION parameterType memberName parameters+ specifier?)
)
(
specifier
{ DefaultArgument da = new DefaultArgument(null);
$parameter.setDefaultArgument(da);
da.setSpecifierExpression($specifier.specifierExpression); }
)?
;
parameterRef returns [Parameter parameter]
@init { ValueParameterDeclaration vp = new ValueParameterDeclaration(null);
vp.setType(new ValueModifier(null));
$parameter = vp; }
: memberName
{ vp.setIdentifier($memberName.identifier); }
(
specifier
{ DefaultArgument da = new DefaultArgument(null);
$parameter.setDefaultArgument(da);
da.setSpecifierExpression($specifier.specifierExpression); }
)?
;
parameterDeclaration returns [Parameter parameter]
: compilerAnnotations
(
(LIDENTIFIER (SPECIFY|COMMA|RPAREN)) =>
r=parameterRef
{ $parameter=$r.parameter; }
|
annotations
p=parameter
{ $parameter=$p.parameter;
$parameter.setAnnotationList($annotations.annotationList); }
)
{ if ($parameter!=null)
$parameter.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
;
valueParameter
: ENTRY_OP type memberName
;
parameterType returns [Type type]
: type
{ $type = $type.type; }
(
ELLIPSIS
{ SequencedType st = new SequencedType($ELLIPSIS);
st.setType($type);
$type = st; }
)?
| VOID_MODIFIER
{ $type = new VoidModifier($VOID_MODIFIER); }
;
typeParameters returns [TypeParameterList typeParameterList]
: SMALLER_OP
{ $typeParameterList = new TypeParameterList($SMALLER_OP); }
tp1=typeParameter
{ if ($tp1.typeParameter instanceof TypeParameterDeclaration)
$typeParameterList.addTypeParameterDeclaration($tp1.typeParameter); }
(
c=COMMA
{ $typeParameterList.setEndToken($c); }
(
tp2=typeParameter
{ if ($tp2.typeParameter instanceof TypeParameterDeclaration)
$typeParameterList.addTypeParameterDeclaration($tp2.typeParameter);
$typeParameterList.setEndToken(null); }
| { displayRecognitionError(getTokenNames(),
new MismatchedTokenException(UIDENTIFIER, input)); }
)
)*
LARGER_OP
{ $typeParameterList.setEndToken($LARGER_OP); }
//-> ^(TYPE_PARAMETER_LIST[$SMALLER_OP] typeParameter+)
;
typeParameter returns [TypeParameterDeclaration typeParameter]
: { $typeParameter = new TypeParameterDeclaration(null); }
(
variance
{ $typeParameter.setTypeVariance($variance.typeVariance); }
)?
typeNameDeclaration
{ $typeParameter.setIdentifier($typeNameDeclaration.identifier); }
(
ELLIPSIS
{ $typeParameter.setSequenced(true);
$typeParameter.setEndToken($ELLIPSIS); }
)?
//-> ^(TYPE_PARAMETER_DECLARATION variance? typeName)
;
variance returns [TypeVariance typeVariance]
: IN_OP //-> ^(TYPE_VARIANCE[$IN_OP])
{ $typeVariance = new TypeVariance($IN_OP); }
| OUT //-> ^(TYPE_VARIANCE[$OUT])
{ $typeVariance = new TypeVariance($OUT); }
;
typeConstraint returns [TypeConstraint typeConstraint]
: compilerAnnotations
TYPE_CONSTRAINT
{ $typeConstraint = new TypeConstraint($TYPE_CONSTRAINT);
$typeConstraint.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
typeNameDeclaration
{ $typeConstraint.setIdentifier($typeNameDeclaration.identifier); }
//(typeParameters)?
(
parameters
{ $typeConstraint.setParameterList($parameters.parameterList); }
)?
(
caseTypes
{ $typeConstraint.setCaseTypes($caseTypes.caseTypes); }
)?
//metatypes?
(
satisfiedTypes
{ $typeConstraint.setSatisfiedTypes($satisfiedTypes.satisfiedTypes); }
)?
(
abstractedType
{ $typeConstraint.setAbstractedType($abstractedType.abstractedType); }
)?
;
typeConstraints returns [TypeConstraintList typeConstraintList]
: { $typeConstraintList=new TypeConstraintList(null); }
(
typeConstraint
{ $typeConstraintList.addTypeConstraint($typeConstraint.typeConstraint); }
)+
//-> ^(TYPE_CONSTRAINT_LIST typeConstraint2+)
;
declarationOrStatement returns [Statement statement]
options {memoize=true;}
: compilerAnnotations
(
(annotatedDeclarationStart) => declaration
{ $statement=$declaration.declaration; }
| (annotatedAssertionStart) =>
assertion
{ $statement = $assertion.assertion; }
| s=statement
{ $statement=$s.statement; }
)
{ if ($statement!=null)
$statement.getCompilerAnnotations().addAll($compilerAnnotations.annotations); }
;