From 0e46ed8420469741d718ac223fad1088c151b497 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Apr 2020 15:44:06 +0200 Subject: Cleanups --- crates/ra_parser/src/grammar/expressions.rs | 4 ++-- crates/ra_parser/src/grammar/patterns.rs | 23 +++++++++++------------ 2 files changed, 13 insertions(+), 14 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 0c170ac5e..c486c0211 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -79,8 +79,6 @@ fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { } pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { - // test block_items - // fn a() { fn b() {} } let m = p.start(); // test attr_on_expr_stmt // fn foo() { @@ -97,6 +95,8 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { return; } + // test block_items + // fn a() { fn b() {} } let m = match items::maybe_item(p, m, items::ItemFlavor::Mod) { Ok(()) => return, Err(m) => m, diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 3afbaa82b..9bfdcfd41 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -70,15 +70,6 @@ fn pattern_single_r(p: &mut Parser, recovery_set: TokenSet) { return; } } - // test marco_pat - // fn main() { - // let m!(x) = 0; - // } - if lhs.kind() == PATH_PAT && p.at(T![!]) { - let m = lhs.undo_completion(p); - items::macro_call_after_excl(p); - m.complete(p, MACRO_CALL); - } } } @@ -92,12 +83,12 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { IDENT => match p.nth(1) { // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro // (T![x]). - T!['('] | T!['{'] | T![!] => path_pat(p), + T!['('] | T!['{'] | T![!] => path_or_macro_pat(p), T![:] if p.nth_at(1, T![::]) => path_pat(p), _ => bind_pat(p, true), }, - _ if paths::is_use_path_start(p) => path_pat(p), + _ if paths::is_use_path_start(p) => path_or_macro_pat(p), _ if is_literal_pat_start(p) => literal_pat(p), T![.] if p.at(T![..]) => dot_dot_pat(p), @@ -146,7 +137,7 @@ fn literal_pat(p: &mut Parser) -> CompletedMarker { // let Bar { .. } = (); // let Bar(..) = (); // } -fn path_pat(p: &mut Parser) -> CompletedMarker { +fn path_or_macro_pat(p: &mut Parser) -> CompletedMarker { assert!(paths::is_use_path_start(p)); let m = p.start(); paths::expr_path(p); @@ -159,6 +150,14 @@ fn path_pat(p: &mut Parser) -> CompletedMarker { record_field_pat_list(p); RECORD_PAT } + // test marco_pat + // fn main() { + // let m!(x) = 0; + // } + T![!] => { + items::macro_call_after_excl(p); + MACRO_CALL + } _ => PATH_PAT, }; m.complete(p, kind) -- cgit v1.2.3 From da8eb29a2f70a58122903bf087bd6c1d0fbd6d3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Apr 2020 15:38:42 +0200 Subject: Macro patterns are not confused with expressions. We treat macro calls as expressions (there's appropriate Into impl), which causes problem if there's expresison and non-expression macro in the same node (like in the match arm). We fix this problem by nesting macor patterns into another node (the same way we nest path into PathExpr or PathPat). Ideally, we probably should add a similar nesting for macro expressions, but that needs some careful thinking about macros in blocks: `{ am_i_expression!() }`. --- crates/ra_parser/src/grammar/patterns.rs | 4 ++-- crates/ra_parser/src/syntax_kind/generated.rs | 1 + 2 files changed, 3 insertions(+), 2 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 9bfdcfd41..936d27575 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -84,7 +84,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro // (T![x]). T!['('] | T!['{'] | T![!] => path_or_macro_pat(p), - T![:] if p.nth_at(1, T![::]) => path_pat(p), + T![:] if p.nth_at(1, T![::]) => path_or_macro_pat(p), _ => bind_pat(p, true), }, @@ -156,7 +156,7 @@ fn path_or_macro_pat(p: &mut Parser) -> CompletedMarker { // } T![!] => { items::macro_call_after_excl(p); - MACRO_CALL + return m.complete(p, MACRO_CALL).precede(p).complete(p, MACRO_PAT); } _ => PATH_PAT, }; diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index dfc30d727..4c16cf1cd 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -167,6 +167,7 @@ pub enum SyntaxKind { SLICE_PAT, RANGE_PAT, LITERAL_PAT, + MACRO_PAT, TUPLE_EXPR, ARRAY_EXPR, PAREN_EXPR, -- cgit v1.2.3