]> git.lizzy.rs Git - rust.git/commitdiff
extract parse_pat_range_starting_with_lit
authorMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 07:01:08 +0000 (09:01 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 12 Aug 2019 10:50:44 +0000 (12:50 +0200)
src/libsyntax/parse/parser/pat.rs

index 5c53a497ff475801b9c8e06e7095cd45245fe578..b821d9da3548fa20cca844f21384dbc18ef26229 100644 (file)
@@ -202,26 +202,10 @@ fn parse_pat_with_range_pat(
             } else {
                 // Try to parse everything else as literal with optional minus
                 match self.parse_literal_maybe_minus() {
-                    Ok(begin) => {
-                        let op_span = self.token.span;
-                        if self.check(&token::DotDot) || self.check(&token::DotDotEq) ||
-                                self.check(&token::DotDotDot) {
-                            let (end_kind, form) = if self.eat(&token::DotDotDot) {
-                                (RangeEnd::Included(RangeSyntax::DotDotDot), "...")
-                            } else if self.eat(&token::DotDotEq) {
-                                (RangeEnd::Included(RangeSyntax::DotDotEq), "..=")
-                            } else if self.eat(&token::DotDot) {
-                                (RangeEnd::Excluded, "..")
-                            } else {
-                                panic!("impossible case: we already matched \
-                                        on a range-operator token")
-                            };
-                            let end = self.parse_pat_range_end_opt(&begin, form)?;
-                            PatKind::Range(begin, end, respan(op_span, end_kind))
-                        } else {
-                            PatKind::Lit(begin)
-                        }
-                    }
+                    Ok(begin) if self.check(&token::DotDot) || self.check(&token::DotDotEq)
+                        || self.check(&token::DotDotDot)
+                        => self.parse_pat_range_starting_with_lit(begin)?,
+                    Ok(begin) => PatKind::Lit(begin),
                     Err(mut err) => {
                         self.cancel(&mut err);
                         let expected = expected.unwrap_or("pattern");
@@ -360,6 +344,23 @@ fn parse_pat_range_starting_with_path(
         Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
     }
 
+    /// Parse a range pattern `$literal $form $end?` where `$form = ".." | "..." | "..=" ;`.
+    /// The `$path` has already been parsed and the next token is the `$form`.
+    fn parse_pat_range_starting_with_lit(&mut self, begin: P<Expr>) -> PResult<'a, PatKind> {
+        let op_span = self.token.span;
+        let (end_kind, form) = if self.eat(&token::DotDotDot) {
+            (RangeEnd::Included(RangeSyntax::DotDotDot), "...")
+        } else if self.eat(&token::DotDotEq) {
+            (RangeEnd::Included(RangeSyntax::DotDotEq), "..=")
+        } else if self.eat(&token::DotDot) {
+            (RangeEnd::Excluded, "..")
+        } else {
+            panic!("impossible case: we already matched on a range-operator token")
+        };
+        let end = self.parse_pat_range_end_opt(&begin, form)?;
+        Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
+    }
+
     // 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 {