Navigation Menu

Skip to content

Commit

Permalink
syntax: extract parse_cfg_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 16, 2019
1 parent 7d7969d commit 41bfe94
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/libsyntax/attr/mod.rs
Expand Up @@ -302,7 +302,6 @@ impl Attribute {
if self.tokens.is_empty() {
return Ok(Vec::new());
}

self.parse(sess, |p| p.parse_derive_paths())
}

Expand Down
21 changes: 1 addition & 20 deletions src/libsyntax/config.rs
Expand Up @@ -10,7 +10,6 @@ use crate::attr;
use crate::ast;
use crate::edition::Edition;
use crate::mut_visit::*;
use crate::parse::token;
use crate::ptr::P;
use crate::sess::ParseSess;
use crate::symbol::sym;
Expand Down Expand Up @@ -112,25 +111,7 @@ impl<'a> StripUnconfigured<'a> {
return vec![];
}

let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |parser| {
parser.expect(&token::OpenDelim(token::Paren))?;

let cfg_predicate = parser.parse_meta_item()?;
parser.expect(&token::Comma)?;

// Presumably, the majority of the time there will only be one attr.
let mut expanded_attrs = Vec::with_capacity(1);

while !parser.check(&token::CloseDelim(token::Paren)) {
let lo = parser.token.span.lo();
let item = parser.parse_attr_item()?;
expanded_attrs.push((item, parser.prev_span.with_lo(lo)));
parser.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?;
}

parser.expect(&token::CloseDelim(token::Paren))?;
Ok((cfg_predicate, expanded_attrs))
}) {
let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |p| p.parse_cfg_attr()) {
Ok(result) => result,
Err(mut e) => {
e.emit();
Expand Down
21 changes: 21 additions & 0 deletions src/libsyntax/parse/parser/attr.rs
Expand Up @@ -260,6 +260,27 @@ impl<'a> Parser<'a> {
Ok(lit)
}

/// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
crate fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
self.expect(&token::OpenDelim(token::Paren))?;

let cfg_predicate = self.parse_meta_item()?;
self.expect(&token::Comma)?;

// Presumably, the majority of the time there will only be one attr.
let mut expanded_attrs = Vec::with_capacity(1);

while !self.check(&token::CloseDelim(token::Paren)) {
let lo = self.token.span.lo();
let item = self.parse_attr_item()?;
expanded_attrs.push((item, self.prev_span.with_lo(lo)));
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?;
}

self.expect(&token::CloseDelim(token::Paren))?;
Ok((cfg_predicate, expanded_attrs))
}

/// Matches the following grammar (per RFC 1559).
///
/// meta_item : PATH ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
Expand Down

0 comments on commit 41bfe94

Please sign in to comment.