Skip to content

Commit

Permalink
Merge pull requests #155 and #156
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Dec 18, 2024
3 parents 76f53c4 + e6e376f + cd637ea commit e30769c
Show file tree
Hide file tree
Showing 13 changed files with 740,365 additions and 545,361 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ To run the unit tests, run:

>`npm run test`
It is also helpful to run the parser on a real Kotlin project's source files.

```shell
./node_modules/.bin/tree-sitter parse "/path/to/some/project/**/*.kt" --quiet --stat
```

## WebAssembly

### Compilation
Expand Down
5 changes: 5 additions & 0 deletions bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/tree-sitter/tree-sitter-kotlin

go 1.22

require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
31 changes: 20 additions & 11 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = grammar({
// ambiguity between multiple user types and class property/function declarations
[$.user_type],
[$.user_type, $.anonymous_function],
[$.user_type, $.function_type],
//[$.user_type, $.function_type],

// ambiguity between annotated_lambda with modifiers and modifiers from var declarations
[$.annotated_lambda, $.modifiers],
Expand All @@ -110,6 +110,9 @@ module.exports = grammar({
[$.type_modifiers],
// ambiguity between associating type modifiers
[$.not_nullable_type],

[$.receiver_type],
[$.receiver_type, $._type],
],

externals: $ => [
Expand Down Expand Up @@ -337,20 +340,20 @@ module.exports = grammar({
optional(seq("=", $._expression))
),

_receiver_type: $ => seq(
receiver_type: $ => seq(
optional($.type_modifiers),
choice (
$._type_reference,
$.parenthesized_type,
$.nullable_type
$.nullable_type,
$._type_reference,
)
),

function_declaration: $ => prec.right(seq( // TODO
optional($.modifiers),
"fun",
optional($.type_parameters),
optional(seq($._receiver_type, optional('.'))),
optional(seq(field("receiver", $.receiver_type), optional('.'))),
$.simple_identifier,
$.function_value_parameters,
optional(seq(":", $._type)),
Expand All @@ -370,7 +373,7 @@ module.exports = grammar({
optional($.modifiers),
$.binding_pattern_kind,
optional($.type_parameters),
optional(seq($._receiver_type, optional('.'))),
optional(seq(field("receiver", $.receiver_type), optional('.'))),
choice($.variable_declaration, $.multi_variable_declaration),
optional($.type_constraints),
optional(choice(
Expand Down Expand Up @@ -464,10 +467,10 @@ module.exports = grammar({
_type: $ => seq(
optional($.type_modifiers),
choice(
$.function_type,
$.parenthesized_type,
$.nullable_type,
$._type_reference,
$.function_type,
$.not_nullable_type
)
),
Expand Down Expand Up @@ -513,7 +516,7 @@ module.exports = grammar({
_type_projection_modifier: $ => $.variance_modifier,

function_type: $ => seq(
optional(seq($._simple_user_type, ".")), // TODO: Support "real" types
optional(seq(field("receiver", $.receiver_type), ".")),
$.function_type_parameters,
"->",
$._type
Expand Down Expand Up @@ -623,7 +626,6 @@ module.exports = grammar({

_unary_expression: $ => choice(
$.postfix_expression,
$.call_expression,
$.indexing_expression,
$.navigation_expression,
$.prefix_expression,
Expand Down Expand Up @@ -738,6 +740,7 @@ module.exports = grammar({
$._literal_constant,
$.string_literal,
$.callable_reference,
$.call_expression,
$._function_literal,
$.object_literal,
$.collection_literal,
Expand All @@ -751,7 +754,13 @@ module.exports = grammar({

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

collection_literal: $ => seq("[", $._expression, repeat(seq(",", $._expression)), "]"),
// https://kotlinlang.org/spec/syntax-and-grammar.html#grammar-rule-collectionLiteral
collection_literal: $ => seq(
"[",
$._expression,
repeat(seq(",", $._expression)),
optional(","),
"]"),

_literal_constant: $ => choice(
$.boolean_literal,
Expand Down Expand Up @@ -836,7 +845,7 @@ module.exports = grammar({
seq(
optional(field('consequence', $.control_structure_body)),
optional(";"),
"else",
"else",
choice(field('alternative', $.control_structure_body), ";")
),
";"
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@
"tree-sitter": [
{
"scope": "source.kotlin",
"file-types": [
"kt",
"kts"
]
"injection-regex": "^kotlin$"
}
]
}
71 changes: 49 additions & 22 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@
}
]
},
"_receiver_type": {
"receiver_type": {
"type": "SEQ",
"members": [
{
Expand All @@ -1260,15 +1260,15 @@
"members": [
{
"type": "SYMBOL",
"name": "_type_reference"
"name": "parenthesized_type"
},
{
"type": "SYMBOL",
"name": "parenthesized_type"
"name": "nullable_type"
},
{
"type": "SYMBOL",
"name": "nullable_type"
"name": "_type_reference"
}
]
}
Expand Down Expand Up @@ -1315,8 +1315,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_receiver_type"
"type": "FIELD",
"name": "receiver",
"content": {
"type": "SYMBOL",
"name": "receiver_type"
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -1490,8 +1494,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_receiver_type"
"type": "FIELD",
"name": "receiver",
"content": {
"type": "SYMBOL",
"name": "receiver_type"
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -2175,19 +2183,19 @@
"members": [
{
"type": "SYMBOL",
"name": "parenthesized_type"
"name": "function_type"
},
{
"type": "SYMBOL",
"name": "nullable_type"
"name": "parenthesized_type"
},
{
"type": "SYMBOL",
"name": "_type_reference"
"name": "nullable_type"
},
{
"type": "SYMBOL",
"name": "function_type"
"name": "_type_reference"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -2408,8 +2416,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_simple_user_type"
"type": "FIELD",
"name": "receiver",
"content": {
"type": "SYMBOL",
"name": "receiver_type"
}
},
{
"type": "STRING",
Expand Down Expand Up @@ -2917,10 +2929,6 @@
"type": "SYMBOL",
"name": "postfix_expression"
},
{
"type": "SYMBOL",
"name": "call_expression"
},
{
"type": "SYMBOL",
"name": "indexing_expression"
Expand Down Expand Up @@ -3690,6 +3698,10 @@
"type": "SYMBOL",
"name": "callable_reference"
},
{
"type": "SYMBOL",
"name": "call_expression"
},
{
"type": "SYMBOL",
"name": "_function_literal"
Expand Down Expand Up @@ -3772,6 +3784,18 @@
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
Expand Down Expand Up @@ -6329,10 +6353,6 @@
"user_type",
"anonymous_function"
],
[
"user_type",
"function_type"
],
[
"annotated_lambda",
"modifiers"
Expand All @@ -6354,6 +6374,13 @@
],
[
"not_nullable_type"
],
[
"receiver_type"
],
[
"receiver_type",
"_type"
]
],
"precedences": [],
Expand Down
Loading

0 comments on commit e30769c

Please sign in to comment.