From 20bbe0127cc6bfac3ced0c7ed1de4f0526f3bbed Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 17 Dec 2018 22:34:18 +0000 Subject: Fix parsing of inclusive ranges (#214) I'm not certain that this is correct, so extra eyes would be good --- crates/ra_syntax/src/grammar/expressions.rs | 4 ++-- crates/ra_syntax/src/grammar/patterns.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index a9449c7bf..bca32f707 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -143,7 +143,7 @@ fn current_op(p: &Parser) -> (u8, Op) { let bp = match p.current() { EQ => 1, - DOTDOT => 2, + DOTDOT | DOTDOTEQ => 2, EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, PIPE => 6, CARET => 7, @@ -173,7 +173,7 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { }; loop { - let is_range = p.current() == DOTDOT; + let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ; let (op_bp, op) = current_op(p); if op_bp < bp { break; diff --git a/crates/ra_syntax/src/grammar/patterns.rs b/crates/ra_syntax/src/grammar/patterns.rs index 10fa0e0be..64cdf0b1b 100644 --- a/crates/ra_syntax/src/grammar/patterns.rs +++ b/crates/ra_syntax/src/grammar/patterns.rs @@ -14,9 +14,13 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { if let Some(lhs) = atom_pat(p, recovery_set) { // test range_pat // fn main() { - // match 92 { 0 ... 100 => () } + // match 92 { + // 0 ... 100 => (), + // 101 ..= 200 => (), + // 200 .. 301=> (), + // } // } - if p.at(DOTDOTDOT) { + if p.at(DOTDOTDOT) || p.at(DOTDOTEQ) || p.at(DOTDOT) { let m = lhs.precede(p); p.bump(); atom_pat(p, recovery_set); -- cgit v1.2.3 From 012537bd6cdf2348dc7bc4ec89c8ecbb9578f5b3 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 18 Dec 2018 21:13:55 +0000 Subject: Fix at_ts doc comment --- crates/ra_syntax/src/grammar/expressions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index bca32f707..512823ddf 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -416,7 +416,7 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); + items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) m.complete(p, MACRO_CALL) } _ => m.complete(p, PATH_EXPR), -- cgit v1.2.3 From 97e70bf50f15007f5782f7f96d19da342b7c9505 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 18 Dec 2018 22:59:34 +0000 Subject: Possibly fix #225 --- crates/ra_syntax/src/grammar/expressions.rs | 45 ++++++++++++++++-------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 +++++---- 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 512823ddf..043f41b98 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,18 +158,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some(lhs) => { + (Some(lhs), blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && is_block(lhs.kind()) { + if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { return BlockLike::Block; } lhs } - None => return BlockLike::NotBlock, + (None, _) => return BlockLike::NotBlock, }; loop { @@ -213,7 +213,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> Option { +fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { let m; let kind = match p.current() { // test ref_expr @@ -246,19 +246,33 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option { if p.at_ts(EXPR_FIRST) { expr_bp(p, r, 2); } - return Some(m.complete(p, RANGE_EXPR)); + return (Some(m.complete(p, RANGE_EXPR)), None); } _ => { - let lhs = atom::atom_expr(p, r)?; - return Some(postfix_expr(p, r, lhs)); + let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + + if let Some(lhs_marker) = lhs_marker { + return ( + Some(postfix_expr(p, r, lhs_marker, macro_block_like)), + macro_block_like, + ); + } else { + return (None, None); + } } }; expr_bp(p, r, 255); - Some(m.complete(p, kind)) + (Some(m.complete(p, kind)), None) } -fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { - let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); +fn postfix_expr( + p: &mut Parser, + r: Restrictions, + mut lhs: CompletedMarker, + macro_block_like: Option, +) -> CompletedMarker { + let mut allow_calls = + !r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block); loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity @@ -406,21 +420,22 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - match p.current() { + let res = match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - m.complete(p, MACRO_CALL) + let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + return (m.complete(p, MACRO_CALL), Some(block_like)); } _ => m.complete(p, PATH_EXPR), - } + }; + (res, None) } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 04087fd60..e86a33090 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,12 +61,16 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { +pub(super) fn atom_expr( + p: &mut Parser, + r: Restrictions, +) -> (Option, Option) { if let Some(m) = literal(p) { - return Some(m); + return (Some(m), None); } if paths::is_path_start(p) || p.at(L_ANGLE) { - return Some(path_expr(p, r)); + let path_expr = path_expr(p, r); + return (Some(path_expr.0), path_expr.1); } let la = p.nth(1); let done = match p.current() { @@ -94,7 +98,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option Option break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return None; + return (None, None); } }; - Some(done) + (Some(done), None) } // test tuple_expr -- cgit v1.2.3 From 7a8560ba382af4b3955b14757518f748e0d67709 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 18:12:19 +0000 Subject: Fix expression parsing by bailing out upon a macro block being found TODO: Fix this when the block like macro is in expression position E.g. `test(test!{})` currently parses --- crates/ra_syntax/src/grammar/expressions.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 043f41b98..9d75bfb90 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,13 +158,14 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), blocklike) => { + (Some(lhs), macro_blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { + if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) + { return BlockLike::Block; } lhs @@ -251,11 +252,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + if macro_block_like == Some(BlockLike::Block) { + return (lhs_marker, macro_block_like); + } if let Some(lhs_marker) = lhs_marker { - return ( - Some(postfix_expr(p, r, lhs_marker, macro_block_like)), - macro_block_like, - ); + return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); } else { return (None, None); } @@ -265,14 +266,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option, -) -> CompletedMarker { - let mut allow_calls = - !r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block); +fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple + // E.g. `while true {break}();` is parsed as + // `while true {break}; ();` + let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity -- cgit v1.2.3 From cd8e33fb7e60d78809da5e77c968ef30433c2317 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 18:33:36 +0000 Subject: Revert to f6f7c5 --- crates/ra_syntax/src/grammar/expressions.rs | 39 ++++++++---------------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 ++++------ 2 files changed, 19 insertions(+), 36 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 9d75bfb90..512823ddf 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,19 +158,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), macro_blocklike) => { + Some(lhs) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) - { + if r.prefer_stmt && is_block(lhs.kind()) { return BlockLike::Block; } lhs } - (None, _) => return BlockLike::NotBlock, + None => return BlockLike::NotBlock, }; loop { @@ -214,7 +213,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { +fn lhs(p: &mut Parser, r: Restrictions) -> Option { let m; let kind = match p.current() { // test ref_expr @@ -247,29 +246,18 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { - let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); - - if macro_block_like == Some(BlockLike::Block) { - return (lhs_marker, macro_block_like); - } - if let Some(lhs_marker) = lhs_marker { - return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); - } else { - return (None, None); - } + let lhs = atom::atom_expr(p, r)?; + return Some(postfix_expr(p, r, lhs)); } }; expr_bp(p, r, 255); - (Some(m.complete(p, kind)), None) + Some(m.complete(p, kind)) } fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { - // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple - // E.g. `while true {break}();` is parsed as - // `while true {break}; ();` let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { @@ -418,22 +406,21 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { +fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - let res = match p.current() { + match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - return (m.complete(p, MACRO_CALL), Some(block_like)); + items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + m.complete(p, MACRO_CALL) } _ => m.complete(p, PATH_EXPR), - }; - (res, None) + } } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index e86a33090..04087fd60 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,16 +61,12 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr( - p: &mut Parser, - r: Restrictions, -) -> (Option, Option) { +pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { if let Some(m) = literal(p) { - return (Some(m), None); + return Some(m); } if paths::is_path_start(p) || p.at(L_ANGLE) { - let path_expr = path_expr(p, r); - return (Some(path_expr.0), path_expr.1); + return Some(path_expr(p, r)); } let la = p.nth(1); let done = match p.current() { @@ -98,7 +94,7 @@ pub(super) fn atom_expr( // } p.error("expected a loop"); m.complete(p, ERROR); - return (None, None); + return None; } } } @@ -115,10 +111,10 @@ pub(super) fn atom_expr( BREAK_KW => break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return (None, None); + return None; } }; - (Some(done), None) + Some(done) } // test tuple_expr -- cgit v1.2.3 From 4dce66ad319cbcbfae5f9e941b80c28cf546590c Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 19:35:14 +0000 Subject: Fix handling of structs in match arms --- crates/ra_syntax/src/grammar/expressions/atom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 04087fd60..452e91485 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -362,7 +362,7 @@ fn match_arm(p: &mut Parser) -> BlockLike { patterns::pattern(p); } if p.eat(IF_KW) { - expr_no_struct(p); + expr(p); } p.expect(FAT_ARROW); let ret = expr_stmt(p); -- cgit v1.2.3 From def0a95d357f334b7329c3ee9e7da3c30563321c Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 20:02:37 +0000 Subject: Revert "Revert to f6f7c5" This approach is correct, but it needs an addition to Restrictions too This reverts commit ad00d0c8a5f64142e6636e8b048204c8f8982f4a. --- crates/ra_syntax/src/grammar/expressions.rs | 39 ++++++++++++++++-------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 ++++++---- 2 files changed, 36 insertions(+), 19 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 512823ddf..9d75bfb90 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,18 +158,19 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some(lhs) => { + (Some(lhs), macro_blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && is_block(lhs.kind()) { + if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) + { return BlockLike::Block; } lhs } - None => return BlockLike::NotBlock, + (None, _) => return BlockLike::NotBlock, }; loop { @@ -213,7 +214,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> Option { +fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { let m; let kind = match p.current() { // test ref_expr @@ -246,18 +247,29 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option { if p.at_ts(EXPR_FIRST) { expr_bp(p, r, 2); } - return Some(m.complete(p, RANGE_EXPR)); + return (Some(m.complete(p, RANGE_EXPR)), None); } _ => { - let lhs = atom::atom_expr(p, r)?; - return Some(postfix_expr(p, r, lhs)); + let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + + if macro_block_like == Some(BlockLike::Block) { + return (lhs_marker, macro_block_like); + } + if let Some(lhs_marker) = lhs_marker { + return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); + } else { + return (None, None); + } } }; expr_bp(p, r, 255); - Some(m.complete(p, kind)) + (Some(m.complete(p, kind)), None) } fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple + // E.g. `while true {break}();` is parsed as + // `while true {break}; ();` let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { @@ -406,21 +418,22 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - match p.current() { + let res = match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - m.complete(p, MACRO_CALL) + let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + return (m.complete(p, MACRO_CALL), Some(block_like)); } _ => m.complete(p, PATH_EXPR), - } + }; + (res, None) } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 452e91485..471f398f5 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,12 +61,16 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { +pub(super) fn atom_expr( + p: &mut Parser, + r: Restrictions, +) -> (Option, Option) { if let Some(m) = literal(p) { - return Some(m); + return (Some(m), None); } if paths::is_path_start(p) || p.at(L_ANGLE) { - return Some(path_expr(p, r)); + let path_expr = path_expr(p, r); + return (Some(path_expr.0), path_expr.1); } let la = p.nth(1); let done = match p.current() { @@ -94,7 +98,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option Option break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return None; + return (None, None); } }; - Some(done) + (Some(done), None) } // test tuple_expr -- cgit v1.2.3 From db677414304bec41a5eae57eea4eb0b546619415 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 20:55:24 +0000 Subject: Move is_block to lower in the call tree --- crates/ra_syntax/src/grammar/expressions.rs | 77 ++++++++++-------------- crates/ra_syntax/src/grammar/expressions/atom.rs | 20 +++--- 2 files changed, 43 insertions(+), 54 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 9d75bfb90..5f5a3077d 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -64,6 +64,16 @@ pub(crate) fn block(p: &mut Parser) { if p.at(R_CURLY) { m.abandon(p); } else { + // test no_semi_after_block + // fn foo() { + // if true {} + // loop {} + // match () {} + // while true {} + // for _ in () {} + // {} + // {} + // } if is_blocklike { p.eat(SEMI); } else { @@ -158,19 +168,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), macro_blocklike) => { + Some((lhs, macro_blocklike)) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) - { + if r.prefer_stmt && macro_blocklike.is_block() { return BlockLike::Block; } lhs } - (None, _) => return BlockLike::NotBlock, + None => return BlockLike::NotBlock, }; loop { @@ -192,29 +201,12 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { BlockLike::NotBlock } -// test no_semi_after_block -// fn foo() { -// if true {} -// loop {} -// match () {} -// while true {} -// for _ in () {} -// {} -// {} -// } -fn is_block(kind: SyntaxKind) -> bool { - match kind { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true, - _ => false, - } -} - const LHS_FIRST: TokenSet = token_set_union![ token_set![AMP, STAR, EXCL, DOTDOT, MINUS], atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { +fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { let m; let kind = match p.current() { // test ref_expr @@ -247,30 +239,28 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { - let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); - - if macro_block_like == Some(BlockLike::Block) { - return (lhs_marker, macro_block_like); - } - if let Some(lhs_marker) = lhs_marker { - return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); - } else { - return (None, None); - } + let (lhs, blocklike) = atom::atom_expr(p, r)?; + return Some(( + postfix_expr(p, lhs, !(r.prefer_stmt && blocklike.is_block())), + blocklike, + )); } }; expr_bp(p, r, 255); - (Some(m.complete(p, kind)), None) + Some((m.complete(p, kind), BlockLike::NotBlock)) } -fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { +fn postfix_expr( + p: &mut Parser, + mut lhs: CompletedMarker, // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple // E.g. `while true {break}();` is parsed as // `while true {break}; ();` - let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); + mut allow_calls: bool, +) -> CompletedMarker { loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity @@ -418,22 +408,21 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - let res = match p.current() { + match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); - m.complete(p, STRUCT_LIT) + (m.complete(p, STRUCT_LIT), BlockLike::Block) } EXCL => { - let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - return (m.complete(p, MACRO_CALL), Some(block_like)); + let block_like = items::macro_call_after_excl(p); + return (m.complete(p, MACRO_CALL), block_like); } - _ => m.complete(p, PATH_EXPR), - }; - (res, None) + _ => (m.complete(p, PATH_EXPR), BlockLike::NotBlock), + } } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 471f398f5..a976799e7 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,16 +61,12 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr( - p: &mut Parser, - r: Restrictions, -) -> (Option, Option) { +pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { if let Some(m) = literal(p) { - return (Some(m), None); + return Some((m, BlockLike::NotBlock)); } if paths::is_path_start(p) || p.at(L_ANGLE) { - let path_expr = path_expr(p, r); - return (Some(path_expr.0), path_expr.1); + return Some(path_expr(p, r)); } let la = p.nth(1); let done = match p.current() { @@ -98,7 +94,7 @@ pub(super) fn atom_expr( // } p.error("expected a loop"); m.complete(p, ERROR); - return (None, None); + return None; } } } @@ -115,10 +111,14 @@ pub(super) fn atom_expr( BREAK_KW => break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return (None, None); + return None; } }; - (Some(done), None) + let blocklike = match done.kind() { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block, + _ => BlockLike::NotBlock, + }; + Some((done, blocklike)) } // test tuple_expr -- cgit v1.2.3 From a3b842fb8b7b5503b1c4fc49355edd4f2fe0d28d Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 21:19:32 +0000 Subject: Add tests and only traverse in the crates directory --- crates/ra_syntax/src/grammar/expressions.rs | 8 ++++++-- crates/ra_syntax/src/grammar/expressions/atom.rs | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 5f5a3077d..4f8c46ab3 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -73,6 +73,10 @@ pub(crate) fn block(p: &mut Parser) { // for _ in () {} // {} // {} + // macro_rules! test { + // () => {} + // } + // test!{} // } if is_blocklike { p.eat(SEMI); @@ -168,13 +172,13 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some((lhs, macro_blocklike)) => { + Some((lhs, blocklike)) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && macro_blocklike.is_block() { + if r.prefer_stmt && blocklike.is_block() { return BlockLike::Block; } lhs diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index a976799e7..cd7d62aff 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -353,6 +353,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // fn foo() { // match () { // _ => (), +// _ if Test>{field: 0} => (), // X | Y if Z => (), // | X | Y if Z => (), // | X => (), -- cgit v1.2.3