Skip to content

Commit 6024115

Browse files
committed
Split parse_items from parse_mod for proc_macro.
1 parent cc8e769 commit 6024115

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree}
88
use rustc_data_structures::sync::Lrc;
99
use rustc_errors::ErrorReported;
1010
use rustc_parse::nt_to_tokenstream;
11-
use rustc_parse::parser::ForceCollect;
1211
use rustc_span::{Span, DUMMY_SP};
1312

1413
const EXEC_STRATEGY: pm::bridge::server::SameThread = pm::bridge::server::SameThread;
@@ -114,37 +113,22 @@ impl MultiItemModifier for ProcMacroDerive {
114113
let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
115114
let mut parser =
116115
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
117-
let mut items = vec![];
118116

119-
loop {
120-
match parser.parse_item(ForceCollect::No) {
121-
Ok(None) => break,
122-
Ok(Some(item)) => {
123-
if is_stmt {
124-
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
125-
} else {
126-
items.push(Annotatable::Item(item));
127-
}
128-
}
129-
Err(mut err) => {
130-
err.emit();
131-
break;
117+
let items = parser
118+
.parse_items(&token::Eof)
119+
.unwrap_or_else(|mut e| {
120+
e.emit();
121+
Vec::new()
122+
})
123+
.into_iter()
124+
.map(|item| {
125+
if is_stmt {
126+
Annotatable::Stmt(P(ecx.stmt_item(span, item)))
127+
} else {
128+
Annotatable::Item(item)
132129
}
133-
}
134-
}
135-
136-
// check if the entire stream was parsed
137-
if parser.token != token::Eof {
138-
ecx.struct_span_err(
139-
parser.token.span,
140-
&format!(
141-
"expected item, found {}",
142-
rustc_parse::parser::token_descr(&parser.token)
143-
),
144-
)
145-
.span_label(parser.token.span, "expected item")
146-
.emit();
147-
}
130+
})
131+
.collect();
148132

149133
// fail if there have been errors emitted
150134
if ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before {

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ impl<'a> Parser<'a> {
5454
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
5555
let lo = self.token.span;
5656
let attrs = self.parse_inner_attributes()?;
57+
let items = self.parse_items(term)?;
58+
Ok((attrs, items, lo.to(self.prev_token.span)))
59+
}
5760

61+
pub fn parse_items(&mut self, term: &TokenKind) -> PResult<'a, Vec<P<Item>>> {
5862
let mut items = vec![];
5963
while let Some(item) = self.parse_item(ForceCollect::No)? {
6064
items.push(item);
@@ -71,7 +75,7 @@ impl<'a> Parser<'a> {
7175
}
7276
}
7377

74-
Ok((attrs, items, lo.to(self.prev_token.span)))
78+
Ok(items)
7579
}
7680
}
7781

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn token_descr_opt(token: &Token) -> Option<&'static str> {
394394
})
395395
}
396396

397-
pub fn token_descr(token: &Token) -> String {
397+
pub(super) fn token_descr(token: &Token) -> String {
398398
let token_str = pprust::token_to_string(token);
399399
match token_descr_opt(token) {
400400
Some(prefix) => format!("{} `{}`", prefix, token_str),

0 commit comments

Comments
 (0)