@willbooster/tree-sitter-kotlin 1.6.3 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -68,10 +68,12 @@ module.exports = grammar({
68
68
  [$.member_modifier, $._reserved_identifier],
69
69
 
70
70
  [$.context_parameters, $._reserved_identifier],
71
- [$.modifiers, $.function_type],
71
+ [$._modifier_context_parameters, $._reserved_identifier],
72
+ [$.context_parameters, $._modifier_context_parameters, $._reserved_identifier],
73
+ [$._context_entry, $._modifier_context_parameters],
74
+ [$._modifier_context_parameters],
72
75
  [$.secondary_constructor, $._reserved_identifier],
73
76
  [$.enum_entry, $.modifiers],
74
- [$.modifiers, $.anonymous_function],
75
77
  [$.qualified_identifier],
76
78
  [$.constructor_invocation, $._unescaped_annotation],
77
79
  [$.nullable_type],
@@ -111,6 +113,18 @@ module.exports = grammar({
111
113
  $._delegation_end,
112
114
  // the `)` that ends a call's arguments (see `value_arguments`)
113
115
  $._arguments_end,
116
+ // empty: they tell the scanner when a statement list or the member list of a class body opens in braces, and when
117
+ // either closes (see `frames` in the scanner)
118
+ $._open_statements,
119
+ $._open_members,
120
+ $._close_braces,
121
+ $._top_level_statement_end,
122
+ // empty: it ends a context list of types among modifiers, except directly in a statement list (see `modifiers`)
123
+ $._context_end,
124
+ // a hard keyword other than `this` right after the `$` of a string template (see `_template_name`)
125
+ $._keyword_reference,
126
+ // never scanned: see `_template_name`
127
+ $._keyword_reference_end,
114
128
  ],
115
129
 
116
130
  inline: ($) => [$._statements, $._identifier, $._control_structure_body],
@@ -170,7 +184,9 @@ module.exports = grammar({
170
184
  repeat($.file_annotation),
171
185
  optional($.package_header),
172
186
  repeat($.import),
173
- repeat(seq($.statement, $._semi))
187
+ // The scanner clears its brace frames after each top-level statement, so that a brace that error recovery
188
+ // consumed affects no later statement.
189
+ repeat(seq($.statement, $._semi, $._top_level_statement_end))
174
190
  ),
175
191
 
176
192
  file_annotation: ($) =>
@@ -344,7 +360,7 @@ module.exports = grammar({
344
360
 
345
361
  function_body: ($) => choice($.block, seq('=', $.expression)),
346
362
 
347
- block: ($) => seq('{', optional($._statements), '}'),
363
+ block: ($) => seq('{', $._open_statements, optional($._statements), '}', $._close_braces),
348
364
 
349
365
  for_statement: ($) =>
350
366
  prec.right(
@@ -388,7 +404,8 @@ module.exports = grammar({
388
404
 
389
405
  _loop_prefix: ($) => prec.dynamic(1, repeat1(choice($.annotation, $.label))),
390
406
 
391
- class_body: ($) => seq('{', repeat(seq($.class_member_declaration, $._class_member_semi)), '}'),
407
+ class_body: ($) =>
408
+ seq('{', $._open_members, repeat(seq($.class_member_declaration, $._class_member_semi)), '}', $._close_braces),
392
409
 
393
410
  class_member_declaration: ($) =>
394
411
  choice($.declaration, $.companion_object, $.anonymous_initializer, $.secondary_constructor),
@@ -398,9 +415,11 @@ module.exports = grammar({
398
415
  enum_class_body: ($) =>
399
416
  seq(
400
417
  '{',
418
+ $._open_members,
401
419
  optionalCommaSep1($.enum_entry),
402
420
  optional(seq(';', repeat(seq($.class_member_declaration, $._class_member_semi)))),
403
- '}'
421
+ '}',
422
+ $._close_braces
404
423
  ),
405
424
 
406
425
  // Only annotations can modify an enum entry, and with the full modifier list a `context` after an annotation would
@@ -440,7 +459,7 @@ module.exports = grammar({
440
459
  $.inheritance_modifier,
441
460
  $.parameter_modifier,
442
461
  $.platform_modifier,
443
- $.context_parameters
462
+ alias($._modifier_context_parameters, $.context_parameters)
444
463
  )
445
464
  )
446
465
  )
@@ -518,13 +537,27 @@ module.exports = grammar({
518
537
  ),
519
538
 
520
539
  // Context parameters (`context(scope: Scope)`, Kotlin 2.2) and the older context receivers (`context(Scope)`).
521
- context_parameters: ($) =>
540
+ context_parameters: ($) => seq('context', '(', commaSep1($._context_entry), optional(','), ')'),
541
+
542
+ _context_entry: ($) => choice(seq(optional($.parameter_modifiers), $.parameter), $.type),
543
+
544
+ // Directly in a statement list, Kotlin reads `context(…)` with only types as a call, e.g. before a local declaration
545
+ // on the next line, so the scanner ends such a list among modifiers only elsewhere. A list with a named parameter
546
+ // cannot be a call.
547
+ _modifier_context_parameters: ($) =>
522
548
  seq(
523
549
  'context',
524
550
  '(',
525
- commaSep1(choice(seq(optional($.parameter_modifiers), $.parameter), $.type)),
526
- optional(','),
527
- ')'
551
+ choice(
552
+ seq(commaSep1($.type), optional(','), ')', $._context_end),
553
+ seq(
554
+ repeat(seq($.type, ',')),
555
+ seq(optional($.parameter_modifiers), $.parameter),
556
+ repeat(seq(',', $._context_entry)),
557
+ optional(','),
558
+ ')'
559
+ )
560
+ )
528
561
  ),
529
562
 
530
563
  function_type_parameters: ($) => seq('(', optionalCommaSep1(choice($.parameter, $.type)), ')'),
@@ -662,7 +695,14 @@ module.exports = grammar({
662
695
  annotated_lambda: ($) => seq(repeat($.annotation), optional($.label), $.lambda_literal),
663
696
 
664
697
  lambda_literal: ($) =>
665
- seq('{', optional(seq(optional($.lambda_parameters), '->')), optionalSep1($.statement, $._semi), '}'),
698
+ seq(
699
+ '{',
700
+ $._open_statements,
701
+ optional(seq(optional($.lambda_parameters), '->')),
702
+ optionalSep1($.statement, $._semi),
703
+ '}',
704
+ $._close_braces
705
+ ),
666
706
 
667
707
  lambda_parameters: ($) => seq(commaSep1($._lambda_parameter), optional(',')),
668
708
 
@@ -724,7 +764,8 @@ module.exports = grammar({
724
764
 
725
765
  collection_literal: ($) => seq('[', optionalCommaSep1($.expression), ']'),
726
766
 
727
- when_expression: ($) => seq('when', optional($.when_subject), '{', repeat($.when_entry), '}'),
767
+ when_expression: ($) =>
768
+ seq('when', optional($.when_subject), '{', $._open_statements, repeat($.when_entry), '}', $._close_braces),
728
769
 
729
770
  when_subject: ($) =>
730
771
  seq('(', optional(seq(repeat($.annotation), 'val', $.variable_declaration, '=')), $.expression, ')'),
@@ -824,7 +865,11 @@ module.exports = grammar({
824
865
  _template_name: ($) =>
825
866
  choice(
826
867
  alias(token.immediate(prec(2, /[\p{L}_][\p{L}_\p{Nd}]*/u)), $.identifier),
827
- alias(token.immediate(prec(2, 'this')), $.this_expression)
868
+ alias(token.immediate(prec(2, 'this')), $.this_expression),
869
+ // As in Kotlin, another hard keyword after `$` is an error: the scanner lexes it, and the grammar then expects
870
+ // `_keyword_reference_end`, which is never scanned. The first alias keeps a name required in `interpolation`; the
871
+ // second makes the end visible when error recovery inserts it as missing, which a hidden token would not be.
872
+ seq(alias($._keyword_reference, $.identifier), alias($._keyword_reference_end, 'keyword_reference_end'))
828
873
  ),
829
874
 
830
875
  // In a string prefixed with n dollars (Kotlin 2.1), exactly n dollars start an interpolation; the scanner
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-kotlin",
3
- "version": "1.6.3",
3
+ "version": "1.6.5",
4
4
  "description": "Kotlin grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -3129,6 +3129,10 @@
3129
3129
  "type": "is",
3130
3130
  "named": false
3131
3131
  },
3132
+ {
3133
+ "type": "keyword_reference_end",
3134
+ "named": false
3135
+ },
3132
3136
  {
3133
3137
  "type": "label",
3134
3138
  "named": true
Binary file
package/tree-sitter.json CHANGED
@@ -13,7 +13,7 @@
13
13
  }
14
14
  ],
15
15
  "metadata": {
16
- "version": "1.6.3",
16
+ "version": "1.6.5",
17
17
  "license": "MIT",
18
18
  "description": "Kotlin grammar for tree-sitter",
19
19
  "authors": [