]> git.lizzy.rs Git - rust.git/commitdiff
extract parse_pat_tuple_or_parens
authorMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 05:41:50 +0000 (07:41 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 07:28:49 +0000 (09:28 +0200)
src/libsyntax/parse/parser/pat.rs

index 95678f9f7a1471efba3d4c1ce80b2f6247fb27aa..b7e40969d3eb5ed2a32037c1ccbd829e62a4533e 100644 (file)
@@ -110,18 +110,7 @@ fn parse_pat_with_range_pat(
         let lo = self.token.span;
         let pat = match self.token.kind {
             token::BinOp(token::And) | token::AndAnd => self.parse_pat_deref(expected)?,
-            token::OpenDelim(token::Paren) => {
-                // Parse a tuple or parenthesis pattern.
-                let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat(None))?;
-
-                // Here, `(pat,)` is a tuple pattern.
-                // For backward compatibility, `(..)` is a tuple pattern as well.
-                if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
-                    PatKind::Paren(fields.into_iter().nth(0).unwrap())
-                } else {
-                    PatKind::Tuple(fields)
-                }
-            }
+            token::OpenDelim(token::Paren) => self.parse_pat_tuple_or_parens()?,
             token::OpenDelim(token::Bracket) => {
                 // Parse `[pat, pat,...]` as a slice pattern.
                 PatKind::Slice(self.parse_delim_comma_seq(token::Bracket, |p| p.parse_pat(None))?.0)
@@ -336,6 +325,19 @@ fn parse_pat_deref(&mut self, expected: Option<&'static str>) -> PResult<'a, Pat
         Ok(PatKind::Ref(subpat, mutbl))
     }
 
+    /// 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(None))?;
+
+        // Here, `(pat,)` is a tuple pattern.
+        // For backward compatibility, `(..)` is a tuple pattern as well.
+        Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
+            PatKind::Paren(fields.into_iter().nth(0).unwrap())
+        } else {
+            PatKind::Tuple(fields)
+        })
+    }
+
     // Helper function to decide whether to parse as ident binding
     // or to try to do something more complex like range patterns.
     fn parse_as_ident(&mut self) -> bool {