Skip to content

Commit 7d7969d

Browse files
committed
syntax: extract parse_derive_paths
1 parent 9dba03f commit 7d7969d

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

src/libsyntax/attr/mod.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -298,24 +298,12 @@ impl Attribute {
298298
Ok(result)
299299
}
300300

301-
pub fn parse_list<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, Vec<T>>
302-
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
303-
{
301+
pub fn parse_derive_paths<'a>(&self, sess: &'a ParseSess) -> PResult<'a, Vec<Path>> {
304302
if self.tokens.is_empty() {
305303
return Ok(Vec::new());
306304
}
307-
self.parse(sess, |parser| {
308-
parser.expect(&token::OpenDelim(token::Paren))?;
309-
let mut list = Vec::new();
310-
while !parser.eat(&token::CloseDelim(token::Paren)) {
311-
list.push(f(parser)?);
312-
if !parser.eat(&token::Comma) {
313-
parser.expect(&token::CloseDelim(token::Paren))?;
314-
break
315-
}
316-
}
317-
Ok(list)
318-
})
305+
306+
self.parse(sess, |p| p.parse_derive_paths())
319307
}
320308

321309
pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {

src/libsyntax/ext/proc_macro.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::errors::{Applicability, FatalError};
44
use crate::ext::base::{self, *};
55
use crate::ext::proc_macro_server;
66
use crate::parse::{self, token};
7-
use crate::parse::parser::PathStyle;
87
use crate::symbol::sym;
98
use crate::tokenstream::{self, TokenStream};
109
use crate::visit::Visitor;
@@ -205,8 +204,7 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
205204
return false;
206205
}
207206

208-
match attr.parse_list(cx.parse_sess,
209-
|parser| parser.parse_path_allowing_meta(PathStyle::Mod)) {
207+
match attr.parse_derive_paths(cx.parse_sess) {
210208
Ok(traits) => {
211209
result.extend(traits);
212210
true

src/libsyntax/parse/parser/path.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> Parser<'a> {
111111
/// Like `parse_path`, but also supports parsing `Word` meta items into paths for
112112
/// backwards-compatibility. This is used when parsing derive macro paths in `#[derive]`
113113
/// attributes.
114-
pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> {
114+
fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> {
115115
let meta_ident = match self.token.kind {
116116
token::Interpolated(ref nt) => match **nt {
117117
token::NtMeta(ref item) => match item.tokens.is_empty() {
@@ -129,6 +129,21 @@ impl<'a> Parser<'a> {
129129
self.parse_path(style)
130130
}
131131

132+
/// Parse a list of paths inside `#[derive(path_0, ..., path_n)]`.
133+
crate fn parse_derive_paths(&mut self) -> PResult<'a, Vec<Path>> {
134+
self.expect(&token::OpenDelim(token::Paren))?;
135+
let mut list = Vec::new();
136+
while !self.eat(&token::CloseDelim(token::Paren)) {
137+
let path = self.parse_path_allowing_meta(PathStyle::Mod)?;
138+
list.push(path);
139+
if !self.eat(&token::Comma) {
140+
self.expect(&token::CloseDelim(token::Paren))?;
141+
break
142+
}
143+
}
144+
Ok(list)
145+
}
146+
132147
crate fn parse_path_segments(
133148
&mut self,
134149
segments: &mut Vec<PathSegment>,

0 commit comments

Comments
 (0)