Only call collect_tokens when we have an attribute to parse

This commit is contained in:
Aaron Hill 2020-10-22 15:17:40 -04:00
parent 920bed1213
commit 5c7d8d049c
No known key found for this signature in database
GPG key ID: B4087E510E98B164

View file

@ -29,9 +29,9 @@ impl<'a> Parser<'a> {
let mut attrs: Vec<ast::Attribute> = Vec::new(); let mut attrs: Vec<ast::Attribute> = Vec::new();
let mut just_parsed_doc_comment = false; let mut just_parsed_doc_comment = false;
loop { loop {
let (attr, tokens) = self.collect_tokens(|this| { debug!("parse_outer_attributes: self.token={:?}", self.token);
debug!("parse_outer_attributes: self.token={:?}", this.token); let (attr, tokens) = if self.check(&token::Pound) {
if this.check(&token::Pound) { self.collect_tokens(|this| {
let inner_error_reason = if just_parsed_doc_comment { let inner_error_reason = if just_parsed_doc_comment {
"an inner attribute is not permitted following an outer doc comment" "an inner attribute is not permitted following an outer doc comment"
} else if !attrs.is_empty() { } else if !attrs.is_empty() {
@ -47,7 +47,9 @@ impl<'a> Parser<'a> {
let attr = this.parse_attribute_with_inner_parse_policy(inner_parse_policy)?; let attr = this.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
just_parsed_doc_comment = false; just_parsed_doc_comment = false;
Ok(Some(attr)) Ok(Some(attr))
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind { })?
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
self.collect_tokens(|this| {
let attr = let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span); attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style != ast::AttrStyle::Outer { if attr.style != ast::AttrStyle::Outer {
@ -67,10 +69,11 @@ impl<'a> Parser<'a> {
this.bump(); this.bump();
just_parsed_doc_comment = true; just_parsed_doc_comment = true;
Ok(Some(attr)) Ok(Some(attr))
} else { })?
Ok(None) } else {
} (None, None)
})?; };
if let Some(mut attr) = attr { if let Some(mut attr) = attr {
attr.tokens = tokens; attr.tokens = tokens;
attrs.push(attr); attrs.push(attr);
@ -192,26 +195,29 @@ impl<'a> Parser<'a> {
crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> { crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
let mut attrs: Vec<ast::Attribute> = vec![]; let mut attrs: Vec<ast::Attribute> = vec![];
loop { loop {
let (attr, tokens) = self.collect_tokens(|this| { // Only try to parse if it is an inner attribute (has `!`).
// Only try to parse if it is an inner attribute (has `!`). let (attr, tokens) =
if this.check(&token::Pound) && this.look_ahead(1, |t| t == &token::Not) { if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
let attr = this.parse_attribute(true)?; self.collect_tokens(|this| {
assert_eq!(attr.style, ast::AttrStyle::Inner); let attr = this.parse_attribute(true)?;
Ok(Some(attr)) assert_eq!(attr.style, ast::AttrStyle::Inner);
} else if let token::DocComment(comment_kind, attr_style, data) = this.token.kind {
// We need to get the position of this token before we bump.
let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style == ast::AttrStyle::Inner {
this.bump();
Ok(Some(attr)) Ok(Some(attr))
} else { })?
Ok(None) } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
} self.collect_tokens(|this| {
// We need to get the position of this token before we bump.
let attr =
attr::mk_doc_comment(comment_kind, attr_style, data, this.token.span);
if attr.style == ast::AttrStyle::Inner {
this.bump();
Ok(Some(attr))
} else {
Ok(None)
}
})?
} else { } else {
Ok(None) (None, None)
} };
})?;
if let Some(mut attr) = attr { if let Some(mut attr) = attr {
attr.tokens = tokens; attr.tokens = tokens;
attrs.push(attr); attrs.push(attr);