]> 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 ca5a9f2a5a8827308c6e45dba78732c25ced99e4..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);
+        }
+
+        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)
+    }
+
+    /// 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);
+        }
 
-        let mut pats = Vec::new();
-        loop {
-            pats.push(self.parse_top_level_pat()?);
+        Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
+    }
 
-            if self.token == token::OrOr {
+    /// 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,43 +108,41 @@ 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)?;
-        if self.token == token::Comma {
-            // An unexpected comma after a top-level pattern is a clue that the
-            // user (perhaps more accustomed to some other language) forgot the
-            // parentheses in what should have been a tuple pattern; return a
-            // suggestion-enhanced error here rather than choking on the comma
-            // later.
-            let comma_span = self.token.span;
-            self.bump();
-            if let Err(mut err) = self.skip_pat_list() {
-                // We didn't expect this to work anyway; we just wanted
-                // to advance to the end of the comma-sequence so we know
-                // the span to suggest parenthesizing
-                err.cancel();
-            }
-            let seq_span = pat.span.to(self.prev_span);
-            let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
-            if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
-                err.span_suggestion(
-                    seq_span,
-                    "try adding parentheses to match on a tuple..",
-                    format!("({})", seq_snippet),
-                    Applicability::MachineApplicable
-                ).span_suggestion(
-                    seq_span,
-                    "..or a vertical bar to match on multiple alternatives",
-                    format!("{}", seq_snippet.replace(",", " |")),
-                    Applicability::MachineApplicable
-                );
-            }
-            return Err(err);
+    /// 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(());
         }
-        Ok(pat)
+
+        // An unexpected comma after a top-level pattern is a clue that the
+        // user (perhaps more accustomed to some other language) forgot the
+        // parentheses in what should have been a tuple pattern; return a
+        // suggestion-enhanced error here rather than choking on the comma later.
+        let comma_span = self.token.span;
+        self.bump();
+        if let Err(mut err) = self.skip_pat_list() {
+            // We didn't expect this to work anyway; we just wanted to advance to the
+            // end of the comma-sequence so we know the span to suggest parenthesizing.
+            err.cancel();
+        }
+        let seq_span = lo.to(self.prev_span);
+        let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
+        if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
+            err.span_suggestion(
+                seq_span,
+                "try adding parentheses to match on a tuple..",
+                format!("({})", seq_snippet),
+                Applicability::MachineApplicable
+            )
+            .span_suggestion(
+                seq_span,
+                "..or a vertical bar to match on multiple alternatives",
+                format!("{}", seq_snippet.replace(",", " |")),
+                Applicability::MachineApplicable
+            );
+        }
+        Err(err)
     }
 
     /// Parse and throw away a parentesized comma separated
@@ -103,35 +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) -> PResult<'a, P<Pat>> {
-        // Parse the first pattern.
-        let first_pat = self.parse_pat(expected)?;
-
-        // 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)) {
-            return Ok(first_pat)
-        }
-
-        let lo = first_pat.span;
-
-        let mut pats = vec![first_pat];
-
-        while self.eat(&token::BinOp(token::Or)) {
-            pats.push(self.parse_pat_with_range_pat(true, expected)?);
-        }
-
-        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(
@@ -150,7 +175,7 @@ fn parse_pat_with_range_pat(
                 // Parse `[pat, pat,...]` as a slice pattern.
                 let (pats, _) = self.parse_delim_comma_seq(
                     token::Bracket,
-                    |p| p.parse_pat_with_or(None, true),
+                    |p| p.parse_pat_with_or(None, true, false),
                 )?;
                 PatKind::Slice(pats)
             }
@@ -280,7 +305,7 @@ fn parse_pat_deref(&mut self, expected: Expected) -> PResult<'a, PatKind> {
     /// Parse a tuple or parenthesis pattern.
     fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
         let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
-            p.parse_pat_with_or(None, true)
+            p.parse_pat_with_or(None, true, false)
         })?;
 
         // Here, `(pat,)` is a tuple pattern.
@@ -524,7 +549,7 @@ fn parse_pat_tuple_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResul
             err.span_label(self.token.span, msg);
             return Err(err);
         }
-        let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true))?;
+        let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true, false))?;
         Ok(PatKind::TupleStruct(path, fields))
     }
 
@@ -668,7 +693,7 @@ fn parse_pat_field(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, Fi
             // Parsing a pattern of the form "fieldname: pat"
             let fieldname = self.parse_field_name()?;
             self.bump();
-            let pat = self.parse_pat_with_or(None, true)?;
+            let pat = self.parse_pat_with_or(None, true, false)?;
             hi = pat.span;
             (pat, fieldname, false)
         } else {