Skip to content

Commit 130cd8e

Browse files
authored
Merge pull request #275 from aspeddro/fix/block-comment-in-switch-expression
2 parents cafdc4f + b7ab762 commit 130cd8e

5 files changed

Lines changed: 131933 additions & 132287 deletions

File tree

grammar.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ module.exports = grammar({
8989
[$.list, $.list_pattern],
9090
[$.array, $.array_pattern],
9191
[$.dict, $.dict_pattern],
92-
[$.type_declaration],
93-
[$.let_declaration],
9492
[$.variant_identifier, $.module_identifier],
9593
[$.variant, $.variant_pattern],
9694
[$.variant_arguments, $._variant_pattern_parameters],
@@ -100,6 +98,7 @@ module.exports = grammar({
10098
[$._record_pun_field, $._record_single_pun_field],
10199
[$._record_field_name, $.record_pattern],
102100
[$._statement, $._one_or_more_statements],
101+
[$._statement, $._switch_body],
103102
[$._inline_type, $.function_type_parameters],
104103
[$.primary_expression, $.parameter, $._pattern],
105104
[$.parameter, $._pattern],
@@ -134,6 +133,13 @@ module.exports = grammar({
134133
_one_or_more_statements: ($) =>
135134
seq(repeat($._statement), $.statement, optional($._statement_delimeter)),
136135

136+
// Like _one_or_more_statements but without a trailing delimiter.
137+
// Used as switch_match / try-catch bodies so that trailing NEWLINEs
138+
// (including those emitted between consecutive block comments) are
139+
// absorbed by the enclosing switch/try rule instead of being orphaned.
140+
_switch_body: ($) =>
141+
seq(repeat($._statement), $.statement),
142+
137143
statement: ($) =>
138144
choice(
139145
$.expression_statement,
@@ -253,7 +259,7 @@ module.exports = grammar({
253259
optional("export"),
254260
"type",
255261
optional("rec"),
256-
sep1(seq(repeat($._newline), "and"), $.type_binding),
262+
sep1("and", $.type_binding),
257263
),
258264

259265
type_binding: ($) =>
@@ -418,7 +424,7 @@ module.exports = grammar({
418424
seq(
419425
choice("export", "let"),
420426
optional("rec"),
421-
sep1(seq(repeat($._newline), "and"), $.let_binding),
427+
sep1("and", $.let_binding),
422428
),
423429

424430
let_binding: ($) =>
@@ -589,7 +595,13 @@ module.exports = grammar({
589595
else_clause: ($) => seq("else", $.block),
590596

591597
switch_expression: ($) =>
592-
seq("switch", $.expression, "{", repeat($.switch_match), "}"),
598+
seq(
599+
"switch",
600+
$.expression,
601+
"{",
602+
repeat(seq($.switch_match, repeat($._statement_delimeter))),
603+
"}",
604+
),
593605

594606
switch_match: ($) =>
595607
prec.dynamic(
@@ -601,7 +613,7 @@ module.exports = grammar({
601613
"=>",
602614
field(
603615
"body",
604-
alias($._one_or_more_statements, $.sequence_expression),
616+
alias($._switch_body, $.sequence_expression),
605617
),
606618
),
607619
),
@@ -616,7 +628,14 @@ module.exports = grammar({
616628
seq($.variant_type_pattern, optional($.as_aliasing)),
617629

618630
try_expression: ($) =>
619-
seq("try", $.expression, "catch", "{", repeat($.switch_match), "}"),
631+
seq(
632+
"try",
633+
$.expression,
634+
"catch",
635+
"{",
636+
repeat(seq($.switch_match, repeat($._statement_delimeter))),
637+
"}",
638+
),
620639

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

src/grammar.json

Lines changed: 53 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)