forked from nez-peg/nez-grammar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx10.nez
1098 lines (1000 loc) · 48.5 KB
/
x10.nez
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
/* Toplevel */
File = _ CompilationUnit !.
CompilationUnit = { $(PackageDeclaration)? _ $(ImportDeclarations)? _ $(PackageDeclaration)? _ $(ImportDeclarations)? _ $(TypeDeclarations)? _ #Source }
/* Code layout */
_ = ( WHITESPACE / BLOCKCOMMENT / LINECOMMENT )*
WHITESPACE = [ \t\f\r\n]
BLOCKCOMMENT = '/*' ( !'*/' . )* '*/'
LINECOMMENT = '//' ( ![\r\n] . )*
W = [a-zA-Z_0-9]
/* Declarations */
PackageDeclaration = { ( $(Annotations) _ )? "package" _ $(PackageName) _ ";" #PackageDeclaration } _
ImportDeclarations = { ( $(ImportDeclaration) _ )+ #List }
ImportDeclaration = SingleTypeImportDeclaration
/ TypeImportOnDemandDeclaration
SingleTypeImportDeclaration = { "import" _ $(TypeName) _ ";" #ImportDeclaration }
TypeImportOnDemandDeclaration = { "import" _ $(PackageOrTypeName {$ _ "." _ "*" #WildCardName }) _ ";" #ImportDeclaration }
PackageName = Identifier {$ _ "." _ $(Identifier) #PackageName }*
PackageOrTypeName = Identifier {$ _ "." _ $(Identifier) #PackageOrTypeName }*
TypeDeclarations = { $(TypeDeclaration) ( _ $(TypeDeclaration) )* #List }
TypeDeclaration = ClassDeclaration
/ StructDeclaration
/ InterfaceDeclaration
/ TypeDefDeclaration
/ ";" { #Empty }
ClassDeclaration = { ( $(Modifiers) _ )? "class" _ $(Identifier) _ $(TypeParametersI)? _ $(Properties)? _ $(Guard)? _ $(Super)? _ $(Interfaces)? _ $(ClassBody) #ClassDeclaration }
StructDeclaration = { ( $(Modifiers) _ )? "struct" _ $(Identifier) _ $(TypeParametersI)? _ $(Properties)? _ $(Guard)? _ $(Interfaces)? _ $(ClassBody) #StructDeclaration }
Super = "extends" _ ClassType
Interfaces = "implements" _ InterfaceTypeList
ClassBody = { "{" _ addClassMemberDeclarations? _ "}" #Block }
addClassMemberDeclarations = $(ClassMemberDeclaration) ( _ $(ClassMemberDeclaration) )*
ClassMemberDeclaration = ConstructorDeclaration
/ InterfaceMemberDeclaration
InterfaceDeclaration = { ( $(Modifiers) _ )? "interface" _ $(Identifier) _ $(TypeParametersI)? _ $(Properties)? _ $(Guard)? _ $(ExtendInterfaces)? _ $(InterfaceBody) #InterfaceDeclaration }
ExtendInterfaces = "extends" _ InterfaceTypeList
InterfaceBody = { "{" _ addInterfaceMemberDeclarations? _ "}" #Block }
addInterfaceMemberDeclarations = $(InterfaceMemberDeclaration) ( _ $(InterfaceMemberDeclaration) )*
InterfaceMemberDeclaration = MethodDeclaration
/ PropertyMethodDeclaration
/ FieldDeclaration
/ TypeDeclaration
TypeDefDeclaration = { ( $(Modifiers) _ )? "type" _ $(Identifier) _ $(TypeParameters)? _ ( "(" _ $(FormalList) _ ")" )? _ $(Guard)? _ "=" _ $(Type) _ ";" #TypeDefDeclaration }
Properties = "(" _ PropertyList _ ")"
PropertyList = { $(Property) ( _ "," _ $(Property) )* #List }
Property = { ( $(Annotations) _ )? $(Identifier) _ $(ResultType) #Property }
MethodDeclaration = { ( $(MethodModifiers) _ )? "def" _ $(Identifier) _ $(TypeParameters)? _ $(Formals) _ $(Guard)? _ $(Throws)? _ $(HasResultType)? _ $(MethodBody) #MethodDeclaration }
/ BinaryOperatorDeclaration
/ PrefixOperatorDeclaration
/ ApplyOperatorDeclaration
/ SetOperatorDeclaration
/ ConversionOperatorDeclaration
PropertyMethodDeclaration = { ( $(MethodModifiers) _ )? $(Identifier) _ ( $(TypeParameters)? _ $(Formals) )? _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #PropertyMethodDeclaration }
Throws = "throws" _ ThrowsList
MethodBody = "=" _ Expression _ ";"
/ { $(Annotations) _ $(Block) #AnnotationStatement }
/ Block
/ { ";" #Empty }
BinaryOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ $(TypeParameters)? _ addBinaryHeader _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #BinaryOperatorDeclaration }
addBinaryHeader = "(" _ $(Formal) _ ")" _ $({ BINARY_OPERATOR #Name }) _ ( "(" _ $(Formal) _ ")" / $({ "this" #This }) )
/ $({ "this" #This }) _ $({ BINARY_OPERATOR #Name }) _ "(" _ $(Formal) _ ")"
PrefixOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ $(TypeParameters)? _ addPrefixHeader _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #PrefixOperatorDeclaration }
addPrefixHeader = $({ PREFIX_OPERATOR #Name }) _ ( "(" _ $(Formal) _ ")" / $({ "this" #This }) )
ApplyOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ "this" _ $(TypeParameters)? _ $(Formals) _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #ApplyOperatorDeclaration }
SetOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ "this" _ $(TypeParameters)? _ addSetHeader _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #SetOperatorDeclaration }
addSetHeader = $(Formals) _ $({ "=" #Name }) _ "(" _ $(Formal) _ ")"
ConversionOperatorDeclaration = ExplicitConversionOperatorDeclaration
/ ImplicitConversionOperatorDeclaration
ExplicitConversionOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ $(TypeParameters)? _ "(" _ $(Formal) _ ")" _ "as" _ $(Type) _ $(Guard)? _ $(MethodBody) #ConversionOperatorDeclaration }
/ { ( $(MethodModifiers) _ )? "operator" _ $(TypeParameters)? _ "(" _ $(Formal) _ ")" _ "as" _ $({ "?" #WildCard }) _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #ExplicitConversionOperatorDeclaration }
ImplicitConversionOperatorDeclaration = { ( $(MethodModifiers) _ )? "operator" _ $(TypeParameters)? _ "(" _ $(Formal) _ ")" _ $(Guard)? _ $(HasResultType)? _ $(MethodBody) #ImplicitConversionOperatorDeclaration }
ConstructorDeclaration = { ( $(Modifiers) _ )? "def" _ "this" _ $(TypeParameters)? _ $(Formals) _ $(Guard)? _ $(HasResultType)? _ $(ConstructorBody) #ConstructorDeclaration }
ConstructorBody = "=" _ ExplicitConstructorInvocation
/ "=" _ AssignPropertyCall
/ ConstructorBlock
/ { ";" #Empty }
ConstructorBlock = { "{" _ $(ExplicitConstructorInvocation)? _ addBlockStatements? _ "}" #Block }
ExplicitConstructorInvocation = { $(Constructor) _ ( $(TypeArguments) _ )? "(" _ $(ArgumentList)? _ ")" _ ";" #ExplicitConstructorInvocation }
Constructor = { "this" #This }
/ { "super" #Super }
/ { $(PrimaryExpression / ExpressionName) _ "." _ "this" #This }
/ { $(PrimaryExpression / ExpressionName) _ "." _ "super" #Super }
FieldDeclaration = { ( $(Modifiers) _ )? ( "var" #Variable / "val"? #Value ) _ $(FieldDeclarators) _ ";" }
FieldDeclarators = { $(FieldDeclarator) ( _ "," _ $(FieldDeclarator) )* #List }
FieldDeclarator = { $(Identifier) _ $(HasResultType)? _ "=" _ $(VariableInitializer) #VarDecl }
/ { $(Identifier) _ $(HasResultType) #VarDecl }
LocalVariableDeclaration = { ( $(Modifiers) _ )? ( "var" #Variable / "val" #Value ) _ $(VariableDeclarators) }
/ { ( $(Modifiers) _ )? $(VariableDeclaratorsWithType) }
/ { ( $(Modifiers) _ )? ( "var" #Variable / "val" #Value ) _ $(VariableDeclaratorsNoInitialize) }
VariableDeclarators = { $(VariableDeclarator) ( _ "," _ $(VariableDeclarator) )* #List }
VariableDeclarator = { "[" _ $(IdentifierList) _ "]" _ $(HasResultType)? _ "=" _ $(VariableInitializer) #VarDecl }
/ { $(Identifier) _ ( "[" _ $(IdentifierList) _ "]" )? _ $(HasResultType)? _ "=" _ $(VariableInitializer) #VarDecl }
VariableDeclaratorsWithType = { $(VariableDeclaratorWithType) ( _ "," _ $(VariableDeclaratorWithType) )* #List }
VariableDeclaratorWithType = { "[" _ $(IdentifierList) _ "]" _ $(HasResultType) _ "=" _ $(VariableInitializer) #VarDecl }
/ { $(Identifier) _ ( "[" _ $(IdentifierList) _ "]" )? _ $(HasResultType) _ "=" _ $(VariableInitializer) #VarDecl }
VariableDeclaratorsNoInitialize = { $(VariableDeclaratorNoInitialize) ( _ "," _ $(VariableDeclaratorNoInitialize) )* #List }
VariableDeclaratorNoInitialize = { "[" _ $(IdentifierList) _ "]" _ $(ResultType) #VarDecl }
/ { $(Identifier) _ ( "[" _ $(IdentifierList) _ "]" )? _ $(ResultType) #VarDecl }
Formals = "(" _ FormalList _ ")"
/ { "(" _ ")" #List }
FormalList = { $(Formal) ( _ "," _ $(Formal) )* #List }
Formal = { ( $(Modifiers) _ )? ( "var" #Variable / "val"? #Value ) _ $(FormalDeclarator) }
/ { $(Type) #OmittedParam }
FormalDeclarator = { "[" _ $(IdentifierList) _ "]" _ $(ResultType) #OmittedParam }
/ { $(Identifier) _ ( "[" _ $(IdentifierList) _ "]" )? _ $(ResultType) #Param }
LoopIndex = { ( $(Modifiers) _ )? ( "var" #Variable / "val"? #Value ) _ $(LoopIndexDeclarator) #LoopIndex }
LoopIndexDeclarator = { "[" _ $(IdentifierList) _ "]" ( _ $(HasResultType) )? #VarDecl }
/ { $(Identifier) ( _ "[" _ $(IdentifierList) _ "]" )? ( _ $(HasResultType) )? #VarDecl }
VariableInitializer = Expression
Modifiers = { $(Modifier) ( _ $(Modifier) )* #List }
Modifier = Annotation
/ { "abstract" #Abstract }
/ { "atomic" #Atomic }
/ { "final" #Final }
/ { "native" #Native }
/ { "private" #Private }
/ { "protected" #Protected }
/ { "public" #Public }
/ { "static" #Static }
/ { "transient" #Transient }
/ { "clocked" #Clocked }
MethodModifiers = { $(MethodModifier) ( _ $(MethodModifier) )* #List }
MethodModifier = { "property" #Property }
/ Modifier
Guard = DepParams
HasResultType = ResultType
/ { "<:" _ $(Type) #SubTypeOf }
ResultType = ":" _ Type
example PackageDeclaration '''
package ID.ID;
'''
example ImportDeclarations '''
import Int;
import ID.ID.*;
'''
example TypeDeclaration '''
class ID extends Int implements Int, Int {}
'''
example TypeDeclaration '''
struct ID {}
'''
example TypeDeclaration '''
interface ID extends Int, Int {}
'''
example MethodDeclaration '''
public def ID(ID : Int) : Int {}
'''
example MethodDeclaration '''
public operator !(ID : Int) {}
'''
example PropertyMethodDeclaration '''
property ID(ID : Int) = exp;
'''
example ConstructorDeclaration '''
def this(ID : Int) {}
'''
example ConstructorDeclaration '''
def this(ID : Int) = this();
'''
example ConstructorDeclaration '''
def this(ID : Int) = exp.super();
'''
example FieldDeclaration '''
private var ID : Int = exp;
'''
example LocalVariableDeclaration '''
var ID : Int = exp
'''
example LocalVariableDeclaration '''
val ID = exp
'''
example LocalVariableDeclaration '''
var ID : Int
'''
/* Types */
TypeList = { $(Type) ( _ "," _ $(Type) )* #List }
Type = UnAnnotatedType {$ _ $(Annotations) #AnnotatedType }?
UnAnnotatedType = FunctionType
/ NamedType
/ Void
FunctionType = { $(TypeParameters)? _ $(Formals)? _ $(Guard)? _ "=>" _ $(Type) #FunctionType }
NamedType = NamedTypeNoConstraints {$ _ addDependence }?
NamedTypeNoConstraints = SimpleNamedType {$ _ addParamized }?
SimpleNamedType = PrimaryType ( ( {$ _ addDependence } / {$ _ addParamized } {$ _ addDependence }? ) {$ _ addTypeName } )*
PrimaryType = TypeName
/ PrimaryExpression {$ _ addTypeName }
addDependence = $(DepParams) #DepNamedType
DepParams = "{" _ ArgumentList _ "}"
addParamized = $(Arguments) #ParamizedType
/ $(TypeArguments) ( _ $(Arguments))? #ParamizedType
addTypeName = "." _ $(Identifier) #Type
Void = { 'void' #Void }
ClassType = NamedType
InterfaceTypeList = TypeList
ThrowsList = TypeList
TypeName = Identifier {$ _ addTypeName }*
Annotations = { $(Annotation) ( _ $(Annotation) )* #List }
Annotation = { "@" _ $(NamedTypeNoConstraints) #Annotation }
TypeArguments = { "[" _ addTypeArgumentList _ "]" #TypeArguments }
addTypeArgumentList = $(Type) ( _ "," _ $(Type) )*
TypeParameters = { "[" _ addTypeParameterList _ "]" #TypeParameters }
addTypeParameterList = $(Identifier) ( _ "," _ $(Identifier) )*
TypeParametersI = { "[" _ addTypeParameterList _ ","? _ "]" #TypeParameters }
example Type Int
example Type ID[ID]
example Type ID[ID,ID]
example Type ID.ID
example Type (ID : Int) => Int
/* Statements */
Block = { "{" _ addBlockStatements? _ "}" #Block }
_BlockStatements = addBlockStatements
addBlockStatements = $(BlockInteriorStatement) ( _ $(BlockInteriorStatement) )*
BlockInteriorStatement = LocalVariableDeclarationStatement
/ ClassDeclaration
/ StructDeclaration
/ TypeDefDeclaration
/ Statement
LocalVariableDeclarationStatement = LocalVariableDeclaration _ ";"
Statement = AnnotationStatement
/ ExpressionStatement
AnnotationStatement = { $(Annotations) _ $(NonExpressionStatement) #AnnotationStatement }
/ NonExpressionStatement
ExpressionStatement = StatementExpression _ ";"
NonExpressionStatement = Block
/ EmptyStatement
/ AssertStatement
/ SwitchStatement
/ LoopStatement
/ BreakStatement
/ ContinueStatement
/ ReturnStatement
/ ThrowStatement
/ TryStatement
/ LabeledStatement
/ IfStatement
/ AsyncStatement
/ AtStatement
/ AtomicStatement
/ WhenStatement
/ FinishStatement
/ AssignPropertyCall
LoopStatement = ForStatement
/ WhileStatement
/ DoStatement
/ AtEachStatement
EmptyStatement = ";" { #Empty }
AssertStatement = { "assert" _ $(Expression) ( _ ":" _ $(Expression) )? _ ";" #Assert }
BreakStatement = { "break" _ $(Identifier)? _ ";" #Break }
ContinueStatement = { "continue" _ $(Identifier)? _ ";" #Continue }
ReturnStatement = { "return" _ $(Expression)? _ ";" #Return }
ThrowStatement = { "throw" _ $(Expression) _ ";" #Throw }
LabeledStatement = { $(Identifier) _ ":" _ $(LoopStatement) #LabeledStatement }
IfStatement = { "if" _ "(" _ $(Expression) _ ")" _ $(Statement) ( _ "else" _ $(Statement) )? #If }
AtStatement = { "at" _ "(" _ $(Expression) _ ")" _ $(Statement) #At }
AtomicStatement = { "atomic" _ $(Statement) #Atomic }
WhenStatement = { "when" _ "(" _ $(Expression) _ ")" _ $(Statement) #When }
ForStatement = BasicForStatement
/ EnhancedForStatement
BasicForStatement = { "for" _ "(" _ $(ForInit)? _ ";" _ $(Expression)? _ ";" _ $(ForUpdate)? _ ")" _ $(Statement) #For }
EnhancedForStatement = { "for" _ "(" _ $(LoopIndex) _ "in" _ $(Expression) _ ")" _ $(Statement) #ForEach }
/ { "for" _ "(" _ $(Expression) _ ")" _ $(Statement) #ForEach }
ForInit = StatementExpressionList
/ LocalVariableDeclaration
ForUpdate = StatementExpressionList
WhileStatement = { "while" _ "(" _ $(Expression) _ ")" _ $(Statement) #While }
DoStatement = { "do" _ $(Statement) _ "while" _ "(" _ $(Expression) _ ")" _ ";" #DoWhile }
SwitchStatement = { "switch" _ "(" _ $(Expression) _ ")" _ $(SwitchBlock) #Switch }
SwitchBlock = { "{" ( _ $(SwitchBlockGroup) )* _ "}" #Block }
SwitchBlockGroup = { $(SwitchLabels) $({ _ addBlockStatements? #Block }) #SwitchCase }
SwitchLabels = { $(SwitchLabel) ( _ $(SwitchLabel) )* #List }
SwitchLabel = { "case" _ $(Expression) _ ":" #SwitchLabel }
/ { "default" _ ":" #SwitchLabel }
TryStatement = { "try" _ $(Block) _ $(Catches)? _ addFinally #Try }
/ { "try" _ $(Block) _ $(Catches) #Try }
Catches = { $(CatchClause) ( _ $(CatchClause) )* #List }
CatchClause = { "catch" _ "(" _ $(Formal) _ ")" _ $(Block) #Catch }
addFinally = "finally" _ $(Block)
AsyncStatement = { "async" _ $(ClockedClause)? _ $(Statement) #Async }
/ { "clocked" _ "async" _ $(Statement) #Async }
AtEachStatement = { "ateach" _ "(" _ $(LoopIndex) _ "in" _ $(Expression) _ ")" _ $(ClockedClause)? _ $(Statement) #AtEach }
/ { "ateach" _ "(" _ $(Expression) _ ")" _ $(ClockedClause)? _ $(Statement) #AtEach }
FinishStatement = { ( "clocked" _ )? "finish" _ $(Statement) #Finish }
ClockedClause = { "clocked" _ $(Arguments) #Clocked }
AssignPropertyCall = { "property" _ $(TypeArguments)? _ "(" _ $(ArgumentList)? _ ")" #PropertyCall }
example Block {}
example Block '''
{
class X{}
var x = 1;
}
'''
example BlockInteriorStatement var x = 1;
example Statement ;
example Statement {}
example Statement exp++;
example Statement break;
example Statement break ID;
example Statement continue;
example Statement if(exp) break;
example Statement while(exp) {}
example Statement do {} while(exp);
example Statement for(var x = 1; exp; exp++) {}
example Statement return exp;
example Statement ID : while(exp) {}
example Statement assert exp;
example Statement assert exp : exp;
example Statement throw exp;
example Statement '''
switch(exp) {
case exp : break;
case exp : break;
default : break;
}
'''
example Statement '''
try {
} catch (ID : Int) {
} catch (ID : Int) {
}
'''
example Statement '''
try {
} finally {
}
'''
example Statement at(exp) {}
example Statement async {}
example Statement async clocked(exp) {}
example Statement clocked async {}
example Statement finish {}
example Statement clocked finish {}
example Statement ateach(li in exp) {}
example Statement ateach(exp) {}
example Statement atomic {}
example Statement when(exp) {}
/* Expressions */
Arguments = "(" _ ArgumentList _ ")"
ArgumentList = { $(Expression) ( _ "," _ $(Expression) )* #List }
Expression = AssignmentExpression
AssignmentExpression = Assignment
/ ConditionalExpression
Assignment = { $(LeftHandSide) _ addAssignmentOperator _ $(AssignmentExpression) }
/ { $(PrimaryExpressionNoSuffix ( {$ _ (addObjectCreation / addFieldAccess / ($(TypeArguments) _ "(" _ $(ArgumentList)? _ ")" #MethodInvocation)) }? {$ _ "(" _ $(ArgumentList)? _ ")" #MethodInvocation } )+) _ addAssignmentOperator _ $(AssignmentExpression) }
/ { $(ExpressionName ( {$ _ (addObjectCreation / addFieldAccess / ($(TypeArguments) _ "(" _ $(ArgumentList)? _ ")" #MethodInvocation)) }? {$ _ "(" _ $(ArgumentList)? _ ")" #MethodInvocation } )+) _ addAssignmentOperator _ $(AssignmentExpression) }
LeftHandSide = ExpressionName
/ FieldAccess
/ PrimaryExpressionNoSuffix {$ _ addFieldAccess }+
addAssignmentOperator = "=" #Assign
/ "*=" #AssignMul
/ "/=" #AssignDiv
/ "%=" #AssignMod
/ "+=" #AssignAdd
/ "-=" #AssignSub
/ "<<=" #AssignLeftShift
/ ">>=" #AssignRightShift
/ ">>>=" #AssignLogicalRightShift
/ "&=" #AssignBitwiseAnd
/ "^=" #AssignBitwiseXOr
/ "|=" #AssignBitwiseOr
ConditionalExpression = ClosureExpression
/ AtExpression
/ ConditionalOrExpression {$ _ "?" _ $(Expression) _ ":" _ $(ConditionalExpression) #Trinary }?
ClosureExpression = { $(Formals) _ $(Guard)? _ $(HasResultType)? _ "=>" _ $(ClosureBody) #Closure }
ClosureBody = Expression
/ { ( $(Annotations) _ )? $({ "{" _ addBlockStatements? _ $(Expression)? _ "}" #Block }) #Closure }
AtExpression = { "at" _ "(" _ $(Expression) _ ")" _ $(ClosureBody) #At }
ConditionalOrExpression = ConditionalAndExpression {$ _ "||" _ $(ConditionalAndExpression) #Or }*
ConditionalAndExpression = InclusiveOrExpression {$ _ "&&" _ $(InclusiveOrExpression) #And }*
InclusiveOrExpression = ExclusiveOrExpression {$ _ "|" _ $(ExclusiveOrExpression) #BitwiseOr }*
ExclusiveOrExpression = AndExpression {$ _ "^" _ $(AndExpression) #BitwiseXor }*
AndExpression = EqualityExpression {$ _ "&" _ $(EqualityExpression) #BitwiseAnd }*
EqualityExpression = EqualityLeftHandSide {$ _ addEqualityOperator _ $(RelationalExpression) }*
EqualityLeftHandSide = { $(Type) _ "==" _ $(Type) #TypeEquals }
/ RelationalExpression
addEqualityOperator = "==" #Equals
/ "!=" #NotEquals
/ "~" #Tilde
/ "!~" #ExclamationTilde
RelationalExpression = RelationalLeftHandSide {$ _ addRelationalExpression }*
RelationalLeftHandSide = HasZeroConstraint
/ SubtypeConstraint
/ ShiftExpression
addRelationalExpression = "<" _ $(ShiftExpression) #LessThan
/ ">" _ $(ShiftExpression) #GreaterThan
/ "<=" _ $(ShiftExpression) #LessThanEquals
/ ">=" _ $(ShiftExpression) #GreaterThanEquals
/ "instanceof" _ $(Type) #InstanceOf
HasZeroConstraint = { $(Type) _ "haszero" #HasZero }
SubtypeConstraint = { $(Type) _ ("<:" #IsSubtypeOf / ":>" #HasSubtype ) _ $(Type) }
ShiftExpression = AdditiveExpression {$ _ addShiftOperator _ $(AdditiveExpression) }*
addShiftOperator = "<<" #LeftShift
/ ">>" #RightShift
/ ">>>" #LogicalRightShift
/ "->" #HyphenGTS
/ "<-" #LTSHyphen
/ "-<" #HyphenLTS
/ ">-" #GTSHyphen
/ "!" #Exclamation
AdditiveExpression = MultiplicativeExpression {$ _ ("+" #Add / "-" #Sub) _ $(MultiplicativeExpression) }*
MultiplicativeExpression = RangeExpression {$ _ ("*" #Mul / "/" #Div / "%" #Mod) _ $right(RangeExpression) }*
RangeExpression = UnaryExpression {$ _ ".." _ $(UnaryExpression) #Range }*
UnaryExpression = { $(Annotations) _ $(UnannotatedUnaryExpression) #AnnotatedUnary }
/ UnannotatedUnaryExpression
UnannotatedUnaryExpression = PreIncrementExpression
/ PreDecrementExpression
/ { "+" _ $(UnaryExpressionNotPlusMinus) #Plus }
/ { "-" _ $(UnaryExpressionNotPlusMinus) #Minus }
/ UnaryExpressionNotPlusMinus
PreIncrementExpression = { "++" _ $(UnaryExpressionNotPlusMinus) #PreInc }
PreDecrementExpression = { "--" _ $(UnaryExpressionNotPlusMinus) #PreDec }
UnaryExpressionNotPlusMinus = { "~" _ $(UnaryExpression) #Compl }
/ { "!" _ $(UnaryExpression) #Not }
/ { "^" _ $(UnaryExpression) #Caret }
/ { "|" _ $(UnaryExpression) #VerticalBar }
/ { "&" _ $(UnaryExpression) #Ampersand }
/ { "*" _ $(UnaryExpression) #Asterisk }
/ { "/" _ $(UnaryExpression) #Slash }
/ { "%" _ $(UnaryExpression) #Percent }
/ PostfixExpression
PostfixExpression = CastExpression {$ _ ("++" #Inc / "--" #Dec) }*
CastExpression = (PrimaryExpression / ExpressionName) {$ _ "as" _ $(Type) #Cast }*
ExpressionName = FullyQualifiedName
PrimaryExpression = PrimaryExpressionNoSuffix {$ _ (addObjectCreation / addMethodInvocation / addFieldAccess) }*
PrimaryExpressionNoSuffix = { "here" #Here }
/ { "[" _ $(ArgumentList)? _ "]" #ArrayLiteral }
/ Literal
/ { "self" #Self }
/ { "this" #This }
/ { $(ClassName) _ "." _ "this" #This }
/ "(" _ Expression _ ")"
/ ObjectCreationExpression
/ MethodInvocation
/ FieldAccess
ClassName = TypeName
SuperExpression = { "super" #Super }
/ { $(ClassName) _ "." _ "super" #Super }
ObjectCreationExpression = { "new" _ $(TypeName) _ $(TypeArguments)? _ "(" _ $(ArgumentList)? _ ")" ( _ $(ClassBody) )? #New }
/ FullyQualifiedName {$ _ addObjectCreation }
addObjectCreation = "." _ "new" _ $(Identifier) _ $(TypeArguments)? _ "(" _ $(ArgumentList)? _ ")" ( _ $(ClassBody) )? #New
MethodInvocation = MethodName {$ _ addMethodInvocation }
addMethodInvocation = ( $(TypeArguments) _ )? "(" _ $(ArgumentList)? _ ")" #MethodInvocation
MethodName = FullyQualifiedName
FieldAccess = SuperExpression {$ _ addFieldAccess }
addFieldAccess = "." _ $(Identifier) #Member
StatementExpressionList = { $(StatementExpression) ( _ "," _ $(StatementExpression) )* #List }
StatementExpression = Assignment
/ PreIncrementExpression
/ PreDecrementExpression
/ CastExpression {$ _ ("++" #Inc / "--" #Dec) }+
/ PrimaryExpressionNoSuffix ( {$ _ addFieldAccess }? {$ _ (addObjectCreation / addMethodInvocation) } )+
/ ObjectCreationExpression
/ MethodInvocation
example Expression ID
example Expression 1
example Expression this
example Expression super.ID
example Expression ID.ID(1, 1)
example Expression ID += 1
example Expression ID as Int
example Expression -ID
example Expression ~ID
example Expression ID + 1
example Expression ID - 1
example Expression ID * 1
example Expression ID / 1
example Expression ID % 1
example Expression ID << 1
example Expression ID >> 1
example Expression ID >>> 1
example Expression ID & 1
example Expression ID | 1
example Expression ID ^ 1
example Expression !ID
example Expression ID && 1
example Expression ID || 1
example Expression ID < 1
example Expression ID <= 1
example Expression ID > 1
example Expression ID >= 1
example Expression ID == 1
example Expression Int == Int
example Expression ID instanceof Int
example Expression (ID != 1) ? ID : ID
example Expression new Int()
example Expression ID.new ID()
example Expression new Int() {}
example Expression [1, 1, 1]
example Expression (ID : Int) => @A {}
example Expression at(this) ID
/* Identifiers */
Identifier = { IDENTIFIER #Identifier }
IDENTIFIER = !KEYWORD [a-zA-Z_] W*
/ '`' ( !'`' QUOTED_CONTENT )+ '`'
QUOTED_CONTENT = '\\' ( '`' / '\\' )
/ .
IdentifierList = { $(Identifier) ( _ "," _ $(Identifier) )* #List }
FullyQualifiedName = Identifier {$ _ "." _ $(Identifier) #FullyQualifiedName }*
example Identifier Hoge_piyo
example Identifier Fuga1
example Identifier As
example Identifier `while`
example Identifier `0`
example Identifier `!`
example Identifier `(unbalanced(`
example Identifier '''
`\`\\`
'''
/* Literals */
Literal = BooleanLiteral
/ NullLiteral
/ CharLiteral
/ StringLiteral
/ NumericLiteral
BooleanLiteral = { "true" #True }
/ { "false" #False }
NullLiteral = { "null" #Null }
NumericLiteral = { SIGN INTEGER signedSuffix }
/ { INTEGER (unsignedSuffix / signedSuffix) }
/ { SIGN? FLOAT floatSuffix }
signedSuffix = [nN] #Int
/ [sS] #Short
/ [yY] #Byte
/ [fF] #Float
/ [dD] #Double
/ [lL]? #Long
unsignedSuffix = [nN] [uU] #UInt
/ [uU] [nN] #UInt
/ [sS] [uU] #UShort
/ [uU] [sS] #UShort
/ [yY] [uU] #UByte
/ [uU] [yY] #UByte
/ [lL] [uU] #ULong
/ [uU] [lL]? #ULong
floatSuffix = [fF] #Float
/ [dD]? #Double
CharLiteral = '\'' { !'\'' CHAR_CONTENT #Char } '\''
StringLiteral = '"' { ( !'"' CHAR_CONTENT )* #Char } '"'
DIGITS = '0'
/ [1-9] [0-9]*
OCTALDIGITS = [0-7]+
HEXDIGITS = [a-fA-F0-9]+
INTEGER = '0' OCTALDIGITS
/ '0'[xX] HEXDIGITS
/ DIGITS ![.eE]
SIGN = [-+]
INTPART = DIGITS ( '.' !'.' DIGITS? )?
/ '.' !'.' DIGITS
EXPONENT = [eE] SIGN? DIGITS
FLOAT = INTPART EXPONENT?
CHAR_CONTENT = '\\' ESCAPE
/ !'\\' .
ESCAPE = [btnfrBTNFR'"]
/ '\\'
/ [0-3] [0-7] [0-7]
example Literal true
example Literal false
example Literal null
example Literal 123n
example Literal -321N
example Literal 0123n
example Literal -0x123N
example Literal 0XEBECN
example Literal 1234567890
example Literal 0xBABEL
example Literal 123un
example Literal 0123un
example Literal 123u
example Literal 0xFU
example Literal 0xDecafC0ffeefU
example Literal 414S
example Literal 7001s
example Literal 0xBeaus
example Literal 50y
example Literal 0xbuy
example Literal 1f
example Literal 6.626068E-34F
example Literal 0.0
example Literal 0e100
example Literal 1.3d
example Literal 314159265e-8
example Literal '''
'c'
'''
example Literal '''
'\n'
'''
example Literal '''
""
'''
example Literal '''
"hello\040world!"
'''
/* Keywords and Operators */
KEYWORD = "abstract" / "as" / "assert" / "async" / "at"
/ "athome" / "ateach" / "atomic" / "break" / "case"
/ "catch" / "class" / "clocked" / "continue" / "def"
/ "default" / "do" / "else" / "extends" / "false"
/ "final" / "finally" / "finish" / "for" / "goto"
/ "haszero" / "here" / "if" / "implements" / "import"
/ "in" / "instanceof" / "interface" / "native" / "new"
/ "null" / "offer" / "offers" / "operator" / "package"
/ "private" / "property" / "protected" / "public" / "return"
/ "self" / "static" / "struct" / "super" / "switch"
/ "this" / "throw" / "transient" / "true" / "try"
/ "type" / "val" / "var" / "void" / "when"
/ "while" / "throws"
BINARY_OPERATOR = "+" / "-" / "*" / "/" / "%"
/ "&" / "|" / "^" / "&&" / "||"
/ "<<" / ">>" / ">>>"
/ ">=" / "<=" / ">" / "<" / "==" / "!="
/ ".." / "->" / "<-" / "-<" / ">-"
/ "**" / "~" / "!~" / "!"
PREFIX_OPERATOR = "+" / "-" / "!" / "~" / "^"
/ "|" / "&" / "*" / "/" / "%"
"abstract" = 'abstract' !W
"as" = 'as' !W
"assert" = 'assert' !W
"async" = 'async' !W
"at" = 'at' !W
"athome" = 'athome' !W
"ateach" = 'ateach' !W
"atomic" = 'atomic' !W
"break" = 'break' !W
"case" = 'case' !W
"catch" = 'catch' !W
"class" = 'class' !W
"clocked" = 'clocked' !W
"continue" = 'continue' !W
"def" = 'def' !W
"default" = 'default' !W
"do" = 'do' !W
"else" = 'else' !W
"extends" = 'extends' !W
"false" = 'false' !W
"final" = 'final' !W
"finally" = 'finally' !W
"finish" = 'finish' !W
"for" = 'for' !W
"goto" = 'goto' !W
"haszero" = 'haszero' !W
"here" = 'here' !W
"if" = 'if' !W
"implements" = 'implements' !W
"import" = 'import' !W
"in" = 'in' !W
"instanceof" = 'instanceof' !W
"interface" = 'interface' !W
"native" = 'native' !W
"new" = 'new' !W
"null" = 'null' !W
"offer" = 'offer' !W
"offers" = 'offers' !W
"operator" = 'operator' !W
"package" = 'package' !W
"private" = 'private' !W
"property" = 'property' !W
"protected" = 'protected' !W
"public" = 'public' !W
"return" = 'return' !W
"self" = 'self' !W
"static" = 'static' !W
"struct" = 'struct' !W
"super" = 'super' !W
"switch" = 'switch' !W
"this" = 'this' !W
"throw" = 'throw' !W
"transient" = 'transient' !W
"true" = 'true' !W
"try" = 'try' !W
"type" = 'type' !W
"val" = 'val' !W
"var" = 'var' !W
"void" = 'void' !W
"when" = 'when' !W
"while" = 'while' !W
"throws" = 'throws' !W
"(" = '('
")" = ')'
"{" = '{'
"}" = '}'
"[" = '['
"]" = ']'
";" = ';'
"," = ','
"." = '.' ![.]
"==" = '=='
"!=" = '!='
"<" = '<' !( '-' / [:<=] )
">" = '>' !( '-' / [=>] )
"<=" = '<='
">=" = '>='
"&&" = '&&'
"||" = '||'
"&" = '&' ![&=]
"|" = '|' ![=|]
"^" = '^' ![=]
"<<" = '<<' ![=]
">>" = '>>' ![=>]
">>>" = '>>>' ![=]
"+" = '+' ![+=]
"-" = '-' !( '-' / [<=>] )
"*" = '*' ![*=]
"/" = '/' ![=]
"%" = '%' ![=]
"++" = '++'
"--" = '--'
"!" = '!' ![=~]
"~" = '~'
"&=" = '&='
"|=" = '|='
"^=" = '^='
"<<=" = '<<='
">>=" = '>>='
">>>=" = '>>>='
"+=" = '+='
"-=" = '-='
"*=" = '*='
"/=" = '/='
"%=" = '%='
"=" = '=' ![=>]
"?" = '?'
":" = ':' ![>]
"=>" = '=>'
"->" = '->'
"<:" = '<:'
":>" = ':>'
"@" = '@'
".." = '..'
"**" = '**'
"!~" = '!~'
"-<" = '-<'
">-" = '>-'
"<-" = '<-'
/* Integration Tests */
example File '''
package examples;
import x10.array.*;
public class ArraySum {
static N = 10;
static def reduce[T](a:Array[T], f:(T,T)=>T){T haszero} {
var result:T = Zero.get[T]();
for(v in a) result = f(result, v);
return result;
}
public static def main(Rail[String]) {
val a = new Array_2[Double](N, N);
for(var i:Int=0; i<N; ++i) for(j in 0..(N-1)) a(i,j) = i+j;
Console.OUT.println("Sum: " + reduce(a, (x:Double,y:Double)=>x+y));
}
}
'''
example File '''
package examples;
public class AsyncScope {
public static def main(Rail[String]) {
val val1 = 1;
var var1:Int = 2;
finish {
val val2 = 3;
var var2:Int = 4;
async {
val tmp1 = val1; // ok
val tmp2 = val2; // ok
val tmp3 = var1; // ok
// val tmp4 = var2; // illegal
}
async {
var1 = 5; // ok
// var2 = 6; // illegal
}
}
}
}
'''
example File '''
package examples;
public class Buffer[T]{T haszero} {
protected var datum:T = null;
public def send(v:T){v!=null} {
when(datum == null) {
datum = v;
}
}
public def receive() {
when(datum != null) {
val v = datum;
datum = null;
return v;
}
}
public static def main(Rail[String]) {
val buffer = new Buffer[Any]();
finish {
async {
for(i in 1..10) buffer.send(i);
}
async {
for(i in 1..10) Console.OUT.println(buffer.receive());
}
}
}
}
'''
example File '''
package examples;
public class Clocks {
public static def main(Rail[String]) {
clocked finish { // anonymous clock
for(1..4) clocked async {
Console.OUT.println("Phase 1");
Clock.advanceAll();
Console.OUT.println("Phase 2");
}
}
finish { // named clock
val c = Clock.make();
for(1..4) async clocked(c) {
Console.OUT.println("Phase 3");
c.advance();
Console.OUT.println("Phase 4");
}
c.drop();
}
}
}
'''
example File '''
package examples;
import x10.util.Random;
public class DistPi {
public static def main(args:Rail[String]) {
val N = Int.parse(args(0));
val result = GlobalRef[Cell[Double]](new Cell[Double](0));
finish for(p in Place.places()) at(p) async {
val myRand = new Random();
var myResult:Double = 0;
for(1..(N/Place.MAX_PLACES)) {
val x = myRand.nextDouble();
val y = myRand.nextDouble();
if(x*x + y*y <= 1) myResult++;
}
val myFinalResult = myResult;
at(result) async atomic result()() += myFinalResult;
}
val pi = 4*result()()/N;
Console.OUT.println("The value of pi is " + pi);
}
}
'''
example File '''
package examples;
public class DistRail[T](size:Long) {
protected val chunk:Long;
protected val raw:PlaceLocalHandle[Rail[T]];
public def this(size:Long){T haszero} {
property(size);
assert(size%Place.MAX_PLACES == 0L); // to keep it simple
val chunk = size/Place.MAX_PLACES; this.chunk = chunk;
raw = PlaceLocalHandle.make[Rail[T]](Place.places(), ()=>new Rail[T](chunk));
}
public operator this(i:Long) = (v:T) { at(Place(i/chunk)) raw()(i%chunk) = v; }
public operator this(i:Long) = at(Place(i/chunk)) raw()(i%chunk);
public static def main(Rail[String]) {
val v = new DistRail[Long](256);
v(135) = Place.MAX_PLACES; Console.OUT.println(v(135));
}
}
'''
example File '''
package examples;
public class Fib {
static def fib(n:Int):Int {
if(n < 2) return n;
val f1:Int;
val f2:Int;
finish {
async f1 = fib(n-1);
f2 = fib(n-2);
}
return f1 + f2;
}
public static def main(Rail[String]) {
Console.OUT.println("fib(20)=" + fib(20));
}
}
'''
example File '''
package examples;
import x10.io.Console;
public class Hello { // class
protected val n:Long; // field
public def this(n:Long) { this.n = n; } // constructor
public def test() = n > 0; // method
public static def main(args:Rail[String]) {
Console.OUT.println("Hello world! ");
val foo = new Hello(args.size); // inferred type
var result:Boolean = foo.test(); // no inference for vars
if(result) Console.OUT.println("The first arg is: " + args(0));
}
}
'''
example File '''
package examples;
class HelloWholeWorld {
public static def main(args:Rail[String]) {
finish
for(p in Place.places())
at(p) async
Console.OUT.println(p + " says " + args(0));
Console.OUT.println("Bye");
}
}
'''