about summary refs log tree commit diff
path: root/src/libsyntax/parse/parser
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-18 18:13:19 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 21:32:48 +0200
commit7b59b4f14dae8c859718d60794021230e1e3ac29 (patch)
treea8d2a2f0185e1d5921bbe4672707b1bd46664256 /src/libsyntax/parse/parser
parenta4a34ab62df777e885cac71ab171225b2cd1a812 (diff)
downloadrust-7b59b4f14dae8c859718d60794021230e1e3ac29.tar.gz
rust-7b59b4f14dae8c859718d60794021230e1e3ac29.zip
parser: extract `eat_or_separator`.
Diffstat (limited to 'src/libsyntax/parse/parser')
-rw-r--r--src/libsyntax/parse/parser/pat.rs58
1 files changed, 28 insertions, 30 deletions
diff --git a/src/libsyntax/parse/parser/pat.rs b/src/libsyntax/parse/parser/pat.rs
index b2a026d0071..3af64cef74f 100644
--- a/src/libsyntax/parse/parser/pat.rs
+++ b/src/libsyntax/parse/parser/pat.rs
@@ -29,27 +29,10 @@ impl<'a> Parser<'a> {
         loop {
             pats.push(self.parse_top_level_pat()?);
 
-            if self.token == token::OrOr {
-                self.ban_unexpected_or_or();
-                self.bump();
-            } else if self.eat(&token::BinOp(token::Or)) {
-                // This is a No-op. Continue the loop to parse the next
-                // pattern.
-            } else {
+            if !self.eat_or_separator() {
                 return Ok(pats);
             }
-        };
-    }
-
-    fn ban_unexpected_or_or(&mut self) {
-        self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
-            .span_suggestion(
-                self.token.span,
-                "use a single `|` to specify multiple patterns",
-                "|".to_owned(),
-                Applicability::MachineApplicable
-            )
-            .emit();
+        }
     }
 
     /// A wrapper around `parse_pat` with some special error handling for the
@@ -127,17 +110,7 @@ impl<'a> Parser<'a> {
 
         let lo = first_pat.span;
         let mut pats = vec![first_pat];
-        loop {
-            if self.token == token::OrOr {
-                // Found `||`; Recover and pretend we parsed `|`.
-                self.ban_unexpected_or_or();
-                self.bump();
-            } else if self.eat(&token::BinOp(token::Or)) {
-                // Found `|`. Working towards a proper or-pattern.
-            } else {
-                break;
-            }
-
+        while self.eat_or_separator() {
             let pat = self.parse_pat(expected)?;
             self.maybe_recover_unexpected_comma(pat.span, top_level)?;
             pats.push(pat);
@@ -152,6 +125,31 @@ impl<'a> Parser<'a> {
         Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
     }
 
+    /// Eat the or-pattern `|` separator.
+    /// If instead a `||` token is encountered, recover and pretend we parsed `|`.
+    fn eat_or_separator(&mut self) -> bool {
+        match self.token.kind {
+            token::OrOr => {
+                // Found `||`; Recover and pretend we parsed `|`.
+                self.ban_unexpected_or_or();
+                self.bump();
+                true
+            }
+            _ => self.eat(&token::BinOp(token::Or)),
+        }
+    }
+
+    fn ban_unexpected_or_or(&mut self) {
+        self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
+            .span_suggestion(
+                self.token.span,
+                "use a single `|` to specify multiple patterns",
+                "|".to_owned(),
+                Applicability::MachineApplicable
+            )
+            .emit();
+    }
+
     /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
     /// allowed).
     fn parse_pat_with_range_pat(