Use maybe_whole! to streamline parse_attr_item.

This commit is contained in:
Nicholas Nethercote 2024-03-20 05:55:44 +11:00
parent b9ead994b3
commit d919dbe370

View File

@ -3,11 +3,12 @@
SuffixedLiteralInAttribute, SuffixedLiteralInAttribute,
}; };
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::maybe_whole;
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle}; use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_ast::token::{self, Delimiter, Nonterminal}; use rustc_ast::token::{self, Delimiter};
use rustc_errors::{codes::*, Diag, PResult}; use rustc_errors::{codes::*, Diag, PResult};
use rustc_span::{sym, BytePos, Span}; use rustc_span::{sym, BytePos, Span};
use thin_vec::ThinVec; use thin_vec::ThinVec;
@ -251,25 +252,15 @@ pub(super) fn error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerA
/// PATH `=` UNSUFFIXED_LIT /// PATH `=` UNSUFFIXED_LIT
/// 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_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> { pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
let item = match &self.token.kind { maybe_whole!(self, NtMeta, |attr| attr.into_inner());
token::Interpolated(nt) => match &nt.0 {
Nonterminal::NtMeta(item) => Some(item.clone().into_inner()),
_ => None,
},
_ => None,
};
Ok(if let Some(item) = item {
self.bump();
item
} else {
let do_parse = |this: &mut Self| { let do_parse = |this: &mut Self| {
let path = this.parse_path(PathStyle::Mod)?; let path = this.parse_path(PathStyle::Mod)?;
let args = this.parse_attr_args()?; let args = this.parse_attr_args()?;
Ok(ast::AttrItem { path, args, tokens: None }) Ok(ast::AttrItem { path, args, tokens: None })
}; };
// Attr items don't have attributes // Attr items don't have attributes
if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }? if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }
})
} }
/// Parses attributes that appear after the opening of an item. These should /// Parses attributes that appear after the opening of an item. These should