From e6f3790106fdfa2aaf6a0510c737fd05939345e7 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 23 Aug 2019 13:04:58 -0700 Subject: Add `BoxPat` variant --- crates/ra_parser/src/syntax_kind/generated.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index f15e98e68..8ba29ebf8 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -149,6 +149,7 @@ pub enum SyntaxKind { IMPL_TRAIT_TYPE, DYN_TRAIT_TYPE, REF_PAT, + BOX_PAT, BIND_PAT, PLACEHOLDER_PAT, PATH_PAT, -- cgit v1.2.3 From ab51f57e1df39554f3443d18d2a49d6ecd881b86 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 23 Aug 2019 13:54:43 -0700 Subject: Parse `BoxPat` --- crates/ra_parser/src/grammar/patterns.rs | 51 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 8979aa499..67f1548a7 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -56,37 +56,33 @@ const PAT_RECOVERY_SET: TokenSet = token_set![LET_KW, IF_KW, WHILE_KW, LOOP_KW, MATCH_KW, R_PAREN, COMMA]; fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { - let la0 = p.nth(0); - let la1 = p.nth(1); - if la0 == T![ref] - || la0 == T![mut] - || la0 == T![box] - || (la0 == IDENT && !(la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!])) - { - return Some(bind_pat(p, true)); - } - if paths::is_use_path_start(p) { - return Some(path_pat(p)); - } + // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro + // (T![x]). + let is_path_or_macro_pat = + |la1| la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!]; - if is_literal_pat_start(p) { - return Some(literal_pat(p)); - } + let m = match p.nth(0) { + T![box] => box_pat(p), + T![ref] | T![mut] | IDENT if !is_path_or_macro_pat(p.nth(1)) => bind_pat(p, true), + + _ if paths::is_use_path_start(p) => path_pat(p), + _ if is_literal_pat_start(p) => literal_pat(p), - let m = match la0 { T![_] => placeholder_pat(p), T![&] => ref_pat(p), T!['('] => tuple_pat(p), T!['['] => slice_pat(p), + _ => { p.err_recover("expected pattern", recovery_set); return None; } }; + Some(m) } -fn is_literal_pat_start(p: &mut Parser) -> bool { +fn is_literal_pat_start(p: &Parser) -> bool { p.at(T![-]) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) || p.at_ts(expressions::LITERAL_FIRST) } @@ -261,11 +257,9 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) { // let ref mut d = (); // let e @ _ = (); // let ref mut f @ g @ _ = (); -// let box i = Box::new(1i32); // } fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker { let m = p.start(); - p.eat(T![box]); p.eat(T![ref]); p.eat(T![mut]); name(p); @@ -274,3 +268,22 @@ fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker { } m.complete(p, BIND_PAT) } + +// test_err ref_box_pat +// fn main() { +// let ref box i = (); +// } + +// test box_pat +// fn main() { +// let box i = (); +// let box Outer { box i, j: box Inner(box &x) } = (); +// let box ref mut i = (); +// } +fn box_pat(p: &mut Parser) -> CompletedMarker { + assert!(p.at(T![box])); + let m = p.start(); + p.bump(); + pattern(p); + m.complete(p, BOX_PAT) +} -- cgit v1.2.3 From 83433cd1f0ce2ebe1818caa6793f61372e4fa5a6 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 23 Aug 2019 16:19:23 -0700 Subject: Centralize `box` pattern tests in `patterns.rs` --- crates/ra_parser/src/grammar/expressions/atom.rs | 2 -- crates/ra_parser/src/grammar/patterns.rs | 5 ----- 2 files changed, 7 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index ab8fb9f6e..bc942ae01 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -414,8 +414,6 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // X | Y if Z => (), // | X | Y if Z => (), // | X => (), -// box X => (), -// Some(box X) => (), // }; // } fn match_arm(p: &mut Parser) -> BlockLike { diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 67f1548a7..f9e6de13c 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -269,11 +269,6 @@ fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker { m.complete(p, BIND_PAT) } -// test_err ref_box_pat -// fn main() { -// let ref box i = (); -// } - // test box_pat // fn main() { // let box i = (); -- cgit v1.2.3 From c08ad1cf8ac2a81e9a772eddccaecbd2a6883edd Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 23 Aug 2019 16:08:33 -0700 Subject: Handle `Struct { box i }` syntax Named structs can have `box` patterns that will bind to their fields. This is similar to the behavior of the `ref` and `mut` fields, but is at least a little bit surprising. --- crates/ra_parser/src/grammar/patterns.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index f9e6de13c..eae70ab85 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -161,6 +161,9 @@ fn record_field_pat_list(p: &mut Parser) { T![..] => p.bump(), IDENT if p.nth(1) == T![:] => record_field_pat(p), T!['{'] => error_block(p, "expected ident"), + T![box] => { + box_pat(p); + } _ => { bind_pat(p, false); } -- cgit v1.2.3