Skip to content

Commit e30769c

Browse files
committed
Merge pull requests #155 and #156
3 parents 76f53c4 + e6e376f + cd637ea commit e30769c

13 files changed

+740365
-545361
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ To run the unit tests, run:
3838

3939
>`npm run test`
4040
41+
It is also helpful to run the parser on a real Kotlin project's source files.
42+
43+
```shell
44+
./node_modules/.bin/tree-sitter parse "/path/to/some/project/**/*.kt" --quiet --stat
45+
```
46+
4147
## WebAssembly
4248

4349
### Compilation

bindings/go/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/tree-sitter/tree-sitter-kotlin
2+
3+
go 1.22
4+
5+
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8

grammar.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = grammar({
9494
// ambiguity between multiple user types and class property/function declarations
9595
[$.user_type],
9696
[$.user_type, $.anonymous_function],
97-
[$.user_type, $.function_type],
97+
//[$.user_type, $.function_type],
9898

9999
// ambiguity between annotated_lambda with modifiers and modifiers from var declarations
100100
[$.annotated_lambda, $.modifiers],
@@ -110,6 +110,9 @@ module.exports = grammar({
110110
[$.type_modifiers],
111111
// ambiguity between associating type modifiers
112112
[$.not_nullable_type],
113+
114+
[$.receiver_type],
115+
[$.receiver_type, $._type],
113116
],
114117

115118
externals: $ => [
@@ -337,20 +340,20 @@ module.exports = grammar({
337340
optional(seq("=", $._expression))
338341
),
339342

340-
_receiver_type: $ => seq(
343+
receiver_type: $ => seq(
341344
optional($.type_modifiers),
342345
choice (
343-
$._type_reference,
344346
$.parenthesized_type,
345-
$.nullable_type
347+
$.nullable_type,
348+
$._type_reference,
346349
)
347350
),
348351

349352
function_declaration: $ => prec.right(seq( // TODO
350353
optional($.modifiers),
351354
"fun",
352355
optional($.type_parameters),
353-
optional(seq($._receiver_type, optional('.'))),
356+
optional(seq(field("receiver", $.receiver_type), optional('.'))),
354357
$.simple_identifier,
355358
$.function_value_parameters,
356359
optional(seq(":", $._type)),
@@ -370,7 +373,7 @@ module.exports = grammar({
370373
optional($.modifiers),
371374
$.binding_pattern_kind,
372375
optional($.type_parameters),
373-
optional(seq($._receiver_type, optional('.'))),
376+
optional(seq(field("receiver", $.receiver_type), optional('.'))),
374377
choice($.variable_declaration, $.multi_variable_declaration),
375378
optional($.type_constraints),
376379
optional(choice(
@@ -464,10 +467,10 @@ module.exports = grammar({
464467
_type: $ => seq(
465468
optional($.type_modifiers),
466469
choice(
470+
$.function_type,
467471
$.parenthesized_type,
468472
$.nullable_type,
469473
$._type_reference,
470-
$.function_type,
471474
$.not_nullable_type
472475
)
473476
),
@@ -513,7 +516,7 @@ module.exports = grammar({
513516
_type_projection_modifier: $ => $.variance_modifier,
514517

515518
function_type: $ => seq(
516-
optional(seq($._simple_user_type, ".")), // TODO: Support "real" types
519+
optional(seq(field("receiver", $.receiver_type), ".")),
517520
$.function_type_parameters,
518521
"->",
519522
$._type
@@ -623,7 +626,6 @@ module.exports = grammar({
623626

624627
_unary_expression: $ => choice(
625628
$.postfix_expression,
626-
$.call_expression,
627629
$.indexing_expression,
628630
$.navigation_expression,
629631
$.prefix_expression,
@@ -738,6 +740,7 @@ module.exports = grammar({
738740
$._literal_constant,
739741
$.string_literal,
740742
$.callable_reference,
743+
$.call_expression,
741744
$._function_literal,
742745
$.object_literal,
743746
$.collection_literal,
@@ -751,7 +754,13 @@ module.exports = grammar({
751754

752755
parenthesized_expression: $ => seq("(", $._expression, ")"),
753756

754-
collection_literal: $ => seq("[", $._expression, repeat(seq(",", $._expression)), "]"),
757+
// https://kotlinlang.org/spec/syntax-and-grammar.html#grammar-rule-collectionLiteral
758+
collection_literal: $ => seq(
759+
"[",
760+
$._expression,
761+
repeat(seq(",", $._expression)),
762+
optional(","),
763+
"]"),
755764

756765
_literal_constant: $ => choice(
757766
$.boolean_literal,
@@ -836,7 +845,7 @@ module.exports = grammar({
836845
seq(
837846
optional(field('consequence', $.control_structure_body)),
838847
optional(";"),
839-
"else",
848+
"else",
840849
choice(field('alternative', $.control_structure_body), ";")
841850
),
842851
";"

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@
5858
"tree-sitter": [
5959
{
6060
"scope": "source.kotlin",
61-
"file-types": [
62-
"kt",
63-
"kts"
64-
]
61+
"injection-regex": "^kotlin$"
6562
}
6663
]
6764
}

src/grammar.json

+49-22
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@
12401240
}
12411241
]
12421242
},
1243-
"_receiver_type": {
1243+
"receiver_type": {
12441244
"type": "SEQ",
12451245
"members": [
12461246
{
@@ -1260,15 +1260,15 @@
12601260
"members": [
12611261
{
12621262
"type": "SYMBOL",
1263-
"name": "_type_reference"
1263+
"name": "parenthesized_type"
12641264
},
12651265
{
12661266
"type": "SYMBOL",
1267-
"name": "parenthesized_type"
1267+
"name": "nullable_type"
12681268
},
12691269
{
12701270
"type": "SYMBOL",
1271-
"name": "nullable_type"
1271+
"name": "_type_reference"
12721272
}
12731273
]
12741274
}
@@ -1315,8 +1315,12 @@
13151315
"type": "SEQ",
13161316
"members": [
13171317
{
1318-
"type": "SYMBOL",
1319-
"name": "_receiver_type"
1318+
"type": "FIELD",
1319+
"name": "receiver",
1320+
"content": {
1321+
"type": "SYMBOL",
1322+
"name": "receiver_type"
1323+
}
13201324
},
13211325
{
13221326
"type": "CHOICE",
@@ -1490,8 +1494,12 @@
14901494
"type": "SEQ",
14911495
"members": [
14921496
{
1493-
"type": "SYMBOL",
1494-
"name": "_receiver_type"
1497+
"type": "FIELD",
1498+
"name": "receiver",
1499+
"content": {
1500+
"type": "SYMBOL",
1501+
"name": "receiver_type"
1502+
}
14951503
},
14961504
{
14971505
"type": "CHOICE",
@@ -2175,19 +2183,19 @@
21752183
"members": [
21762184
{
21772185
"type": "SYMBOL",
2178-
"name": "parenthesized_type"
2186+
"name": "function_type"
21792187
},
21802188
{
21812189
"type": "SYMBOL",
2182-
"name": "nullable_type"
2190+
"name": "parenthesized_type"
21832191
},
21842192
{
21852193
"type": "SYMBOL",
2186-
"name": "_type_reference"
2194+
"name": "nullable_type"
21872195
},
21882196
{
21892197
"type": "SYMBOL",
2190-
"name": "function_type"
2198+
"name": "_type_reference"
21912199
},
21922200
{
21932201
"type": "SYMBOL",
@@ -2408,8 +2416,12 @@
24082416
"type": "SEQ",
24092417
"members": [
24102418
{
2411-
"type": "SYMBOL",
2412-
"name": "_simple_user_type"
2419+
"type": "FIELD",
2420+
"name": "receiver",
2421+
"content": {
2422+
"type": "SYMBOL",
2423+
"name": "receiver_type"
2424+
}
24132425
},
24142426
{
24152427
"type": "STRING",
@@ -2917,10 +2929,6 @@
29172929
"type": "SYMBOL",
29182930
"name": "postfix_expression"
29192931
},
2920-
{
2921-
"type": "SYMBOL",
2922-
"name": "call_expression"
2923-
},
29242932
{
29252933
"type": "SYMBOL",
29262934
"name": "indexing_expression"
@@ -3690,6 +3698,10 @@
36903698
"type": "SYMBOL",
36913699
"name": "callable_reference"
36923700
},
3701+
{
3702+
"type": "SYMBOL",
3703+
"name": "call_expression"
3704+
},
36933705
{
36943706
"type": "SYMBOL",
36953707
"name": "_function_literal"
@@ -3772,6 +3784,18 @@
37723784
]
37733785
}
37743786
},
3787+
{
3788+
"type": "CHOICE",
3789+
"members": [
3790+
{
3791+
"type": "STRING",
3792+
"value": ","
3793+
},
3794+
{
3795+
"type": "BLANK"
3796+
}
3797+
]
3798+
},
37753799
{
37763800
"type": "STRING",
37773801
"value": "]"
@@ -6329,10 +6353,6 @@
63296353
"user_type",
63306354
"anonymous_function"
63316355
],
6332-
[
6333-
"user_type",
6334-
"function_type"
6335-
],
63366356
[
63376357
"annotated_lambda",
63386358
"modifiers"
@@ -6354,6 +6374,13 @@
63546374
],
63556375
[
63566376
"not_nullable_type"
6377+
],
6378+
[
6379+
"receiver_type"
6380+
],
6381+
[
6382+
"receiver_type",
6383+
"_type"
63576384
]
63586385
],
63596386
"precedences": [],

0 commit comments

Comments
 (0)