Skip to content
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

Merged
merged 3 commits into from
Oct 7, 2017
Merged
Changes from 2 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 src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,7 @@ pub enum PatternKind {
String(PatternString),
Struct(PatternStruct),
Tuple(PatternTuple),
Box(PatternBox)
Copy link
Owner

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.

}

#[derive(Debug, HasExtent, Visit)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just reuse kw_box, defined above (kw_ is to avoid conflict with the real keyword) :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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>
{
Expand Down Expand Up @@ -5336,6 +5360,12 @@ mod test {
assert_extent!(p, (0, 15))
}

#[test]
fn fn_with_arguments_with_box_pattern() {
Copy link
Owner

Choose a reason for hiding this comment

The 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");
Expand Down Expand Up @@ -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)");
Expand Down