@@ -3,6 +3,7 @@ const PREC = {
3
3
using_directive : 2 ,
4
4
control : 1 ,
5
5
stable_type_id : 2 ,
6
+ type : 2 ,
6
7
while : 2 ,
7
8
assign : 3 ,
8
9
case : 3 ,
@@ -93,10 +94,19 @@ module.exports = grammar({
93
94
[ $ . _type , $ . compound_type ] ,
94
95
// 'given' '(' '[' _type_parameter • ',' …
95
96
[ $ . _variant_type_parameter , $ . type_lambda ] ,
97
+ // 'given' '(' operator_identifier ':' _type • ',' …
98
+ [ $ . name_and_type , $ . parameter ] ,
99
+ [ $ . _simple_expression , $ . binding , $ . tuple_pattern ] ,
100
+ [ $ . _simple_expression , $ . tuple_pattern ] ,
101
+ [ $ . _simple_expression , $ . _type_identifier ] ,
96
102
// 'if' parenthesized_expression • '{' …
97
103
[ $ . _if_condition , $ . _simple_expression ] ,
98
- // _postfix_expression_choice ':' '(' wildcard • ':' …
99
- [ $ . binding , $ . _simple_type ] ,
104
+ [ $ . block , $ . _braced_template_body1 ] ,
105
+ [ $ . _simple_expression , $ . self_type , $ . _type_identifier ] ,
106
+ [ $ . _simple_expression , $ . _type_identifier ] ,
107
+ [ $ . lambda_expression , $ . self_type , $ . _type_identifier ] ,
108
+ [ $ . lambda_expression , $ . _type_identifier ] ,
109
+ [ $ . binding , $ . _simple_expression , $ . _type_identifier ] ,
100
110
] ,
101
111
102
112
word : $ => $ . _alpha_identifier ,
@@ -366,7 +376,7 @@ module.exports = grammar({
366
376
field ( "bound" , optional ( $ . lower_bound ) ) ,
367
377
field ( "bound" , optional ( $ . upper_bound ) ) ,
368
378
field ( "bound" , optional ( repeat ( $ . view_bound ) ) ) ,
369
- field ( "bound" , optional ( repeat ( $ . context_bound ) ) ) ,
379
+ field ( "bound" , optional ( $ . _context_bounds ) ) ,
370
380
) ,
371
381
372
382
upper_bound : $ => seq ( "<:" , field ( "type" , $ . _type ) ) ,
@@ -375,7 +385,26 @@ module.exports = grammar({
375
385
376
386
view_bound : $ => seq ( "<%" , field ( "type" , $ . _type ) ) ,
377
387
378
- context_bound : $ => seq ( ":" , field ( "type" , $ . _type ) ) ,
388
+ _context_bounds : $ => choice (
389
+ repeat1 ( seq (
390
+ ":" ,
391
+ $ . context_bound
392
+ ) ) ,
393
+ seq (
394
+ ":" ,
395
+ "{" ,
396
+ trailingCommaSep1 ( $ . context_bound ) ,
397
+ "}" ,
398
+ )
399
+ ) ,
400
+
401
+ context_bound : $ => seq (
402
+ field ( "type" , $ . _type ) ,
403
+ optional ( seq (
404
+ "as" ,
405
+ field ( "name" , $ . _identifier ) ,
406
+ ) ) ,
407
+ ) ,
379
408
380
409
/*
381
410
* TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
@@ -522,6 +551,7 @@ module.exports = grammar({
522
551
field ( "type_parameters" , optional ( $ . type_parameters ) ) ,
523
552
field ( "bound" , optional ( $ . lower_bound ) ) ,
524
553
field ( "bound" , optional ( $ . upper_bound ) ) ,
554
+ field ( "bound" , optional ( $ . _context_bounds ) ) ,
525
555
) ,
526
556
) ,
527
557
@@ -552,10 +582,14 @@ module.exports = grammar({
552
582
prec . right (
553
583
seq (
554
584
field ( "name" , $ . _identifier ) ,
555
- field ( "type_parameters" , optional ( $ . type_parameters ) ) ,
556
585
field (
557
586
"parameters" ,
558
- repeat ( seq ( optional ( $ . _automatic_semicolon ) , $ . parameters ) ) ,
587
+ repeat (
588
+ seq (
589
+ optional ( $ . _automatic_semicolon ) ,
590
+ choice ( $ . parameters , $ . type_parameters ) ,
591
+ ) ,
592
+ ) ,
559
593
) ,
560
594
optional ( $ . _automatic_semicolon ) ,
561
595
) ,
@@ -595,6 +629,7 @@ module.exports = grammar({
595
629
optional ( $ . modifiers ) ,
596
630
"given" ,
597
631
optional ( $ . _given_constructor ) ,
632
+ repeat ( $ . _given_sig ) ,
598
633
choice (
599
634
field ( "return_type" , $ . _structural_instance ) ,
600
635
seq (
@@ -605,6 +640,14 @@ module.exports = grammar({
605
640
) ,
606
641
) ,
607
642
643
+ _given_sig : $ =>
644
+ seq (
645
+ $ . _given_conditional ,
646
+ "=>"
647
+ ) ,
648
+
649
+ _given_conditional : $ => alias ( $ . parameters , $ . given_conditional ) ,
650
+
608
651
_given_constructor : $ =>
609
652
prec . right (
610
653
seq (
@@ -627,7 +670,10 @@ module.exports = grammar({
627
670
PREC . compound ,
628
671
seq (
629
672
$ . _constructor_application ,
630
- "with" ,
673
+ choice (
674
+ ":" ,
675
+ "with"
676
+ ) ,
631
677
field ( "body" , $ . with_template_body ) ,
632
678
) ,
633
679
) ,
@@ -783,6 +829,19 @@ module.exports = grammar({
783
829
) ,
784
830
) ,
785
831
832
+ /*
833
+ * NameAndType ::= id ':' Type
834
+ */
835
+ name_and_type : $ =>
836
+ prec . left (
837
+ PREC . control ,
838
+ seq (
839
+ field ( "name" , $ . _identifier ) ,
840
+ ":" ,
841
+ field ( "type" , $ . _param_type ) ,
842
+ ) ,
843
+ ) ,
844
+
786
845
_block : $ =>
787
846
prec . left (
788
847
seq (
@@ -831,14 +890,18 @@ module.exports = grammar({
831
890
annotated_type : $ => prec . right ( seq ( $ . _simple_type , repeat1 ( $ . annotation ) ) ) ,
832
891
833
892
_simple_type : $ =>
834
- choice (
835
- $ . generic_type ,
836
- $ . projected_type ,
837
- $ . tuple_type ,
838
- $ . singleton_type ,
839
- $ . stable_type_identifier ,
840
- $ . _type_identifier ,
841
- $ . wildcard ,
893
+ prec . left (
894
+ PREC . type ,
895
+ choice (
896
+ $ . generic_type ,
897
+ $ . projected_type ,
898
+ $ . tuple_type ,
899
+ $ . named_tuple_type ,
900
+ $ . singleton_type ,
901
+ $ . stable_type_identifier ,
902
+ $ . _type_identifier ,
903
+ $ . wildcard ,
904
+ )
842
905
) ,
843
906
844
907
compound_type : $ =>
@@ -895,6 +958,12 @@ module.exports = grammar({
895
958
896
959
tuple_type : $ => seq ( "(" , trailingCommaSep1 ( $ . _type ) , ")" ) ,
897
960
961
+ named_tuple_type : $ => seq (
962
+ "(" ,
963
+ trailingCommaSep1 ( $ . name_and_type ) ,
964
+ ")" ,
965
+ ) ,
966
+
898
967
singleton_type : $ =>
899
968
prec . left (
900
969
PREC . stable_type_id ,
@@ -992,6 +1061,7 @@ module.exports = grammar({
992
1061
$ . interpolated_string_expression ,
993
1062
$ . capture_pattern ,
994
1063
$ . tuple_pattern ,
1064
+ $ . named_tuple_pattern ,
995
1065
$ . case_class_pattern ,
996
1066
$ . infix_pattern ,
997
1067
$ . alternative_pattern ,
@@ -1007,7 +1077,10 @@ module.exports = grammar({
1007
1077
seq (
1008
1078
field ( "type" , choice ( $ . _type_identifier , $ . stable_type_identifier ) ) ,
1009
1079
"(" ,
1010
- field ( "pattern" , trailingCommaSep ( $ . _pattern ) ) ,
1080
+ choice (
1081
+ field ( "pattern" , trailingCommaSep ( $ . _pattern ) ) ,
1082
+ field ( "pattern" , trailingCommaSep ( $ . named_pattern ) ) ,
1083
+ ) ,
1011
1084
")" ,
1012
1085
) ,
1013
1086
@@ -1035,15 +1108,28 @@ module.exports = grammar({
1035
1108
1036
1109
typed_pattern : $ =>
1037
1110
prec . right (
1111
+ - 1 ,
1038
1112
seq ( field ( "pattern" , $ . _pattern ) , ":" , field ( "type" , $ . _type ) ) ,
1039
1113
) ,
1040
1114
1041
1115
given_pattern : $ => seq ( "given" , field ( "type" , $ . _type ) ) ,
1042
1116
1043
1117
// TODO: Flatten this.
1044
- alternative_pattern : $ => prec . left ( - 1 , seq ( $ . _pattern , "|" , $ . _pattern ) ) ,
1118
+ alternative_pattern : $ => prec . left ( - 2 , seq ( $ . _pattern , "|" , $ . _pattern ) ) ,
1119
+
1120
+ tuple_pattern : $ => seq (
1121
+ "(" ,
1122
+ trailingCommaSep1 ( $ . _pattern ) ,
1123
+ ")" ,
1124
+ ) ,
1125
+
1126
+ named_pattern : $ => prec . left ( - 1 , seq ( $ . _identifier , "=" , $ . _pattern ) ) ,
1045
1127
1046
- tuple_pattern : $ => seq ( "(" , $ . _pattern , repeat ( seq ( "," , $ . _pattern ) ) , ")" ) ,
1128
+ named_tuple_pattern : $ => seq (
1129
+ "(" ,
1130
+ trailingCommaSep1 ( $ . named_pattern ) ,
1131
+ ")" ,
1132
+ ) ,
1047
1133
1048
1134
// ---------------------------------------------------------------
1049
1135
// Expressions
@@ -1530,7 +1616,8 @@ module.exports = grammar({
1530
1616
$ . string ,
1531
1617
) ,
1532
1618
1533
- literal_type : $ => $ . _non_null_literal ,
1619
+ literal_type : $ =>
1620
+ prec . left ( PREC . type , $ . _non_null_literal ) ,
1534
1621
1535
1622
literal : $ => choice ( $ . _non_null_literal , $ . null_literal ) ,
1536
1623
0 commit comments