Rename RefTokenTreeCursor
.
Because `TokenStreamIter` is a much better name for a `TokenStream` iterator. Also rename the `TokenStream::trees` method as `TokenStream::iter`, and some local variables.
This commit is contained in:
parent
3575e7943b
commit
809975c94a
17 changed files with 117 additions and 119 deletions
|
@ -366,12 +366,12 @@ impl MetaItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tokens<'a, I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
|
fn from_tokens<'a, I>(iter: &mut iter::Peekable<I>) -> Option<MetaItem>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = &'a TokenTree>,
|
I: Iterator<Item = &'a TokenTree>,
|
||||||
{
|
{
|
||||||
// FIXME: Share code with `parse_path`.
|
// FIXME: Share code with `parse_path`.
|
||||||
let tt = tokens.next().map(|tt| TokenTree::uninterpolate(tt));
|
let tt = iter.next().map(|tt| TokenTree::uninterpolate(tt));
|
||||||
let path = match tt.as_deref() {
|
let path = match tt.as_deref() {
|
||||||
Some(&TokenTree::Token(
|
Some(&TokenTree::Token(
|
||||||
Token { kind: ref kind @ (token::Ident(..) | token::PathSep), span },
|
Token { kind: ref kind @ (token::Ident(..) | token::PathSep), span },
|
||||||
|
@ -379,9 +379,9 @@ impl MetaItem {
|
||||||
)) => 'arm: {
|
)) => 'arm: {
|
||||||
let mut segments = if let &token::Ident(name, _) = kind {
|
let mut segments = if let &token::Ident(name, _) = kind {
|
||||||
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
|
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
|
||||||
tokens.peek()
|
iter.peek()
|
||||||
{
|
{
|
||||||
tokens.next();
|
iter.next();
|
||||||
thin_vec![PathSegment::from_ident(Ident::new(name, span))]
|
thin_vec![PathSegment::from_ident(Ident::new(name, span))]
|
||||||
} else {
|
} else {
|
||||||
break 'arm Path::from_ident(Ident::new(name, span));
|
break 'arm Path::from_ident(Ident::new(name, span));
|
||||||
|
@ -391,16 +391,16 @@ impl MetaItem {
|
||||||
};
|
};
|
||||||
loop {
|
loop {
|
||||||
if let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
|
if let Some(&TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
|
||||||
tokens.next().map(|tt| TokenTree::uninterpolate(tt)).as_deref()
|
iter.next().map(|tt| TokenTree::uninterpolate(tt)).as_deref()
|
||||||
{
|
{
|
||||||
segments.push(PathSegment::from_ident(Ident::new(name, span)));
|
segments.push(PathSegment::from_ident(Ident::new(name, span)));
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
|
if let Some(TokenTree::Token(Token { kind: token::PathSep, .. }, _)) =
|
||||||
tokens.peek()
|
iter.peek()
|
||||||
{
|
{
|
||||||
tokens.next();
|
iter.next();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -421,8 +421,8 @@ impl MetaItem {
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
|
let list_closing_paren_pos = iter.peek().map(|tt| tt.span().hi());
|
||||||
let kind = MetaItemKind::from_tokens(tokens)?;
|
let kind = MetaItemKind::from_tokens(iter)?;
|
||||||
let hi = match &kind {
|
let hi = match &kind {
|
||||||
MetaItemKind::NameValue(lit) => lit.span.hi(),
|
MetaItemKind::NameValue(lit) => lit.span.hi(),
|
||||||
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
|
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
|
||||||
|
@ -439,12 +439,12 @@ impl MetaItem {
|
||||||
impl MetaItemKind {
|
impl MetaItemKind {
|
||||||
// public because it can be called in the hir
|
// public because it can be called in the hir
|
||||||
pub fn list_from_tokens(tokens: TokenStream) -> Option<ThinVec<MetaItemInner>> {
|
pub fn list_from_tokens(tokens: TokenStream) -> Option<ThinVec<MetaItemInner>> {
|
||||||
let mut tokens = tokens.trees().peekable();
|
let mut iter = tokens.iter().peekable();
|
||||||
let mut result = ThinVec::new();
|
let mut result = ThinVec::new();
|
||||||
while tokens.peek().is_some() {
|
while iter.peek().is_some() {
|
||||||
let item = MetaItemInner::from_tokens(&mut tokens)?;
|
let item = MetaItemInner::from_tokens(&mut iter)?;
|
||||||
result.push(item);
|
result.push(item);
|
||||||
match tokens.next() {
|
match iter.next() {
|
||||||
None | Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) => {}
|
None | Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) => {}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
|
@ -453,11 +453,11 @@ impl MetaItemKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name_value_from_tokens<'a>(
|
fn name_value_from_tokens<'a>(
|
||||||
tokens: &mut impl Iterator<Item = &'a TokenTree>,
|
iter: &mut impl Iterator<Item = &'a TokenTree>,
|
||||||
) -> Option<MetaItemKind> {
|
) -> Option<MetaItemKind> {
|
||||||
match tokens.next() {
|
match iter.next() {
|
||||||
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
|
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
|
||||||
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
|
MetaItemKind::name_value_from_tokens(&mut inner_tokens.iter())
|
||||||
}
|
}
|
||||||
Some(TokenTree::Token(token, _)) => {
|
Some(TokenTree::Token(token, _)) => {
|
||||||
MetaItemLit::from_token(token).map(MetaItemKind::NameValue)
|
MetaItemLit::from_token(token).map(MetaItemKind::NameValue)
|
||||||
|
@ -467,18 +467,18 @@ impl MetaItemKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tokens<'a>(
|
fn from_tokens<'a>(
|
||||||
tokens: &mut iter::Peekable<impl Iterator<Item = &'a TokenTree>>,
|
iter: &mut iter::Peekable<impl Iterator<Item = &'a TokenTree>>,
|
||||||
) -> Option<MetaItemKind> {
|
) -> Option<MetaItemKind> {
|
||||||
match tokens.peek() {
|
match iter.peek() {
|
||||||
Some(TokenTree::Delimited(.., Delimiter::Parenthesis, inner_tokens)) => {
|
Some(TokenTree::Delimited(.., Delimiter::Parenthesis, inner_tokens)) => {
|
||||||
let inner_tokens = inner_tokens.clone();
|
let inner_tokens = inner_tokens.clone();
|
||||||
tokens.next();
|
iter.next();
|
||||||
MetaItemKind::list_from_tokens(inner_tokens).map(MetaItemKind::List)
|
MetaItemKind::list_from_tokens(inner_tokens).map(MetaItemKind::List)
|
||||||
}
|
}
|
||||||
Some(TokenTree::Delimited(..)) => None,
|
Some(TokenTree::Delimited(..)) => None,
|
||||||
Some(TokenTree::Token(Token { kind: token::Eq, .. }, _)) => {
|
Some(TokenTree::Token(Token { kind: token::Eq, .. }, _)) => {
|
||||||
tokens.next();
|
iter.next();
|
||||||
MetaItemKind::name_value_from_tokens(tokens)
|
MetaItemKind::name_value_from_tokens(iter)
|
||||||
}
|
}
|
||||||
_ => Some(MetaItemKind::Word),
|
_ => Some(MetaItemKind::Word),
|
||||||
}
|
}
|
||||||
|
@ -594,22 +594,22 @@ impl MetaItemInner {
|
||||||
self.meta_item().is_some()
|
self.meta_item().is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tokens<'a, I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItemInner>
|
fn from_tokens<'a, I>(iter: &mut iter::Peekable<I>) -> Option<MetaItemInner>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = &'a TokenTree>,
|
I: Iterator<Item = &'a TokenTree>,
|
||||||
{
|
{
|
||||||
match tokens.peek() {
|
match iter.peek() {
|
||||||
Some(TokenTree::Token(token, _)) if let Some(lit) = MetaItemLit::from_token(token) => {
|
Some(TokenTree::Token(token, _)) if let Some(lit) = MetaItemLit::from_token(token) => {
|
||||||
tokens.next();
|
iter.next();
|
||||||
return Some(MetaItemInner::Lit(lit));
|
return Some(MetaItemInner::Lit(lit));
|
||||||
}
|
}
|
||||||
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
|
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
|
||||||
tokens.next();
|
iter.next();
|
||||||
return MetaItemInner::from_tokens(&mut inner_tokens.trees().peekable());
|
return MetaItemInner::from_tokens(&mut inner_tokens.iter().peekable());
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
MetaItem::from_tokens(tokens).map(MetaItemInner::MetaItem)
|
MetaItem::from_tokens(iter).map(MetaItemInner::MetaItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -99,7 +99,7 @@ where
|
||||||
CTX: crate::HashStableContext,
|
CTX: crate::HashStableContext,
|
||||||
{
|
{
|
||||||
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
||||||
for sub_tt in self.trees() {
|
for sub_tt in self.iter() {
|
||||||
sub_tt.hash_stable(hcx, hasher);
|
sub_tt.hash_stable(hcx, hasher);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -406,7 +406,7 @@ impl Eq for TokenStream {}
|
||||||
|
|
||||||
impl PartialEq<TokenStream> for TokenStream {
|
impl PartialEq<TokenStream> for TokenStream {
|
||||||
fn eq(&self, other: &TokenStream) -> bool {
|
fn eq(&self, other: &TokenStream) -> bool {
|
||||||
self.trees().eq(other.trees())
|
self.iter().eq(other.iter())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,8 +423,8 @@ impl TokenStream {
|
||||||
self.0.len()
|
self.0.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trees(&self) -> RefTokenTreeCursor<'_> {
|
pub fn iter(&self) -> TokenStreamIter<'_> {
|
||||||
RefTokenTreeCursor::new(self)
|
TokenStreamIter::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_trees(self) -> TokenTreeCursor {
|
pub fn into_trees(self) -> TokenTreeCursor {
|
||||||
|
@ -433,14 +433,14 @@ impl TokenStream {
|
||||||
|
|
||||||
/// Compares two `TokenStream`s, checking equality without regarding span information.
|
/// Compares two `TokenStream`s, checking equality without regarding span information.
|
||||||
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
|
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
|
||||||
let mut t1 = self.trees();
|
let mut iter1 = self.iter();
|
||||||
let mut t2 = other.trees();
|
let mut iter2 = other.iter();
|
||||||
for (t1, t2) in iter::zip(&mut t1, &mut t2) {
|
for (tt1, tt2) in iter::zip(&mut iter1, &mut iter2) {
|
||||||
if !t1.eq_unspanned(t2) {
|
if !tt1.eq_unspanned(tt2) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t1.next().is_none() && t2.next().is_none()
|
iter1.next().is_none() && iter2.next().is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a token stream containing a single token with alone spacing. The
|
/// Create a token stream containing a single token with alone spacing. The
|
||||||
|
@ -509,7 +509,7 @@ impl TokenStream {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn flattened(&self) -> TokenStream {
|
pub fn flattened(&self) -> TokenStream {
|
||||||
fn can_skip(stream: &TokenStream) -> bool {
|
fn can_skip(stream: &TokenStream) -> bool {
|
||||||
stream.trees().all(|tree| match tree {
|
stream.iter().all(|tree| match tree {
|
||||||
TokenTree::Token(token, _) => !matches!(
|
TokenTree::Token(token, _) => !matches!(
|
||||||
token.kind,
|
token.kind,
|
||||||
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
|
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
|
||||||
|
@ -522,7 +522,7 @@ impl TokenStream {
|
||||||
return self.clone();
|
return self.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.trees().map(|tree| TokenStream::flatten_token_tree(tree)).collect()
|
self.iter().map(|tree| TokenStream::flatten_token_tree(tree)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// If `vec` is not empty, try to glue `tt` onto its last token. The return
|
// If `vec` is not empty, try to glue `tt` onto its last token. The return
|
||||||
|
@ -665,17 +665,15 @@ impl TokenStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// By-reference iterator over a [`TokenStream`], that produces `&TokenTree`
|
|
||||||
/// items.
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RefTokenTreeCursor<'t> {
|
pub struct TokenStreamIter<'t> {
|
||||||
stream: &'t TokenStream,
|
stream: &'t TokenStream,
|
||||||
index: usize,
|
index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> RefTokenTreeCursor<'t> {
|
impl<'t> TokenStreamIter<'t> {
|
||||||
fn new(stream: &'t TokenStream) -> Self {
|
fn new(stream: &'t TokenStream) -> Self {
|
||||||
RefTokenTreeCursor { stream, index: 0 }
|
TokenStreamIter { stream, index: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn peek(&self) -> Option<&TokenTree> {
|
pub fn peek(&self) -> Option<&TokenTree> {
|
||||||
|
@ -683,7 +681,7 @@ impl<'t> RefTokenTreeCursor<'t> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> Iterator for RefTokenTreeCursor<'t> {
|
impl<'t> Iterator for TokenStreamIter<'t> {
|
||||||
type Item = &'t TokenTree;
|
type Item = &'t TokenTree;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<&'t TokenTree> {
|
fn next(&mut self) -> Option<&'t TokenTree> {
|
||||||
|
@ -701,7 +699,7 @@ impl<'t> Iterator for RefTokenTreeCursor<'t> {
|
||||||
/// return `&T` from `next`; the need for an explicit lifetime in the `Item`
|
/// return `&T` from `next`; the need for an explicit lifetime in the `Item`
|
||||||
/// associated type gets in the way. Instead, use `next_ref` (which doesn't
|
/// associated type gets in the way. Instead, use `next_ref` (which doesn't
|
||||||
/// involve associated types) for getting individual elements, or
|
/// involve associated types) for getting individual elements, or
|
||||||
/// `RefTokenTreeCursor` if you really want an `Iterator`, e.g. in a `for`
|
/// `TokenStreamIter` if you really want an `Iterator`, e.g. in a `for`
|
||||||
/// loop.
|
/// loop.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TokenTreeCursor {
|
pub struct TokenTreeCursor {
|
||||||
|
|
|
@ -725,7 +725,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
|
||||||
// E.g. we have seen cases where a proc macro can handle `a :: b` but not
|
// E.g. we have seen cases where a proc macro can handle `a :: b` but not
|
||||||
// `a::b`. See #117433 for some examples.
|
// `a::b`. See #117433 for some examples.
|
||||||
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
|
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
|
||||||
let mut iter = tts.trees().peekable();
|
let mut iter = tts.iter().peekable();
|
||||||
while let Some(tt) = iter.next() {
|
while let Some(tt) = iter.next() {
|
||||||
let spacing = self.print_tt(tt, convert_dollar_crate);
|
let spacing = self.print_tt(tt, convert_dollar_crate);
|
||||||
if let Some(next) = iter.peek() {
|
if let Some(next) = iter.peek() {
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub(crate) fn expand_concat_idents<'cx>(
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut res_str = String::new();
|
let mut res_str = String::new();
|
||||||
for (i, e) in tts.trees().enumerate() {
|
for (i, e) in tts.iter().enumerate() {
|
||||||
if i & 1 == 1 {
|
if i & 1 == 1 {
|
||||||
match e {
|
match e {
|
||||||
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
|
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
|
||||||
|
|
|
@ -10,9 +10,9 @@ pub(crate) fn expand_trace_macros(
|
||||||
sp: Span,
|
sp: Span,
|
||||||
tt: TokenStream,
|
tt: TokenStream,
|
||||||
) -> MacroExpanderResult<'static> {
|
) -> MacroExpanderResult<'static> {
|
||||||
let mut cursor = tt.trees();
|
let mut iter = tt.iter();
|
||||||
let mut err = false;
|
let mut err = false;
|
||||||
let value = match &cursor.next() {
|
let value = match iter.next() {
|
||||||
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::True) => true,
|
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::True) => true,
|
||||||
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::False) => false,
|
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::False) => false,
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -20,7 +20,7 @@ pub(crate) fn expand_trace_macros(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
err |= cursor.next().is_some();
|
err |= iter.next().is_some();
|
||||||
if err {
|
if err {
|
||||||
cx.dcx().emit_err(errors::TraceMacros { span: sp });
|
cx.dcx().emit_err(errors::TraceMacros { span: sp });
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, Token, TokenKind};
|
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, Token, TokenKind};
|
||||||
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
|
use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree};
|
||||||
use rustc_ast::{LitIntType, LitKind};
|
use rustc_ast::{LitIntType, LitKind};
|
||||||
use rustc_ast_pretty::pprust;
|
use rustc_ast_pretty::pprust;
|
||||||
use rustc_errors::{Applicability, PResult};
|
use rustc_errors::{Applicability, PResult};
|
||||||
|
@ -39,14 +39,14 @@ impl MetaVarExpr {
|
||||||
outer_span: Span,
|
outer_span: Span,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
) -> PResult<'psess, MetaVarExpr> {
|
) -> PResult<'psess, MetaVarExpr> {
|
||||||
let mut tts = input.trees();
|
let mut iter = input.iter();
|
||||||
let ident = parse_ident(&mut tts, psess, outer_span)?;
|
let ident = parse_ident(&mut iter, psess, outer_span)?;
|
||||||
let Some(TokenTree::Delimited(.., Delimiter::Parenthesis, args)) = tts.next() else {
|
let Some(TokenTree::Delimited(.., Delimiter::Parenthesis, args)) = iter.next() else {
|
||||||
let msg = "meta-variable expression parameter must be wrapped in parentheses";
|
let msg = "meta-variable expression parameter must be wrapped in parentheses";
|
||||||
return Err(psess.dcx().struct_span_err(ident.span, msg));
|
return Err(psess.dcx().struct_span_err(ident.span, msg));
|
||||||
};
|
};
|
||||||
check_trailing_token(&mut tts, psess)?;
|
check_trailing_token(&mut iter, psess)?;
|
||||||
let mut iter = args.trees();
|
let mut iter = args.iter();
|
||||||
let rslt = match ident.as_str() {
|
let rslt = match ident.as_str() {
|
||||||
"concat" => {
|
"concat" => {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
@ -143,7 +143,7 @@ pub(crate) enum MetaVarExprConcatElem {
|
||||||
|
|
||||||
// Checks if there are any remaining tokens. For example, `${ignore(ident ... a b c ...)}`
|
// Checks if there are any remaining tokens. For example, `${ignore(ident ... a b c ...)}`
|
||||||
fn check_trailing_token<'psess>(
|
fn check_trailing_token<'psess>(
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
) -> PResult<'psess, ()> {
|
) -> PResult<'psess, ()> {
|
||||||
if let Some(tt) = iter.next() {
|
if let Some(tt) = iter.next() {
|
||||||
|
@ -159,7 +159,7 @@ fn check_trailing_token<'psess>(
|
||||||
|
|
||||||
/// Parse a meta-variable `count` expression: `count(ident[, depth])`
|
/// Parse a meta-variable `count` expression: `count(ident[, depth])`
|
||||||
fn parse_count<'psess>(
|
fn parse_count<'psess>(
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> PResult<'psess, MetaVarExpr> {
|
) -> PResult<'psess, MetaVarExpr> {
|
||||||
|
@ -181,7 +181,7 @@ fn parse_count<'psess>(
|
||||||
|
|
||||||
/// Parses the depth used by index(depth) and len(depth).
|
/// Parses the depth used by index(depth) and len(depth).
|
||||||
fn parse_depth<'psess>(
|
fn parse_depth<'psess>(
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> PResult<'psess, usize> {
|
) -> PResult<'psess, usize> {
|
||||||
|
@ -204,7 +204,7 @@ fn parse_depth<'psess>(
|
||||||
|
|
||||||
/// Parses an generic ident
|
/// Parses an generic ident
|
||||||
fn parse_ident<'psess>(
|
fn parse_ident<'psess>(
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
fallback_span: Span,
|
fallback_span: Span,
|
||||||
) -> PResult<'psess, Ident> {
|
) -> PResult<'psess, Ident> {
|
||||||
|
@ -236,7 +236,7 @@ fn parse_ident_from_token<'psess>(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_token<'psess, 't>(
|
fn parse_token<'psess, 't>(
|
||||||
iter: &mut RefTokenTreeCursor<'t>,
|
iter: &mut TokenStreamIter<'t>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
fallback_span: Span,
|
fallback_span: Span,
|
||||||
) -> PResult<'psess, &'t Token> {
|
) -> PResult<'psess, &'t Token> {
|
||||||
|
@ -251,7 +251,7 @@ fn parse_token<'psess, 't>(
|
||||||
|
|
||||||
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
|
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
|
||||||
/// iterator is not modified and the result is `false`.
|
/// iterator is not modified and the result is `false`.
|
||||||
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
|
fn try_eat_comma(iter: &mut TokenStreamIter<'_>) -> bool {
|
||||||
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.peek() {
|
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.peek() {
|
||||||
let _ = iter.next();
|
let _ = iter.next();
|
||||||
return true;
|
return true;
|
||||||
|
@ -261,7 +261,7 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
|
||||||
|
|
||||||
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
|
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
|
||||||
/// iterator is not modified and the result is `false`.
|
/// iterator is not modified and the result is `false`.
|
||||||
fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
|
fn try_eat_dollar(iter: &mut TokenStreamIter<'_>) -> bool {
|
||||||
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.peek() {
|
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.peek() {
|
||||||
let _ = iter.next();
|
let _ = iter.next();
|
||||||
return true;
|
return true;
|
||||||
|
@ -271,7 +271,7 @@ fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
|
||||||
|
|
||||||
/// Expects that the next item is a dollar sign.
|
/// Expects that the next item is a dollar sign.
|
||||||
fn eat_dollar<'psess>(
|
fn eat_dollar<'psess>(
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> PResult<'psess, ()> {
|
) -> PResult<'psess, ()> {
|
||||||
|
|
|
@ -49,25 +49,25 @@ pub(super) fn parse(
|
||||||
|
|
||||||
// For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
|
// For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
|
||||||
// additional trees if need be.
|
// additional trees if need be.
|
||||||
let mut trees = input.trees().peekable();
|
let mut iter = input.iter().peekable();
|
||||||
while let Some(tree) = trees.next() {
|
while let Some(tree) = iter.next() {
|
||||||
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
|
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
|
||||||
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
|
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
|
||||||
let tree = parse_tree(tree, &mut trees, parsing_patterns, sess, node_id, features, edition);
|
let tree = parse_tree(tree, &mut iter, parsing_patterns, sess, node_id, features, edition);
|
||||||
match tree {
|
match tree {
|
||||||
TokenTree::MetaVar(start_sp, ident) if parsing_patterns => {
|
TokenTree::MetaVar(start_sp, ident) if parsing_patterns => {
|
||||||
// Not consuming the next token immediately, as it may not be a colon
|
// Not consuming the next token immediately, as it may not be a colon
|
||||||
let span = match trees.peek() {
|
let span = match iter.peek() {
|
||||||
Some(&tokenstream::TokenTree::Token(
|
Some(&tokenstream::TokenTree::Token(
|
||||||
Token { kind: token::Colon, span: colon_span },
|
Token { kind: token::Colon, span: colon_span },
|
||||||
_,
|
_,
|
||||||
)) => {
|
)) => {
|
||||||
// Consume the colon first
|
// Consume the colon first
|
||||||
trees.next();
|
iter.next();
|
||||||
|
|
||||||
// It's ok to consume the next tree no matter how,
|
// It's ok to consume the next tree no matter how,
|
||||||
// since if it's not a token then it will be an invalid declaration.
|
// since if it's not a token then it will be an invalid declaration.
|
||||||
match trees.next() {
|
match iter.next() {
|
||||||
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
|
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
|
||||||
Some((fragment, _)) => {
|
Some((fragment, _)) => {
|
||||||
let span = token.span.with_lo(start_sp.lo());
|
let span = token.span.with_lo(start_sp.lo());
|
||||||
|
@ -143,14 +143,14 @@ fn maybe_emit_macro_metavar_expr_concat_feature(features: &Features, sess: &Sess
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// - `tree`: the tree we wish to convert.
|
/// - `tree`: the tree we wish to convert.
|
||||||
/// - `outer_trees`: an iterator over trees. We may need to read more tokens from it in order to finish
|
/// - `outer_iter`: an iterator over trees. We may need to read more tokens from it in order to finish
|
||||||
/// converting `tree`
|
/// converting `tree`
|
||||||
/// - `parsing_patterns`: same as [parse].
|
/// - `parsing_patterns`: same as [parse].
|
||||||
/// - `sess`: the parsing session. Any errors will be emitted to this session.
|
/// - `sess`: the parsing session. Any errors will be emitted to this session.
|
||||||
/// - `features`: language features so we can do feature gating.
|
/// - `features`: language features so we can do feature gating.
|
||||||
fn parse_tree<'a>(
|
fn parse_tree<'a>(
|
||||||
tree: &'a tokenstream::TokenTree,
|
tree: &'a tokenstream::TokenTree,
|
||||||
outer_trees: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
outer_iter: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
||||||
parsing_patterns: bool,
|
parsing_patterns: bool,
|
||||||
sess: &Session,
|
sess: &Session,
|
||||||
node_id: NodeId,
|
node_id: NodeId,
|
||||||
|
@ -163,14 +163,14 @@ fn parse_tree<'a>(
|
||||||
&tokenstream::TokenTree::Token(Token { kind: token::Dollar, span: dollar_span }, _) => {
|
&tokenstream::TokenTree::Token(Token { kind: token::Dollar, span: dollar_span }, _) => {
|
||||||
// FIXME: Handle `Invisible`-delimited groups in a more systematic way
|
// FIXME: Handle `Invisible`-delimited groups in a more systematic way
|
||||||
// during parsing.
|
// during parsing.
|
||||||
let mut next = outer_trees.next();
|
let mut next = outer_iter.next();
|
||||||
let mut trees: Box<dyn Iterator<Item = &tokenstream::TokenTree>>;
|
let mut iter: Box<dyn Iterator<Item = &tokenstream::TokenTree>>;
|
||||||
match next {
|
match next {
|
||||||
Some(tokenstream::TokenTree::Delimited(.., delim, tts)) if delim.skip() => {
|
Some(tokenstream::TokenTree::Delimited(.., delim, tts)) if delim.skip() => {
|
||||||
trees = Box::new(tts.trees());
|
iter = Box::new(tts.iter());
|
||||||
next = trees.next();
|
next = iter.next();
|
||||||
}
|
}
|
||||||
_ => trees = Box::new(outer_trees),
|
_ => iter = Box::new(outer_iter),
|
||||||
}
|
}
|
||||||
|
|
||||||
match next {
|
match next {
|
||||||
|
@ -230,7 +230,7 @@ fn parse_tree<'a>(
|
||||||
let sequence = parse(tts, parsing_patterns, sess, node_id, features, edition);
|
let sequence = parse(tts, parsing_patterns, sess, node_id, features, edition);
|
||||||
// Get the Kleene operator and optional separator
|
// Get the Kleene operator and optional separator
|
||||||
let (separator, kleene) =
|
let (separator, kleene) =
|
||||||
parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess);
|
parse_sep_and_kleene_op(&mut iter, delim_span.entire(), sess);
|
||||||
// Count the number of captured "names" (i.e., named metavars)
|
// Count the number of captured "names" (i.e., named metavars)
|
||||||
let num_captures =
|
let num_captures =
|
||||||
if parsing_patterns { count_metavar_decls(&sequence) } else { 0 };
|
if parsing_patterns { count_metavar_decls(&sequence) } else { 0 };
|
||||||
|
@ -314,10 +314,10 @@ fn kleene_op(token: &Token) -> Option<KleeneOp> {
|
||||||
/// - Ok(Err(tok, span)) if the next token tree is a token but not a KleeneOp
|
/// - Ok(Err(tok, span)) if the next token tree is a token but not a KleeneOp
|
||||||
/// - Err(span) if the next token tree is not a token
|
/// - Err(span) if the next token tree is not a token
|
||||||
fn parse_kleene_op<'a>(
|
fn parse_kleene_op<'a>(
|
||||||
input: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
iter: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<Result<(KleeneOp, Span), Token>, Span> {
|
) -> Result<Result<(KleeneOp, Span), Token>, Span> {
|
||||||
match input.next() {
|
match iter.next() {
|
||||||
Some(tokenstream::TokenTree::Token(token, _)) => match kleene_op(token) {
|
Some(tokenstream::TokenTree::Token(token, _)) => match kleene_op(token) {
|
||||||
Some(op) => Ok(Ok((op, token.span))),
|
Some(op) => Ok(Ok((op, token.span))),
|
||||||
None => Ok(Err(token.clone())),
|
None => Ok(Err(token.clone())),
|
||||||
|
@ -334,22 +334,22 @@ fn parse_kleene_op<'a>(
|
||||||
/// itself. Note that here we are parsing the _macro_ itself, rather than trying to match some
|
/// itself. Note that here we are parsing the _macro_ itself, rather than trying to match some
|
||||||
/// stream of tokens in an invocation of a macro.
|
/// stream of tokens in an invocation of a macro.
|
||||||
///
|
///
|
||||||
/// This function will take some input iterator `input` corresponding to `span` and a parsing
|
/// This function will take some input iterator `iter` corresponding to `span` and a parsing
|
||||||
/// session `sess`. If the next one (or possibly two) tokens in `input` correspond to a Kleene
|
/// session `sess`. If the next one (or possibly two) tokens in `iter` correspond to a Kleene
|
||||||
/// operator and separator, then a tuple with `(separator, KleeneOp)` is returned. Otherwise, an
|
/// operator and separator, then a tuple with `(separator, KleeneOp)` is returned. Otherwise, an
|
||||||
/// error with the appropriate span is emitted to `sess` and a dummy value is returned.
|
/// error with the appropriate span is emitted to `sess` and a dummy value is returned.
|
||||||
fn parse_sep_and_kleene_op<'a>(
|
fn parse_sep_and_kleene_op<'a>(
|
||||||
input: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
iter: &mut impl Iterator<Item = &'a tokenstream::TokenTree>,
|
||||||
span: Span,
|
span: Span,
|
||||||
sess: &Session,
|
sess: &Session,
|
||||||
) -> (Option<Token>, KleeneToken) {
|
) -> (Option<Token>, KleeneToken) {
|
||||||
// We basically look at two token trees here, denoted as #1 and #2 below
|
// We basically look at two token trees here, denoted as #1 and #2 below
|
||||||
let span = match parse_kleene_op(input, span) {
|
let span = match parse_kleene_op(iter, span) {
|
||||||
// #1 is a `?`, `+`, or `*` KleeneOp
|
// #1 is a `?`, `+`, or `*` KleeneOp
|
||||||
Ok(Ok((op, span))) => return (None, KleeneToken::new(op, span)),
|
Ok(Ok((op, span))) => return (None, KleeneToken::new(op, span)),
|
||||||
|
|
||||||
// #1 is a separator followed by #2, a KleeneOp
|
// #1 is a separator followed by #2, a KleeneOp
|
||||||
Ok(Err(token)) => match parse_kleene_op(input, token.span) {
|
Ok(Err(token)) => match parse_kleene_op(iter, token.span) {
|
||||||
// #2 is the `?` Kleene op, which does not take a separator (error)
|
// #2 is the `?` Kleene op, which does not take a separator (error)
|
||||||
Ok(Ok((KleeneOp::ZeroOrOne, span))) => {
|
Ok(Ok((KleeneOp::ZeroOrOne, span))) => {
|
||||||
// Error!
|
// Error!
|
||||||
|
|
|
@ -112,9 +112,9 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
|
||||||
// Estimate the capacity as `stream.len()` rounded up to the next power
|
// Estimate the capacity as `stream.len()` rounded up to the next power
|
||||||
// of two to limit the number of required reallocations.
|
// of two to limit the number of required reallocations.
|
||||||
let mut trees = Vec::with_capacity(stream.len().next_power_of_two());
|
let mut trees = Vec::with_capacity(stream.len().next_power_of_two());
|
||||||
let mut cursor = stream.trees();
|
let mut iter = stream.iter();
|
||||||
|
|
||||||
while let Some(tree) = cursor.next() {
|
while let Some(tree) = iter.next() {
|
||||||
let (Token { kind, span }, joint) = match tree.clone() {
|
let (Token { kind, span }, joint) = match tree.clone() {
|
||||||
tokenstream::TokenTree::Delimited(span, _, delim, tts) => {
|
tokenstream::TokenTree::Delimited(span, _, delim, tts) => {
|
||||||
let delimiter = pm::Delimiter::from_internal(delim);
|
let delimiter = pm::Delimiter::from_internal(delim);
|
||||||
|
|
|
@ -1829,7 +1829,7 @@ impl KeywordIdents {
|
||||||
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: &TokenStream) {
|
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: &TokenStream) {
|
||||||
// Check if the preceding token is `$`, because we want to allow `$async`, etc.
|
// Check if the preceding token is `$`, because we want to allow `$async`, etc.
|
||||||
let mut prev_dollar = false;
|
let mut prev_dollar = false;
|
||||||
for tt in tokens.trees() {
|
for tt in tokens.iter() {
|
||||||
match tt {
|
match tt {
|
||||||
// Only report non-raw idents.
|
// Only report non-raw idents.
|
||||||
TokenTree::Token(token, _) => {
|
TokenTree::Token(token, _) => {
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl Expr2024 {
|
||||||
let mut prev_colon = false;
|
let mut prev_colon = false;
|
||||||
let mut prev_identifier = false;
|
let mut prev_identifier = false;
|
||||||
let mut prev_dollar = false;
|
let mut prev_dollar = false;
|
||||||
for tt in tokens.trees() {
|
for tt in tokens.iter() {
|
||||||
debug!(
|
debug!(
|
||||||
"check_tokens: {:?} - colon {prev_dollar} - ident {prev_identifier} - colon {prev_colon}",
|
"check_tokens: {:?} - colon {prev_dollar} - ident {prev_identifier} - colon {prev_colon}",
|
||||||
tt
|
tt
|
||||||
|
|
|
@ -2285,7 +2285,7 @@ fn bad_path_expr_1() {
|
||||||
fn string_to_tts_macro() {
|
fn string_to_tts_macro() {
|
||||||
create_default_session_globals_then(|| {
|
create_default_session_globals_then(|| {
|
||||||
let stream = string_to_stream("macro_rules! zip (($a)=>($a))".to_string());
|
let stream = string_to_stream("macro_rules! zip (($a)=>($a))".to_string());
|
||||||
let tts = &stream.trees().collect::<Vec<_>>()[..];
|
let tts = &stream.iter().collect::<Vec<_>>()[..];
|
||||||
|
|
||||||
match tts {
|
match tts {
|
||||||
[
|
[
|
||||||
|
@ -2297,14 +2297,14 @@ fn string_to_tts_macro() {
|
||||||
TokenTree::Token(Token { kind: token::Ident(name_zip, IdentIsRaw::No), .. }, _),
|
TokenTree::Token(Token { kind: token::Ident(name_zip, IdentIsRaw::No), .. }, _),
|
||||||
TokenTree::Delimited(.., macro_delim, macro_tts),
|
TokenTree::Delimited(.., macro_delim, macro_tts),
|
||||||
] if name_macro_rules == &kw::MacroRules && name_zip.as_str() == "zip" => {
|
] if name_macro_rules == &kw::MacroRules && name_zip.as_str() == "zip" => {
|
||||||
let tts = ¯o_tts.trees().collect::<Vec<_>>();
|
let tts = ¯o_tts.iter().collect::<Vec<_>>();
|
||||||
match &tts[..] {
|
match &tts[..] {
|
||||||
[
|
[
|
||||||
TokenTree::Delimited(.., first_delim, first_tts),
|
TokenTree::Delimited(.., first_delim, first_tts),
|
||||||
TokenTree::Token(Token { kind: token::FatArrow, .. }, _),
|
TokenTree::Token(Token { kind: token::FatArrow, .. }, _),
|
||||||
TokenTree::Delimited(.., second_delim, second_tts),
|
TokenTree::Delimited(.., second_delim, second_tts),
|
||||||
] if macro_delim == &Delimiter::Parenthesis => {
|
] if macro_delim == &Delimiter::Parenthesis => {
|
||||||
let tts = &first_tts.trees().collect::<Vec<_>>();
|
let tts = &first_tts.iter().collect::<Vec<_>>();
|
||||||
match &tts[..] {
|
match &tts[..] {
|
||||||
[
|
[
|
||||||
TokenTree::Token(Token { kind: token::Dollar, .. }, _),
|
TokenTree::Token(Token { kind: token::Dollar, .. }, _),
|
||||||
|
@ -2316,7 +2316,7 @@ fn string_to_tts_macro() {
|
||||||
}
|
}
|
||||||
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
|
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
|
||||||
}
|
}
|
||||||
let tts = &second_tts.trees().collect::<Vec<_>>();
|
let tts = &second_tts.iter().collect::<Vec<_>>();
|
||||||
match &tts[..] {
|
match &tts[..] {
|
||||||
[
|
[
|
||||||
TokenTree::Token(Token { kind: token::Dollar, .. }, _),
|
TokenTree::Token(Token { kind: token::Dollar, .. }, _),
|
||||||
|
@ -2544,7 +2544,7 @@ fn ttdelim_span() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let ast::ExprKind::MacCall(mac) = &expr.kind else { panic!("not a macro") };
|
let ast::ExprKind::MacCall(mac) = &expr.kind else { panic!("not a macro") };
|
||||||
let span = mac.args.tokens.trees().last().unwrap().span();
|
let span = mac.args.tokens.iter().last().unwrap().span();
|
||||||
|
|
||||||
match psess.source_map().span_to_snippet(span) {
|
match psess.source_map().span_to_snippet(span) {
|
||||||
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
||||||
|
|
|
@ -23,8 +23,8 @@ fn test_concat() {
|
||||||
let mut eq_res = TokenStream::default();
|
let mut eq_res = TokenStream::default();
|
||||||
eq_res.push_stream(test_fst);
|
eq_res.push_stream(test_fst);
|
||||||
eq_res.push_stream(test_snd);
|
eq_res.push_stream(test_snd);
|
||||||
assert_eq!(test_res.trees().count(), 5);
|
assert_eq!(test_res.iter().count(), 5);
|
||||||
assert_eq!(eq_res.trees().count(), 5);
|
assert_eq!(eq_res.iter().count(), 5);
|
||||||
assert_eq!(test_res.eq_unspanned(&eq_res), true);
|
assert_eq!(test_res.eq_unspanned(&eq_res), true);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ fn test_concat() {
|
||||||
fn test_to_from_bijection() {
|
fn test_to_from_bijection() {
|
||||||
create_default_session_globals_then(|| {
|
create_default_session_globals_then(|| {
|
||||||
let test_start = string_to_ts("foo::bar(baz)");
|
let test_start = string_to_ts("foo::bar(baz)");
|
||||||
let test_end = test_start.trees().cloned().collect();
|
let test_end = test_start.iter().cloned().collect();
|
||||||
assert_eq!(test_start, test_end)
|
assert_eq!(test_start, test_end)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,6 @@ fn test_dotdotdot() {
|
||||||
stream.push_tree(TokenTree::token_joint(token::Dot, sp(1, 2)));
|
stream.push_tree(TokenTree::token_joint(token::Dot, sp(1, 2)));
|
||||||
stream.push_tree(TokenTree::token_alone(token::Dot, sp(2, 3)));
|
stream.push_tree(TokenTree::token_alone(token::Dot, sp(2, 3)));
|
||||||
assert!(stream.eq_unspanned(&string_to_ts("...")));
|
assert!(stream.eq_unspanned(&string_to_ts("...")));
|
||||||
assert_eq!(stream.trees().count(), 1);
|
assert_eq!(stream.iter().count(), 1);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2604,7 +2604,7 @@ fn filter_tokens_from_list(
|
||||||
) -> Vec<TokenTree> {
|
) -> Vec<TokenTree> {
|
||||||
let mut tokens = Vec::with_capacity(args_tokens.len());
|
let mut tokens = Vec::with_capacity(args_tokens.len());
|
||||||
let mut skip_next_comma = false;
|
let mut skip_next_comma = false;
|
||||||
for token in args_tokens.trees() {
|
for token in args_tokens.iter() {
|
||||||
match token {
|
match token {
|
||||||
TokenTree::Token(Token { kind: TokenKind::Comma, .. }, _) if skip_next_comma => {
|
TokenTree::Token(Token { kind: TokenKind::Comma, .. }, _) if skip_next_comma => {
|
||||||
skip_next_comma = false;
|
skip_next_comma = false;
|
||||||
|
|
|
@ -131,7 +131,7 @@ fn print_tts(printer: &mut Printer<'_>, tts: &TokenStream) {
|
||||||
use State::*;
|
use State::*;
|
||||||
|
|
||||||
let mut state = Start;
|
let mut state = Start;
|
||||||
for tt in tts.trees() {
|
for tt in tts.iter() {
|
||||||
let (needs_space, next_state) = match &tt {
|
let (needs_space, next_state) = match &tt {
|
||||||
TokenTree::Token(tt, _) => match (state, &tt.kind) {
|
TokenTree::Token(tt, _) => match (state, &tt.kind) {
|
||||||
(Dollar, token::Ident(..)) => (false, DollarIdent),
|
(Dollar, token::Ident(..)) => (false, DollarIdent),
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let AttrArgs::Delimited(args) = &normal_attr.item.args
|
if let AttrArgs::Delimited(args) = &normal_attr.item.args
|
||||||
&& let mut tt_iter = args.tokens.trees()
|
&& let mut tt_iter = args.tokens.iter()
|
||||||
&& let Some(TokenTree::Token(
|
&& let Some(TokenTree::Token(
|
||||||
Token {
|
Token {
|
||||||
kind: TokenKind::Ident(sym::expected, _),
|
kind: TokenKind::Ident(sym::expected, _),
|
||||||
|
|
|
@ -82,11 +82,11 @@ fn is_macro_export(attr: &Attribute) -> bool {
|
||||||
|
|
||||||
fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
|
fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
|
||||||
let mut prev_is_dollar = false;
|
let mut prev_is_dollar = false;
|
||||||
let mut cursor = tts.trees();
|
let mut iter = tts.iter();
|
||||||
while let Some(curr) = cursor.next() {
|
while let Some(curr) = iter.next() {
|
||||||
if !prev_is_dollar
|
if !prev_is_dollar
|
||||||
&& let Some(span) = is_crate_keyword(curr)
|
&& let Some(span) = is_crate_keyword(curr)
|
||||||
&& let Some(next) = cursor.peek()
|
&& let Some(next) = iter.peek()
|
||||||
&& is_token(next, &TokenKind::PathSep)
|
&& is_token(next, &TokenKind::PathSep)
|
||||||
{
|
{
|
||||||
return Some(span);
|
return Some(span);
|
||||||
|
|
|
@ -13,7 +13,7 @@ use std::collections::HashMap;
|
||||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||||
|
|
||||||
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
|
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
|
||||||
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
|
use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree};
|
||||||
use rustc_ast::{ast, ptr};
|
use rustc_ast::{ast, ptr};
|
||||||
use rustc_ast_pretty::pprust;
|
use rustc_ast_pretty::pprust;
|
||||||
use rustc_span::{
|
use rustc_span::{
|
||||||
|
@ -443,7 +443,7 @@ pub(crate) fn rewrite_macro_def(
|
||||||
}
|
}
|
||||||
|
|
||||||
let ts = def.body.tokens.clone();
|
let ts = def.body.tokens.clone();
|
||||||
let mut parser = MacroParser::new(ts.trees());
|
let mut parser = MacroParser::new(ts.iter());
|
||||||
let parsed_def = match parser.parse() {
|
let parsed_def = match parser.parse() {
|
||||||
Some(def) => def,
|
Some(def) => def,
|
||||||
None => return snippet,
|
None => return snippet,
|
||||||
|
@ -794,7 +794,7 @@ impl MacroArgParser {
|
||||||
self.buf.clear();
|
self.buf.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_meta_variable(&mut self, iter: &mut RefTokenTreeCursor<'_>) -> Option<()> {
|
fn add_meta_variable(&mut self, iter: &mut TokenStreamIter<'_>) -> Option<()> {
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
Some(&TokenTree::Token(
|
Some(&TokenTree::Token(
|
||||||
Token {
|
Token {
|
||||||
|
@ -826,7 +826,7 @@ impl MacroArgParser {
|
||||||
&mut self,
|
&mut self,
|
||||||
inner: Vec<ParsedMacroArg>,
|
inner: Vec<ParsedMacroArg>,
|
||||||
delim: Delimiter,
|
delim: Delimiter,
|
||||||
iter: &mut RefTokenTreeCursor<'_>,
|
iter: &mut TokenStreamIter<'_>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
|
@ -926,7 +926,7 @@ impl MacroArgParser {
|
||||||
|
|
||||||
/// Returns a collection of parsed macro def's arguments.
|
/// Returns a collection of parsed macro def's arguments.
|
||||||
fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
|
fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
|
||||||
let mut iter = tokens.trees();
|
let mut iter = tokens.iter();
|
||||||
|
|
||||||
while let Some(tok) = iter.next() {
|
while let Some(tok) = iter.next() {
|
||||||
match tok {
|
match tok {
|
||||||
|
@ -1063,7 +1063,7 @@ fn format_macro_args(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn span_for_token_stream(token_stream: &TokenStream) -> Option<Span> {
|
fn span_for_token_stream(token_stream: &TokenStream) -> Option<Span> {
|
||||||
token_stream.trees().next().map(|tt| tt.span())
|
token_stream.iter().next().map(|tt| tt.span())
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should insert a space if the next token is a:
|
// We should insert a space if the next token is a:
|
||||||
|
@ -1179,18 +1179,18 @@ pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> D
|
||||||
// A very simple parser that just parses a macros 2.0 definition into its branches.
|
// A very simple parser that just parses a macros 2.0 definition into its branches.
|
||||||
// Currently we do not attempt to parse any further than that.
|
// Currently we do not attempt to parse any further than that.
|
||||||
struct MacroParser<'a> {
|
struct MacroParser<'a> {
|
||||||
toks: RefTokenTreeCursor<'a>,
|
iter: TokenStreamIter<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MacroParser<'a> {
|
impl<'a> MacroParser<'a> {
|
||||||
const fn new(toks: RefTokenTreeCursor<'a>) -> Self {
|
const fn new(iter: TokenStreamIter<'a>) -> Self {
|
||||||
Self { toks }
|
Self { iter }
|
||||||
}
|
}
|
||||||
|
|
||||||
// (`(` ... `)` `=>` `{` ... `}`)*
|
// (`(` ... `)` `=>` `{` ... `}`)*
|
||||||
fn parse(&mut self) -> Option<Macro> {
|
fn parse(&mut self) -> Option<Macro> {
|
||||||
let mut branches = vec![];
|
let mut branches = vec![];
|
||||||
while self.toks.peek().is_some() {
|
while self.iter.peek().is_some() {
|
||||||
branches.push(self.parse_branch()?);
|
branches.push(self.parse_branch()?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1199,13 +1199,13 @@ impl<'a> MacroParser<'a> {
|
||||||
|
|
||||||
// `(` ... `)` `=>` `{` ... `}`
|
// `(` ... `)` `=>` `{` ... `}`
|
||||||
fn parse_branch(&mut self) -> Option<MacroBranch> {
|
fn parse_branch(&mut self) -> Option<MacroBranch> {
|
||||||
let tok = self.toks.next()?;
|
let tok = self.iter.next()?;
|
||||||
let (lo, args_paren_kind) = match tok {
|
let (lo, args_paren_kind) = match tok {
|
||||||
TokenTree::Token(..) => return None,
|
TokenTree::Token(..) => return None,
|
||||||
&TokenTree::Delimited(delimited_span, _, d, _) => (delimited_span.open.lo(), d),
|
&TokenTree::Delimited(delimited_span, _, d, _) => (delimited_span.open.lo(), d),
|
||||||
};
|
};
|
||||||
let args = TokenStream::new(vec![tok.clone()]);
|
let args = TokenStream::new(vec![tok.clone()]);
|
||||||
match self.toks.next()? {
|
match self.iter.next()? {
|
||||||
TokenTree::Token(
|
TokenTree::Token(
|
||||||
Token {
|
Token {
|
||||||
kind: TokenKind::FatArrow,
|
kind: TokenKind::FatArrow,
|
||||||
|
@ -1215,7 +1215,7 @@ impl<'a> MacroParser<'a> {
|
||||||
) => {}
|
) => {}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
let (mut hi, body, whole_body) = match self.toks.next()? {
|
let (mut hi, body, whole_body) = match self.iter.next()? {
|
||||||
TokenTree::Token(..) => return None,
|
TokenTree::Token(..) => return None,
|
||||||
TokenTree::Delimited(delimited_span, ..) => {
|
TokenTree::Delimited(delimited_span, ..) => {
|
||||||
let data = delimited_span.entire().data();
|
let data = delimited_span.entire().data();
|
||||||
|
@ -1237,10 +1237,10 @@ impl<'a> MacroParser<'a> {
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
_,
|
_,
|
||||||
)) = self.toks.peek()
|
)) = self.iter.peek()
|
||||||
{
|
{
|
||||||
hi = span.hi();
|
hi = span.hi();
|
||||||
self.toks.next();
|
self.iter.next();
|
||||||
}
|
}
|
||||||
Some(MacroBranch {
|
Some(MacroBranch {
|
||||||
span: mk_sp(lo, hi),
|
span: mk_sp(lo, hi),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue