]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/tokenstream.rs
Rollup merge of #81465 - joshtriplett:duration-formatting-documentation, r=m-ou-se
[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::token::{self, DelimToken, Token, TokenKind};
17
18 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
19 use rustc_data_structures::sync::{self, Lrc};
20 use rustc_macros::HashStable_Generic;
21 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
22 use rustc_span::{Span, DUMMY_SP};
23 use smallvec::{smallvec, SmallVec};
24
25 use std::{fmt, iter, mem};
26
27 /// When the main Rust parser encounters a syntax-extension invocation, it
28 /// parses the arguments to the invocation as a token tree. This is a very
29 /// loose structure, such that all sorts of different AST fragments can
30 /// be passed to syntax extensions using a uniform type.
31 ///
32 /// If the syntax extension is an MBE macro, it will attempt to match its
33 /// LHS token tree against the provided token tree, and if it finds a
34 /// match, will transcribe the RHS token tree, splicing in any captured
35 /// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
36 ///
37 /// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
38 /// Nothing special happens to misnamed or misplaced `SubstNt`s.
39 #[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
40 pub enum TokenTree {
41     /// A single token.
42     Token(Token),
43     /// A delimited sequence of token trees.
44     Delimited(DelimSpan, DelimToken, TokenStream),
45 }
46
47 #[derive(Copy, Clone)]
48 pub enum CanSynthesizeMissingTokens {
49     Yes,
50     No,
51 }
52
53 // Ensure all fields of `TokenTree` is `Send` and `Sync`.
54 #[cfg(parallel_compiler)]
55 fn _dummy()
56 where
57     Token: Send + Sync,
58     DelimSpan: Send + Sync,
59     DelimToken: Send + Sync,
60     TokenStream: Send + Sync,
61 {
62 }
63
64 impl TokenTree {
65     /// Checks if this `TokenTree` is equal to the other, regardless of span information.
66     pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
67         match (self, other) {
68             (TokenTree::Token(token), TokenTree::Token(token2)) => token.kind == token2.kind,
69             (TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
70                 delim == delim2 && tts.eq_unspanned(&tts2)
71             }
72             _ => false,
73         }
74     }
75
76     /// Retrieves the `TokenTree`'s span.
77     pub fn span(&self) -> Span {
78         match self {
79             TokenTree::Token(token) => token.span,
80             TokenTree::Delimited(sp, ..) => sp.entire(),
81         }
82     }
83
84     /// Modify the `TokenTree`'s span in-place.
85     pub fn set_span(&mut self, span: Span) {
86         match self {
87             TokenTree::Token(token) => token.span = span,
88             TokenTree::Delimited(dspan, ..) => *dspan = DelimSpan::from_single(span),
89         }
90     }
91
92     pub fn joint(self) -> TokenStream {
93         TokenStream::new(vec![(self, Spacing::Joint)])
94     }
95
96     pub fn token(kind: TokenKind, span: Span) -> TokenTree {
97         TokenTree::Token(Token::new(kind, span))
98     }
99
100     /// Returns the opening delimiter as a token tree.
101     pub fn open_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
102         TokenTree::token(token::OpenDelim(delim), span.open)
103     }
104
105     /// Returns the closing delimiter as a token tree.
106     pub fn close_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
107         TokenTree::token(token::CloseDelim(delim), span.close)
108     }
109
110     pub fn uninterpolate(self) -> TokenTree {
111         match self {
112             TokenTree::Token(token) => TokenTree::Token(token.uninterpolate().into_owned()),
113             tt => tt,
114         }
115     }
116 }
117
118 impl<CTX> HashStable<CTX> for TokenStream
119 where
120     CTX: crate::HashStableContext,
121 {
122     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
123         for sub_tt in self.trees() {
124             sub_tt.hash_stable(hcx, hasher);
125         }
126     }
127 }
128
129 pub trait CreateTokenStream: sync::Send + sync::Sync {
130     fn create_token_stream(&self) -> TokenStream;
131 }
132
133 impl CreateTokenStream for TokenStream {
134     fn create_token_stream(&self) -> TokenStream {
135         self.clone()
136     }
137 }
138
139 /// A lazy version of [`TokenStream`], which defers creation
140 /// of an actual `TokenStream` until it is needed.
141 /// `Box` is here only to reduce the structure size.
142 #[derive(Clone)]
143 pub struct LazyTokenStream(Lrc<Box<dyn CreateTokenStream>>);
144
145 impl LazyTokenStream {
146     pub fn new(inner: impl CreateTokenStream + 'static) -> LazyTokenStream {
147         LazyTokenStream(Lrc::new(Box::new(inner)))
148     }
149
150     pub fn create_token_stream(&self) -> TokenStream {
151         self.0.create_token_stream()
152     }
153 }
154
155 impl fmt::Debug for LazyTokenStream {
156     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157         fmt::Debug::fmt("LazyTokenStream", f)
158     }
159 }
160
161 impl<S: Encoder> Encodable<S> for LazyTokenStream {
162     fn encode(&self, s: &mut S) -> Result<(), S::Error> {
163         // Used by AST json printing.
164         Encodable::encode(&self.create_token_stream(), s)
165     }
166 }
167
168 impl<D: Decoder> Decodable<D> for LazyTokenStream {
169     fn decode(_d: &mut D) -> Result<Self, D::Error> {
170         panic!("Attempted to decode LazyTokenStream");
171     }
172 }
173
174 impl<CTX> HashStable<CTX> for LazyTokenStream {
175     fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
176         panic!("Attempted to compute stable hash for LazyTokenStream");
177     }
178 }
179
180 /// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
181 ///
182 /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
183 /// instead of a representation of the abstract syntax tree.
184 /// Today's `TokenTree`s can still contain AST via `token::Interpolated` for
185 /// backwards compatability.
186 #[derive(Clone, Debug, Default, Encodable, Decodable)]
187 pub struct TokenStream(pub(crate) Lrc<Vec<TreeAndSpacing>>);
188
189 pub type TreeAndSpacing = (TokenTree, Spacing);
190
191 // `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
192 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
193 rustc_data_structures::static_assert_size!(TokenStream, 8);
194
195 #[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable)]
196 pub enum Spacing {
197     Alone,
198     Joint,
199 }
200
201 impl TokenStream {
202     /// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
203     /// separating the two arguments with a comma for diagnostic suggestions.
204     pub fn add_comma(&self) -> Option<(TokenStream, Span)> {
205         // Used to suggest if a user writes `foo!(a b);`
206         let mut suggestion = None;
207         let mut iter = self.0.iter().enumerate().peekable();
208         while let Some((pos, ts)) = iter.next() {
209             if let Some((_, next)) = iter.peek() {
210                 let sp = match (&ts, &next) {
211                     (_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
212                     (
213                         (TokenTree::Token(token_left), Spacing::Alone),
214                         (TokenTree::Token(token_right), _),
215                     ) if ((token_left.is_ident() && !token_left.is_reserved_ident())
216                         || token_left.is_lit())
217                         && ((token_right.is_ident() && !token_right.is_reserved_ident())
218                             || token_right.is_lit()) =>
219                     {
220                         token_left.span
221                     }
222                     ((TokenTree::Delimited(sp, ..), Spacing::Alone), _) => sp.entire(),
223                     _ => continue,
224                 };
225                 let sp = sp.shrink_to_hi();
226                 let comma = (TokenTree::token(token::Comma, sp), Spacing::Alone);
227                 suggestion = Some((pos, comma, sp));
228             }
229         }
230         if let Some((pos, comma, sp)) = suggestion {
231             let mut new_stream = Vec::with_capacity(self.0.len() + 1);
232             let parts = self.0.split_at(pos + 1);
233             new_stream.extend_from_slice(parts.0);
234             new_stream.push(comma);
235             new_stream.extend_from_slice(parts.1);
236             return Some((TokenStream::new(new_stream), sp));
237         }
238         None
239     }
240 }
241
242 impl From<TokenTree> for TokenStream {
243     fn from(tree: TokenTree) -> TokenStream {
244         TokenStream::new(vec![(tree, Spacing::Alone)])
245     }
246 }
247
248 impl From<TokenTree> for TreeAndSpacing {
249     fn from(tree: TokenTree) -> TreeAndSpacing {
250         (tree, Spacing::Alone)
251     }
252 }
253
254 impl iter::FromIterator<TokenTree> for TokenStream {
255     fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
256         TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndSpacing>>())
257     }
258 }
259
260 impl Eq for TokenStream {}
261
262 impl PartialEq<TokenStream> for TokenStream {
263     fn eq(&self, other: &TokenStream) -> bool {
264         self.trees().eq(other.trees())
265     }
266 }
267
268 impl TokenStream {
269     pub fn new(streams: Vec<TreeAndSpacing>) -> TokenStream {
270         TokenStream(Lrc::new(streams))
271     }
272
273     pub fn is_empty(&self) -> bool {
274         self.0.is_empty()
275     }
276
277     pub fn len(&self) -> usize {
278         self.0.len()
279     }
280
281     pub fn span(&self) -> Option<Span> {
282         match &**self.0 {
283             [] => None,
284             [(tt, _)] => Some(tt.span()),
285             [(tt_start, _), .., (tt_end, _)] => Some(tt_start.span().to(tt_end.span())),
286         }
287     }
288
289     pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
290         match streams.len() {
291             0 => TokenStream::default(),
292             1 => streams.pop().unwrap(),
293             _ => {
294                 // We are going to extend the first stream in `streams` with
295                 // the elements from the subsequent streams. This requires
296                 // using `make_mut()` on the first stream, and in practice this
297                 // doesn't cause cloning 99.9% of the time.
298                 //
299                 // One very common use case is when `streams` has two elements,
300                 // where the first stream has any number of elements within
301                 // (often 1, but sometimes many more) and the second stream has
302                 // a single element within.
303
304                 // Determine how much the first stream will be extended.
305                 // Needed to avoid quadratic blow up from on-the-fly
306                 // reallocations (#57735).
307                 let num_appends = streams.iter().skip(1).map(|ts| ts.len()).sum();
308
309                 // Get the first stream. If it's `None`, create an empty
310                 // stream.
311                 let mut iter = streams.drain(..);
312                 let mut first_stream_lrc = iter.next().unwrap().0;
313
314                 // Append the elements to the first stream, after reserving
315                 // space for them.
316                 let first_vec_mut = Lrc::make_mut(&mut first_stream_lrc);
317                 first_vec_mut.reserve(num_appends);
318                 for stream in iter {
319                     first_vec_mut.extend(stream.0.iter().cloned());
320                 }
321
322                 // Create the final `TokenStream`.
323                 TokenStream(first_stream_lrc)
324             }
325         }
326     }
327
328     pub fn trees_ref(&self) -> CursorRef<'_> {
329         CursorRef::new(self)
330     }
331
332     pub fn trees(&self) -> Cursor {
333         self.clone().into_trees()
334     }
335
336     pub fn into_trees(self) -> Cursor {
337         Cursor::new(self)
338     }
339
340     /// Compares two `TokenStream`s, checking equality without regarding span information.
341     pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
342         let mut t1 = self.trees();
343         let mut t2 = other.trees();
344         for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
345             if !t1.eq_unspanned(&t2) {
346                 return false;
347             }
348         }
349         t1.next().is_none() && t2.next().is_none()
350     }
351
352     pub fn map_enumerated<F: FnMut(usize, &TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
353         TokenStream(Lrc::new(
354             self.0
355                 .iter()
356                 .enumerate()
357                 .map(|(i, (tree, is_joint))| (f(i, tree), *is_joint))
358                 .collect(),
359         ))
360     }
361 }
362
363 // 99.5%+ of the time we have 1 or 2 elements in this vector.
364 #[derive(Clone)]
365 pub struct TokenStreamBuilder(SmallVec<[TokenStream; 2]>);
366
367 impl TokenStreamBuilder {
368     pub fn new() -> TokenStreamBuilder {
369         TokenStreamBuilder(SmallVec::new())
370     }
371
372     pub fn push<T: Into<TokenStream>>(&mut self, stream: T) {
373         let mut stream = stream.into();
374
375         // If `self` is not empty and the last tree within the last stream is a
376         // token tree marked with `Joint`...
377         if let Some(TokenStream(ref mut last_stream_lrc)) = self.0.last_mut() {
378             if let Some((TokenTree::Token(last_token), Spacing::Joint)) = last_stream_lrc.last() {
379                 // ...and `stream` is not empty and the first tree within it is
380                 // a token tree...
381                 let TokenStream(ref mut stream_lrc) = stream;
382                 if let Some((TokenTree::Token(token), spacing)) = stream_lrc.first() {
383                     // ...and the two tokens can be glued together...
384                     if let Some(glued_tok) = last_token.glue(&token) {
385                         // ...then do so, by overwriting the last token
386                         // tree in `self` and removing the first token tree
387                         // from `stream`. This requires using `make_mut()`
388                         // on the last stream in `self` and on `stream`,
389                         // and in practice this doesn't cause cloning 99.9%
390                         // of the time.
391
392                         // Overwrite the last token tree with the merged
393                         // token.
394                         let last_vec_mut = Lrc::make_mut(last_stream_lrc);
395                         *last_vec_mut.last_mut().unwrap() = (TokenTree::Token(glued_tok), *spacing);
396
397                         // Remove the first token tree from `stream`. (This
398                         // is almost always the only tree in `stream`.)
399                         let stream_vec_mut = Lrc::make_mut(stream_lrc);
400                         stream_vec_mut.remove(0);
401
402                         // Don't push `stream` if it's empty -- that could
403                         // block subsequent token gluing, by getting
404                         // between two token trees that should be glued
405                         // together.
406                         if !stream.is_empty() {
407                             self.0.push(stream);
408                         }
409                         return;
410                     }
411                 }
412             }
413         }
414         self.0.push(stream);
415     }
416
417     pub fn build(self) -> TokenStream {
418         TokenStream::from_streams(self.0)
419     }
420 }
421
422 /// By-reference iterator over a [`TokenStream`].
423 #[derive(Clone)]
424 pub struct CursorRef<'t> {
425     stream: &'t TokenStream,
426     index: usize,
427 }
428
429 impl<'t> CursorRef<'t> {
430     fn new(stream: &TokenStream) -> CursorRef<'_> {
431         CursorRef { stream, index: 0 }
432     }
433
434     fn next_with_spacing(&mut self) -> Option<&'t TreeAndSpacing> {
435         self.stream.0.get(self.index).map(|tree| {
436             self.index += 1;
437             tree
438         })
439     }
440 }
441
442 impl<'t> Iterator for CursorRef<'t> {
443     type Item = &'t TokenTree;
444
445     fn next(&mut self) -> Option<&'t TokenTree> {
446         self.next_with_spacing().map(|(tree, _)| tree)
447     }
448 }
449
450 /// Owning by-value iterator over a [`TokenStream`].
451 // FIXME: Many uses of this can be replaced with by-reference iterator to avoid clones.
452 #[derive(Clone)]
453 pub struct Cursor {
454     pub stream: TokenStream,
455     index: usize,
456 }
457
458 impl Iterator for Cursor {
459     type Item = TokenTree;
460
461     fn next(&mut self) -> Option<TokenTree> {
462         self.next_with_spacing().map(|(tree, _)| tree)
463     }
464 }
465
466 impl Cursor {
467     fn new(stream: TokenStream) -> Self {
468         Cursor { stream, index: 0 }
469     }
470
471     pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
472         if self.index < self.stream.len() {
473             self.index += 1;
474             Some(self.stream.0[self.index - 1].clone())
475         } else {
476             None
477         }
478     }
479
480     pub fn append(&mut self, new_stream: TokenStream) {
481         if new_stream.is_empty() {
482             return;
483         }
484         let index = self.index;
485         let stream = mem::take(&mut self.stream);
486         *self = TokenStream::from_streams(smallvec![stream, new_stream]).into_trees();
487         self.index = index;
488     }
489
490     pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
491         self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
492     }
493 }
494
495 #[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
496 pub struct DelimSpan {
497     pub open: Span,
498     pub close: Span,
499 }
500
501 impl DelimSpan {
502     pub fn from_single(sp: Span) -> Self {
503         DelimSpan { open: sp, close: sp }
504     }
505
506     pub fn from_pair(open: Span, close: Span) -> Self {
507         DelimSpan { open, close }
508     }
509
510     pub fn dummy() -> Self {
511         Self::from_single(DUMMY_SP)
512     }
513
514     pub fn entire(self) -> Span {
515         self.open.with_hi(self.close.hi())
516     }
517 }