Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of receiver types #155

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 19 additions & 10 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 @@ -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
63 changes: 45 additions & 18 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 @@ -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