]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/parse/parser/pat.rs
parser: `parse_pats` -> `parse_top_pat{_unpack}`.
[rust.git] / src / libsyntax / parse / parser / pat.rs
index b2a026d0071e48fdedcd27f3dc7466399eef538b..e4a9dc0097752055bee591085b6162ced7c9b082 100644 (file)
@@ -20,27 +20,83 @@ pub fn parse_pat(&mut self, expected: Expected) -> PResult<'a, P<Pat>> {
         self.parse_pat_with_range_pat(true, expected)
     }
 
-    /// Parses patterns, separated by '|' s.
-    pub(super) fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
-        // Allow a '|' before the pats (RFC 1925 + RFC 2530)
-        self.eat(&token::BinOp(token::Or));
+    // FIXME(or_patterns, Centril | dlrobertson):
+    // remove this and use `parse_top_pat` everywhere it is used instead.
+    pub(super) fn parse_top_pat_unpack(&mut self, gate_or: bool) -> PResult<'a, Vec<P<Pat>>> {
+        self.parse_top_pat(gate_or)
+            .map(|pat| pat.and_then(|pat| match pat.node {
+                PatKind::Or(pats) => pats,
+                node => vec![self.mk_pat(pat.span, node)],
+            }))
+    }
+
+    /// Entry point to the main pattern parser.
+    /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
+    pub(super) fn parse_top_pat(&mut self, gate_or: bool) -> PResult<'a, P<Pat>> {
+        // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
+        if self.eat_or_separator() && gate_or {
+            self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
+        }
 
-        let mut pats = Vec::new();
-        loop {
-            pats.push(self.parse_top_level_pat()?);
+        self.parse_pat_with_or(None, gate_or, true)
+    }
+
+    pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
+        let pat = self.parse_pat(None)?;
+        self.maybe_recover_unexpected_comma(pat.span, true)?;
+        Ok(pat)
+    }
 
-            if self.token == token::OrOr {
+    /// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
+    /// Corresponds to `pat<allow_top_alt>` in RFC 2535.
+    fn parse_pat_with_or(
+        &mut self,
+        expected: Expected,
+        gate_or: bool,
+        top_level: bool
+    ) -> PResult<'a, P<Pat>> {
+        // Parse the first pattern.
+        let first_pat = self.parse_pat(expected)?;
+        self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
+
+        // If the next token is not a `|`,
+        // this is not an or-pattern and we should exit here.
+        if !self.check(&token::BinOp(token::Or)) && self.token != token::OrOr {
+            return Ok(first_pat)
+        }
+
+        let lo = first_pat.span;
+        let mut pats = vec![first_pat];
+        while self.eat_or_separator() {
+            let pat = self.parse_pat(expected)?;
+            self.maybe_recover_unexpected_comma(pat.span, top_level)?;
+            pats.push(pat);
+        }
+        let or_pattern_span = lo.to(self.prev_span);
+
+        // Feature gate the or-pattern if instructed:
+        if gate_or {
+            self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
+        }
+
+        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();
-            } else if self.eat(&token::BinOp(token::Or)) {
-                // This is a No-op. Continue the loop to parse the next
-                // pattern.
-            } else {
-                return Ok(pats);
+                true
             }
-        };
+            _ => self.eat(&token::BinOp(token::Or)),
+        }
     }
 
+    /// We have parsed `||` instead of `|`. Error and suggest `|` instead.
     fn ban_unexpected_or_or(&mut self) {
         self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
             .span_suggestion(
@@ -52,15 +108,8 @@ fn ban_unexpected_or_or(&mut self) {
             .emit();
     }
 
-    /// A wrapper around `parse_pat` with some special error handling for the
-    /// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
-    /// to subpatterns within such).
-    pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
-        let pat = self.parse_pat(None)?;
-        self.maybe_recover_unexpected_comma(pat.span, true)?;
-        Ok(pat)
-    }
-
+    /// Some special error handling for the "top-level" patterns in a match arm,
+    /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
     fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
         if !top_level || self.token != token::Comma {
             return Ok(());
@@ -108,50 +157,6 @@ fn skip_pat_list(&mut self) -> PResult<'a, ()> {
         Ok(())
     }
 
-    /// Parses a pattern, that may be a or-pattern (e.g. `Some(Foo | Bar)`).
-    fn parse_pat_with_or(
-        &mut self,
-        expected: Expected,
-        gate_or: bool,
-        top_level: bool
-    ) -> PResult<'a, P<Pat>> {
-        // Parse the first pattern.
-        let first_pat = self.parse_pat(expected)?;
-        self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
-
-        // If the next token is not a `|`,
-        // this is not an or-pattern and we should exit here.
-        if !self.check(&token::BinOp(token::Or)) && self.token != token::OrOr {
-            return Ok(first_pat)
-        }
-
-        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;
-            }
-
-            let pat = self.parse_pat(expected)?;
-            self.maybe_recover_unexpected_comma(pat.span, top_level)?;
-            pats.push(pat);
-        }
-        let or_pattern_span = lo.to(self.prev_span);
-
-        // Feature gate the or-pattern if instructed:
-        if gate_or {
-            self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
-        }
-
-        Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
-    }
-
     /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
     /// allowed).
     fn parse_pat_with_range_pat(