Skip to content

Commit

Permalink
fix(kotlin-metavariable-grammar): try to implement it more like the p…
Browse files Browse the repository at this point in the history
…ython-metavariable-grammar with fewer $.grit_metavariable but at 'higher' level

fixes #570
  • Loading branch information
Alex-ley-scrub committed Nov 15, 2024
1 parent 8e6a695 commit 3c8b058
Show file tree
Hide file tree
Showing 7 changed files with 511,775 additions and 503,864 deletions.
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-kotlin.wasm
Binary file not shown.
39 changes: 29 additions & 10 deletions resources/language-metavariables/tree-sitter-kotlin/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const HEX_DIGITS = token(sep1(/[0-9a-fA-F]+/, /_+/));
const BIN_DIGITS = token(sep1(/[01]/, /_+/));
const REAL_EXPONENT = token(seq(/[eE]/, optional(/[+-]/), DEC_DIGITS));

/// <reference types="tree-sitter-cli/dsl" />

module.exports = grammar({
name: "kotlin",

Expand Down Expand Up @@ -112,7 +114,15 @@ module.exports = grammar({
// ambiguity between associating type modifiers
[$.not_nullable_type],
// grit_metavariable conflicts
[$._statement, $._literal_constant],
[$._declaration, $.simple_identifier],
[$._primary_expression, $.simple_identifier],
[$._declaration, $._primary_expression, $.simple_identifier],
// [$._statement, $._literal_constant],
// [$.value_arguments, $._literal_constant],
// [$._literal_constant, $._lexical_identifier],
// [$._statement, $._literal_constant, $._lexical_identifier],
// [$.value_arguments, $._literal_constant, $._lexical_identifier],
// [$.function_value_parameters, $._lexical_identifier],
],

externals: $ => [
Expand Down Expand Up @@ -192,6 +202,7 @@ module.exports = grammar({
),

_declaration: $ => choice(
$.grit_metavariable,
$.class_declaration,
$.object_declaration,
$.function_declaration,
Expand All @@ -205,7 +216,7 @@ module.exports = grammar({
// simpler to accept them here.
$.getter,
$.setter,
$.type_alias
$.type_alias,
),

// ==========
Expand Down Expand Up @@ -240,7 +251,7 @@ module.exports = grammar({
$._class_parameters
),

class_body: $ => seq("{", optional(choice($._class_member_declarations, $.grit_metavariable)), "}"),
class_body: $ => seq("{", optional($._class_member_declarations), "}"),

_class_parameters: $ => seq(
"(",
Expand Down Expand Up @@ -313,7 +324,7 @@ module.exports = grammar({
$._declaration,
$.companion_object,
$.anonymous_initializer,
$.secondary_constructor
$.secondary_constructor,
),

anonymous_initializer: $ => seq("init", $._block),
Expand Down Expand Up @@ -447,7 +458,7 @@ module.exports = grammar({
enum_class_body: $ => seq(
"{",
optional($._enum_entries),
optional(seq(";", optional(choice($._class_member_declarations, $.grit_metavariable)))),
optional(seq(";", optional($._class_member_declarations))),
"}"
),

Expand Down Expand Up @@ -557,7 +568,6 @@ module.exports = grammar({
$._expression
)
),
$.grit_metavariable,
),

label: $ => token(seq(
Expand Down Expand Up @@ -620,7 +630,7 @@ module.exports = grammar({
_expression: $ => choice(
$._unary_expression,
$._binary_expression,
$._primary_expression
$._primary_expression,
),

// Unary expressions
Expand Down Expand Up @@ -750,7 +760,8 @@ module.exports = grammar({
$.if_expression,
$.when_expression,
$.try_expression,
$.jump_expression
$.jump_expression,
$.grit_metavariable,
),

parenthesized_expression: $ => seq("(", $._expression, ")"),
Expand All @@ -767,12 +778,17 @@ module.exports = grammar({
$.null_literal,
$.long_literal,
$.unsigned_literal,
$.grit_metavariable,
),

// Patch the metavariable grammar to include $.grit_metavariable anywhere we want to substitute a metavariable.
// This is usually at least $identifier and $literal.
string_literal: $ => seq(
$._string_start,
choice($.grit_metavariable, repeat(choice($.string_content, $._interpolation))),
field('fragment',
choice(
$.grit_metavariable,
repeat(choice($.string_content, $._interpolation))
)),
$._string_end,
),

Expand Down Expand Up @@ -1095,7 +1111,10 @@ module.exports = grammar({
// Identifiers
// ==========

// Patch the metavariable grammar to include $.grit_metavariable anywhere we want to substitute a metavariable.
// This is usually at least $identifier and $literal.
simple_identifier: $ => choice(
$.grit_metavariable,
$._lexical_identifier,
"expect",
"data",
Expand Down
107 changes: 53 additions & 54 deletions resources/language-metavariables/tree-sitter-kotlin/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@
"_declaration": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "grit_metavariable"
},
{
"type": "SYMBOL",
"name": "class_declaration"
Expand Down Expand Up @@ -608,17 +612,8 @@
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_class_member_declarations"
},
{
"type": "SYMBOL",
"name": "grit_metavariable"
}
]
"type": "SYMBOL",
"name": "_class_member_declarations"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -2057,17 +2052,8 @@
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_class_member_declarations"
},
{
"type": "SYMBOL",
"name": "grit_metavariable"
}
]
"type": "SYMBOL",
"name": "_class_member_declarations"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -2647,10 +2633,6 @@
]
}
]
},
{
"type": "SYMBOL",
"name": "grit_metavariable"
}
]
},
Expand Down Expand Up @@ -3748,6 +3730,10 @@
{
"type": "SYMBOL",
"name": "jump_expression"
},
{
"type": "SYMBOL",
"name": "grit_metavariable"
}
]
},
Expand Down Expand Up @@ -3839,10 +3825,6 @@
{
"type": "SYMBOL",
"name": "unsigned_literal"
},
{
"type": "SYMBOL",
"name": "grit_metavariable"
}
]
},
Expand All @@ -3854,29 +3836,33 @@
"name": "_string_start"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "grit_metavariable"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string_content"
},
{
"type": "SYMBOL",
"name": "_interpolation"
}
]
"type": "FIELD",
"name": "fragment",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "grit_metavariable"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string_content"
},
{
"type": "SYMBOL",
"name": "_interpolation"
}
]
}
}
}
]
]
}
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -5471,6 +5457,10 @@
"simple_identifier": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "grit_metavariable"
},
{
"type": "SYMBOL",
"name": "_lexical_identifier"
Expand Down Expand Up @@ -6412,8 +6402,17 @@
"not_nullable_type"
],
[
"_statement",
"_literal_constant"
"_declaration",
"simple_identifier"
],
[
"_primary_expression",
"simple_identifier"
],
[
"_declaration",
"_primary_expression",
"simple_identifier"
]
],
"precedences": [],
Expand Down
Loading

0 comments on commit 3c8b058

Please sign in to comment.