]> 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 680a58720568653873da79910e2e612ee39fee49..e4a9dc0097752055bee591085b6162ced7c9b082 100644 (file)
@@ -20,19 +20,25 @@ 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 (RFCs 1925, 2530, and 2535).
-        self.eat_or_separator();
-
-        let mut pats = Vec::new();
-        loop {
-            pats.push(self.parse_top_level_pat()?);
+    // 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)],
+            }))
+    }
 
-            if !self.eat_or_separator() {
-                return Ok(pats);
-            }
+    /// 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);
         }
+
+        self.parse_pat_with_or(None, gate_or, true)
     }
 
     pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
@@ -90,6 +96,7 @@ fn eat_or_separator(&mut self) -> bool {
         }
     }
 
+    /// 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(