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') 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