Skip to content

Commit cb985ba

Browse files
committed
extract parse_array_or_repeat_expr
1 parent 9cb2b08 commit cb985ba

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ impl<'a> Parser<'a> {
820820
//
821821
// Therefore, prevent sub-parser from parsing
822822
// attributes by giving them a empty "already-parsed" list.
823-
let mut attrs = ThinVec::new();
823+
let attrs = ThinVec::new();
824824

825825
let lo = self.token.span;
826826
let mut hi = self.token.span;
@@ -849,46 +849,8 @@ impl<'a> Parser<'a> {
849849
token::OpenDelim(token::Brace) => {
850850
return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs);
851851
}
852-
token::BinOp(token::Or) | token::OrOr => {
853-
return self.parse_closure_expr(attrs);
854-
}
855-
token::OpenDelim(token::Bracket) => {
856-
self.bump();
857-
858-
attrs.extend(self.parse_inner_attributes()?);
859-
860-
if self.eat(&token::CloseDelim(token::Bracket)) {
861-
// Empty vector
862-
ex = ExprKind::Array(Vec::new());
863-
} else {
864-
// Non-empty vector
865-
let first_expr = self.parse_expr()?;
866-
if self.eat(&token::Semi) {
867-
// Repeating array syntax: `[ 0; 512 ]`
868-
let count = AnonConst {
869-
id: DUMMY_NODE_ID,
870-
value: self.parse_expr()?,
871-
};
872-
self.expect(&token::CloseDelim(token::Bracket))?;
873-
ex = ExprKind::Repeat(first_expr, count);
874-
} else if self.eat(&token::Comma) {
875-
// Vector with two or more elements
876-
let remaining_exprs = self.parse_seq_to_end(
877-
&token::CloseDelim(token::Bracket),
878-
SeqSep::trailing_allowed(token::Comma),
879-
|p| Ok(p.parse_expr()?)
880-
)?;
881-
let mut exprs = vec![first_expr];
882-
exprs.extend(remaining_exprs);
883-
ex = ExprKind::Array(exprs);
884-
} else {
885-
// Vector with one element
886-
self.expect(&token::CloseDelim(token::Bracket))?;
887-
ex = ExprKind::Array(vec![first_expr]);
888-
}
889-
}
890-
hi = self.prev_span;
891-
}
852+
token::BinOp(token::Or) | token::OrOr => return self.parse_closure_expr(attrs),
853+
token::OpenDelim(token::Bracket) => return self.parse_array_or_repeat_expr(),
892854
_ => {
893855
if self.eat_lt() {
894856
let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
@@ -1092,6 +1054,46 @@ impl<'a> Parser<'a> {
10921054
self.maybe_recover_from_bad_qpath(expr, true)
10931055
}
10941056

1057+
fn parse_array_or_repeat_expr(&mut self) -> PResult<'a, P<Expr>> {
1058+
let lo = self.token.span;
1059+
self.bump(); // `[`
1060+
1061+
let attrs = self.parse_inner_attributes()?.into();
1062+
1063+
let kind = if self.eat(&token::CloseDelim(token::Bracket)) {
1064+
// Empty vector
1065+
ExprKind::Array(Vec::new())
1066+
} else {
1067+
// Non-empty vector
1068+
let first_expr = self.parse_expr()?;
1069+
if self.eat(&token::Semi) {
1070+
// Repeating array syntax: `[ 0; 512 ]`
1071+
let count = AnonConst {
1072+
id: DUMMY_NODE_ID,
1073+
value: self.parse_expr()?,
1074+
};
1075+
self.expect(&token::CloseDelim(token::Bracket))?;
1076+
ExprKind::Repeat(first_expr, count)
1077+
} else if self.eat(&token::Comma) {
1078+
// Vector with two or more elements.
1079+
let remaining_exprs = self.parse_seq_to_end(
1080+
&token::CloseDelim(token::Bracket),
1081+
SeqSep::trailing_allowed(token::Comma),
1082+
|p| Ok(p.parse_expr()?)
1083+
)?;
1084+
let mut exprs = vec![first_expr];
1085+
exprs.extend(remaining_exprs);
1086+
ExprKind::Array(exprs)
1087+
} else {
1088+
// Vector with one element
1089+
self.expect(&token::CloseDelim(token::Bracket))?;
1090+
ExprKind::Array(vec![first_expr])
1091+
}
1092+
};
1093+
let expr = self.mk_expr(lo.to(self.prev_span), kind, attrs);
1094+
self.maybe_recover_from_bad_qpath(expr, true)
1095+
}
1096+
10951097
/// Returns a string literal if the next token is a string literal.
10961098
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
10971099
/// and returns `None` if the next token is not literal at all.

0 commit comments

Comments
 (0)