Skip to content

Commit

Permalink
Move is_block to lower in the call tree
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Dec 19, 2018
1 parent def0a95 commit db67741
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 115 deletions.
77 changes: 33 additions & 44 deletions crates/ra_syntax/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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<CompletedMarker>, Option<BlockLike>) {
fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
let m;
let kind = match p.current() {
// test ref_expr
Expand Down Expand Up @@ -247,30 +239,28 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option<CompletedMarker>, Option<Bloc
if p.at_ts(EXPR_FIRST) {
expr_bp(p, r, 2);
}
return (Some(m.complete(p, RANGE_EXPR)), None);
return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
}
_ => {
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
Expand Down Expand Up @@ -418,22 +408,21 @@ fn arg_list(p: &mut Parser) {
// let _ = ::a::<b>;
// let _ = format!();
// }
fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option<BlockLike>) {
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
Expand Down
20 changes: 10 additions & 10 deletions crates/ra_syntax/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletedMarker>, Option<BlockLike>) {
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() {
Expand Down Expand Up @@ -98,7 +94,7 @@ pub(super) fn atom_expr(
// }
p.error("expected a loop");
m.complete(p, ERROR);
return (None, None);
return None;
}
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
match 92 {
match 92 {
0 ... 100 => (),
101 ..= 200 => (),
200 .. 301=> (),
Expand Down
120 changes: 60 additions & 60 deletions crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SOURCE_FILE@[0; 113)
FN_DEF@[0; 112)
SOURCE_FILE@[0; 112)
FN_DEF@[0; 111)
FN_KW@[0; 2)
WHITESPACE@[2; 3)
NAME@[3; 7)
Expand All @@ -8,69 +8,69 @@ SOURCE_FILE@[0; 113)
L_PAREN@[7; 8)
R_PAREN@[8; 9)
WHITESPACE@[9; 10)
BLOCK@[10; 112)
BLOCK@[10; 111)
L_CURLY@[10; 11)
WHITESPACE@[11; 16)
MATCH_EXPR@[16; 110)
MATCH_EXPR@[16; 109)
MATCH_KW@[16; 21)
WHITESPACE@[21; 22)
LITERAL@[22; 24)
INT_NUMBER@[22; 24) "92"
WHITESPACE@[24; 25)
MATCH_ARM_LIST@[25; 110)
MATCH_ARM_LIST@[25; 109)
L_CURLY@[25; 26)
WHITESPACE@[26; 36)
MATCH_ARM@[36; 51)
RANGE_PAT@[36; 45)
LITERAL@[36; 37)
INT_NUMBER@[36; 37) "0"
WHITESPACE@[37; 38)
DOTDOTDOT@[38; 41)
WHITESPACE@[41; 42)
LITERAL@[42; 45)
INT_NUMBER@[42; 45) "100"
WHITESPACE@[45; 46)
FAT_ARROW@[46; 48)
WHITESPACE@[48; 49)
TUPLE_EXPR@[49; 51)
L_PAREN@[49; 50)
R_PAREN@[50; 51)
COMMA@[51; 52)
WHITESPACE@[52; 61)
MATCH_ARM@[61; 78)
RANGE_PAT@[61; 72)
LITERAL@[61; 64)
INT_NUMBER@[61; 64) "101"
WHITESPACE@[64; 65)
DOTDOTEQ@[65; 68)
WHITESPACE@[68; 69)
LITERAL@[69; 72)
INT_NUMBER@[69; 72) "200"
WHITESPACE@[72; 73)
FAT_ARROW@[73; 75)
WHITESPACE@[75; 76)
TUPLE_EXPR@[76; 78)
L_PAREN@[76; 77)
R_PAREN@[77; 78)
COMMA@[78; 79)
WHITESPACE@[79; 88)
MATCH_ARM@[88; 103)
RANGE_PAT@[88; 98)
LITERAL@[88; 91)
INT_NUMBER@[88; 91) "200"
WHITESPACE@[91; 92)
DOTDOT@[92; 94)
WHITESPACE@[94; 95)
LITERAL@[95; 98)
INT_NUMBER@[95; 98) "301"
FAT_ARROW@[98; 100)
WHITESPACE@[100; 101)
TUPLE_EXPR@[101; 103)
L_PAREN@[101; 102)
R_PAREN@[102; 103)
COMMA@[103; 104)
WHITESPACE@[104; 109)
R_CURLY@[109; 110)
WHITESPACE@[110; 111)
R_CURLY@[111; 112)
WHITESPACE@[112; 113)
WHITESPACE@[26; 35)
MATCH_ARM@[35; 50)
RANGE_PAT@[35; 44)
LITERAL@[35; 36)
INT_NUMBER@[35; 36) "0"
WHITESPACE@[36; 37)
DOTDOTDOT@[37; 40)
WHITESPACE@[40; 41)
LITERAL@[41; 44)
INT_NUMBER@[41; 44) "100"
WHITESPACE@[44; 45)
FAT_ARROW@[45; 47)
WHITESPACE@[47; 48)
TUPLE_EXPR@[48; 50)
L_PAREN@[48; 49)
R_PAREN@[49; 50)
COMMA@[50; 51)
WHITESPACE@[51; 60)
MATCH_ARM@[60; 77)
RANGE_PAT@[60; 71)
LITERAL@[60; 63)
INT_NUMBER@[60; 63) "101"
WHITESPACE@[63; 64)
DOTDOTEQ@[64; 67)
WHITESPACE@[67; 68)
LITERAL@[68; 71)
INT_NUMBER@[68; 71) "200"
WHITESPACE@[71; 72)
FAT_ARROW@[72; 74)
WHITESPACE@[74; 75)
TUPLE_EXPR@[75; 77)
L_PAREN@[75; 76)
R_PAREN@[76; 77)
COMMA@[77; 78)
WHITESPACE@[78; 87)
MATCH_ARM@[87; 102)
RANGE_PAT@[87; 97)
LITERAL@[87; 90)
INT_NUMBER@[87; 90) "200"
WHITESPACE@[90; 91)
DOTDOT@[91; 93)
WHITESPACE@[93; 94)
LITERAL@[94; 97)
INT_NUMBER@[94; 97) "301"
FAT_ARROW@[97; 99)
WHITESPACE@[99; 100)
TUPLE_EXPR@[100; 102)
L_PAREN@[100; 101)
R_PAREN@[101; 102)
COMMA@[102; 103)
WHITESPACE@[103; 108)
R_CURLY@[108; 109)
WHITESPACE@[109; 110)
R_CURLY@[110; 111)
WHITESPACE@[111; 112)

0 comments on commit db67741

Please sign in to comment.