]> git.lizzy.rs Git - rust.git/commitdiff
extract parse_labeled_expr
authorMazdak Farrokhzad <twingoow@gmail.com>
Tue, 3 Dec 2019 11:48:08 +0000 (12:48 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Fri, 20 Dec 2019 21:41:29 +0000 (22:41 +0100)
src/librustc_parse/parser/expr.rs

index aaba59c520c94191f353b60573f6cafb0b527429..923e5d378c56cb61fb228242e22abdbb3ccd9124 100644 (file)
@@ -867,35 +867,13 @@ macro_rules! parse_lit {
                     return self.parse_if_expr(attrs);
                 }
                 if self.eat_keyword(kw::For) {
-                    let lo = self.prev_span;
-                    return self.parse_for_expr(None, lo, attrs);
+                    return self.parse_for_expr(None, self.prev_span, attrs);
                 }
                 if self.eat_keyword(kw::While) {
-                    let lo = self.prev_span;
-                    return self.parse_while_expr(None, lo, attrs);
+                    return self.parse_while_expr(None, self.prev_span, attrs);
                 }
                 if let Some(label) = self.eat_label() {
-                    let lo = label.ident.span;
-                    self.expect(&token::Colon)?;
-                    if self.eat_keyword(kw::While) {
-                        return self.parse_while_expr(Some(label), lo, attrs)
-                    }
-                    if self.eat_keyword(kw::For) {
-                        return self.parse_for_expr(Some(label), lo, attrs)
-                    }
-                    if self.eat_keyword(kw::Loop) {
-                        return self.parse_loop_expr(Some(label), lo, attrs)
-                    }
-                    if self.token == token::OpenDelim(token::Brace) {
-                        return self.parse_block_expr(Some(label),
-                                                     lo,
-                                                     BlockCheckMode::Default,
-                                                     attrs);
-                    }
-                    let msg = "expected `while`, `for`, `loop` or `{` after a label";
-                    let mut err = self.fatal(msg);
-                    err.span_label(self.token.span, msg);
-                    return Err(err);
+                    return self.parse_labeled_expr(label, attrs);
                 }
                 if self.eat_keyword(kw::Loop) {
                     let lo = self.prev_span;
@@ -1097,6 +1075,32 @@ fn parse_path_start_expr(&mut self) -> PResult<'a, P<Expr>> {
         self.maybe_recover_from_bad_qpath(expr, true)
     }
 
+    fn parse_labeled_expr(
+        &mut self,
+        label: Label,
+        attrs: ThinVec<Attribute>,
+    ) -> PResult<'a, P<Expr>> {
+        let lo = label.ident.span;
+        self.expect(&token::Colon)?;
+        if self.eat_keyword(kw::While) {
+            return self.parse_while_expr(Some(label), lo, attrs)
+        }
+        if self.eat_keyword(kw::For) {
+            return self.parse_for_expr(Some(label), lo, attrs)
+        }
+        if self.eat_keyword(kw::Loop) {
+            return self.parse_loop_expr(Some(label), lo, attrs)
+        }
+        if self.token == token::OpenDelim(token::Brace) {
+            return self.parse_block_expr(Some(label), lo, BlockCheckMode::Default, attrs);
+        }
+
+        let msg = "expected `while`, `for`, `loop` or `{` after a label";
+        let mut err = self.fatal(msg);
+        err.span_label(self.token.span, msg);
+        return Err(err);
+    }
+
     /// Returns a string literal if the next token is a string literal.
     /// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
     /// and returns `None` if the next token is not literal at all.