Skip to content

Commit a86fc72

Browse files
committed
Rename Cursor/CursorRef as TokenTreeCursor/RefTokenTreeCursor.
This makes it clear they return token trees, and makes for a nice comparison against `TokenCursor` which returns tokens.
1 parent b5ecbbb commit a86fc72

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

compiler/rustc_ast/src/tokenstream.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,12 @@ impl TokenStream {
389389
self.0.len()
390390
}
391391

392-
pub fn trees(&self) -> CursorRef<'_> {
393-
CursorRef::new(self)
392+
pub fn trees(&self) -> RefTokenTreeCursor<'_> {
393+
RefTokenTreeCursor::new(self)
394394
}
395395

396-
pub fn into_trees(self) -> Cursor {
397-
Cursor::new(self)
396+
pub fn into_trees(self) -> TokenTreeCursor {
397+
TokenTreeCursor::new(self)
398398
}
399399

400400
/// Compares two `TokenStream`s, checking equality without regarding span information.
@@ -552,24 +552,25 @@ impl TokenStream {
552552
}
553553
}
554554

555-
/// By-reference iterator over a [`TokenStream`].
555+
/// By-reference iterator over a [`TokenStream`], that produces `&TokenTree`
556+
/// items.
556557
#[derive(Clone)]
557-
pub struct CursorRef<'t> {
558+
pub struct RefTokenTreeCursor<'t> {
558559
stream: &'t TokenStream,
559560
index: usize,
560561
}
561562

562-
impl<'t> CursorRef<'t> {
563+
impl<'t> RefTokenTreeCursor<'t> {
563564
fn new(stream: &'t TokenStream) -> Self {
564-
CursorRef { stream, index: 0 }
565+
RefTokenTreeCursor { stream, index: 0 }
565566
}
566567

567568
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
568569
self.stream.0.get(self.index + n)
569570
}
570571
}
571572

