Skip to content

Commit 1632940

Browse files
committed
syntax/attr: reduce reliance on parser
1 parent b7176b4 commit 1632940

File tree

6 files changed

+36
-32
lines changed

6 files changed

+36
-32
lines changed

src/libsyntax/attr/mod.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
1515
use crate::mut_visit::visit_clobber;
1616
use crate::source_map::{BytePos, Spanned};
1717
use crate::parse::lexer::comments::doc_comment_style;
18-
use crate::parse::parser::Parser;
18+
use crate::parse;
1919
use crate::parse::PResult;
2020
use crate::parse::token::{self, Token};
2121
use crate::ptr::P;
@@ -280,35 +280,10 @@ impl Attribute {
280280
self.item.meta(self.span)
281281
}
282282

283-
crate fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
284-
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
285-
{
286-
let mut parser = Parser::new(
287-
sess,
288-
self.tokens.clone(),
289-
None,
290-
false,
291-
false,
292-
Some("attribute"),
293-
);
294-
let result = f(&mut parser)?;
295-
if parser.token != token::Eof {
296-
parser.unexpected()?;
297-
}
298-
Ok(result)
299-
}
300-
301-
pub fn parse_derive_paths<'a>(&self, sess: &'a ParseSess) -> PResult<'a, Vec<Path>> {
302-
if self.tokens.is_empty() {
303-
return Ok(Vec::new());
304-
}
305-
self.parse(sess, |p| p.parse_derive_paths())
306-
}
307-
308283
pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
309284
Ok(MetaItem {
310285
path: self.path.clone(),
311-
kind: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
286+
kind: parse::parse_in_attr(sess, self, |p| p.parse_meta_item_kind())?,
312287
span: self.span,
313288
})
314289
}

src/libsyntax/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::attr;
1010
use crate::ast;
1111
use crate::edition::Edition;
1212
use crate::mut_visit::*;
13+
use crate::parse;
1314
use crate::ptr::P;
1415
use crate::sess::ParseSess;
1516
use crate::symbol::sym;
@@ -112,7 +113,8 @@ impl<'a> StripUnconfigured<'a> {
112113
return vec![];
113114
}
114115

115-
let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |p| p.parse_cfg_attr()) {
116+
let res = parse::parse_in_attr(self.sess, &attr, |p| p.parse_cfg_attr());
117+
let (cfg_predicate, expanded_attrs) = match res {
116118
Ok(result) => result,
117119
Err(mut e) => {
118120
e.emit();

src/libsyntax/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ pub mod json;
9696
pub mod ast;
9797
pub mod attr;
9898
pub mod source_map;
99-
#[macro_use]
100-
pub mod config;
99+
#[macro_use] pub mod config;
101100
pub mod entry;
102101
pub mod feature_gate;
103102
pub mod mut_visit;

src/libsyntax/parse/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ pub fn stream_to_parser_with_base_dir<'a>(
288288
Parser::new(sess, stream, Some(base_dir), true, false, None)
289289
}
290290

291+
/// Runs the given subparser `f` on the tokens of the given `attr`'s item.
292+
pub fn parse_in_attr<'a, T>(
293+
sess: &'a ParseSess,
294+
attr: &ast::Attribute,
295+
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
296+
) -> PResult<'a, T> {
297+
let mut parser = Parser::new(
298+
sess,
299+
attr.tokens.clone(),
300+
None,
301+
false,
302+
false,
303+
Some("attribute"),
304+
);
305+
let result = f(&mut parser)?;
306+
if parser.token != token::Eof {
307+
parser.unexpected()?;
308+
}
309+
Ok(result)
310+
}
311+
291312
// NOTE(Centril): The following probably shouldn't be here but it acknowledges the
292313
// fact that architecturally, we are using parsing (read on below to understand why).
293314

src/libsyntax/parse/parser/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a> Parser<'a> {
130130
}
131131

132132
/// Parse a list of paths inside `#[derive(path_0, ..., path_n)]`.
133-
crate fn parse_derive_paths(&mut self) -> PResult<'a, Vec<Path>> {
133+
pub fn parse_derive_paths(&mut self) -> PResult<'a, Vec<Path>> {
134134
self.expect(&token::OpenDelim(token::Paren))?;
135135
let mut list = Vec::new();
136136
while !self.eat(&token::CloseDelim(token::Paren)) {

src/libsyntax_expand/proc_macro.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
200200
return false;
201201
}
202202

203-
match attr.parse_derive_paths(cx.parse_sess) {
203+
let parse_derive_paths = |attr: &ast::Attribute| {
204+
if attr.tokens.is_empty() {
205+
return Ok(Vec::new());
206+
}
207+
parse::parse_in_attr(cx.parse_sess, attr, |p| p.parse_derive_paths())
208+
};
209+
210+
match parse_derive_paths(attr) {
204211
Ok(traits) => {
205212
result.extend(traits);
206213
true

0 commit comments

Comments
 (0)