syntax: Support modern attribute syntax in the `meta` matcher

This commit is contained in:
Vadim Petrochenkov 2019-08-18 01:10:56 +03:00
parent 535d4743a4
commit 957986d056
11 changed files with 69 additions and 57 deletions

View File

@ -255,9 +255,8 @@ impl MetaItem {
} }
} }
impl Attribute { impl AttrItem {
/// Extracts the `MetaItem` from inside this `Attribute`. crate fn meta(&self, span: Span) -> Option<MetaItem> {
pub fn meta(&self) -> Option<MetaItem> {
let mut tokens = self.tokens.trees().peekable(); let mut tokens = self.tokens.trees().peekable();
Some(MetaItem { Some(MetaItem {
path: self.path.clone(), path: self.path.clone(),
@ -269,9 +268,16 @@ impl Attribute {
} else { } else {
return None; return None;
}, },
span: self.span, span,
}) })
} }
}
impl Attribute {
/// Extracts the MetaItem from inside this Attribute.
pub fn meta(&self) -> Option<MetaItem> {
self.item.meta(self.span)
}
pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T> pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
@ -524,7 +530,7 @@ impl MetaItem {
} }
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt { Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident), token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident),
token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()), token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
token::Nonterminal::NtPath(ref path) => path.clone(), token::Nonterminal::NtPath(ref path) => path.clone(),
_ => return None, _ => return None,
}, },

View File

