From 6646d49f238bb92d55fcb4900830f19faa2994a5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 13 Apr 2019 18:38:31 +0800 Subject: Fix bug and add expr , pat , ty matcher --- crates/ra_parser/src/grammar/expressions.rs | 50 +++++++++++++++++++++++------ crates/ra_parser/src/grammar/patterns.rs | 2 +- 2 files changed, 42 insertions(+), 10 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 9b38b0a31..295577325 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -8,17 +8,20 @@ const EXPR_FIRST: TokenSet = LHS_FIRST; pub(super) fn expr(p: &mut Parser) -> BlockLike { let r = Restrictions { forbid_structs: false, prefer_stmt: false }; - expr_bp(p, r, 1).1 + let mut dollar_lvl = 0; + expr_bp(p, r, 1, &mut dollar_lvl).1 } pub(super) fn expr_stmt(p: &mut Parser) -> (Option, BlockLike) { let r = Restrictions { forbid_structs: false, prefer_stmt: true }; - expr_bp(p, r, 1) + let mut dollar_lvl = 0; + expr_bp(p, r, 1, &mut dollar_lvl) } fn expr_no_struct(p: &mut Parser) { let r = Restrictions { forbid_structs: true, prefer_stmt: false }; - expr_bp(p, r, 1); + let mut dollar_lvl = 0; + expr_bp(p, r, 1, &mut dollar_lvl); } // test block @@ -206,8 +209,23 @@ 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) -> (Option, BlockLike) { - let mut lhs = match lhs(p, r) { +fn expr_bp( + p: &mut Parser, + r: Restrictions, + mut bp: u8, + dollar_lvl: &mut usize, +) -> (Option, BlockLike) { + // `newly_dollar_open` is a flag indicated that dollar is just closed after lhs, e.g. + // `$1$ + a` + // We use this flag to skip handling it. + let mut newly_dollar_open = false; + + if p.at_l_dollar() { + *dollar_lvl += p.eat_l_dollars(); + newly_dollar_open = true; + } + + let mut lhs = match lhs(p, r, dollar_lvl) { Some((lhs, blocklike)) => { // test stmt_bin_expr_ambiguity // fn foo() { @@ -223,6 +241,15 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, }; loop { + if *dollar_lvl > 0 && p.at_r_dollar() { + *dollar_lvl -= p.eat_r_dollars(*dollar_lvl); + if !newly_dollar_open { + // We "pump" bp for make it highest priority + bp = 255; + } + newly_dollar_open = false; + } + let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ; let (op_bp, op) = current_op(p); if op_bp < bp { @@ -235,7 +262,8 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, p.bump_compound(kind, n); } } - expr_bp(p, r, op_bp + 1); + + expr_bp(p, r, op_bp + 1, dollar_lvl); lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); } (Some(lhs), BlockLike::NotBlock) @@ -244,7 +272,11 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, const LHS_FIRST: TokenSet = atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOTDOT, DOTDOTEQ, MINUS]); -fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { +fn lhs( + p: &mut Parser, + r: Restrictions, + dollar_lvl: &mut usize, +) -> Option<(CompletedMarker, BlockLike)> { let m; let kind = match p.current() { // test ref_expr @@ -275,7 +307,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> m = p.start(); p.bump(); if p.at_ts(EXPR_FIRST) { - expr_bp(p, r, 2); + expr_bp(p, r, 2, dollar_lvl); } return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock)); } @@ -287,7 +319,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> )); } }; - expr_bp(p, r, 255); + expr_bp(p, r, 255, dollar_lvl); Some((m.complete(p, kind), BlockLike::NotBlock)) } diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 9a307559b..03fa9b71e 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -5,7 +5,7 @@ pub(super) const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST .union(token_set![REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]); pub(super) fn pattern(p: &mut Parser) { - pattern_r(p, PAT_RECOVERY_SET) + pattern_r(p, PAT_RECOVERY_SET); } /// Parses a pattern list separated by pipes `|` -- cgit v1.2.3