Skip to content

Commit 4311a4d

Browse files
committed
extract parse_lit_expr and simplify
1 parent e9a4d94 commit 4311a4d

File tree

1 file changed

+64
-79
lines changed

1 file changed

+64
-79
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -822,98 +822,62 @@ impl<'a> Parser<'a> {
822822
// attributes by giving them a empty "already-parsed" list.
823823
let attrs = ThinVec::new();
824824

825-
let lo = self.token.span;
826-
827-
macro_rules! parse_lit {
828-
() => {
829-
match self.parse_opt_lit() {
830-
Some(literal) => (self.prev_span, ExprKind::Lit(literal)),
831-
None => return Err(self.expected_expression_found()),
832-
}
833-
}
834-
}
835-
836825
// Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`.
837-
let (hi, ex) = match self.token.kind {
826+
let lo = self.token.span;
827+
match self.token.kind {
838828
// This match arm is a special-case of the `_` match arm below and
839829
// could be removed without changing functionality, but it's faster
840830
// to have it here, especially for programs with large constants.
841-
token::Literal(_) => parse_lit!(),
842-
token::OpenDelim(token::Paren) => return self.parse_tuple_parens_expr(attrs),
831+
token::Literal(_) => self.parse_lit_expr(attrs),
832+
token::OpenDelim(token::Paren) => self.parse_tuple_parens_expr(attrs),
843833
token::OpenDelim(token::Brace) => {
844-
return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs);
834+
self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs)
845835
}
846-
token::BinOp(token::Or) | token::OrOr => return self.parse_closure_expr(attrs),
847-
token::OpenDelim(token::Bracket) => return self.parse_array_or_repeat_expr(attrs),
836+
token::BinOp(token::Or) | token::OrOr => self.parse_closure_expr(attrs),
837+
token::OpenDelim(token::Bracket) => self.parse_array_or_repeat_expr(attrs),
848838
_ => {
849839
if self.eat_lt() {
850840
let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
851-
let hi = path.span;
852-
return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs));
853-
}
854-
if self.token.is_path_start() {
855-
return self.parse_path_start_expr(attrs);
856-
}
857-
if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
858-
return self.parse_closure_expr(attrs);
859-
}
860-
if self.eat_keyword(kw::If) {
861-
return self.parse_if_expr(attrs);
862-
}
863-
if self.eat_keyword(kw::For) {
864-
return self.parse_for_expr(None, self.prev_span, attrs);
865-
}
866-
if self.eat_keyword(kw::While) {
867-
return self.parse_while_expr(None, self.prev_span, attrs);
868-
}
869-
if let Some(label) = self.eat_label() {
870-
return self.parse_labeled_expr(label, attrs);
871-
}
872-
if self.eat_keyword(kw::Loop) {
873-
return self.parse_loop_expr(None, self.prev_span, attrs);
874-
}
875-
if self.eat_keyword(kw::Continue) {
841+
Ok(self.mk_expr(lo.to(path.span), ExprKind::Path(Some(qself), path), attrs))
842+
} else if self.token.is_path_start() {
843+
self.parse_path_start_expr(attrs)
844+
} else if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
845+
self.parse_closure_expr(attrs)
846+
} else if self.eat_keyword(kw::If) {
847+
self.parse_if_expr(attrs)
848+
} else if self.eat_keyword(kw::For) {
849+
self.parse_for_expr(None, self.prev_span, attrs)
850+
} else if self.eat_keyword(kw::While) {
851+
self.parse_while_expr(None, self.prev_span, attrs)
852+
} else if let Some(label) = self.eat_label() {
853+
self.parse_labeled_expr(label, attrs)
854+
} else if self.eat_keyword(kw::Loop) {
855+
self.parse_loop_expr(None, self.prev_span, attrs)
856+
} else if self.eat_keyword(kw::Continue) {
876857
let kind = ExprKind::Continue(self.eat_label());
877-
return Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs));
878-
}
879-
if self.eat_keyword(kw::Match) {
858+
Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs))
859+
} else if self.eat_keyword(kw::Match) {
880860
let match_sp = self.prev_span;
881-
return self.parse_match_expr(attrs).map_err(|mut err| {
861+
self.parse_match_expr(attrs).map_err(|mut err| {
882862
err.span_label(match_sp, "while parsing this match expression");
883863
err
884-
});
885-
}
886-
if self.eat_keyword(kw::Unsafe) {
864+
})
865+
} else if self.eat_keyword(kw::Unsafe) {
887866
let mode = BlockCheckMode::Unsafe(ast::UserProvided);
888-
return self.parse_block_expr(None, lo, mode, attrs);
889-
}
890-
if self.is_do_catch_block() {
891-
return self.recover_do_catch(attrs);
892-
}
893-
if self.is_try_block() {
867+
self.parse_block_expr(None, lo, mode, attrs)
868+
} else if self.is_do_catch_block() {
869+
self.recover_do_catch(attrs)
870+
} else if self.is_try_block() {
894871
self.expect_keyword(kw::Try)?;
895-
return self.parse_try_block(lo, attrs);
896-
}
897-
898-
// `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
899-
let is_span_rust_2018 = self.token.span.rust_2018();
900-
if is_span_rust_2018 && self.check_keyword(kw::Async) {
901-
return if self.is_async_block() { // Check for `async {` and `async move {`.
902-
self.parse_async_block(attrs)
903-
} else {
904-
self.parse_closure_expr(attrs)
905-
};
906-
}
907-
if self.eat_keyword(kw::Return) {
908-
return self.parse_return_expr(attrs);
872+
self.parse_try_block(lo, attrs)
873+
} else if self.eat_keyword(kw::Return) {
874+
self.parse_return_expr(attrs)
909875
} else if self.eat_keyword(kw::Break) {
910-
return self.parse_break_expr(attrs);
876+
self.parse_break_expr(attrs)
911877
} else if self.eat_keyword(kw::Yield) {
912-
return self.parse_yield_expr(attrs);
878+
self.parse_yield_expr(attrs)
913879
} else if self.eat_keyword(kw::Let) {
914-
return self.parse_let_expr(attrs);
915-
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
916-
return self.recover_incorrect_await_syntax(lo, self.prev_span, attrs);
880+
self.parse_let_expr(attrs)
917881
} else if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
918882
// Don't complain about bare semicolons after unclosed braces
919883
// recovery in order to keep the error count down. Fixing the
@@ -926,15 +890,36 @@ impl<'a> Parser<'a> {
926890
// 2 | foo(bar(;
927891
// | ^ expected expression
928892
self.bump();
929-
return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()));
893+
Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()))
894+
} else if self.token.span.rust_2018() {
895+
// `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
896+
if self.check_keyword(kw::Async) {
897+
if self.is_async_block() { // Check for `async {` and `async move {`.
898+
self.parse_async_block(attrs)
899+
} else {
900+
self.parse_closure_expr(attrs)
901+
}
902+
} else if self.eat_keyword(kw::Await) {
903+
self.recover_incorrect_await_syntax(lo, self.prev_span, attrs)
904+
} else {
905+
self.parse_lit_expr(attrs)
906+
}
930907
} else {
931-
parse_lit!()
908+
self.parse_lit_expr(attrs)
932909
}
933910
}
934-
};
911+
}
912+
}
935913

936-
let expr = self.mk_expr(lo.to(hi), ex, attrs);
937-
self.maybe_recover_from_bad_qpath(expr, true)
914+
fn parse_lit_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
915+
let lo = self.token.span;
916+
match self.parse_opt_lit() {
917+
Some(literal) => {
918+
let expr = self.mk_expr(lo.to(self.prev_span), ExprKind::Lit(literal), attrs);
919+
self.maybe_recover_from_bad_qpath(expr, true)
920+
}
921+
None => return Err(self.expected_expression_found()),
922+
}
938923
}
939924

940925
fn parse_tuple_parens_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {

0 commit comments

Comments
 (0)