From 5205c016e9f704796aa7893f89ef108248bda2e2 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 20 Dec 2018 11:35:02 +0000 Subject: Fix ambiguity with if break Brought up by #290 --- crates/ra_syntax/src/grammar/expressions.rs | 5 +++++ crates/ra_syntax/src/grammar/expressions/atom.rs | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 5 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 4f8c46ab3..79de0add0 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -5,6 +5,7 @@ pub(super) use self::atom::{literal, LITERAL_FIRST}; use super::*; const EXPR_FIRST: TokenSet = LHS_FIRST; +const EXPR_FIRST_NO_BLOCK: TokenSet = LHS_FIRST_NO_BLOCK; pub(super) fn expr(p: &mut Parser) -> BlockLike { let r = Restrictions { @@ -209,6 +210,10 @@ const LHS_FIRST: TokenSet = token_set_union![ token_set![AMP, STAR, EXCL, DOTDOT, MINUS], atom::ATOM_EXPR_FIRST, ]; +const LHS_FIRST_NO_BLOCK: TokenSet = token_set_union![ + token_set![AMP, STAR, EXCL, DOTDOT, MINUS], + atom::ATOM_EXPR_FIRST_NO_BLOCK, +]; fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { let m; diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index cd7d62aff..fcc0a4490 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -35,10 +35,10 @@ pub(crate) fn literal(p: &mut Parser) -> Option { Some(m.complete(p, LITERAL)) } -pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ +// E.g. for after the break in `if break {}`, this should not match +pub(super) const ATOM_EXPR_FIRST_NO_BLOCK: TokenSet = token_set_union![ LITERAL_FIRST, token_set![ - L_CURLY, L_PAREN, L_BRACK, PIPE, @@ -59,6 +59,9 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ ], ]; +pub(super) const ATOM_EXPR_FIRST: TokenSet = + token_set_union![ATOM_EXPR_FIRST_NO_BLOCK, token_set![L_CURLY],]; + const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { @@ -108,7 +111,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar L_CURLY => block_expr(p, None), RETURN_KW => return_expr(p), CONTINUE_KW => continue_expr(p), - BREAK_KW => break_expr(p), + BREAK_KW => break_expr(p, r), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); return None; @@ -427,12 +430,20 @@ fn continue_expr(p: &mut Parser) -> CompletedMarker { // break 'l 92; // } // } -fn break_expr(p: &mut Parser) -> CompletedMarker { +fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { assert!(p.at(BREAK_KW)); let m = p.start(); p.bump(); p.eat(LIFETIME); - if p.at_ts(EXPR_FIRST) { + // test break_ambiguity + // fn foo(){ + // if break {} + // while break {} + // for i in break {} + // match break {} + // } + if r.forbid_structs && p.at_ts(EXPR_FIRST_NO_BLOCK) || !r.forbid_structs && p.at_ts(EXPR_FIRST) + { expr(p); } m.complete(p, BREAK_EXPR) -- cgit v1.2.3