]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/tokenstream.rs
Rollup merge of #105405 - sunfishcode:sunfishcode/export-dynamic, r=TaKO8Ki
[rust.git] / compiler / rustc_ast / src / tokenstream.rs
1 //! # Token Streams
2 //!
3 //! `TokenStream`s represent syntactic objects before they are converted into ASTs.
4 //! A `TokenStream` is, roughly speaking, a sequence of [`TokenTree`]s,
5 //! which are themselves a single [`Token`] or a `Delimited` subsequence of tokens.
6 //!
7 //! ## Ownership
8 //!
9 //! `TokenStream`s are persistent data structures constructed as ropes with reference
10 //! counted-children. In general, this means that calling an operation on a `TokenStream`
11 //! (such as `slice`) produces an entirely new `TokenStream` from the borrowed reference to
12 //! the original. This essentially coerces `TokenStream`s into "views" of their subparts,
13 //! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
14 //! ownership of the original.
15
16 use crate::ast::StmtKind;
17 use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
18 use crate::token::{self, Delimiter, Nonterminal, Token, TokenKind};
19 use crate::AttrVec;
20
21 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22 use rustc_data_structures::sync::{self, Lrc};
23 use rustc_macros::HashStable_Generic;
24 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
25 use rustc_span::{Span, DUMMY_SP};
26 use smallvec::{smallvec, SmallVec};
27
28 use std::{fmt, iter};
29
30 /// When the main Rust parser encounters a syntax-extension invocation, it
31 /// parses the arguments to the invocation as a token tree. This is a very
32 /// loose structure, such that all sorts of different AST fragments can
33 /// be passed to syntax extensions using a uniform type.
34 ///
35 /// If the syntax extension is an MBE macro, it will attempt to match its
36 /// LHS token tree against the provided token tree, and if it finds a
37 /// match, will transcribe the RHS token tree, splicing in any captured
38 /// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
39 ///
40 /// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
41 /// Nothing special happens to misnamed or misplaced `SubstNt`s.
42 #[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
43 pub enum TokenTree {
44     /// A single token.
45     Token(Token, Spacing),
46     /// A delimited sequence of token trees.
47     Delimited(DelimSpan, Delimiter, TokenStream),
48 }
49
50 // Ensure all fields of `TokenTree` is `Send` and `Sync`.
51 #[cfg(parallel_compiler)]
52 fn _dummy()
53 where
54     Token: Send + Sync,
55     DelimSpan: Send + Sync,
56     Delimiter: Send + Sync,
57     TokenStream: Send + Sync,
58 {
59 }
60
61 impl TokenTree {
62     /// Checks if this `TokenTree` is equal to the other, regardless of span information.
63     pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
64         match (self, other) {
65             (TokenTree::Token(token, _), TokenTree::Token(token2, _)) => token.kind == token2.kind,
66             (TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
67                 delim == delim2 && tts.eq_unspanned(tts2)
68             }
69             _ => false,
70         }
71     }
72
73     /// Retrieves the `TokenTree`'s span.
74     pub fn span(&self) -> Span {
75         match self {
76             TokenTree::Token(token, _) => token.span,
77             TokenTree::Delimited(sp, ..) => sp.entire(),
78         }
79     }
80
81     /// Modify the `TokenTree`'s span in-place.
82     pub fn set_span(&mut self, span: Span) {
83         match self {
84             TokenTree::Token(token, _) => token.span = span,
85             TokenTree::Delimited(dspan, ..) => *dspan = DelimSpan::from_single(span),
86         }
87     }
88
89     /// Create a `TokenTree::Token` with alone spacing.
90     pub fn token_alone(kind: TokenKind, span: Span) -> TokenTree {
91         TokenTree::Token(Token::new(kind, span), Spacing::Alone)
92     }
93
94     /// Create a `TokenTree::Token` with joint spacing.
95     pub fn token_joint(kind: TokenKind, span: Span) -> TokenTree {
96         TokenTree::Token(Token::new(kind, span), Spacing::Joint)
97     }
98
99     pub fn uninterpolate(self) -> TokenTree {
100         match self {
101             TokenTree::Token(token, spacing) => {
102                 TokenTree::Token(token.uninterpolate().into_owned(), spacing)
103             }
104             tt => tt,
105         }
106     }
107 }
108
109 impl<CTX> HashStable<CTX> for TokenStream
110 where
111     CTX: crate::HashStableContext,
112 {
113     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
114         for sub_tt in self.trees() {
115             sub_tt.hash_stable(hcx, hasher);
116         }
117     }
118 }
119
120 pub trait ToAttrTokenStream: sync::Send + sync::Sync {
121     fn to_attr_token_stream(&self) -> AttrTokenStream;
122 }
123
124 impl ToAttrTokenStream for AttrTokenStream {
125     fn to_attr_token_stream(&self) -> AttrTokenStream {
126         self.clone()
127     }
128 }
129
130 /// A lazy version of [`TokenStream`], which defers creation
131 /// of an actual `TokenStream` until it is needed.
132 /// `Box` is here only to reduce the structure size.
133 #[derive(Clone)]
134 pub struct LazyAttrTokenStream(Lrc<Box<dyn ToAttrTokenStream>>);
135
136 impl LazyAttrTokenStream {
137     pub fn new(inner: impl ToAttrTokenStream + 'static) -> LazyAttrTokenStream {
138         LazyAttrTokenStream(Lrc::new(Box::new(inner)))
139     }
140
141     pub fn to_attr_token_stream(&self) -> AttrTokenStream {
142         self.0.to_attr_token_stream()
143     }
144 }
145
146 impl fmt::Debug for LazyAttrTokenStream {
147     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148         write!(f, "LazyAttrTokenStream({:?})", self.to_attr_token_stream())
149     }
150 }
151
152 impl<S: Encoder> Encodable<S> for LazyAttrTokenStream {
153     fn encode(&self, s: &mut S) {
154         // Used by AST json printing.
155         Encodable::encode(&self.to_attr_token_stream(), s);
156     }
157 }
158
159 impl<D: Decoder> Decodable<D> for LazyAttrTokenStream {
160     fn decode(_d: &mut D) -> Self {
161         panic!("Attempted to decode LazyAttrTokenStream");
162     }
163 }
164
165 impl<CTX> HashStable<CTX> for LazyAttrTokenStream {
166     fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
167         panic!("Attempted to compute stable hash for LazyAttrTokenStream");
168     }
169 }
170
171 /// An `AttrTokenStream` is similar to a `TokenStream`, but with extra
172 /// information about the tokens for attribute targets. This is used
173 /// during expansion to perform early cfg-expansion, and to process attributes
174 /// during proc-macro invocations.
175 #[derive(Clone, Debug, Default, Encodable, Decodable)]
176 pub struct AttrTokenStream(pub Lrc<Vec<AttrTokenTree>>);
177
178 /// Like `TokenTree`, but for `AttrTokenStream`.
179 #[derive(Clone, Debug, Encodable, Decodable)]
180 pub enum AttrTokenTree {
181     Token(Token, Spacing),
182     Delimited(DelimSpan, Delimiter, AttrTokenStream),
183     /// Stores the attributes for an attribute target,
184     /// along with the tokens for that attribute target.
185     /// See `AttributesData` for more information
186     Attributes(AttributesData),
187 }
188
189 impl AttrTokenStream {
190     pub fn new(tokens: Vec<AttrTokenTree>) -> AttrTokenStream {
191         AttrTokenStream(Lrc::new(tokens))
192     }
193
194     /// Converts this `AttrTokenStream` to a plain `TokenStream`.
195     /// During conversion, `AttrTokenTree::Attributes` get 'flattened'
196     /// back to a `TokenStream` of the form `outer_attr attr_target`.
197     /// If there are inner attributes, they are inserted into the proper
198     /// place in the attribute target tokens.
199     pub fn to_tokenstream(&self) -> TokenStream {
200         let trees: Vec<_> = self
201             .0
202             .iter()
203             .flat_map(|tree| match &tree {
204                 AttrTokenTree::Token(inner, spacing) => {
205                     smallvec![TokenTree::Token(inner.clone(), *spacing)].into_iter()
206                 }
207                 AttrTokenTree::Delimited(span, delim, stream) => {
208                     smallvec![TokenTree::Delimited(*span, *delim, stream.to_tokenstream()),]
209                         .into_iter()
210                 }
211                 AttrTokenTree::Attributes(data) => {
212                     let mut outer_attrs = Vec::new();
213                     let mut inner_attrs = Vec::new();
214                     for attr in &data.attrs {
215                         match attr.style {
216                             crate::AttrStyle::Outer => outer_attrs.push(attr),
217                             crate::AttrStyle::Inner => inner_attrs.push(attr),
218                         }
219                     }
220
221                     let mut target_tokens: Vec<_> = data
222                         .tokens
223                         .to_attr_token_stream()
224                         .to_tokenstream()
225                         .0
226                         .iter()
227                         .cloned()
228                         .collect();
229                     if !inner_attrs.is_empty() {
230                         let mut found = false;
231                         // Check the last two trees (to account for a trailing semi)
232                         for tree in target_tokens.iter_mut().rev().take(2) {
233                             if let TokenTree::Delimited(span, delim, delim_tokens) = tree {
234                                 // Inner attributes are only supported on extern blocks, functions,
235                                 // impls, and modules. All of these have their inner attributes
236                                 // placed at the beginning of the rightmost outermost braced group:
237                                 // e.g. fn foo() { #![my_attr} }
238                                 //
239                                 // Therefore, we can insert them back into the right location
240                                 // without needing to do any extra position tracking.
241                                 //
242                                 // Note: Outline modules are an exception - they can
243                                 // have attributes like `#![my_attr]` at the start of a file.
244                                 // Support for custom attributes in this position is not
245                                 // properly implemented - we always synthesize fake tokens,
246                                 // so we never reach this code.
247
248                                 let mut stream = TokenStream::default();
249                                 for inner_attr in inner_attrs {
250                                     stream.push_stream(inner_attr.tokens());
251                                 }
252                                 stream.push_stream(delim_tokens.clone());
253                                 *tree = TokenTree::Delimited(*span, *delim, stream);
254                                 found = true;
255                                 break;
256                             }
257                         }
258
259                         assert!(
260                             found,
261                             "Failed to find trailing delimited group in: {:?}",
262                             target_tokens
263                         );
264                     }
265                     let mut flat: SmallVec<[_; 1]> = SmallVec::new();
266                     for attr in outer_attrs {
267                         // FIXME: Make this more efficient
268                         flat.extend(attr.tokens().0.clone().iter().cloned());
269                     }
270                     flat.extend(target_tokens);
271                     flat.into_iter()
272                 }
273             })
274             .collect();
275         TokenStream::new(trees)
276     }
277 }
278
279 /// Stores the tokens for an attribute target, along
280 /// with its attributes.
281 ///
282 /// This is constructed during parsing when we need to capture
283 /// tokens.
284 ///
285 /// For example, `#[cfg(FALSE)] struct Foo {}` would
286 /// have an `attrs` field containing the `#[cfg(FALSE)]` attr,
287 /// and a `tokens` field storing the (unparsed) tokens `struct Foo {}`
288 #[derive(Clone, Debug, Encodable, Decodable)]
289 pub struct AttributesData {
290     /// Attributes, both outer and inner.
291     /// These are stored in the original order that they were parsed in.
292     pub attrs: AttrVec,
293     /// The underlying tokens for the attribute target that `attrs`
294     /// are applied to
295     pub tokens: LazyAttrTokenStream,
296 }
297
298 /// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
299 ///
300 /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
301 /// instead of a representation of the abstract syntax tree.
302 /// Today's `TokenTree`s can still contain AST via `token::Interpolated` for
303 /// backwards compatibility.
304 #[derive(Clone, Debug, Default, Encodable, Decodable)]
305 pub struct TokenStream(pub(crate) Lrc<Vec<TokenTree>>);
306
307 /// Similar to `proc_macro::Spacing`, but for tokens.
308 ///
309 /// Note that all `ast::TokenTree::Token` instances have a `Spacing`, but when
310 /// we convert to `proc_macro::TokenTree` for proc macros only `Punct`
311 /// `TokenTree`s have a `proc_macro::Spacing`.
312 #[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
313 pub enum Spacing {
314     /// The token is not immediately followed by an operator token (as
315     /// determined by `Token::is_op`). E.g. a `+` token is `Alone` in `+ =`,
316     /// `+/*foo*/=`, `+ident`, and `+()`.
317     Alone,
318
319     /// The token is immediately followed by an operator token. E.g. a `+`
320     /// token is `Joint` in `+=` and `++`.
321     Joint,
322 }
323
324 impl TokenStream {
325     /// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
326     /// separating the two arguments with a comma for diagnostic suggestions.
327     pub fn add_comma(&self) -> Option<(TokenStream, Span)> {
328         // Used to suggest if a user writes `foo!(a b);`
329         let mut suggestion = None;
330         let mut iter = self.0.iter().enumerate().peekable();
331         while let Some((pos, ts)) = iter.next() {
332             if let Some((_, next)) = iter.peek() {
333                 let sp = match (&ts, &next) {
334                     (_, TokenTree::Token(Token { kind: token::Comma, .. }, _)) => continue,
335                     (
336                         TokenTree::Token(token_left, Spacing::Alone),
337                         TokenTree::Token(token_right, _),
338                     ) if ((token_left.is_ident() && !token_left.is_reserved_ident())
339                         || token_left.is_lit())
340                         && ((token_right.is_ident() && !token_right.is_reserved_ident())
341                             || token_right.is_lit()) =>
342                     {
343                         token_left.span
344                     }
345                     (TokenTree::Delimited(sp, ..), _) => sp.entire(),
346                     _ => continue,
347                 };
348                 let sp = sp.shrink_to_hi();
349                 let comma = TokenTree::token_alone(token::Comma, sp);
350                 suggestion = Some((pos, comma, sp));
351             }
352         }
353         if let Some((pos, comma, sp)) = suggestion {
354             let mut new_stream = Vec::with_capacity(self.0.len() + 1);
355             let parts = self.0.split_at(pos + 1);
356             new_stream.extend_from_slice(parts.0);
357             new_stream.push(comma);
358             new_stream.extend_from_slice(parts.1);
359             return Some((TokenStream::new(new_stream), sp));
360         }
361         None
362     }
363 }
364
365 impl iter::FromIterator<TokenTree> for TokenStream {
366     fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
367         TokenStream::new(iter.into_iter().collect::<Vec<TokenTree>>())
368     }
369 }
370
371 impl Eq for TokenStream {}
372
373 impl PartialEq<TokenStream> for TokenStream {
374     fn eq(&self, other: &TokenStream) -> bool {
375         self.trees().eq(other.trees())
376     }
377 }
378
379 impl TokenStream {
380     pub fn new(streams: Vec<TokenTree>) -> TokenStream {
381         TokenStream(Lrc::new(streams))
382     }
383
384     pub fn is_empty(&self) -> bool {
385         self.0.is_empty()
386     }
387
388     pub fn len(&self) -> usize {
389         self.0.len()
390     }
391
392     pub fn trees(&self) -> CursorRef<'_> {
393         CursorRef::new(self)
394     }
395
396     pub fn into_trees(self) -> Cursor {
397         Cursor::new(self)
398     }
399
400     /// Compares two `TokenStream`s, checking equality without regarding span information.
401     pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
402         let mut t1 = self.trees();
403         let mut t2 = other.trees();
404         for (t1, t2) in iter::zip(&mut t1, &mut t2) {
405             if !t1.eq_unspanned(t2) {
406                 return false;
407             }
408         }
409         t1.next().is_none() && t2.next().is_none()
410     }
411
412     pub fn map_enumerated<F: FnMut(usize, &TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
413         TokenStream(Lrc::new(self.0.iter().enumerate().map(|(i, tree)| f(i, tree)).collect()))
414     }
415
416     /// Create a token stream containing a single token with alone spacing.
417     pub fn token_alone(kind: TokenKind, span: Span) -> TokenStream {
418         TokenStream::new(vec![TokenTree::token_alone(kind, span)])
419     }
420
421     /// Create a token stream containing a single token with joint spacing.
422     pub fn token_joint(kind: TokenKind, span: Span) -> TokenStream {
423         TokenStream::new(vec![TokenTree::token_joint(kind, span)])
424     }
425
426     /// Create a token stream containing a single `Delimited`.
427     pub fn delimited(span: DelimSpan, delim: Delimiter, tts: TokenStream) -> TokenStream {
428         TokenStream::new(vec![TokenTree::Delimited(span, delim, tts)])
429     }
430
431     pub fn from_ast(node: &(impl HasAttrs + HasSpan + HasTokens + fmt::Debug)) -> TokenStream {
432         let Some(tokens) = node.tokens() else {
433             panic!("missing tokens for node at {:?}: {:?}", node.span(), node);
434         };
435         let attrs = node.attrs();
436         let attr_stream = if attrs.is_empty() {
437             tokens.to_attr_token_stream()
438         } else {
439             let attr_data =
440                 AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
441             AttrTokenStream::new(vec![AttrTokenTree::Attributes(attr_data)])
442         };
443         attr_stream.to_tokenstream()
444     }
445
446     pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
447         match nt {
448             Nonterminal::NtIdent(ident, is_raw) => {
449                 TokenStream::token_alone(token::Ident(ident.name, *is_raw), ident.span)
450             }
451             Nonterminal::NtLifetime(ident) => {
452                 TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
453             }
454             Nonterminal::NtItem(item) => TokenStream::from_ast(item),
455             Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
456             Nonterminal::NtStmt(stmt) if let StmtKind::Empty = stmt.kind => {
457                 // FIXME: Properly collect tokens for empty statements.
458                 TokenStream::token_alone(token::Semi, stmt.span)
459             }
460             Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
461             Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
462             Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
463             Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
464             Nonterminal::NtPath(path) => TokenStream::from_ast(path),
465             Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
466             Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
467         }
468     }
469
470     fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
471         match &token.kind {
472             token::Interpolated(nt) if let token::NtIdent(ident, is_raw) = **nt => {
473                 TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
474             }
475             token::Interpolated(nt) => TokenTree::Delimited(
476                 DelimSpan::from_single(token.span),
477                 Delimiter::Invisible,
478                 TokenStream::from_nonterminal_ast(nt).flattened(),
479             ),
480             _ => TokenTree::Token(token.clone(), spacing),
481         }
482     }
483
484     fn flatten_token_tree(tree: &TokenTree) -> TokenTree {
485         match tree {
486             TokenTree::Token(token, spacing) => TokenStream::flatten_token(token, *spacing),
487             TokenTree::Delimited(span, delim, tts) => {
488                 TokenTree::Delimited(*span, *delim, tts.flattened())
489             }
490         }
491     }
492
493     #[must_use]
494     pub fn flattened(&self) -> TokenStream {
495         fn can_skip(stream: &TokenStream) -> bool {
496             stream.trees().all(|tree| match tree {
497                 TokenTree::Token(token, _) => !matches!(token.kind, token::Interpolated(_)),
498                 TokenTree::Delimited(_, _, inner) => can_skip(inner),
499             })
500         }
501
502         if can_skip(self) {
503             return self.clone();
504         }
505
506         self.trees().map(|tree| TokenStream::flatten_token_tree(tree)).collect()
507     }
508
509     // If `vec` is not empty, try to glue `tt` onto its last token. The return
510     // value indicates if gluing took place.
511     fn try_glue_to_last(vec: &mut Vec<TokenTree>, tt: &TokenTree) -> bool {
512         if let Some(TokenTree::Token(last_tok, Spacing::Joint)) = vec.last()
513             && let TokenTree::Token(tok, spacing) = tt
514             && let Some(glued_tok) = last_tok.glue(tok)
515         {
516             // ...then overwrite the last token tree in `vec` with the
517             // glued token, and skip the first token tree from `stream`.
518             *vec.last_mut().unwrap() = TokenTree::Token(glued_tok, *spacing);
519             true
520         } else {
521             false
522         }
523     }
524
525     /// Push `tt` onto the end of the stream, possibly gluing it to the last
526     /// token. Uses `make_mut` to maximize efficiency.
527     pub fn push_tree(&mut self, tt: TokenTree) {
528         let vec_mut = Lrc::make_mut(&mut self.0);
529
530         if Self::try_glue_to_last(vec_mut, &tt) {
531             // nothing else to do
532         } else {
533             vec_mut.push(tt);
534         }
535     }
536
537     /// Push `stream` onto the end of the stream, possibly gluing the first
538     /// token tree to the last token. (No other token trees will be glued.)
539     /// Uses `make_mut` to maximize efficiency.
540     pub fn push_stream(&mut self, stream: TokenStream) {
541         let vec_mut = Lrc::make_mut(&mut self.0);
542
543         let stream_iter = stream.0.iter().cloned();
544
545         if let Some(first) = stream.0.first() && Self::try_glue_to_last(vec_mut, first) {
546             // Now skip the first token tree from `stream`.
547             vec_mut.extend(stream_iter.skip(1));
548         } else {
549             // Append all of `stream`.
550             vec_mut.extend(stream_iter);
551         }
552     }
553 }
554
555 /// By-reference iterator over a [`TokenStream`].
556 #[derive(Clone)]
557 pub struct CursorRef<'t> {
558     stream: &'t TokenStream,
559     index: usize,
560 }
561
562 impl<'t> CursorRef<'t> {
563     fn new(stream: &'t TokenStream) -> Self {
564         CursorRef { stream, index: 0 }
565     }
566
567     pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
568         self.stream.0.get(self.index + n)
569     }
570 }
571
572 impl<'t> Iterator for CursorRef<'t> {
573     type Item = &'t TokenTree;
574
575     fn next(&mut self) -> Option<&'t TokenTree> {
576         self.stream.0.get(self.index).map(|tree| {
577             self.index += 1;
578             tree
579         })
580     }
581 }
582
583 /// Owning by-value iterator over a [`TokenStream`].
584 // FIXME: Many uses of this can be replaced with by-reference iterator to avoid clones.
585 #[derive(Clone)]
586 pub struct Cursor {
587     pub stream: TokenStream,
588     index: usize,
589 }
590
591 impl Iterator for Cursor {
592     type Item = TokenTree;
593
594     fn next(&mut self) -> Option<TokenTree> {
595         self.stream.0.get(self.index).map(|tree| {
596             self.index += 1;
597             tree.clone()
598         })
599     }
600 }
601
602 impl Cursor {
603     fn new(stream: TokenStream) -> Self {
604         Cursor { stream, index: 0 }
605     }
606
607     #[inline]
608     pub fn next_ref(&mut self) -> Option<&TokenTree> {
609         self.stream.0.get(self.index).map(|tree| {
610             self.index += 1;
611             tree
612         })
613     }
614
615     pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
616         self.stream.0.get(self.index + n)
617     }
618 }
619
620 #[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
621 pub struct DelimSpan {
622     pub open: Span,
623     pub close: Span,
624 }
625
626 impl DelimSpan {
627     pub fn from_single(sp: Span) -> Self {
628         DelimSpan { open: sp, close: sp }
629     }
630
631     pub fn from_pair(open: Span, close: Span) -> Self {
632         DelimSpan { open, close }
633     }
634
635     pub fn dummy() -> Self {
636         Self::from_single(DUMMY_SP)
637     }
638
639     pub fn entire(self) -> Span {
640         self.open.with_hi(self.close.hi())
641     }
642 }
643
644 // Some types are used a lot. Make sure they don't unintentionally get bigger.
645 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
646 mod size_asserts {
647     use super::*;
648     use rustc_data_structures::static_assert_size;
649     // tidy-alphabetical-start
650     static_assert_size!(AttrTokenStream, 8);
651     static_assert_size!(AttrTokenTree, 32);
652     static_assert_size!(LazyAttrTokenStream, 8);
653     static_assert_size!(TokenStream, 8);
654     static_assert_size!(TokenTree, 32);
655     // tidy-alphabetical-end
656 }