Overhaul token collection.
This commit does the following. - Renames `collect_tokens_trailing_token` as `collect_tokens`, because (a) it's annoying long, and (b) the `_trailing_token` bit is less accurate now that its types have changed. - In `collect_tokens`, adds a `Option<CollectPos>` argument and a `UsePreAttrPos` in the return type of `f`. These are used in `parse_expr_force_collect` (for vanilla expressions) and in `parse_stmt_without_recovery` (for two different cases of expression statements). Together these ensure are enough to fix all the problems with token collection and assoc expressions. The changes to the `stringify.rs` test demonstrate some of these. - Adds a new test. The code in this test was causing an assertion failure prior to this commit, due to an invalid `NodeRange`. The extra complexity is annoying, but necessary to fix the existing problems.
This commit is contained in:
parent
fe460ac28b
commit
9d31f86f0d
12 changed files with 414 additions and 292 deletions
|
@ -10,6 +10,7 @@ use tracing::debug;
|
|||
|
||||
use super::{
|
||||
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
|
||||
UsePreAttrPos,
|
||||
};
|
||||
use crate::{errors, fluent_generated as fluent, maybe_whole};
|
||||
|
||||
|
@ -259,7 +260,8 @@ impl<'a> Parser<'a> {
|
|||
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
|
||||
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
|
||||
|
||||
let do_parse = |this: &mut Self, _empty_attrs| {
|
||||
// Attr items don't have attributes.
|
||||
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
|
||||
let is_unsafe = this.eat_keyword(kw::Unsafe);
|
||||
let unsafety = if is_unsafe {
|
||||
let unsafe_span = this.prev_token.span;
|
||||
|
@ -275,10 +277,12 @@ impl<'a> Parser<'a> {
|
|||
if is_unsafe {
|
||||
this.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
||||
}
|
||||
Ok((ast::AttrItem { unsafety, path, args, tokens: None }, Trailing::No))
|
||||
};
|
||||
// Attr items don't have attributes.
|
||||
self.collect_tokens_trailing_token(AttrWrapper::empty(), force_collect, do_parse)
|
||||
Ok((
|
||||
ast::AttrItem { unsafety, path, args, tokens: None },
|
||||
Trailing::No,
|
||||
UsePreAttrPos::No,
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
/// Parses attributes that appear after the opening of an item. These should
|
||||
|
@ -311,8 +315,8 @@ impl<'a> Parser<'a> {
|
|||
};
|
||||
if let Some(attr) = attr {
|
||||
// If we are currently capturing tokens (i.e. we are within a call to
|
||||
// `Parser::collect_tokens_trailing_tokens`) record the token positions of this
|
||||
// inner attribute, for possible later processing in a `LazyAttrTokenStream`.
|
||||
// `Parser::collect_tokens`) record the token positions of this inner attribute,
|
||||
// for possible later processing in a `LazyAttrTokenStream`.
|
||||
if let Capturing::Yes = self.capture_state.capturing {
|
||||
let end_pos = self.num_bump_calls;
|
||||
let parser_range = ParserRange(start_pos..end_pos);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue