Skip to content

Commit

Permalink
Add field names (fwcd#106)
Browse files Browse the repository at this point in the history
* Add named fields to function_declaration

* Add named fields to class_declaration

* Add named fields to object_declaration

* Add named fields to property_declaration

* Add named fields to type_alias

* Add named fields to constructors

* Add named fields to anonymous_initializer

* Add named fields to companion_object

* Add named fields to lambda_literal

* Add named fields to anonymous_fonction

* Add named fields to object_literal

* Add named fields to control statements/expressions
  • Loading branch information
nicolas-guichard authored Nov 9, 2023
1 parent 16de60e commit 0ef8789
Show file tree
Hide file tree
Showing 15 changed files with 1,960,336 additions and 604,161 deletions.
161 changes: 82 additions & 79 deletions grammar.js
Original file line number Diff line number Diff line change
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,8 +811,8 @@ 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: $ => choice(
Expand All @@ -825,15 +828,15 @@ module.exports = grammar({

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 @@ -852,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 @@ -880,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 @@ -895,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
Loading

0 comments on commit 0ef8789

Please sign in to comment.