about summary refs log tree commit diff
path: root/src/librustc_parse/parser
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-12-03 12:48:08 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2019-12-20 22:41:29 +0100
commit3ed5ba7fa867d84ca9e7bc9213a5b571fdb7c5ed (patch)
tree740f9bcba5a7a1dbc20a9ba278bab534b20bf1d6 /src/librustc_parse/parser
parent5f0f86b6cae9a3ce8007eff5a655ae63899047dc (diff)
downloadrust-3ed5ba7fa867d84ca9e7bc9213a5b571fdb7c5ed.tar.gz
rust-3ed5ba7fa867d84ca9e7bc9213a5b571fdb7c5ed.zip
extract parse_labeled_expr
Diffstat (limited to 'src/librustc_parse/parser')
-rw-r--r--src/librustc_parse/parser/expr.rs54
1 files changed, 29 insertions, 25 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index aaba59c520c..923e5d378c5 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -867,35 +867,13 @@ impl<'a> Parser<'a> {
                     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 @@ impl<'a> Parser<'a> {
         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.