Fix parsing of stement-ish binary expressions

closes #3512
This commit is contained in:
Aleksey Kladov 2020-03-11 12:40:38 +01:00
parent bddf6b5266
commit 4a745cc8cf
3 changed files with 49 additions and 2 deletions

View file

@ -278,7 +278,7 @@ fn current_op(p: &Parser) -> (u8, SyntaxKind) {
} }
// Parses expression with binding power of at least bp. // Parses expression with binding power of at least bp.
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) { fn expr_bp(p: &mut Parser, mut r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) {
let mut lhs = match lhs(p, r) { let mut lhs = match lhs(p, r) {
Some((lhs, blocklike)) => { Some((lhs, blocklike)) => {
// test stmt_bin_expr_ambiguity // test stmt_bin_expr_ambiguity
@ -311,6 +311,12 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(op); p.bump(op);
// test binop_resets_statementness
// fn foo() {
// v = {1}&2;
// }
r = Restrictions { prefer_stmt: false, ..r };
if is_range { if is_range {
// test postfix_range // test postfix_range
// fn foo() { // fn foo() {
@ -327,7 +333,7 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
} }
} }
expr_bp(p, r, op_bp + 1); expr_bp(p, Restrictions { prefer_stmt: false, ..r }, op_bp + 1);
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
} }
(Some(lhs), BlockLike::NotBlock) (Some(lhs), BlockLike::NotBlock)

View file

@ -0,0 +1,3 @@
fn foo() {
v = {1}&2;
}

View file

@ -0,0 +1,38 @@
SOURCE_FILE@[0; 28)
FN_DEF@[0; 27)
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 27)
BLOCK@[9; 27)
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 15) "\n "
EXPR_STMT@[15; 25)
BIN_EXPR@[15; 24)
PATH_EXPR@[15; 16)
PATH@[15; 16)
PATH_SEGMENT@[15; 16)
NAME_REF@[15; 16)
IDENT@[15; 16) "v"
WHITESPACE@[16; 17) " "
EQ@[17; 18) "="
WHITESPACE@[18; 19) " "
BIN_EXPR@[19; 24)
BLOCK_EXPR@[19; 22)
BLOCK@[19; 22)
L_CURLY@[19; 20) "{"
LITERAL@[20; 21)
INT_NUMBER@[20; 21) "1"
R_CURLY@[21; 22) "}"
AMP@[22; 23) "&"
LITERAL@[23; 24)
INT_NUMBER@[23; 24) "2"
SEMI@[24; 25) ";"
WHITESPACE@[25; 26) "\n"
R_CURLY@[26; 27) "}"
WHITESPACE@[27; 28) "\n"