Rollup merge of #69447 - Centril:minor-stmt-refactor, r=estebank
Minor refactoring of statement parsing Extracted out of https://github.com/rust-lang/rust/pull/69445. r? @estebank
This commit is contained in:
commit
ae383e2af8
1 changed files with 86 additions and 93 deletions
|
@ -35,29 +35,66 @@ impl<'a> Parser<'a> {
|
||||||
let attrs = self.parse_outer_attributes()?;
|
let attrs = self.parse_outer_attributes()?;
|
||||||
let lo = self.token.span;
|
let lo = self.token.span;
|
||||||
|
|
||||||
if self.eat_keyword(kw::Let) {
|
let stmt = if self.eat_keyword(kw::Let) {
|
||||||
return self.parse_local_mk(lo, attrs.into()).map(Some);
|
self.parse_local_mk(lo, attrs.into())?
|
||||||
}
|
} else if self.is_kw_followed_by_ident(kw::Mut) {
|
||||||
if self.is_kw_followed_by_ident(kw::Mut) {
|
self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut")?
|
||||||
return self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut");
|
} else if self.is_kw_followed_by_ident(kw::Auto) {
|
||||||
}
|
|
||||||
if self.is_kw_followed_by_ident(kw::Auto) {
|
|
||||||
self.bump(); // `auto`
|
self.bump(); // `auto`
|
||||||
let msg = "write `let` instead of `auto` to introduce a new variable";
|
let msg = "write `let` instead of `auto` to introduce a new variable";
|
||||||
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
|
self.recover_stmt_local(lo, attrs.into(), msg, "let")?
|
||||||
}
|
} else if self.is_kw_followed_by_ident(sym::var) {
|
||||||
if self.is_kw_followed_by_ident(sym::var) {
|
|
||||||
self.bump(); // `var`
|
self.bump(); // `var`
|
||||||
let msg = "write `let` instead of `var` to introduce a new variable";
|
let msg = "write `let` instead of `var` to introduce a new variable";
|
||||||
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
|
self.recover_stmt_local(lo, attrs.into(), msg, "let")?
|
||||||
|
} else if self.token.is_path_start()
|
||||||
|
&& !self.token.is_qpath_start()
|
||||||
|
&& !self.is_path_start_item()
|
||||||
|
{
|
||||||
|
// We have avoided contextual keywords like `union`, items with `crate` visibility,
|
||||||
|
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
|
||||||
|
// that starts like a path (1 token), but it fact not a path.
|
||||||
|
// Also, we avoid stealing syntax from `parse_item_`.
|
||||||
|
self.parse_stmt_path_start(lo, attrs)?
|
||||||
|
} else if let Some(item) = self.parse_stmt_item(attrs.clone())? {
|
||||||
|
// FIXME: Bad copy of attrs
|
||||||
|
self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))
|
||||||
|
} else if self.token == token::Semi {
|
||||||
|
// Do not attempt to parse an expression if we're done here.
|
||||||
|
self.error_outer_attrs(&attrs);
|
||||||
|
self.bump();
|
||||||
|
let mut last_semi = lo;
|
||||||
|
while self.token == token::Semi {
|
||||||
|
last_semi = self.token.span;
|
||||||
|
self.bump();
|
||||||
|
}
|
||||||
|
// We are encoding a string of semicolons as an an empty tuple that spans
|
||||||
|
// the excess semicolons to preserve this info until the lint stage.
|
||||||
|
let kind = StmtKind::Semi(self.mk_expr(
|
||||||
|
lo.to(last_semi),
|
||||||
|
ExprKind::Tup(Vec::new()),
|
||||||
|
AttrVec::new(),
|
||||||
|
));
|
||||||
|
self.mk_stmt(lo.to(last_semi), kind)
|
||||||
|
} else if self.token != token::CloseDelim(token::Brace) {
|
||||||
|
// Remainder are line-expr stmts.
|
||||||
|
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs.into()))?;
|
||||||
|
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
|
||||||
|
} else {
|
||||||
|
self.error_outer_attrs(&attrs);
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
Ok(Some(stmt))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts like a simple path, being careful to avoid contextual keywords,
|
fn parse_stmt_item(&mut self, attrs: Vec<Attribute>) -> PResult<'a, Option<ast::Item>> {
|
||||||
// e.g., `union`, items with `crate` visibility, or `auto trait` items.
|
let old = mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
|
||||||
// We aim to parse an arbitrary path `a::b` but not something that starts like a path
|
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
|
||||||
// (1 token), but it fact not a path. Also, we avoid stealing syntax from `parse_item_`.
|
self.directory.ownership = old;
|
||||||
if self.token.is_path_start() && !self.token.is_qpath_start() && !self.is_path_start_item()
|
Ok(item)
|
||||||
{
|
}
|
||||||
|
|
||||||
|
fn parse_stmt_path_start(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, Stmt> {
|
||||||
let path = self.parse_path(PathStyle::Expr)?;
|
let path = self.parse_path(PathStyle::Expr)?;
|
||||||
|
|
||||||
if self.eat(&token::Not) {
|
if self.eat(&token::Not) {
|
||||||
|
@ -75,56 +112,12 @@ impl<'a> Parser<'a> {
|
||||||
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
|
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
|
||||||
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
|
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
|
||||||
})?;
|
})?;
|
||||||
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr))));
|
Ok(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr)))
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: Bad copy of attrs
|
|
||||||
let old_directory_ownership =
|
|
||||||
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
|
|
||||||
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
|
|
||||||
self.directory.ownership = old_directory_ownership;
|
|
||||||
|
|
||||||
if let Some(item) = item {
|
|
||||||
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not attempt to parse an expression if we're done here.
|
|
||||||
if self.token == token::Semi {
|
|
||||||
self.error_outer_attrs(&attrs);
|
|
||||||
self.bump();
|
|
||||||
let mut last_semi = lo;
|
|
||||||
while self.token == token::Semi {
|
|
||||||
last_semi = self.token.span;
|
|
||||||
self.bump();
|
|
||||||
}
|
|
||||||
// We are encoding a string of semicolons as an an empty tuple that spans
|
|
||||||
// the excess semicolons to preserve this info until the lint stage.
|
|
||||||
let kind = StmtKind::Semi(self.mk_expr(
|
|
||||||
lo.to(last_semi),
|
|
||||||
ExprKind::Tup(Vec::new()),
|
|
||||||
AttrVec::new(),
|
|
||||||
));
|
|
||||||
return Ok(Some(self.mk_stmt(lo.to(last_semi), kind)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.token == token::CloseDelim(token::Brace) {
|
|
||||||
self.error_outer_attrs(&attrs);
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remainder are line-expr stmts.
|
|
||||||
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs.into()))?;
|
|
||||||
Ok(Some(self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.
|
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.
|
||||||
/// At this point, the `!` token after the path has already been eaten.
|
/// At this point, the `!` token after the path has already been eaten.
|
||||||
fn parse_stmt_mac(
|
fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResult<'a, Stmt> {
|
||||||
&mut self,
|
|
||||||
lo: Span,
|
|
||||||
attrs: AttrVec,
|
|
||||||
path: ast::Path,
|
|
||||||
) -> PResult<'a, Option<Stmt>> {
|
|
||||||
let args = self.parse_mac_args()?;
|
let args = self.parse_mac_args()?;
|
||||||
let delim = args.delim();
|
let delim = args.delim();
|
||||||
let hi = self.prev_span;
|
let hi = self.prev_span;
|
||||||
|
@ -145,7 +138,7 @@ impl<'a> Parser<'a> {
|
||||||
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
|
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
|
||||||
StmtKind::Expr(e)
|
StmtKind::Expr(e)
|
||||||
};
|
};
|
||||||
Ok(Some(self.mk_stmt(lo.to(hi), kind)))
|
Ok(self.mk_stmt(lo.to(hi), kind))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error on outer attributes in this context.
|
/// Error on outer attributes in this context.
|
||||||
|
@ -167,12 +160,12 @@ impl<'a> Parser<'a> {
|
||||||
attrs: AttrVec,
|
attrs: AttrVec,
|
||||||
msg: &str,
|
msg: &str,
|
||||||
sugg: &str,
|
sugg: &str,
|
||||||
) -> PResult<'a, Option<Stmt>> {
|
) -> PResult<'a, Stmt> {
|
||||||
let stmt = self.parse_local_mk(lo, attrs)?;
|
let stmt = self.parse_local_mk(lo, attrs)?;
|
||||||
self.struct_span_err(lo, "invalid variable declaration")
|
self.struct_span_err(lo, "invalid variable declaration")
|
||||||
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
|
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
|
||||||
.emit();
|
.emit();
|
||||||
Ok(Some(stmt))
|
Ok(stmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_local_mk(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, Stmt> {
|
fn parse_local_mk(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, Stmt> {
|
||||||
|
@ -372,9 +365,10 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
let mut eat_semi = true;
|
let mut eat_semi = true;
|
||||||
match stmt.kind {
|
match stmt.kind {
|
||||||
StmtKind::Expr(ref expr) if self.token != token::Eof => {
|
// Expression without semicolon.
|
||||||
// expression without semicolon
|
StmtKind::Expr(ref expr)
|
||||||
if classify::expr_requires_semi_to_be_stmt(expr) {
|
if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) =>
|
||||||
|
{
|
||||||
// Just check for errors and recover; do not eat semicolon yet.
|
// Just check for errors and recover; do not eat semicolon yet.
|
||||||
if let Err(mut e) =
|
if let Err(mut e) =
|
||||||
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
|
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
|
||||||
|
@ -403,7 +397,6 @@ impl<'a> Parser<'a> {
|
||||||
stmt.kind = StmtKind::Expr(self.mk_expr_err(sp));
|
stmt.kind = StmtKind::Expr(self.mk_expr_err(sp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
StmtKind::Local(..) => {
|
StmtKind::Local(..) => {
|
||||||
self.expect_semi()?;
|
self.expect_semi()?;
|
||||||
eat_semi = false;
|
eat_semi = false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue