@@ -51,7 +51,7 @@ const PREC = {
51
51
const DEC_DIGITS = token ( sep1 ( / [ 0 - 9 ] + / , / _ + / ) ) ;
52
52
const HEX_DIGITS = token ( sep1 ( / [ 0 - 9 a - f A - F ] + / , / _ + / ) ) ;
53
53
const BIN_DIGITS = token ( sep1 ( / [ 0 1 ] / , / _ + / ) ) ;
54
- const REAL_EXPONENT = token ( seq ( / [ e E ] / , optional ( / [ + - ] / ) , DEC_DIGITS ) )
54
+ const REAL_EXPONENT = token ( seq ( / [ e E ] / , optional ( / [ + - ] / ) , DEC_DIGITS ) ) ;
55
55
56
56
module . exports = grammar ( {
57
57
name : "kotlin" ,
@@ -177,12 +177,12 @@ module.exports = grammar({
177
177
top_level_object : $ => seq ( $ . _declaration , optional ( $ . _semi ) ) ,
178
178
179
179
type_alias : $ => seq (
180
- optional ( $ . modifiers ) ,
180
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
181
181
"typealias" ,
182
- alias ( $ . simple_identifier , $ . type_identifier ) ,
183
- optional ( $ . type_parameters ) ,
182
+ field ( 'name' , alias ( $ . simple_identifier , $ . type_identifier ) ) ,
183
+ optional ( field ( 'type_parameters' , $ . type_parameters ) ) ,
184
184
"=" ,
185
- $ . _type
185
+ field ( 'type' , $ . _type )
186
186
) ,
187
187
188
188
_declaration : $ => choice (
@@ -208,30 +208,30 @@ module.exports = grammar({
208
208
209
209
class_declaration : $ => prec . right ( choice (
210
210
seq (
211
- optional ( $ . modifiers ) ,
212
- choice ( "class" , "interface" ) ,
213
- alias ( $ . simple_identifier , $ . type_identifier ) ,
214
- optional ( $ . type_parameters ) ,
215
- optional ( $ . primary_constructor ) ,
216
- optional ( seq ( ":" , $ . _delegation_specifiers ) ) ,
217
- optional ( $ . type_constraints ) ,
218
- optional ( $ . class_body )
211
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
212
+ field ( 'kind' , choice ( "class" , "interface" ) ) ,
213
+ field ( 'name' , alias ( $ . simple_identifier , $ . type_identifier ) ) ,
214
+ optional ( field ( 'type_parameters' , $ . type_parameters ) ) ,
215
+ optional ( field ( 'primary_constructor' , $ . primary_constructor ) ) ,
216
+ optional ( seq ( ":" , field ( 'delegation_specifiers' , $ . _delegation_specifiers ) ) ) ,
217
+ optional ( field ( 'constraints' , $ . type_constraints ) ) ,
218
+ optional ( field ( 'body' , $ . class_body ) )
219
219
) ,
220
220
seq (
221
- optional ( $ . modifiers ) ,
222
- "enum" , "class" ,
223
- alias ( $ . simple_identifier , $ . type_identifier ) ,
224
- optional ( $ . type_parameters ) ,
225
- optional ( $ . primary_constructor ) ,
226
- optional ( seq ( ":" , $ . _delegation_specifiers ) ) ,
227
- optional ( $ . type_constraints ) ,
228
- optional ( $ . enum_class_body )
221
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
222
+ field ( 'kind' , "enum" ) , "class" ,
223
+ field ( 'name' , alias ( $ . simple_identifier , $ . type_identifier ) ) ,
224
+ optional ( field ( 'type_parameters' , $ . type_parameters ) ) ,
225
+ optional ( field ( 'primary_constructor' , $ . primary_constructor ) ) ,
226
+ optional ( seq ( ":" , field ( 'delegation_specifiers' , $ . _delegation_specifiers ) ) ) ,
227
+ optional ( field ( 'constraints' , $ . type_constraints ) ) ,
228
+ optional ( field ( 'body' , $ . enum_class_body ) )
229
229
)
230
230
) ) ,
231
231
232
232
primary_constructor : $ => seq (
233
- optional ( seq ( optional ( $ . modifiers ) , "constructor" ) ) ,
234
- $ . _class_parameters
233
+ optional ( seq ( optional ( field ( 'modifiers' , $ . modifiers ) ) , "constructor" ) ) ,
234
+ field ( 'parameters' , $ . _class_parameters )
235
235
) ,
236
236
237
237
class_body : $ => seq ( "{" , optional ( $ . _class_member_declarations ) , "}" ) ,
@@ -308,15 +308,15 @@ module.exports = grammar({
308
308
$ . secondary_constructor
309
309
) ,
310
310
311
- anonymous_initializer : $ => seq ( "init" , $ . _block ) ,
311
+ anonymous_initializer : $ => seq ( "init" , field ( 'body' , $ . _block ) ) ,
312
312
313
313
companion_object : $ => seq (
314
- optional ( $ . modifiers ) ,
314
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
315
315
"companion" ,
316
316
"object" ,
317
- optional ( alias ( $ . simple_identifier , $ . type_identifier ) ) ,
318
- optional ( seq ( ":" , $ . _delegation_specifiers ) ) ,
319
- optional ( $ . class_body )
317
+ optional ( field ( 'name' , alias ( $ . simple_identifier , $ . type_identifier ) ) ) ,
318
+ optional ( seq ( ":" , field ( 'delegation_specifiers' , $ . _delegation_specifiers ) ) ) ,
319
+ optional ( field ( 'body' , $ . class_body ) )
320
320
) ,
321
321
322
322
function_value_parameters : $ => seq (
@@ -342,15 +342,15 @@ module.exports = grammar({
342
342
) ,
343
343
344
344
function_declaration : $ => prec . right ( seq ( // TODO
345
- optional ( $ . modifiers ) ,
345
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
346
346
"fun" ,
347
- optional ( $ . type_parameters ) ,
348
- optional ( seq ( $ . _receiver_type , optional ( '.' ) ) ) ,
349
- $ . simple_identifier ,
350
- $ . function_value_parameters ,
351
- optional ( seq ( ":" , $ . _type ) ) ,
352
- optional ( $ . type_constraints ) ,
353
- optional ( $ . function_body )
347
+ optional ( field ( 'type_parameters' , $ . type_parameters ) ) ,
348
+ optional ( seq ( field ( 'receiver' , $ . _receiver_type ) , optional ( '.' ) ) ) ,
349
+ field ( 'name' , $ . simple_identifier ) ,
350
+ field ( 'parameters' , $ . function_value_parameters ) ,
351
+ optional ( seq ( ":" , field ( 'return_type' , $ . _type ) ) ) ,
352
+ optional ( field ( 'constraints' , $ . type_constraints ) ) ,
353
+ optional ( field ( 'body' , $ . function_body ) )
354
354
) ) ,
355
355
356
356
function_body : $ => choice ( $ . _block , seq ( "=" , $ . _expression ) ) ,
@@ -362,21 +362,21 @@ module.exports = grammar({
362
362
) ) ,
363
363
364
364
property_declaration : $ => prec . right ( seq (
365
- optional ( $ . modifiers ) ,
366
- choice ( "val" , "var" ) ,
367
- optional ( $ . type_parameters ) ,
368
- optional ( seq ( $ . _receiver_type , optional ( '.' ) ) ) ,
369
- choice ( $ . variable_declaration , $ . multi_variable_declaration ) ,
370
- optional ( $ . type_constraints ) ,
365
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
366
+ field ( 'kind' , choice ( "val" , "var" ) ) ,
367
+ optional ( field ( 'type_parameters' , $ . type_parameters ) ) ,
368
+ optional ( seq ( field ( 'receiver' , $ . _receiver_type ) , optional ( '.' ) ) ) ,
369
+ choice ( field ( 'variable' , $ . variable_declaration ) , field ( 'variables' , $ . multi_variable_declaration ) ) ,
370
+ optional ( field ( 'constraints' , $ . type_constraints ) ) ,
371
371
optional ( choice (
372
- seq ( "=" , $ . _expression ) ,
373
- $ . property_delegate
372
+ seq ( "=" , field ( 'expression' , $ . _expression ) ) ,
373
+ field ( 'delegate' , $ . property_delegate )
374
374
) ) ,
375
375
optional ( ';' ) ,
376
376
choice (
377
377
// TODO: Getter-setter combinations
378
- optional ( $ . getter ) ,
379
- optional ( $ . setter )
378
+ optional ( field ( 'getter' , $ . getter ) ) ,
379
+ optional ( field ( 'setter' , $ . setter ) )
380
380
)
381
381
) ) ,
382
382
@@ -415,19 +415,19 @@ module.exports = grammar({
415
415
parameter : $ => seq ( $ . simple_identifier , ":" , $ . _type ) ,
416
416
417
417
object_declaration : $ => prec . right ( seq (
418
- optional ( $ . modifiers ) ,
418
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
419
419
"object" ,
420
- alias ( $ . simple_identifier , $ . type_identifier ) ,
421
- optional ( seq ( ":" , $ . _delegation_specifiers ) ) ,
422
- optional ( $ . class_body )
420
+ field ( 'name' , alias ( $ . simple_identifier , $ . type_identifier ) ) ,
421
+ optional ( seq ( ":" , field ( 'delegation_specifiers' , $ . _delegation_specifiers ) ) ) ,
422
+ optional ( field ( 'body' , $ . class_body ) )
423
423
) ) ,
424
424
425
425
secondary_constructor : $ => seq (
426
- optional ( $ . modifiers ) ,
426
+ optional ( field ( 'modifiers' , $ . modifiers ) ) ,
427
427
"constructor" ,
428
- $ . function_value_parameters ,
429
- optional ( seq ( ":" , $ . constructor_delegation_call ) ) ,
430
- optional ( $ . _block )
428
+ field ( 'parameters' , $ . function_value_parameters ) ,
429
+ optional ( seq ( ":" , field ( 'delegation' , $ . constructor_delegation_call ) ) ) ,
430
+ optional ( field ( 'body' , $ . _block ) )
431
431
) ,
432
432
433
433
constructor_delegation_call : $ => seq ( choice ( "this" , "super" ) , $ . value_arguments ) ,
@@ -570,27 +570,30 @@ module.exports = grammar({
570
570
"for" ,
571
571
"(" ,
572
572
repeat ( $ . annotation ) ,
573
- choice ( $ . variable_declaration , $ . multi_variable_declaration ) ,
573
+ choice (
574
+ field ( 'variable' , $ . variable_declaration ) ,
575
+ field ( 'variables' , $ . multi_variable_declaration )
576
+ ) ,
574
577
"in" ,
575
- $ . _expression ,
578
+ field ( 'value' , $ . _expression ) ,
576
579
")" ,
577
- optional ( $ . control_structure_body )
580
+ optional ( field ( 'body' , $ . control_structure_body ) )
578
581
) ) ,
579
582
580
583
while_statement : $ => seq (
581
584
"while" ,
582
585
"(" ,
583
- $ . _expression ,
586
+ field ( 'condition' , $ . _expression ) ,
584
587
")" ,
585
- choice ( ";" , $ . control_structure_body )
588
+ choice ( ";" , field ( 'body' , $ . control_structure_body ) )
586
589
) ,
587
590
588
591
do_while_statement : $ => prec . right ( seq (
589
592
"do" ,
590
- optional ( $ . control_structure_body ) ,
593
+ optional ( field ( 'body' , $ . control_structure_body ) ) ,
591
594
"while" ,
592
595
"(" ,
593
- $ . _expression ,
596
+ field ( 'condition' , $ . _expression ) ,
594
597
")" ,
595
598
) ) ,
596
599
@@ -775,8 +778,8 @@ module.exports = grammar({
775
778
776
779
lambda_literal : $ => prec ( PREC . LAMBDA_LITERAL , seq (
777
780
"{" ,
778
- optional ( seq ( optional ( $ . lambda_parameters ) , "->" ) ) ,
779
- optional ( $ . statements ) ,
781
+ optional ( seq ( optional ( field ( 'parameters' , $ . lambda_parameters ) ) , "->" ) ) ,
782
+ optional ( field ( 'body' , $ . statements ) ) ,
780
783
"}"
781
784
) ) ,
782
785
@@ -796,9 +799,9 @@ module.exports = grammar({
796
799
anonymous_function : $ => prec . right ( seq (
797
800
"fun" ,
798
801
optional ( seq ( sep1 ( $ . _simple_user_type , "." ) , "." ) ) , // TODO
799
- $ . function_value_parameters ,
800
- optional ( seq ( ":" , $ . _type ) ) ,
801
- optional ( $ . function_body )
802
+ field ( 'parameters' , $ . function_value_parameters ) ,
803
+ optional ( seq ( ":" , field ( 'return_type' , $ . _type ) ) ) ,
804
+ optional ( field ( 'body' , $ . function_body ) )
802
805
) ) ,
803
806
804
807
_function_literal : $ => choice (
@@ -808,29 +811,32 @@ module.exports = grammar({
808
811
809
812
object_literal : $ => seq (
810
813
"object" ,
811
- optional ( seq ( ":" , $ . _delegation_specifiers ) ) ,
812
- $ . class_body
814
+ optional ( seq ( ":" , field ( 'delegation_specifiers' , $ . _delegation_specifiers ) ) ) ,
815
+ field ( 'body' , $ . class_body )
813
816
) ,
814
817
815
- this_expression : $ => "this" ,
818
+ this_expression : $ => choice (
819
+ "this" ,
820
+ $ . _this_at
821
+ ) ,
816
822
817
- super_expression : $ => seq (
823
+ super_expression : $ => prec . right ( choice (
818
824
"super" ,
819
- // TODO optional( seq("<", $._type, ">") ),
820
- // TODO optional(seq("@", $.simple_identifier))
821
- ) ,
825
+ seq ( "super" , " <", $ . _type , ">" ) ,
826
+ $ . _super_at
827
+ ) ) ,
822
828
823
829
if_expression : $ => prec . right ( seq (
824
830
"if" ,
825
- "(" , $ . _expression , ")" ,
831
+ "(" , field ( 'condition' , $ . _expression ) , ")" ,
826
832
choice (
827
- $ . control_structure_body ,
833
+ field ( 'consequence' , $ . control_structure_body ) ,
828
834
";" ,
829
835
seq (
830
- optional ( $ . control_structure_body ) ,
836
+ optional ( field ( 'consequence' , $ . control_structure_body ) ) ,
831
837
optional ( ";" ) ,
832
838
"else" ,
833
- choice ( $ . control_structure_body , ";" )
839
+ choice ( field ( 'alternative' , $ . control_structure_body ) , ";" )
834
840
)
835
841
)
836
842
) ) ,
@@ -849,19 +855,19 @@ module.exports = grammar({
849
855
850
856
when_expression : $ => seq (
851
857
"when" ,
852
- optional ( $ . when_subject ) ,
858
+ optional ( field ( 'subject' , $ . when_subject ) ) ,
853
859
"{" ,
854
- repeat ( $ . when_entry ) ,
860
+ repeat ( field ( 'entry' , $ . when_entry ) ) ,
855
861
"}"
856
862
) ,
857
863
858
864
when_entry : $ => seq (
859
865
choice (
860
- seq ( $ . when_condition , repeat ( seq ( "," , $ . when_condition ) ) ) ,
866
+ seq ( field ( 'condition' , $ . when_condition ) , repeat ( seq ( "," , field ( 'condition' , $ . when_condition ) ) ) ) ,
861
867
"else"
862
868
) ,
863
869
"->" ,
864
- $ . control_structure_body ,
870
+ field ( 'body' , $ . control_structure_body ) ,
865
871
optional ( $ . _semi )
866
872
) ,
867
873
@@ -877,9 +883,9 @@ module.exports = grammar({
877
883
878
884
try_expression : $ => seq (
879
885
"try" ,
880
- $ . _block ,
886
+ field ( 'body' , $ . _block ) ,
881
887
choice (
882
- seq ( repeat1 ( $ . catch_block ) , optional ( $ . finally_block ) ) ,
888
+ seq ( repeat1 ( field ( 'catch' , $ . catch_block ) ) , optional ( field ( 'finally' , $ . finally_block ) ) ) ,
883
889
$ . finally_block
884
890
)
885
891
) ,
@@ -892,10 +898,10 @@ module.exports = grammar({
892
898
":" ,
893
899
$ . _type ,
894
900
")" ,
895
- $ . _block ,
901
+ field ( 'body' , $ . _block ) ,
896
902
) ,
897
903
898
- finally_block : $ => seq ( "finally" , $ . _block ) ,
904
+ finally_block : $ => seq ( "finally" , field ( 'body' , $ . _block ) ) ,
899
905
900
906
jump_expression : $ => choice (
901
907
prec . right ( PREC . RETURN_OR_THROW , seq ( "throw" , $ . _expression ) ) ,
@@ -986,7 +992,8 @@ module.exports = grammar({
986
992
"sealed" ,
987
993
"annotation" ,
988
994
"data" ,
989
- "inner"
995
+ "inner" ,
996
+ "value" ,
990
997
) ,
991
998
992
999
member_modifier : $ => choice (
@@ -1086,6 +1093,7 @@ module.exports = grammar({
1086
1093
"expect" ,
1087
1094
"data" ,
1088
1095
"inner" ,
1096
+ "value" ,
1089
1097
"actual" ,
1090
1098
"set" ,
1091
1099
"get"
@@ -1114,15 +1122,38 @@ module.exports = grammar({
1114
1122
// Keywords
1115
1123
// ==========
1116
1124
1117
- _return_at : $ => seq ( "return@" , $ . _lexical_identifier ) ,
1125
+ _return_at : $ => seq (
1126
+ "return@" ,
1127
+ alias ( $ . _lexical_identifier , $ . label )
1128
+ ) ,
1118
1129
1119
- _continue_at : $ => seq ( "continue@" , $ . _lexical_identifier ) ,
1130
+ _continue_at : $ => seq (
1131
+ "continue@" ,
1132
+ alias ( $ . _lexical_identifier , $ . label )
1133
+ ) ,
1120
1134
1121
- _break_at : $ => seq ( "break@" , $ . _lexical_identifier ) ,
1135
+ _break_at : $ => seq (
1136
+ "break@" ,
1137
+ alias ( $ . _lexical_identifier , $ . label )
1138
+ ) ,
1122
1139
1123
- _this_at : $ => seq ( "this@" , $ . _lexical_identifier ) ,
1140
+ _this_at : $ => seq (
1141
+ "this@" ,
1142
+ alias ( $ . _lexical_identifier , $ . type_identifier )
1143
+ ) ,
1124
1144
1125
- _super_at : $ => seq ( "super@" , $ . _lexical_identifier ) ,
1145
+ _super_at : $ => choice (
1146
+ seq (
1147
+ "super@" ,
1148
+ alias ( $ . _lexical_identifier , $ . type_identifier )
1149
+ ) ,
1150
+ seq (
1151
+ "super" ,
1152
+ "<" , $ . _type , ">" ,
1153
+ token . immediate ( "@" ) ,
1154
+ alias ( $ . _lexical_identifier , $ . type_identifier )
1155
+ )
1156
+ ) ,
1126
1157
1127
1158
// ==========
1128
1159
// Literals
0 commit comments