Skip to content
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
74 changes: 58 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default grammar({
$._dict_constructor,
$._decorator,
$._decorator_inline,
$._finally_clause_start,
],

extras: ($) => [
Expand Down Expand Up @@ -66,7 +67,7 @@ export default grammar({
$.module_primary_expression,
],

inline: ($) => [$._module_definition, $._semicolon],
inline: ($) => [$._module_definition, $._semicolon, $._record_pattern_field],

precedences: ($) => [
// + - Operators -> precendence
Expand Down Expand Up @@ -111,6 +112,8 @@ export default grammar({
],

conflicts: ($) => [
[$.primary_expression, $.record_rest_pattern],
[$.record_pattern],
[$.unit, $.formal_parameters],
[$.primary_expression, $._pattern],
[$.primary_expression, $.record_pattern],
Expand Down Expand Up @@ -673,7 +676,7 @@ export default grammar({
seq(
alias($._dict_constructor, 'dict'),
'{',
commaSept($.dict_entry),
commaSept(choice($.dict_entry, $.spread_element)),
'}',
),

Expand Down Expand Up @@ -723,15 +726,28 @@ export default grammar({
seq($.variant_type_pattern, optional($.as_aliasing)),

try_expression: ($) =>
prec.right(
seq(
'try',
$.expression,
choice(
seq($._catch_clause, optional($.finally_clause)),
$.finally_clause,
),
),
),

_catch_clause: ($) =>
seq(
'try',
$.expression,
'catch',
'{',
repeat(seq($.switch_match, optional($._semicolon))),
'}',
),

finally_clause: ($) =>
seq($._finally_clause_start, alias($._identifier, 'finally'), $.block),

as_aliasing: ($) =>
prec.left(seq('as', $._pattern, optional($.type_annotation))),

Expand Down Expand Up @@ -933,16 +949,32 @@ export default grammar({
record_pattern: ($) =>
seq(
'{',
commaSep1t(
choice(
commaSep1t($._record_pattern_field),
seq(
optional('?'),
choice($.value_identifier, $.value_identifier_path),
optional(seq(':', $._pattern)),
repeat(seq($._record_pattern_field, ',')),
$.record_rest_pattern,
repeat(seq(',', $._record_pattern_field)),
optional(','),
),
),
'}',
),

_record_pattern_field: ($) =>
seq(
optional('?'),
choice($.value_identifier, $.value_identifier_path),
optional(seq(':', $._pattern)),
),

record_rest_pattern: ($) =>
seq(
'...',
optional(seq($._non_function_inline_type, 'as')),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve unit as an inferred record-rest binding

For let {a, ...unit} = value, the optional _non_function_inline_type prefix makes unit tokenize as unit_type instead of the non-reserved value_identifier. The parser then requires as and rejects the closing }, so the new inferred-rest syntax fails for a binding name that works in ordinary record patterns and let bindings. Retain the identifier interpretation when there is no following as.

Useful? React with 👍 / 👎.

field('name', $.value_identifier),
),

tuple_item_pattern: ($) => seq($._pattern, optional($.type_annotation)),

tuple_pattern: ($) => seq('(', commaSep2t($.tuple_item_pattern), ')'),
Expand Down Expand Up @@ -1136,16 +1168,26 @@ export default grammar({
),

for_expression: ($) =>
seq(
'for',
$.value_identifier,
'in',
$.expression,
choice('to', 'downto'),
$.expression,
$.block,
choice(
seq(
'for',
optional('await'),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve await as a numeric-loop binding name

For an existing numeric loop such as for await in 0 to 3 { () }, the newly available await literal wins over value_identifier immediately after for. The parser therefore commits to the asynchronous iterable branch and errors at in, whereas the parent parser accepts this loop (await is not globally reserved). Keep the async prefix contextual so the numeric branch can still use this binding name.

Useful? React with 👍 / 👎.

choice($._for_of_header, parenthesize($._for_of_header)),
$.block,
),
seq(
'for',
$.value_identifier,
'in',
$.expression,
choice('to', 'downto'),
$.expression,
$.block,
),
),

_for_of_header: ($) => seq($.value_identifier, 'of', $.expression),

while_expression: ($) => seq('while', $.expression, $.block),

lazy_expression: ($) => seq('lazy', $.expression),
Expand Down
6 changes: 6 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
(parameter (tuple_pattern (tuple_item_pattern (value_identifier) @variable.parameter)))
(parameter (array_pattern (value_identifier) @variable.parameter))
(parameter (record_pattern (value_identifier) @variable.parameter))
(parameter (record_pattern (record_rest_pattern name: (value_identifier) @variable.parameter)))

; function identifier in let binding
(let_binding
Expand Down Expand Up @@ -205,6 +206,7 @@
"exception"
"try"
"catch"
"finally"
] @keyword.exception

(call_expression
Expand All @@ -219,6 +221,10 @@
"while"
] @keyword.repeat

(expression_statement
(value_identifier) @keyword.repeat
(#any-of? @keyword.repeat "break" "continue"))

[
"."
","
Expand Down
Loading
Loading