From eb1ac43867921501723457846f7aaaa36cd4ebdd Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 5 Mar 2019 11:28:53 +0200 Subject: Introduce pattern_list to parse pipe separated patterns pattern_list comes in two variants, one uses the default PAT_RECOVERY_SET as the recovery set, while other allows the user to provide a recovery set. --- crates/ra_parser/src/grammar/expressions/atom.rs | 12 ++---------- crates/ra_parser/src/grammar/patterns.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index dfa391632..9f282c74d 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -290,11 +290,7 @@ fn for_expr(p: &mut Parser, m: Option) -> CompletedMarker { fn cond(p: &mut Parser) { let m = p.start(); if p.eat(LET_KW) { - p.eat(PIPE); - patterns::pattern(p); - while p.eat(PIPE) { - patterns::pattern(p); - } + patterns::pattern_list(p); p.expect(EQ); } expr_no_struct(p); @@ -386,11 +382,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // } fn match_arm(p: &mut Parser) -> BlockLike { let m = p.start(); - p.eat(PIPE); - patterns::pattern_r(p, TokenSet::empty()); - while p.eat(PIPE) { - patterns::pattern(p); - } + patterns::pattern_list_r(p, TokenSet::empty()); if p.at(IF_KW) { match_guard(p); } diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 9d7da639d..befe6687d 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -8,6 +8,22 @@ pub(super) fn pattern(p: &mut Parser) { pattern_r(p, PAT_RECOVERY_SET) } +/// Parses a pattern list separated by pipes `|` +pub(super) fn pattern_list(p: &mut Parser) { + pattern_list_r(p, PAT_RECOVERY_SET) +} + +/// Parses a pattern list separated by pipes `|` +/// using the given `recovery_set` +pub(super) fn pattern_list_r(p: &mut Parser, recovery_set: TokenSet) { + p.eat(PIPE); + pattern_r(p, recovery_set); + + while p.eat(PIPE) { + pattern_r(p, recovery_set); + } +} + pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { if let Some(lhs) = atom_pat(p, recovery_set) { // test range_pat -- cgit v1.2.3