Skip to content

Commit

Permalink
Merge branch 'fwcd:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky authored Nov 9, 2023
2 parents 900327f + 0ef8789 commit e22c586
Show file tree
Hide file tree
Showing 15 changed files with 1,969,564 additions and 588,413 deletions.
213 changes: 122 additions & 91 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const PREC = {
const DEC_DIGITS = token(sep1(/[0-9]+/, /_+/));
const HEX_DIGITS = token(sep1(/[0-9a-fA-F]+/, /_+/));
const BIN_DIGITS = token(sep1(/[01]/, /_+/));
const REAL_EXPONENT = token(seq(/[eE]/, optional(/[+-]/), DEC_DIGITS))
const REAL_EXPONENT = token(seq(/[eE]/, optional(/[+-]/), DEC_DIGITS));

module.exports = grammar({
name: "kotlin",
Expand Down Expand Up @@ -177,12 +177,12 @@ module.exports = grammar({
top_level_object: $ => seq($._declaration, optional($._semi)),

type_alias: $ => seq(
optional($.modifiers),
optional(field('modifiers', $.modifiers)),
"typealias",
alias($.simple_identifier, $.type_identifier),
optional($.type_parameters),
field('name', alias($.simple_identifier, $.type_identifier)),
optional(field('type_parameters', $.type_parameters)),
"=",
$._type
field('type', $._type)
),

_declaration: $ => choice(
Expand All @@ -208,30 +208,30 @@ module.exports = grammar({

class_declaration: $ => prec.right(choice(
seq(
optional($.modifiers),
choice("class", "interface"),
alias($.simple_identifier, $.type_identifier),
optional($.type_parameters),
optional($.primary_constructor),
optional(seq(":", $._delegation_specifiers)),
optional($.type_constraints),
optional($.class_body)
optional(field('modifiers', $.modifiers)),
field('kind', choice("class", "interface")),
field('name', alias($.simple_identifier, $.type_identifier)),
optional(field('type_parameters', $.type_parameters)),
optional(field('primary_constructor', $.primary_constructor)),
optional(seq(":", field('delegation_specifiers', $._delegation_specifiers))),
optional(field('constraints', $.type_constraints)),
optional(field('body', $.class_body))
),
seq(
optional($.modifiers),
"enum", "class",
alias($.simple_identifier, $.type_identifier),
optional($.type_parameters),
optional($.primary_constructor),
optional(seq(":", $._delegation_specifiers)),
optional($.type_constraints),
optional($.enum_class_body)
optional(field('modifiers', $.modifiers)),
field('kind', "enum"), "class",
field('name', alias($.simple_identifier, $.type_identifier)),
optional(field('type_parameters', $.type_parameters)),
optional(field('primary_constructor', $.primary_constructor)),
optional(seq(":", field('delegation_specifiers', $._delegation_specifiers))),
optional(field('constraints', $.type_constraints)),
optional(field('body', $.enum_class_body))
)
)),

primary_constructor: $ => seq(
optional(seq(optional($.modifiers), "constructor")),
$._class_parameters
optional(seq(optional(field('modifiers', $.modifiers)), "constructor")),
field('parameters', $._class_parameters)
),

class_body: $ => seq("{", optional($._class_member_declarations), "}"),
Expand Down Expand Up @@ -308,15 +308,15 @@ module.exports = grammar({
$.secondary_constructor
),

anonymous_initializer: $ => seq("init", $._block),
anonymous_initializer: $ => seq("init", field('body', $._block)),

companion_object: $ => seq(
optional($.modifiers),
optional(field('modifiers', $.modifiers)),
"companion",
"object",
optional(alias($.simple_identifier, $.type_identifier)),
optional(seq(":", $._delegation_specifiers)),
optional($.class_body)
optional(field('name', alias($.simple_identifier, $.type_identifier))),
optional(seq(":", field('delegation_specifiers', $._delegation_specifiers))),
optional(field('body', $.class_body))
),

function_value_parameters: $ => seq(
Expand All @@ -342,15 +342,15 @@ module.exports = grammar({
),

function_declaration: $ => prec.right(seq( // TODO
optional($.modifiers),
optional(field('modifiers', $.modifiers)),
"fun",
optional($.type_parameters),
optional(seq($._receiver_type, optional('.'))),
$.simple_identifier,
$.function_value_parameters,
optional(seq(":", $._type)),
optional($.type_constraints),
optional($.function_body)
optional(field('type_parameters', $.type_parameters)),
optional(seq(field('receiver', $._receiver_type), optional('.'))),
field('name', $.simple_identifier),
field('parameters', $.function_value_parameters),
optional(seq(":", field('return_type', $._type))),
optional(field('constraints', $.type_constraints)),
optional(field('body', $.function_body))
)),

function_body: $ => choice($._block, seq("=", $._expression)),
Expand All @@ -362,21 +362,21 @@ module.exports = grammar({
)),

property_declaration: $ => prec.right(seq(
optional($.modifiers),
choice("val", "var"),
optional($.type_parameters),
optional(seq($._receiver_type, optional('.'))),
choice($.variable_declaration, $.multi_variable_declaration),
optional($.type_constraints),
optional(field('modifiers', $.modifiers)),
field('kind', choice("val", "var")),
optional(field('type_parameters', $.type_parameters)),
optional(seq(field('receiver', $._receiver_type), optional('.'))),
choice(field('variable', $.variable_declaration), field('variables', $.multi_variable_declaration)),
optional(field('constraints', $.type_constraints)),
optional(choice(
seq("=", $._expression),
$.property_delegate
seq("=", field('expression', $._expression)),
field('delegate', $.property_delegate)
)),
optional(';'),
choice(
// TODO: Getter-setter combinations
optional($.getter),
optional($.setter)
optional(field('getter', $.getter)),
optional(field('setter', $.setter))
)
)),

Expand Down Expand Up @@ -415,19 +415,19 @@ module.exports = grammar({
parameter: $ => seq($.simple_identifier, ":", $._type),

object_declaration: $ => prec.right(seq(
optional($.modifiers),
optional(field('modifiers', $.modifiers)),
"object",
alias($.simple_identifier, $.type_identifier),
optional(seq(":", $._delegation_specifiers)),
optional($.class_body)
field('name', alias($.simple_identifier, $.type_identifier)),
optional(seq(":", field('delegation_specifiers', $._delegation_specifiers))),
optional(field('body', $.class_body))
)),

secondary_constructor: $ => seq(
optional($.modifiers),
optional(field('modifiers', $.modifiers)),
"constructor",
$.function_value_parameters,
optional(seq(":", $.constructor_delegation_call)),
optional($._block)
field('parameters', $.function_value_parameters),
optional(seq(":", field('delegation', $.constructor_delegation_call))),
optional(field('body', $._block))
),

constructor_delegation_call: $ => seq(choice("this", "super"), $.value_arguments),
Expand Down Expand Up @@ -570,27 +570,30 @@ module.exports = grammar({
"for",
"(",
repeat($.annotation),
choice($.variable_declaration, $.multi_variable_declaration),
choice(
field('variable', $.variable_declaration),
field('variables', $.multi_variable_declaration)
),
"in",
$._expression,
field('value', $._expression),
")",
optional($.control_structure_body)
optional(field('body', $.control_structure_body))
)),

while_statement: $ => seq(
"while",
"(",
$._expression,
field('condition', $._expression),
")",
choice(";", $.control_structure_body)
choice(";", field('body', $.control_structure_body))
),

do_while_statement: $ => prec.right(seq(
"do",
optional($.control_structure_body),
optional(field('body', $.control_structure_body)),
"while",
"(",
$._expression,
field('condition', $._expression),
")",
)),

Expand Down Expand Up @@ -775,8 +778,8 @@ module.exports = grammar({

lambda_literal: $ => prec(PREC.LAMBDA_LITERAL, seq(
"{",
optional(seq(optional($.lambda_parameters), "->")),
optional($.statements),
optional(seq(optional(field('parameters', $.lambda_parameters)), "->")),
optional(field('body', $.statements)),
"}"
)),

Expand All @@ -796,9 +799,9 @@ module.exports = grammar({
anonymous_function: $ => prec.right(seq(
"fun",
optional(seq(sep1($._simple_user_type, "."), ".")), // TODO
$.function_value_parameters,
optional(seq(":", $._type)),
optional($.function_body)
field('parameters', $.function_value_parameters),
optional(seq(":", field('return_type', $._type))),
optional(field('body', $.function_body))
)),

_function_literal: $ => choice(
Expand All @@ -808,29 +811,32 @@ module.exports = grammar({

object_literal: $ => seq(
"object",
optional(seq(":", $._delegation_specifiers)),
$.class_body
optional(seq(":", field('delegation_specifiers', $._delegation_specifiers))),
field('body', $.class_body)
),

this_expression: $ => "this",
this_expression: $ => choice(
"this",
$._this_at
),

super_expression: $ => seq(
super_expression: $ => prec.right(choice(
"super",
// TODO optional(seq("<", $._type, ">")),
// TODO optional(seq("@", $.simple_identifier))
),
seq("super", "<", $._type, ">"),
$._super_at
)),

if_expression: $ => prec.right(seq(
"if",
"(", $._expression, ")",
"(", field('condition', $._expression), ")",
choice(
$.control_structure_body,
field('consequence', $.control_structure_body),
";",
seq(
optional($.control_structure_body),
optional(field('consequence', $.control_structure_body)),
optional(";"),
"else",
choice($.control_structure_body, ";")
choice(field('alternative', $.control_structure_body), ";")
)
)
)),
Expand All @@ -849,19 +855,19 @@ module.exports = grammar({

when_expression: $ => seq(
"when",
optional($.when_subject),
optional(field('subject', $.when_subject)),
"{",
repeat($.when_entry),
repeat(field('entry', $.when_entry)),
"}"
),

when_entry: $ => seq(
choice(
seq($.when_condition, repeat(seq(",", $.when_condition))),
seq(field('condition', $.when_condition), repeat(seq(",", field('condition', $.when_condition)))),
"else"
),
"->",
$.control_structure_body,
field('body', $.control_structure_body),
optional($._semi)
),

Expand All @@ -877,9 +883,9 @@ module.exports = grammar({

try_expression: $ => seq(
"try",
$._block,
field('body', $._block),
choice(
seq(repeat1($.catch_block), optional($.finally_block)),
seq(repeat1(field('catch', $.catch_block)), optional(field('finally', $.finally_block))),
$.finally_block
)
),
Expand All @@ -892,10 +898,10 @@ module.exports = grammar({
":",
$._type,
")",
$._block,
field('body', $._block),
),

finally_block: $ => seq("finally", $._block),
finally_block: $ => seq("finally", field('body', $._block)),

jump_expression: $ => choice(
prec.right(PREC.RETURN_OR_THROW, seq("throw", $._expression)),
Expand Down Expand Up @@ -986,7 +992,8 @@ module.exports = grammar({
"sealed",
"annotation",
"data",
"inner"
"inner",
"value",
),

member_modifier: $ => choice(
Expand Down Expand Up @@ -1086,6 +1093,7 @@ module.exports = grammar({
"expect",
"data",
"inner",
"value",
"actual",
"set",
"get"
Expand Down Expand Up @@ -1114,15 +1122,38 @@ module.exports = grammar({
// Keywords
// ==========

_return_at: $ => seq("return@", $._lexical_identifier),
_return_at: $ => seq(
"return@",
alias($._lexical_identifier, $.label)
),

_continue_at: $ => seq("continue@", $._lexical_identifier),
_continue_at: $ => seq(
"continue@",
alias($._lexical_identifier, $.label)
),

_break_at: $ => seq("break@", $._lexical_identifier),
_break_at: $ => seq(
"break@",
alias($._lexical_identifier, $.label)
),

_this_at: $ => seq("this@", $._lexical_identifier),
_this_at: $ => seq(
"this@",
alias($._lexical_identifier, $.type_identifier)
),

_super_at: $ => seq("super@", $._lexical_identifier),
_super_at: $ => choice(
seq(
"super@",
alias($._lexical_identifier, $.type_identifier)
),
seq(
"super",
"<", $._type, ">",
token.immediate("@"),
alias($._lexical_identifier, $.type_identifier)
)
),

// ==========
// Literals
Expand Down
Loading

0 comments on commit e22c586

Please sign in to comment.