@ -122,8 +122,8 @@ impl<'a> StripUnconfigured<'a> {
while !parser.check(&token::CloseDelim(token::Paren)) { while !parser.check(&token::CloseDelim(token::Paren)) {
let lo = parser.token.span.lo(); let lo = parser.token.span.lo();
let (path, tokens) = parser.parse_meta_item_unrestricted()?; let item = parser.parse_attr_item()?;
expanded_attrs.push((path, tokens, parser.prev_span.with_lo(lo))); expanded_attrs.push((item, parser.prev_span.with_lo(lo)));
parser.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?; parser.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?;
} }
@ -150,8 +150,8 @@ impl<'a> StripUnconfigured<'a> {
// `cfg_attr` inside of another `cfg_attr`. E.g. // `cfg_attr` inside of another `cfg_attr`. E.g.
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`. // `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
expanded_attrs.into_iter() expanded_attrs.into_iter()
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute { .flat_map(|(item, span)| self.process_cfg_attr(ast::Attribute {
item: ast::AttrItem { path, tokens }, item,
id: attr::mk_attr_id(), id: attr::mk_attr_id(),
style: attr.style, style: attr.style,
is_sugared_doc: false, is_sugared_doc: false,

View File

@ -924,7 +924,7 @@ fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
FatalError.raise() FatalError.raise()
} }
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))), sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
sym::meta => token::NtMeta(panictry!(p.parse_meta_item())), sym::meta => token::NtMeta(panictry!(p.parse_attr_item())),
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))), sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
sym::lifetime => if p.check_lifetime() { sym::lifetime => if p.check_lifetime() {
token::NtLifetime(p.expect_lifetime().ident) token::NtLifetime(p.expect_lifetime().ident)

View File

@ -682,7 +682,10 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident), token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
token::NtLifetime(ident) => vis.visit_ident(ident), token::NtLifetime(ident) => vis.visit_ident(ident),
token::NtLiteral(expr) => vis.visit_expr(expr), token::NtLiteral(expr) => vis.visit_expr(expr),
token::NtMeta(meta) => vis.visit_meta_item(meta), token::NtMeta(AttrItem { path, tokens }) => {
vis.visit_path(path);
vis.visit_tts(tokens);
}
token::NtPath(path) => vis.visit_path(path), token::NtPath(path) => vis.visit_path(path),
token::NtTT(tt) => vis.visit_tt(tt), token::NtTT(tt) => vis.visit_tt(tt),
token::NtImplItem(item) => token::NtImplItem(item) =>

View File

@ -90,7 +90,7 @@ impl<'a> Parser<'a> {
debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}", debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
inner_parse_policy, inner_parse_policy,
self.token); self.token);
let (span, path, tokens, style) = match self.token.kind { let (span, item, style) = match self.token.kind {
token::Pound => { token::Pound => {
let lo = self.token.span; let lo = self.token.span;
self.bump(); self.bump();
@ -107,7 +107,7 @@ impl<'a> Parser<'a> {
}; };
self.expect(&token::OpenDelim(token::Bracket))?; self.expect(&token::OpenDelim(token::Bracket))?;
let (path, tokens) = self.parse_meta_item_unrestricted()?; let item = self.parse_attr_item()?;
self.expect(&token::CloseDelim(token::Bracket))?; self.expect(&token::CloseDelim(token::Bracket))?;
let hi = self.prev_span; let hi = self.prev_span;
@ -142,7 +142,7 @@ impl<'a> Parser<'a> {
} }
} }
(attr_sp, path, tokens, style) (attr_sp, item, style)
} }
_ => { _ => {
let token_str = self.this_token_to_string(); let token_str = self.this_token_to_string();
@ -151,7 +151,7 @@ impl<'a> Parser<'a> {
}; };
Ok(ast::Attribute { Ok(ast::Attribute {
item: ast::AttrItem { path, tokens }, item,
id: attr::mk_attr_id(), id: attr::mk_attr_id(),
style, style,
is_sugared_doc: false, is_sugared_doc: false,
@ -168,17 +168,17 @@ impl<'a> Parser<'a> {
/// PATH /// PATH
/// PATH `=` TOKEN_TREE /// PATH `=` TOKEN_TREE
/// The delimiters or `=` are still put into the resulting token stream. /// The delimiters or `=` are still put into the resulting token stream.
pub fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> { pub fn parse_attr_item(&mut self) -> PResult<'a, ast::AttrItem> {
let meta = match self.token.kind { let item = match self.token.kind {
token::Interpolated(ref nt) => match **nt { token::Interpolated(ref nt) => match **nt {
Nonterminal::NtMeta(ref meta) => Some(meta.clone()), Nonterminal::NtMeta(ref item) => Some(item.clone()),
_ => None, _ => None,
}, },
_ => None, _ => None,
}; };
Ok(if let Some(meta) = meta { Ok(if let Some(item) = item {
self.bump(); self.bump();
(meta.path, meta.kind.tokens(meta.span)) item
} else { } else {
let path = self.parse_path(PathStyle::Mod)?; let path = self.parse_path(PathStyle::Mod)?;
let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) || let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) ||
@ -205,7 +205,7 @@ impl<'a> Parser<'a> {
} else { } else {
TokenStream::empty() TokenStream::empty()
}; };
(path, tokens) ast::AttrItem { path, tokens }
}) })
} }
@ -273,9 +273,14 @@ impl<'a> Parser<'a> {
_ => None, _ => None,
}; };
if let Some(meta) = nt_meta { if let Some(item) = nt_meta {
self.bump(); return match item.meta(item.path.span) {
return Ok(meta); Some(meta) => {
self.bump();
Ok(meta)
}
None => self.unexpected(),
}
} }
let lo = self.token.span; let lo = self.token.span;

View File

@ -114,9 +114,9 @@ impl<'a> Parser<'a> {
pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> { pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> {
let meta_ident = match self.token.kind { let meta_ident = match self.token.kind {
token::Interpolated(ref nt) => match **nt { token::Interpolated(ref nt) => match **nt {
token::NtMeta(ref meta) => match meta.kind { token::NtMeta(ref item) => match item.tokens.is_empty() {
ast::MetaItemKind::Word => Some(meta.path.clone()), true => Some(item.path.clone()),
_ => None, false => None,
}, },
_ => None, _ => None,
}, },

View File

@ -687,7 +687,7 @@ pub enum Nonterminal {
NtLifetime(ast::Ident), NtLifetime(ast::Ident),
NtLiteral(P<ast::Expr>), NtLiteral(P<ast::Expr>),
/// Stuff inside brackets for attributes /// Stuff inside brackets for attributes
NtMeta(ast::MetaItem), NtMeta(ast::AttrItem),
NtPath(ast::Path), NtPath(ast::Path),
NtVis(ast::Visibility), NtVis(ast::Visibility),
NtTT(TokenTree), NtTT(TokenTree),

View File

@ -324,7 +324,7 @@ fn token_to_string_ext(token: &Token, convert_dollar_crate: bool) -> String {
crate fn nonterminal_to_string(nt: &Nonterminal) -> String { crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
match *nt { match *nt {
token::NtExpr(ref e) => expr_to_string(e), token::NtExpr(ref e) => expr_to_string(e),
token::NtMeta(ref e) => meta_item_to_string(e), token::NtMeta(ref e) => attr_item_to_string(e),
token::NtTy(ref e) => ty_to_string(e), token::NtTy(ref e) => ty_to_string(e),
token::NtPath(ref e) => path_to_string(e), token::NtPath(ref e) => path_to_string(e),
token::NtItem(ref e) => item_to_string(e), token::NtItem(ref e) => item_to_string(e),
@ -412,8 +412,8 @@ pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
to_string(|s| s.print_meta_list_item(li)) to_string(|s| s.print_meta_list_item(li))
} }
pub fn meta_item_to_string(mi: &ast::MetaItem) -> String { fn attr_item_to_string(ai: &ast::AttrItem) -> String {
to_string(|s| s.print_meta_item(mi)) to_string(|s| s.print_attr_item(ai, ai.path.span))
} }
pub fn attribute_to_string(attr: &ast::Attribute) -> String { pub fn attribute_to_string(attr: &ast::Attribute) -> String {
@ -629,26 +629,30 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
ast::AttrStyle::Inner => self.word("#!["), ast::AttrStyle::Inner => self.word("#!["),
ast::AttrStyle::Outer => self.word("#["), ast::AttrStyle::Outer => self.word("#["),
} }
self.ibox(0); self.print_attr_item(&attr.item, attr.span);
match attr.tokens.trees().next() {
Some(TokenTree::Delimited(_, delim, tts)) => {
self.print_mac_common(
Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span
);
}
tree => {
self.print_path(&attr.path, false, 0);
if tree.is_some() {
self.space();
self.print_tts(attr.tokens.clone(), true);
}
}
}
self.end();
self.word("]"); self.word("]");
} }
} }
fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
self.ibox(0);
match item.tokens.trees().next() {
Some(TokenTree::Delimited(_, delim, tts)) => {
self.print_mac_common(
Some(MacHeader::Path(&item.path)), false, None, delim, tts, true, span
);
}
tree => {
self.print_path(&item.path, false, 0);
if tree.is_some() {
self.space();
self.print_tts(item.tokens.clone(), true);
}
}
}
self.end();
}
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) { fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
match item { match item {
ast::NestedMetaItem::MetaItem(ref mi) => { ast::NestedMetaItem::MetaItem(ref mi) => {

View File

@ -1,6 +1,6 @@
//! Attributes injected into the crate root from command line using `-Z crate-attr`. //! Attributes injected into the crate root from command line using `-Z crate-attr`.
use syntax::ast::{self, AttrStyle}; use syntax::ast::{self, AttrItem, AttrStyle};
use syntax::attr::mk_attr; use syntax::attr::mk_attr;
use syntax::panictry; use syntax::panictry;
use syntax::parse::{self, token, ParseSess}; use syntax::parse::{self, token, ParseSess};
@ -15,7 +15,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
); );
let start_span = parser.token.span; let start_span = parser.token.span;
let (path, tokens) = panictry!(parser.parse_meta_item_unrestricted()); let AttrItem { path, tokens } = panictry!(parser.parse_attr_item());
let end_span = parser.token.span; let end_span = parser.token.span;
if parser.token != token::Eof { if parser.token != token::Eof {
parse_sess.span_diagnostic parse_sess.span_diagnostic

View File

@ -57,7 +57,7 @@ fn main() {
// check that macro expanded code works // check that macro expanded code works
macro_rules! if_cfg { macro_rules! if_cfg {
($cfg:meta $ib:block else $eb:block) => { ($cfg:meta? $ib:block else $eb:block) => {
{ {
let r; let r;
#[cfg($cfg)] #[cfg($cfg)]
@ -69,7 +69,7 @@ fn main() {
} }
} }
let n = if_cfg!(unset { let n = if_cfg!(unset? {
413 413
} else { } else {
612 612

View File

@ -252,12 +252,6 @@ test_path!(::std);
test_path!(std::u8,); test_path!(std::u8,);
test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>); test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>);
macro_rules! test_meta_block {
($($m:meta)* $b:block) => {};
}
test_meta_block!(windows {});
macro_rules! test_lifetime { macro_rules! test_lifetime {
(1. $($l:lifetime)* $($b:block)*) => {}; (1. $($l:lifetime)* $($b:block)*) => {};
(2. $($b:block)* $($l:lifetime)*) => {}; (2. $($b:block)* $($l:lifetime)*) => {};