-
Notifications
You must be signed in to change notification settings - Fork 2
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
Parse box pattern syntax #95
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1631,6 +1631,7 @@ pub enum PatternKind { | |
String(PatternString), | ||
Struct(PatternStruct), | ||
Tuple(PatternTuple), | ||
Box(PatternBox) | ||
} | ||
|
||
#[derive(Debug, HasExtent, Visit)] | ||
|
@@ -1763,6 +1764,13 @@ pub struct PatternReference { | |
whitespace: Vec<Whitespace>, | ||
} | ||
|
||
#[derive(Debug, HasExtent, Visit)] | ||
pub struct PatternBox { | ||
extent: Extent, | ||
pattern: Box<Pattern>, | ||
whitespace: Vec<Whitespace>, | ||
} | ||
|
||
#[derive(Debug, HasExtent, Visit)] | ||
pub struct Trait { | ||
extent: Extent, | ||
|
@@ -2177,6 +2185,7 @@ pub trait Visitor { | |
fn visit_pattern_struct_field_short(&mut self, &PatternStructFieldShort) {} | ||
fn visit_pattern_tuple(&mut self, &PatternTuple) {} | ||
fn visit_pattern_wildcard(&mut self, &PatternWildcard) {} | ||
fn visit_pattern_box(&mut self, &PatternBox) {} | ||
fn visit_range(&mut self, &Range) {} | ||
fn visit_range_inclusive(&mut self, &RangeInclusive) {} | ||
fn visit_reference(&mut self, &Reference) {} | ||
|
@@ -2364,6 +2373,7 @@ pub trait Visitor { | |
fn exit_pattern_struct_field_short(&mut self, &PatternStructFieldShort) {} | ||
fn exit_pattern_tuple(&mut self, &PatternTuple) {} | ||
fn exit_pattern_wildcard(&mut self, &PatternWildcard) {} | ||
fn exit_pattern_box(&mut self, &PatternBox) {} | ||
fn exit_range(&mut self, &Range) {} | ||
fn exit_range_inclusive(&mut self, &RangeInclusive) {} | ||
fn exit_reference(&mut self, &Reference) {} | ||
|
@@ -2855,6 +2865,7 @@ shims! [ | |
(asterisk, Token::into_asterisk, Error::ExpectedAsterisk), | ||
(at, Token::into_at, Error::ExpectedAt), | ||
(bang, Token::into_bang, Error::ExpectedBang), | ||
(box_pattern, Token::into_box, Error::ExpectedBox), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah dang, i knew it would be there but must have missed it! :D |
||
(caret, Token::into_caret, Error::ExpectedCaret), | ||
(caret_equals, Token::into_caret_equals, Error::ExpectedCaretEquals), | ||
(colon, Token::into_colon, Error::ExpectedColon), | ||
|
@@ -3492,6 +3503,7 @@ fn pattern_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternK | |
.one(map(pattern_tuple, PatternKind::Tuple)) | ||
.one(map(pattern_slice, PatternKind::Slice)) | ||
.one(map(pattern_macro_call, PatternKind::MacroCall)) | ||
.one(map(pattern_box, PatternKind::Box)) | ||
// Must be last, otherwise it collides with struct names | ||
.one(map(pattern_ident, PatternKind::Ident)) | ||
.finish() | ||
|
@@ -3609,6 +3621,18 @@ fn pattern_reference<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Pat | |
}) | ||
} | ||
|
||
fn pattern_box<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternBox> { | ||
sequence!(pm, pt, { | ||
spt = point; | ||
_ = box_pattern; | ||
pattern = pattern; | ||
}, |pm: &mut Master, pt| PatternBox { | ||
extent: pm.state.ex(spt, pt), | ||
pattern: Box::new(pattern), | ||
whitespace: Vec::new(), | ||
}) | ||
} | ||
|
||
fn pattern_range_exclusive<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> | ||
Progress<'s, PatternRangeExclusive> | ||
{ | ||
|
@@ -5336,6 +5360,12 @@ mod test { | |
assert_extent!(p, (0, 15)) | ||
} | ||
|
||
#[test] | ||
fn fn_with_arguments_with_box_pattern() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's safe to remove this test; we already cover that functions use the general set of "pattern" above. The presence of this test makes it look like there's something extra-special about box patterns in function arguments, which there isn't. |
||
let p = qp(function_header, "fn foo(box a: Box<u8>)"); | ||
assert_extent!(p, (0, 22)) | ||
} | ||
|
||
#[test] | ||
fn fn_with_return_type() { | ||
let p = qp(function_header, "fn foo() -> bool"); | ||
|
@@ -5718,6 +5748,12 @@ mod test { | |
assert_extent!(p, (0, 6)) | ||
} | ||
|
||
#[test] | ||
fn pattern_with_box() { | ||
let p = qp(pattern, "box a"); | ||
assert_extent!(p, (0, 5)) | ||
} | ||
|
||
#[test] | ||
fn type_tuple() { | ||
let p = qp(typ, "(u8, u8)"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go ahead and place this alphabetically in the list, please.