Skip to content

Commit

Permalink
feat: add support for property hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jun 6, 2024
1 parent eef38de commit 62c5005
Show file tree
Hide file tree
Showing 3 changed files with 659 additions and 399 deletions.
49 changes: 29 additions & 20 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ module.exports = function defineGrammar(dialect) {
repeat1($._modifier),
optional(field('type', $.type)),
commaSep1($.property_element),
$._semicolon,
choice(
$._semicolon,
$.property_hook_list,
),
),

_modifier: $ => prec.left(choice(
Expand All @@ -344,10 +347,27 @@ module.exports = function defineGrammar(dialect) {
)),

property_element: $ => seq(
$.variable_name,
field('name', $.variable_name),
optional(seq('=', field('default_value', $.expression))),
),

property_hook_list: $ => seq('{', repeat($.property_hook), '}'),

property_hook: $ => seq(
optional(field('attributes', $.attribute_list)),
optional(field('final', $.final_modifier)),
optional(field('reference_modifier', $.reference_modifier)),
$.name,
optional(field('parameters', $.formal_parameters)),
$._property_hook_body,
),

_property_hook_body: $ => choice(
seq('=>', field('body', $.expression), $._semicolon),
field('body', $.compound_statement),
$._semicolon,
),

method_declaration: $ => seq(
optional(field('attributes', $.attribute_list)),
repeat($._modifier),
Expand Down Expand Up @@ -475,21 +495,16 @@ module.exports = function defineGrammar(dialect) {
field('readonly', optional($.readonly_modifier)),
field('type', optional($.type)), // Note: callable is not a valid type here, but instead of complicating the parser, we defer this checking to any intelligence using the parser
field('name', $.variable_name),
optional(seq(
'=',
field('default_value', $.expression),
)),
optional(seq('=', field('default_value', $.expression))),
optional($.property_hook_list),
),

simple_parameter: $ => seq(
optional(field('attributes', $.attribute_list)),
field('type', optional($.type)),
optional(field('reference_modifier', $.reference_modifier)),
field('name', $.variable_name),
optional(seq(
'=',
field('default_value', $.expression),
)),
optional(seq('=', field('default_value', $.expression))),
),

variadic_parameter: $ => seq(
Expand Down Expand Up @@ -1243,18 +1258,12 @@ module.exports = function defineGrammar(dialect) {

_variable_subscript_expression: $ => seq(
$._new_variable,
choice(
seq('[', optional($.expression), ']'),
seq('{', $.expression, '}'),
),
seq('[', optional($.expression), ']'),
),

_dereferencable_subscript_expression: $ => seq(
$._dereferencable_expression,
choice(
seq('[', optional($.expression), ']'),
seq('{', $.expression, '}'),
),
seq('[', optional($.expression), ']'),
),

_dereferencable_expression: $ => prec(PREC.DEREF, choice(
Expand All @@ -1266,10 +1275,10 @@ module.exports = function defineGrammar(dialect) {
$._name,
)),

_dereferencable_scalar: $ => choice(
_dereferencable_scalar: $ => prec(PREC.DEREF, choice(
$.array_creation_expression,
$._string,
),
)),

array_creation_expression: $ => choice(
seq(keyword('array'), '(', commaSep($.array_element_initializer), optional(','), ')'),
Expand Down
Loading

0 comments on commit 62c5005

Please sign in to comment.