Skip to content

Commit 245e15b

Browse files
committed
parse: extract parse_stmt_item & parse_stmt_path_start.
1 parent 834bc56 commit 245e15b

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/librustc_parse/parser/stmt.rs

+33-32
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,11 @@ impl<'a> Parser<'a> {
5858
// (1 token), but it fact not a path. Also, we avoid stealing syntax from `parse_item_`.
5959
if self.token.is_path_start() && !self.token.is_qpath_start() && !self.is_path_start_item()
6060
{
61-
let path = self.parse_path(PathStyle::Expr)?;
62-
63-
if self.eat(&token::Not) {
64-
return self.parse_stmt_mac(lo, attrs.into(), path);
65-
}
66-
67-
let expr = if self.check(&token::OpenDelim(token::Brace)) {
68-
self.parse_struct_expr(lo, path, AttrVec::new())?
69-
} else {
70-
let hi = self.prev_span;
71-
self.mk_expr(lo.to(hi), ExprKind::Path(None, path), AttrVec::new())
72-
};
73-
74-
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
75-
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
76-
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
77-
})?;
78-
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr))));
61+
return self.parse_stmt_path_start(lo, attrs).map(Some);
7962
}
8063

81-
// FIXME: Bad copy of attrs
82-
let old_directory_ownership =
83-
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
84-
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
85-
self.directory.ownership = old_directory_ownership;
86-
87-
if let Some(item) = item {
64+
if let Some(item) = self.parse_stmt_item(attrs.clone())? {
65+
// FIXME: Bad copy of attrs
8866
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
8967
}
9068

@@ -117,14 +95,37 @@ impl<'a> Parser<'a> {
11795
Ok(Some(self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))))
11896
}
11997

98+
fn parse_stmt_item(&mut self, attrs: Vec<Attribute>) -> PResult<'a, Option<ast::Item>> {
99+
let old = mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
100+
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
101+
self.directory.ownership = old;
102+
Ok(item)
103+
}
104+
105+
fn parse_stmt_path_start(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, Stmt> {
106+
let path = self.parse_path(PathStyle::Expr)?;
107+
108+
if self.eat(&token::Not) {
109+
return self.parse_stmt_mac(lo, attrs.into(), path);
110+
}
111+
112+
let expr = if self.check(&token::OpenDelim(token::Brace)) {
113+
self.parse_struct_expr(lo, path, AttrVec::new())?
114+
} else {
115+
let hi = self.prev_span;
116+
self.mk_expr(lo.to(hi), ExprKind::Path(None, path), AttrVec::new())
117+
};
118+
119+
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
120+
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
121+
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
122+
})?;
123+
Ok(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr)))
124+
}
125+
120126
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.
121127
/// At this point, the `!` token after the path has already been eaten.
122-
fn parse_stmt_mac(
123-
&mut self,
124-
lo: Span,
125-
attrs: AttrVec,
126-
path: ast::Path,
127-
) -> PResult<'a, Option<Stmt>> {
128+
fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResult<'a, Stmt> {
128129
let args = self.parse_mac_args()?;
129130
let delim = args.delim();
130131
let hi = self.prev_span;
@@ -145,7 +146,7 @@ impl<'a> Parser<'a> {
145146
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
146147
StmtKind::Expr(e)
147148
};
148-
Ok(Some(self.mk_stmt(lo.to(hi), kind)))
149+
Ok(self.mk_stmt(lo.to(hi), kind))
149150
}
150151

151152
/// Error on outer attributes in this context.

0 commit comments

Comments
 (0)