Skip to content

Commit

Permalink
Merge pull request nushell#81 from kit494way/fix-match
Browse files Browse the repository at this point in the history
Fix pasing of match pattern
  • Loading branch information
fdncred authored Feb 8, 2024
2 parents f8681c1 + 5648875 commit 16e041c
Show file tree
Hide file tree
Showing 5 changed files with 222,174 additions and 216,322 deletions.
62 changes: 62 additions & 0 deletions corpus/ctrl/match.nu
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,65 @@ match $xs {
(block))
(default_arm
(block))))))

=====
match-007-unquoted
=====

match foo {
foo => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_string)
(match_arm
(match_pattern
(val_string))
(block))))))

=====
match-008-record
=====

match $x {
{$y} => {}
{$foo, $bar} => {}
{$a, b: c} => {}
}

-----

(nu_script
(pipeline
(pipe_element
(ctrl_match
(val_variable
(identifier))
(match_arm
(match_pattern
(val_record
(val_variable
(identifier))))
(block))
(match_arm
(match_pattern
(val_record
(val_variable
(identifier))
(val_variable
(identifier))))
(block))
(match_arm
(match_pattern
(val_record
(val_variable
(identifier))
(record_entry
(identifier)
(val_string))))
(block))))))
35 changes: 28 additions & 7 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module.exports = grammar({
[$._match_pattern_value, $._value],
[$._match_pattern_expression, $._list_item_expression],
[$._match_pattern_list, $.val_list],
[$._match_pattern_record, $.val_record],
[$._match_pattern_record_variable, $._value],
],

rules: {
Expand Down Expand Up @@ -375,7 +377,10 @@ module.exports = grammar({
ctrl_match: ($) =>
seq(
KEYWORD().match,
field("scrutinee", $._expression),
field(
"scrutinee",
choice($._expression, alias($.unquoted, $.val_string)),
),
BRACK().open_brace,
repeat($.match_arm),
optional($.default_arm),
Expand All @@ -402,13 +407,13 @@ module.exports = grammar({

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

_match_pattern: ($) =>
choice($._match_pattern_expression, alias($.unquoted, $.val_string)),

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

_match_pattern_expression: ($) =>
Expand All @@ -431,7 +436,7 @@ module.exports = grammar({
$.val_string,
$.val_date,
alias($._match_pattern_list, $.val_list),
$.val_record,
alias($._match_pattern_record, $.val_record),
$.val_table,
),

Expand Down Expand Up @@ -475,6 +480,22 @@ module.exports = grammar({
_match_pattern_ignore_rest: ($) =>
seq(PUNC().dot, token.immediate(PUNC().dot)),

_match_pattern_record: ($) =>
seq(
BRACK().open_brace,
repeat(
field(
"entry",
choice($.record_entry, $._match_pattern_record_variable),
),
),
BRACK().close_brace,
optional($.cell_path),
),

_match_pattern_record_variable: ($) =>
seq($.val_variable, optional(PUNC().comma)),

ctrl_try: ($) =>
seq(
KEYWORD().try,
Expand Down
125 changes: 118 additions & 7 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -3062,8 +3062,22 @@
"type": "FIELD",
"name": "scrutinee",
"content": {
"type": "SYMBOL",
"name": "_expression"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "unquoted"
},
"named": true,
"value": "val_string"
}
]
}
},
{
Expand Down Expand Up @@ -3194,7 +3208,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_match_pattern_expression"
"name": "_match_pattern"
},
{
"type": "CHOICE",
Expand All @@ -3215,7 +3229,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_match_pattern_expression"
"name": "_match_pattern"
},
{
"type": "REPEAT",
Expand All @@ -3228,7 +3242,7 @@
},
{
"type": "SYMBOL",
"name": "_match_pattern_expression"
"name": "_match_pattern"
}
]
}
Expand All @@ -3237,6 +3251,24 @@
}
]
},
"_match_pattern": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_match_pattern_expression"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "unquoted"
},
"named": true,
"value": "val_string"
}
]
},
"match_guard": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -3325,8 +3357,13 @@
"value": "val_list"
},
{
"type": "SYMBOL",
"name": "val_record"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_match_pattern_record"
},
"named": true,
"value": "val_record"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -3494,6 +3531,72 @@
}
]
},
"_match_pattern_record": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "FIELD",
"name": "entry",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "record_entry"
},
{
"type": "SYMBOL",
"name": "_match_pattern_record_variable"
}
]
}
}
},
{
"type": "STRING",
"value": "}"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "cell_path"
},
{
"type": "BLANK"
}
]
}
]
},
"_match_pattern_record_variable": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "val_variable"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
"ctrl_try": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -11259,6 +11362,14 @@
[
"_match_pattern_list",
"val_list"
],
[
"_match_pattern_record",
"val_record"
],
[
"_match_pattern_record_variable",
"_value"
]
],
"precedences": [],
Expand Down
8 changes: 8 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -4730,9 +4730,17 @@
"multiple": true,
"required": false,
"types": [
{
"type": ",",
"named": false
},
{
"type": "record_entry",
"named": true
},
{
"type": "val_variable",
"named": true
}
]
}
Expand Down
Loading

0 comments on commit 16e041c

Please sign in to comment.