Skip to content

Commit

Permalink
Merge pull request #79 from kit494way/fix-parsing-match-pattern
Browse files Browse the repository at this point in the history
Fix parsing of match pattern
  • Loading branch information
fdncred authored Feb 2, 2024
2 parents 4e51ef8 + 9ebbf1c commit 358c4f5
Show file tree
Hide file tree
Showing 5 changed files with 212,431 additions and 211,362 deletions.
189 changes: 189 additions & 0 deletions corpus/ctrl/match.nu
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,192 @@ match $x {
(pipeline
(pipe_element
(val_record)))))))))

=====
match-003-list
=====

match $xs {
[] => {},
[1] => {},
[1, 2] => {},
[$x] => {},
_ => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_variable
(identifier))
(match_arm
(match_pattern
(val_list))
(block))
(match_arm
(match_pattern
(val_list
(val_number)))
(block))
(match_arm
(match_pattern
(val_list
(val_number)
(val_number)))
(block))
(match_arm
(match_pattern
(val_list
(val_variable
(identifier))))
(block))
(default_arm
(block))))))

=====
match-004-range
=====

match $xs {
1..3 => {}
4..<6 => {}
6..=9 => {}
10.. => {}
[1..3, 4..<10] => {}
_ => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_variable
(identifier))
(match_arm
(match_pattern
(val_range
(val_number)
(val_number)))
(block))
(match_arm
(match_pattern
(val_range
(val_number)
(val_number)))
(block))
(match_arm
(match_pattern
(val_range
(val_number)
(val_number)))
(block))
(match_arm
(match_pattern
(val_range
(val_number)))
(block))
(match_arm
(match_pattern
(val_list
(val_range
(val_number)
(val_number))
(val_range
(val_number)
(val_number))))
(block))
(default_arm
(block))))))

=====
match-005-rest-pattern
=====

match $xs {
[1 ..$tail] => {}
[2, ..] => {}
[$head ..$tail] if ($tail | length) < 3 => {}
[..$tail] => {}
_ => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_variable
(identifier))
(match_arm
(match_pattern
(val_list
(val_number)
(val_variable
(identifier))))
(block))
(match_arm
(match_pattern
(val_list
(val_number)))
(block))
(match_arm
(match_pattern
(val_list
(val_variable
(identifier))
(val_variable
(identifier)))
(match_guard
(expr_binary
(expr_parenthesized
(pipeline
(pipe_element
(val_variable
(identifier)))
(pipe_element
(command
(cmd_identifier)))))
(val_number))))
(block))
(match_arm
(match_pattern
(val_list
(val_variable
(identifier))))
(block))
(default_arm
(block))))))

=====
match-006-or
=====

match $xs {
[1] | [2 3] => {},
_ => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_variable
(identifier))
(match_arm
(match_pattern
(val_list
(val_number))
(val_list
(val_number)
(val_number)))
(block))
(default_arm
(block))))))
84 changes: 68 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module.exports = grammar({
[$.val_variable],
[$.val_range, $.unquoted],
[$._expression, $._expr_binary_expression],
[$._match_pattern_value, $._value],
[$._match_pattern_expression, $._list_item_expression],
[$._match_pattern_list, $.val_list],
],

rules: {
Expand Down Expand Up @@ -398,31 +401,80 @@ module.exports = grammar({
_match_expression: ($) => choice($._expression, prec.dynamic(10, $.block)),

match_pattern: ($) =>
choice($._match_or_pattern, $._match_list_destructure_pattern),

_match_or_pattern: ($) =>
seq(
seq($._expression, optional($.match_guard)),
repeat(seq(PUNC().pipe, $._expression)),
choice(
seq($._match_pattern_expression, optional($.match_guard)),
seq(
$._match_pattern_expression,
repeat(seq(PUNC().pipe, $._match_pattern_expression)),
),
),

match_guard: ($) => seq(KEYWORD().if, $._expression),

_match_list_destructure_pattern: ($) =>
prec(
1,
seq(
BRACK().open_brack,
field("head", seq(PUNC().dollar, $.identifier)),
optional(PUNC().comma),
_match_pattern_expression: ($) =>
choice(
$._match_pattern_value,
alias($._expr_unary_minus, $.expr_unary),
$.val_range,
$.expr_parenthesized,
),

_match_pattern_value: ($) =>
choice(
$.val_variable,
$.val_nothing,
$.val_bool,
$.val_number,
$.val_duration,
$.val_filesize,
$.val_binary,
$.val_string,
$.val_date,
alias($._match_pattern_list, $.val_list),
$.val_record,
$.val_table,
),

_match_pattern_list: ($) =>
seq(
BRACK().open_brack,
repeat(
field(
"item",
seq(
choice(
$._match_pattern_expression,
alias($._unquoted_in_list, $.val_string),
alias($.short_flag, $.val_string),
alias($.long_flag, $.val_string),
alias($._list_item_starts_with_sign, $.val_string),
),
optional(PUNC().comma),
),
),
),
optional(
field(
"tail",
seq(PUNC().dot, PUNC().dot, PUNC().dollar, $.identifier),
"rest",
choice(
alias($._match_pattern_rest, $.val_variable),
$._match_pattern_ignore_rest,
),
),
BRACK().close_brack,
),
BRACK().close_brack,
),

_match_pattern_rest: ($) =>
seq(
PUNC().dot,
token.immediate(PUNC().dot),
seq(token.immediate(PUNC().dollar), $.identifier),
),

_match_pattern_ignore_rest: ($) =>
seq(PUNC().dot, token.immediate(PUNC().dot)),

ctrl_try: ($) =>
seq(
KEYWORD().try,
Expand Down
Loading

0 comments on commit 358c4f5

Please sign in to comment.