572-
impl<'t> Iterator for CursorRef<'t> {
573+
impl<'t> Iterator for RefTokenTreeCursor<'t> {
573574
type Item = &'t TokenTree;
574575

575576
fn next(&mut self) -> Option<&'t TokenTree> {
@@ -580,15 +581,16 @@ impl<'t> Iterator for CursorRef<'t> {
580581
}
581582
}
582583

583-
/// Owning by-value iterator over a [`TokenStream`].
584+
/// Owning by-value iterator over a [`TokenStream`], that produces `TokenTree`
585+
/// items.
584586
// FIXME: Many uses of this can be replaced with by-reference iterator to avoid clones.
585587
#[derive(Clone)]
586-
pub struct Cursor {
588+
pub struct TokenTreeCursor {
587589
pub stream: TokenStream,
588590
index: usize,
589591
}
590592

591-
impl Iterator for Cursor {
593+
impl Iterator for TokenTreeCursor {
592594
type Item = TokenTree;
593595

594596
fn next(&mut self) -> Option<TokenTree> {
@@ -599,9 +601,9 @@ impl Iterator for Cursor {
599601
}
600602
}
601603

602-
impl Cursor {
604+
impl TokenTreeCursor {
603605
fn new(stream: TokenStream) -> Self {
604-
Cursor { stream, index: 0 }
606+
TokenTreeCursor { stream, index: 0 }
605607
}
606608

607609
#[inline]

compiler/rustc_expand/src/mbe/metavar_expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::token::{self, Delimiter};
2-
use rustc_ast::tokenstream::{CursorRef, TokenStream, TokenTree};
2+
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
33
use rustc_ast::{LitIntType, LitKind};
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::{Applicability, PResult};
@@ -72,7 +72,7 @@ impl MetaVarExpr {
7272

7373
// Checks if there are any remaining tokens. For example, `${ignore(ident ... a b c ...)}`
7474
fn check_trailing_token<'sess>(
75-
iter: &mut CursorRef<'_>,
75+
iter: &mut RefTokenTreeCursor<'_>,
7676
sess: &'sess ParseSess,
7777
) -> PResult<'sess, ()> {
7878
if let Some(tt) = iter.next() {
@@ -88,7 +88,7 @@ fn check_trailing_token<'sess>(
8888

8989
/// Parse a meta-variable `count` expression: `count(ident[, depth])`
9090
fn parse_count<'sess>(
91-
iter: &mut CursorRef<'_>,
91+
iter: &mut RefTokenTreeCursor<'_>,
9292
sess: &'sess ParseSess,
9393
span: Span,
9494
) -> PResult<'sess, MetaVarExpr> {
@@ -99,7 +99,7 @@ fn parse_count<'sess>(
9999

100100
/// Parses the depth used by index(depth) and length(depth).
101101
fn parse_depth<'sess>(
102-
iter: &mut CursorRef<'_>,
102+
iter: &mut RefTokenTreeCursor<'_>,
103103
sess: &'sess ParseSess,
104104
span: Span,
105105
) -> PResult<'sess, usize> {
@@ -126,7 +126,7 @@ fn parse_depth<'sess>(
126126

127127
/// Parses an generic ident
128128
fn parse_ident<'sess>(
129-
iter: &mut CursorRef<'_>,
129+
iter: &mut RefTokenTreeCursor<'_>,
130130
sess: &'sess ParseSess,
131131
span: Span,
132132
) -> PResult<'sess, Ident> {
@@ -152,7 +152,7 @@ fn parse_ident<'sess>(
152152

153153
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
154154
/// iterator is not modified and the result is `false`.
155-
fn try_eat_comma(iter: &mut CursorRef<'_>) -> bool {
155+
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
156156
if let Some(TokenTree::Token(token::Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
157157
let _ = iter.next();
158158
return true;

compiler/rustc_parse/src/parser/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ pub use path::PathStyle;
1919

2020
use rustc_ast::ptr::P;
2121
use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind};
22-
use rustc_ast::tokenstream::AttributesData;
23-
use rustc_ast::tokenstream::{self, DelimSpan, Spacing};
24-
use rustc_ast::tokenstream::{TokenStream, TokenTree};
22+
use rustc_ast::tokenstream::{AttributesData, DelimSpan, Spacing};
23+
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
2524
use rustc_ast::util::case::Case;
2625
use rustc_ast::AttrId;
2726
use rustc_ast::DUMMY_NODE_ID;
@@ -221,17 +220,21 @@ impl<'a> Drop for Parser<'a> {
221220
}
222221
}
223222

223+
/// Iterator over a `TokenStream` that produces `Token`s. It's a bit odd that
224+
/// we (a) lex tokens into a nice tree structure (`TokenStream`), and then (b)
225+
/// use this type to emit them as a linear sequence. But a linear sequence is
226+
/// what the parser expects, for the most part.
224227
#[derive(Clone)]
225228
struct TokenCursor {
226229
// Cursor for the current (innermost) token stream. The delimiters for this
227230
// token stream are found in `self.stack.last()`; when that is `None` then
228231
// we are in the outermost token stream which never has delimiters.
229-
tree_cursor: tokenstream::Cursor,
232+
tree_cursor: TokenTreeCursor,
230233

231234
// Token streams surrounding the current one. The delimiters for stack[n]'s
232235
// tokens are in `stack[n-1]`. `stack[0]` (when present) has no delimiters
233236
// because it's the outermost token stream which never has delimiters.
234-
stack: Vec<(tokenstream::Cursor, Delimiter, DelimSpan)>,
237+
stack: Vec<(TokenTreeCursor, Delimiter, DelimSpan)>,
235238

236239
desugar_doc_comments: bool,
237240

src/tools/rustfmt/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::collections::HashMap;
1313
use std::panic::{catch_unwind, AssertUnwindSafe};
1414

1515
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
16-
use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
16+
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
1717
use rustc_ast::{ast, ptr};
1818
use rustc_ast_pretty::pprust;
1919
use rustc_span::{
@@ -736,7 +736,7 @@ impl MacroArgParser {
736736
self.buf.clear();
737737
}
738738

739-
fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
739+
fn add_meta_variable(&mut self, iter: &mut TokenTreeCursor) -> Option<()> {
740740
match iter.next() {
741741
Some(TokenTree::Token(
742742
Token {
@@ -768,7 +768,7 @@ impl MacroArgParser {
768768
&mut self,
769769
inner: Vec<ParsedMacroArg>,
770770
delim: Delimiter,
771-
iter: &mut Cursor,
771+
iter: &mut TokenTreeCursor,
772772
) -> Option<()> {
773773
let mut buffer = String::new();
774774
let mut first = true;
@@ -1121,7 +1121,7 @@ pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> D
11211121
// Currently we do not attempt to parse any further than that.
11221122
#[derive(new)]
11231123
struct MacroParser {
1124-
toks: Cursor,
1124+
toks: TokenTreeCursor,
11251125
}
11261126

11271127
impl MacroParser {

0 commit comments

Comments
 (0)