Skip to content

Commit f342837

Browse files
committed
rewrite switch match
expose more node add field for switch case
1 parent 65b4288 commit f342837

File tree

3 files changed

+146
-141
lines changed

3 files changed

+146
-141
lines changed

grammar.js

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ module.exports = grammar({
101101
[$._inline_type, $.function_type_parameters],
102102
[$.primary_expression, $.parameter, $._pattern],
103103
[$.parameter, $._pattern],
104-
[$.parameter, $._parenthesized_pattern],
105-
[$._switch_value_pattern, $._parenthesized_pattern],
104+
[$.parameter, $.parenthesized_pattern],
106105
[$.variant_declaration],
107106
[$.unit, $._function_type_parameter_list],
108107
[$.functor_parameter, $.module_primary_expression, $.module_identifier_path],
109108
[$._reserved_identifier, $.function],
110-
[$.polyvar_type]
109+
[$.polyvar_type],
110+
[$._let_binding, $.or_pattern],
111+
[$.exception_pattern, $.or_pattern]
111112
],
112113

113114
rules: {
@@ -690,45 +691,17 @@ module.exports = grammar({
690691

691692
switch_match: $ => prec.dynamic(-1, seq(
692693
'|',
693-
$._switch_pattern,
694+
field('pattern', $._pattern),
695+
optional($.guard),
694696
'=>',
695-
$._one_or_more_statements,
696-
)),
697-
698-
_switch_pattern: $ => barSep1(choice(
699-
alias($._switch_exception_pattern, $.exception),
700-
$._parenthesized_switch_pattern,
701-
$._switch_value_pattern,
702-
$._switch_range_pattern,
697+
field('body', alias($._one_or_more_statements, $.sequence_expression)),
703698
)),
704699

705-
_switch_exception_pattern: $ => seq(
706-
'exception',
707-
$._switch_value_pattern,
708-
),
709-
710-
_parenthesized_switch_pattern: $ => seq(
711-
'(',
712-
$._switch_pattern,
713-
')',
714-
),
715-
716-
_switch_value_pattern: $ => seq(
717-
$._pattern,
718-
optional($.switch_pattern_condition),
719-
),
720-
721-
switch_pattern_condition: $ => seq(
700+
guard: $ => seq(
722701
choice('if', 'when'),
723702
$.expression,
724703
),
725704

726-
_switch_range_pattern: $ => seq(
727-
$._literal_pattern,
728-
'..',
729-
$._literal_pattern,
730-
),
731-
732705
polyvar_type_pattern: $ => seq(
733706
'#',
734707
'...',
@@ -867,21 +840,34 @@ module.exports = grammar({
867840
// unfinished constructs are generally treated as literal expressions,
868841
// not patterns.
869842
_pattern: $ => prec.dynamic(-1, seq(
870-
barSep1(choice(
843+
choice(
871844
$.value_identifier,
872845
$._literal_pattern,
873846
$._destructuring_pattern,
874847
$.polyvar_type_pattern,
875848
$.unit,
876849
$.module_pack,
877850
$.lazy_pattern,
878-
$._parenthesized_pattern,
879-
)),
851+
$.parenthesized_pattern,
852+
$.or_pattern,
853+
$.range_pattern,
854+
$.exception_pattern
855+
),
880856
optional($.type_annotation),
881857
optional($.as_aliasing),
882858
)),
883859

884-
_parenthesized_pattern: $ => seq('(', $._pattern, ')'),
860+
parenthesized_pattern: $ => seq('(', $._pattern, ')'),
861+
862+
range_pattern: $ => seq(
863+
$._literal_pattern,
864+
'..',
865+
$._literal_pattern,
866+
),
867+
868+
or_pattern: $ => prec.left(seq($._pattern, '|', $._pattern)),
869+
870+
exception_pattern: $ => seq('exception', $._pattern),
885871

886872
_destructuring_pattern: $ => choice(
887873
$.variant_pattern,
@@ -916,7 +902,7 @@ module.exports = grammar({
916902
),
917903

918904
_variant_pattern_parameter: $ => seq(
919-
barSep1($._pattern),
905+
$._pattern,
920906
optional($.type_annotation),
921907
),
922908

@@ -934,7 +920,7 @@ module.exports = grammar({
934920
),
935921
optional(seq(
936922
':',
937-
barSep1($._pattern),
923+
$._pattern,
938924
)),
939925
)),
940926
'}'
@@ -984,7 +970,7 @@ module.exports = grammar({
984970
$._literal_pattern,
985971
$._destructuring_pattern,
986972
$.polyvar_type_pattern,
987-
$._parenthesized_pattern,
973+
$.parenthesized_pattern,
988974
)
989975
),
990976

test/corpus/comments.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@ switch foo {
6666
(expression_statement
6767
(switch_expression
6868
(value_identifier)
69-
(switch_match (number) (expression_statement (number)))
69+
(switch_match
70+
(number)
71+
(sequence_expression (expression_statement (number))))
7072
(comment))))

0 commit comments

Comments
 (0)