Skip to content

Add box patterns #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 36 additions & 0 deletions corpus/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,39 @@ match x {
(match_arm
(match_pattern (captured_pattern (identifier) (range_pattern (identifier) (identifier))))
(identifier)))))

=================================
Box patterns
=================================

match x {
box a @ 10 => a,
box a @ 11 => a,
box a => a,
}

---

(source_file
(match_expression
value: (identifier)
body: (match_block
(match_arm
pattern: (match_pattern
(box_pattern
(captured_pattern
(identifier)
(integer_literal))))
value: (identifier))
(match_arm
pattern: (match_pattern
(box_pattern
(captured_pattern
(identifier)
(integer_literal))))
value: (identifier))
(match_arm
pattern: (match_pattern
(box_pattern
(identifier)))
value: (identifier)))))
12 changes: 9 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ module.exports = grammar({
$.remaining_field_pattern,
$.mut_pattern,
$.range_pattern,
$.box_pattern,
'_'
),

Expand Down Expand Up @@ -1296,7 +1297,7 @@ module.exports = grammar({

mut_pattern: $ => prec(-1, seq(
$.mutable_specifier,
$._pattern
$._pattern,
)),

range_pattern: $ => seq(
Expand All @@ -1313,7 +1314,7 @@ module.exports = grammar({

ref_pattern: $ => seq(
'ref',
$._pattern
$._pattern,
),

captured_pattern: $ => seq(
Expand All @@ -1325,7 +1326,12 @@ module.exports = grammar({
reference_pattern: $ => seq(
'&',
optional($.mutable_specifier),
$._pattern
$._pattern,
),

box_pattern: $ => seq(
'box',
$._pattern,
),

// Section - Literals
Expand Down
19 changes: 18 additions & 1 deletion src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@
},
{
"type": "PATTERN",
"value": "[\\/_\\-=->,;:::!=?.@*=\\/=&=#%=^=+<>|~]+"
"value": "[/_\\-=->,;:::!=?.@*=/=&=#%=^=+<>|~]+"
},
{
"type": "STRING",
Expand Down Expand Up @@ -7216,6 +7216,10 @@
"type": "SYMBOL",
"name": "range_pattern"
},
{
"type": "SYMBOL",
"name": "box_pattern"
},
{
"type": "STRING",
"value": "_"
Expand Down Expand Up @@ -7698,6 +7702,19 @@
}
]
},
"box_pattern": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "box"
},
{
"type": "SYMBOL",
"name": "_pattern"
}
]
},
"_literal": {
"type": "CHOICE",
"members": [
Expand Down
23 changes: 23 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@
"type": "_literal_pattern",
"named": true
},
{
"type": "box_pattern",
"named": true
},
{
"type": "captured_pattern",
"named": true
Expand Down Expand Up @@ -805,6 +809,21 @@
]
}
},
{
"type": "box_pattern",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "_pattern",
"named": true
}
]
}
},
{
"type": "bracketed_type",
"named": true,
Expand Down Expand Up @@ -4674,6 +4693,10 @@
"type": "block_comment",
"named": true
},
{
"type": "box",
"named": false
},
{
"type": "break",
"named": false
Expand Down
Loading