]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/tokenstream.rs
Translate CRLF -> LF in raw (byte) strings
[rust.git] / src / libsyntax / 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 (eg stream) of `TokenTree`s,
5 //! which are themselves a single `Token` or a `Delimited` subsequence of tokens.
6 //!
7 //! ## Ownership
8 //!
9 //! `TokenStreams` 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::ext::base;
17 use crate::ext::tt::{macro_parser, quoted};
18 use crate::parse::Directory;
19 use crate::parse::token::{self, DelimToken, Token, TokenKind};
20 use crate::print::pprust;
21
22 use syntax_pos::{BytePos, Mark, Span, DUMMY_SP};
23 #[cfg(target_arch = "x86_64")]
24 use rustc_data_structures::static_assert_size;
25 use rustc_data_structures::sync::Lrc;
26 use serialize::{Decoder, Decodable, Encoder, Encodable};
27 use smallvec::{SmallVec, smallvec};
28
29 use std::borrow::Cow;
30 use std::{fmt, iter, mem};
31
32 /// When the main rust parser encounters a syntax-extension invocation, it
33 /// parses the arguments to the invocation as a token-tree. This is a very
34 /// loose structure, such that all sorts of different AST-fragments can
35 /// be passed to syntax extensions using a uniform type.
36 ///
37 /// If the syntax extension is an MBE macro, it will attempt to match its
38 /// LHS token tree against the provided token tree, and if it finds a
39 /// match, will transcribe the RHS token tree, splicing in any captured
40 /// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
41 ///
42 /// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
43 /// Nothing special happens to misnamed or misplaced `SubstNt`s.
44 #[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
45 pub enum TokenTree {
46     /// A single token
47     Token(Token),
48     /// A delimited sequence of token trees
49     Delimited(DelimSpan, DelimToken, TokenStream),
50 }
51
52 // Ensure all fields of `TokenTree` is `Send` and `Sync`.
53 #[cfg(parallel_compiler)]
54 fn _dummy()
55 where
56     Token: Send + Sync,
57     DelimSpan: Send + Sync,
58     DelimToken: Send + Sync,
59     TokenStream: Send + Sync,
60 {}
61
62 // These are safe since we ensure that they hold for all fields in the `_dummy` function.
63 //
64 // These impls are only here because the compiler takes forever to compute the Send and Sync
65 // bounds without them.
66 // FIXME: Remove these impls when the compiler can compute the bounds quickly again.
67 // See https://github.com/rust-lang/rust/issues/60846
68 #[cfg(parallel_compiler)]
69 unsafe impl Send for TokenTree {}
70 #[cfg(parallel_compiler)]
71 unsafe impl Sync for TokenTree {}
72
73 impl TokenTree {
74     /// Use this token tree as a matcher to parse given tts.
75     pub fn parse(cx: &base::ExtCtxt<'_>, mtch: &[quoted::TokenTree], tts: TokenStream)
76                  -> macro_parser::NamedParseResult {
77         // `None` is because we're not interpolating
78         let directory = Directory {
79             path: Cow::from(cx.current_expansion.module.directory.as_path()),
80             ownership: cx.current_expansion.directory_ownership,
81         };
82         macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory), true)
83     }
84
85     /// Checks if this TokenTree is equal to the other, regardless of span information.
86     pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
87         match (self, other) {
88             (TokenTree::Token(token), TokenTree::Token(token2)) => token.kind == token2.kind,
89             (TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
90                 delim == delim2 && tts.eq_unspanned(&tts2)
91             }
92             _ => false,
93         }
94     }
95
96     // See comments in `Nonterminal::to_tokenstream` for why we care about
97     // *probably* equal here rather than actual equality
98     //
99     // This is otherwise the same as `eq_unspanned`, only recursing with a
100     // different method.
101     pub fn probably_equal_for_proc_macro(&self, other: &TokenTree) -> bool {
102         match (self, other) {
103             (TokenTree::Token(token), TokenTree::Token(token2)) => {
104                 token.probably_equal_for_proc_macro(token2)
105             }
106             (TokenTree::Delimited(_, delim, tts), TokenTree::Delimited(_, delim2, tts2)) => {
107                 delim == delim2 && tts.probably_equal_for_proc_macro(&tts2)
108             }
109             _ => false,
110         }
111     }
112
113     /// Retrieves the TokenTree's span.
114     pub fn span(&self) -> Span {
115         match self {
116             TokenTree::Token(token) => token.span,
117             TokenTree::Delimited(sp, ..) => sp.entire(),
118         }
119     }
120
121     /// Modify the `TokenTree`'s span in-place.
122     pub fn set_span(&mut self, span: Span) {
123         match self {
124             TokenTree::Token(token) => token.span = span,
125             TokenTree::Delimited(dspan, ..) => *dspan = DelimSpan::from_single(span),
126         }
127     }
128
129     /// Indicates if the stream is a token that is equal to the provided token.
130     pub fn eq_token(&self, t: TokenKind) -> bool {
131         match self {
132             TokenTree::Token(token) => *token == t,
133             _ => false,
134         }
135     }
136
137     pub fn joint(self) -> TokenStream {
138         TokenStream::new(vec![(self, Joint)])
139     }
140
141     pub fn token(kind: TokenKind, span: Span) -> TokenTree {
142         TokenTree::Token(Token::new(kind, span))
143     }
144
145     /// Returns the opening delimiter as a token tree.
146     pub fn open_tt(span: Span, delim: DelimToken) -> TokenTree {
147         let open_span = if span.is_dummy() {
148             span
149         } else {
150             span.with_hi(span.lo() + BytePos(delim.len() as u32))
151         };
152         TokenTree::token(token::OpenDelim(delim), open_span)
153     }
154
155     /// Returns the closing delimiter as a token tree.
156     pub fn close_tt(span: Span, delim: DelimToken) -> TokenTree {
157         let close_span = if span.is_dummy() {
158             span
159         } else {
160             span.with_lo(span.hi() - BytePos(delim.len() as u32))
161         };
162         TokenTree::token(token::CloseDelim(delim), close_span)
163     }
164 }
165
166 /// # Token Streams
167 ///
168 /// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s.
169 /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
170 /// instead of a representation of the abstract syntax tree.
171 /// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
172 ///
173 /// The use of `Option` is an optimization that avoids the need for an
174 /// allocation when the stream is empty. However, it is not guaranteed that an
175 /// empty stream is represented with `None`; it may be represented as a `Some`
176 /// around an empty `Vec`.
177 #[derive(Clone, Debug)]
178 pub struct TokenStream(pub Option<Lrc<Vec<TreeAndJoint>>>);
179
180 pub type TreeAndJoint = (TokenTree, IsJoint);
181
182 // `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
183 #[cfg(target_arch = "x86_64")]
184 static_assert_size!(TokenStream, 8);
185
186 #[derive(Clone, Copy, Debug, PartialEq)]
187 pub enum IsJoint {
188     Joint,
189     NonJoint
190 }
191
192 use IsJoint::*;
193
194 impl TokenStream {
195     /// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
196     /// separating the two arguments with a comma for diagnostic suggestions.
197     pub(crate) fn add_comma(&self) -> Option<(TokenStream, Span)> {
198         // Used to suggest if a user writes `foo!(a b);`
199         if let Some(ref stream) = self.0 {
200             let mut suggestion = None;
201             let mut iter = stream.iter().enumerate().peekable();
202             while let Some((pos, ts)) = iter.next() {
203                 if let Some((_, next)) = iter.peek() {
204                     let sp = match (&ts, &next) {
205                         (_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
206                         ((TokenTree::Token(token_left), NonJoint),
207                          (TokenTree::Token(token_right), _))
208                         if ((token_left.is_ident() && !token_left.is_reserved_ident())
209                             || token_left.is_lit()) &&
210                             ((token_right.is_ident() && !token_right.is_reserved_ident())
211                             || token_right.is_lit()) => token_left.span,
212                         ((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
213                         _ => continue,
214                     };
215                     let sp = sp.shrink_to_hi();
216                     let comma = (TokenTree::token(token::Comma, sp), NonJoint);
217                     suggestion = Some((pos, comma, sp));
218                 }
219             }
220             if let Some((pos, comma, sp)) = suggestion {
221                 let mut new_stream = vec![];
222                 let parts = stream.split_at(pos + 1);
223                 new_stream.extend_from_slice(parts.0);
224                 new_stream.push(comma);
225                 new_stream.extend_from_slice(parts.1);
226                 return Some((TokenStream::new(new_stream), sp));
227             }
228         }
229         None
230     }
231 }
232
233 impl From<TokenTree> for TokenStream {
234     fn from(tree: TokenTree) -> TokenStream {
235         TokenStream::new(vec![(tree, NonJoint)])
236     }
237 }
238
239 impl From<TokenTree> for TreeAndJoint {
240     fn from(tree: TokenTree) -> TreeAndJoint {
241         (tree, NonJoint)
242     }
243 }
244
245 impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream {
246     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
247         TokenStream::from_streams(iter.into_iter().map(Into::into).collect::<SmallVec<_>>())
248     }
249 }
250
251 impl Eq for TokenStream {}
252
253 impl PartialEq<TokenStream> for TokenStream {
254     fn eq(&self, other: &TokenStream) -> bool {
255         self.trees().eq(other.trees())
256     }
257 }
258
259 impl TokenStream {
260     pub fn len(&self) -> usize {
261         if let Some(ref slice) = self.0 {
262             slice.len()
263         } else {
264             0
265         }
266     }
267
268     pub fn empty() -> TokenStream {
269         TokenStream(None)
270     }
271
272     pub fn is_empty(&self) -> bool {
273         match self.0 {
274             None => true,
275             Some(ref stream) => stream.is_empty(),
276         }
277     }
278
279     pub(crate) fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
280         match streams.len() {
281             0 => TokenStream::empty(),
282             1 => streams.pop().unwrap(),
283             _ => {
284                 // rust-lang/rust#57735: pre-allocate vector to avoid
285                 // quadratic blow-up due to on-the-fly reallocations.
286                 let tree_count = streams.iter()
287                     .map(|ts| match &ts.0 { None => 0, Some(s) => s.len() })
288                     .sum();
289                 let mut vec = Vec::with_capacity(tree_count);
290
291                 for stream in streams {
292                     match stream.0 {
293                         None => {},
294                         Some(stream2) => vec.extend(stream2.iter().cloned()),
295                     }
296                 }
297                 TokenStream::new(vec)
298             }
299         }
300     }
301
302     pub fn new(streams: Vec<TreeAndJoint>) -> TokenStream {
303         match streams.len() {
304             0 => TokenStream(None),
305             _ => TokenStream(Some(Lrc::new(streams))),
306         }
307     }
308
309     pub fn append_to_tree_and_joint_vec(self, vec: &mut Vec<TreeAndJoint>) {
310         if let Some(stream) = self.0 {
311             vec.extend(stream.iter().cloned());
312         }
313     }
314
315     pub fn trees(&self) -> Cursor {
316         self.clone().into_trees()
317     }
318
319     pub fn into_trees(self) -> Cursor {
320         Cursor::new(self)
321     }
322
323     /// Compares two TokenStreams, checking equality without regarding span information.
324     pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
325         let mut t1 = self.trees();
326         let mut t2 = other.trees();
327         for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
328             if !t1.eq_unspanned(&t2) {
329                 return false;
330             }
331         }
332         t1.next().is_none() && t2.next().is_none()
333     }
334
335     // See comments in `Nonterminal::to_tokenstream` for why we care about
336     // *probably* equal here rather than actual equality
337     //
338     // This is otherwise the same as `eq_unspanned`, only recursing with a
339     // different method.
340     pub fn probably_equal_for_proc_macro(&self, other: &TokenStream) -> bool {
341         // When checking for `probably_eq`, we ignore certain tokens that aren't
342         // preserved in the AST. Because they are not preserved, the pretty
343         // printer arbitrarily adds or removes them when printing as token
344         // streams, making a comparison between a token stream generated from an
345         // AST and a token stream which was parsed into an AST more reliable.
346         fn semantic_tree(tree: &TokenTree) -> bool {
347             if let TokenTree::Token(token) = tree {
348                 if let
349                     // The pretty printer tends to add trailing commas to
350                     // everything, and in particular, after struct fields.
351                     | token::Comma
352                     // The pretty printer emits `NoDelim` as whitespace.
353                     | token::OpenDelim(DelimToken::NoDelim)
354                     | token::CloseDelim(DelimToken::NoDelim)
355                     // The pretty printer collapses many semicolons into one.
356                     | token::Semi
357                     // The pretty printer collapses whitespace arbitrarily and can
358                     // introduce whitespace from `NoDelim`.
359                     | token::Whitespace
360                     // The pretty printer can turn `$crate` into `::crate_name`
361                     | token::ModSep = token.kind {
362                     return false;
363                 }
364             }
365             true
366         }
367
368         let mut t1 = self.trees().filter(semantic_tree);
369         let mut t2 = other.trees().filter(semantic_tree);
370         for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
371             if !t1.probably_equal_for_proc_macro(&t2) {
372                 return false;
373             }
374         }
375         t1.next().is_none() && t2.next().is_none()
376     }
377
378     pub fn map_enumerated<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
379         TokenStream(self.0.map(|stream| {
380             Lrc::new(
381                 stream
382                     .iter()
383                     .enumerate()
384                     .map(|(i, (tree, is_joint))| (f(i, tree.clone()), *is_joint))
385                     .collect())
386         }))
387     }
388
389     pub fn map<F: FnMut(TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
390         TokenStream(self.0.map(|stream| {
391             Lrc::new(
392                 stream
393                     .iter()
394                     .map(|(tree, is_joint)| (f(tree.clone()), *is_joint))
395                     .collect())
396         }))
397     }
398
399     fn first_tree_and_joint(&self) -> Option<TreeAndJoint> {
400         self.0.as_ref().map(|stream| {
401             stream.first().unwrap().clone()
402         })
403     }
404
405     fn last_tree_if_joint(&self) -> Option<TokenTree> {
406         match self.0 {
407             None => None,
408             Some(ref stream) => {
409                 if let (tree, Joint) = stream.last().unwrap() {
410                     Some(tree.clone())
411                 } else {
412                     None
413                 }
414             }
415         }
416     }
417 }
418
419 // 99.5%+ of the time we have 1 or 2 elements in this vector.
420 #[derive(Clone)]
421 pub struct TokenStreamBuilder(SmallVec<[TokenStream; 2]>);
422
423 impl TokenStreamBuilder {
424     pub fn new() -> TokenStreamBuilder {
425         TokenStreamBuilder(SmallVec::new())
426     }
427
428     pub fn push<T: Into<TokenStream>>(&mut self, stream: T) {
429         let stream = stream.into();
430         let last_tree_if_joint = self.0.last().and_then(TokenStream::last_tree_if_joint);
431         if let Some(TokenTree::Token(last_token)) = last_tree_if_joint {
432             if let Some((TokenTree::Token(token), is_joint)) = stream.first_tree_and_joint() {
433                 if let Some(glued_tok) = last_token.kind.glue(token.kind) {
434                     let last_stream = self.0.pop().unwrap();
435                     self.push_all_but_last_tree(&last_stream);
436                     let glued_span = last_token.span.to(token.span);
437                     let glued_tt = TokenTree::token(glued_tok, glued_span);
438                     let glued_tokenstream = TokenStream::new(vec![(glued_tt, is_joint)]);
439                     self.0.push(glued_tokenstream);
440                     self.push_all_but_first_tree(&stream);
441                     return
442                 }
443             }
444         }
445         self.0.push(stream);
446     }
447
448     pub fn build(self) -> TokenStream {
449         TokenStream::from_streams(self.0)
450     }
451
452     fn push_all_but_last_tree(&mut self, stream: &TokenStream) {
453         if let Some(ref streams) = stream.0 {
454             let len = streams.len();
455             match len {
456                 1 => {}
457                 _ => self.0.push(TokenStream(Some(Lrc::new(streams[0 .. len - 1].to_vec())))),
458             }
459         }
460     }
461
462     fn push_all_but_first_tree(&mut self, stream: &TokenStream) {
463         if let Some(ref streams) = stream.0 {
464             let len = streams.len();
465             match len {
466                 1 => {}
467                 _ => self.0.push(TokenStream(Some(Lrc::new(streams[1 .. len].to_vec())))),
468             }
469         }
470     }
471 }
472
473 #[derive(Clone)]
474 pub struct Cursor {
475     pub stream: TokenStream,
476     index: usize,
477 }
478
479 impl Iterator for Cursor {
480     type Item = TokenTree;
481
482     fn next(&mut self) -> Option<TokenTree> {
483         self.next_with_joint().map(|(tree, _)| tree)
484     }
485 }
486
487 impl Cursor {
488     fn new(stream: TokenStream) -> Self {
489         Cursor { stream, index: 0 }
490     }
491
492     pub fn next_with_joint(&mut self) -> Option<TreeAndJoint> {
493         match self.stream.0 {
494             None => None,
495             Some(ref stream) => {
496                 if self.index < stream.len() {
497                     self.index += 1;
498                     Some(stream[self.index - 1].clone())
499                 } else {
500                     None
501                 }
502             }
503         }
504     }
505
506     pub fn append(&mut self, new_stream: TokenStream) {
507         if new_stream.is_empty() {
508             return;
509         }
510         let index = self.index;
511         let stream = mem::replace(&mut self.stream, TokenStream(None));
512         *self = TokenStream::from_streams(smallvec![stream, new_stream]).into_trees();
513         self.index = index;
514     }
515
516     pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
517         match self.stream.0 {
518             None => None,
519             Some(ref stream) => stream[self.index ..].get(n).map(|(tree, _)| tree.clone()),
520         }
521     }
522 }
523
524 impl fmt::Display for TokenStream {
525     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
526         f.write_str(&pprust::tokens_to_string(self.clone()))
527     }
528 }
529
530 impl Encodable for TokenStream {
531     fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
532         self.trees().collect::<Vec<_>>().encode(encoder)
533     }
534 }
535
536 impl Decodable for TokenStream {
537     fn decode<D: Decoder>(decoder: &mut D) -> Result<TokenStream, D::Error> {
538         Vec::<TokenTree>::decode(decoder).map(|vec| vec.into_iter().collect())
539     }
540 }
541
542 #[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
543 pub struct DelimSpan {
544     pub open: Span,
545     pub close: Span,
546 }
547
548 impl DelimSpan {
549     pub fn from_single(sp: Span) -> Self {
550         DelimSpan {
551             open: sp,
552             close: sp,
553         }
554     }
555
556     pub fn from_pair(open: Span, close: Span) -> Self {
557         DelimSpan { open, close }
558     }
559
560     pub fn dummy() -> Self {
561         Self::from_single(DUMMY_SP)
562     }
563
564     pub fn entire(self) -> Span {
565         self.open.with_hi(self.close.hi())
566     }
567
568     pub fn apply_mark(self, mark: Mark) -> Self {
569         DelimSpan {
570             open: self.open.apply_mark(mark),
571             close: self.close.apply_mark(mark),
572         }
573     }
574 }
575
576 #[cfg(test)]
577 mod tests {
578     use super::*;
579     use crate::ast::Name;
580     use crate::with_default_globals;
581     use crate::util::parser_testing::string_to_stream;
582     use syntax_pos::{Span, BytePos, NO_EXPANSION};
583
584     fn string_to_ts(string: &str) -> TokenStream {
585         string_to_stream(string.to_owned())
586     }
587
588     fn sp(a: u32, b: u32) -> Span {
589         Span::new(BytePos(a), BytePos(b), NO_EXPANSION)
590     }
591
592     #[test]
593     fn test_concat() {
594         with_default_globals(|| {
595             let test_res = string_to_ts("foo::bar::baz");
596             let test_fst = string_to_ts("foo::bar");
597             let test_snd = string_to_ts("::baz");
598             let eq_res = TokenStream::from_streams(smallvec![test_fst, test_snd]);
599             assert_eq!(test_res.trees().count(), 5);
600             assert_eq!(eq_res.trees().count(), 5);
601             assert_eq!(test_res.eq_unspanned(&eq_res), true);
602         })
603     }
604
605     #[test]
606     fn test_to_from_bijection() {
607         with_default_globals(|| {
608             let test_start = string_to_ts("foo::bar(baz)");
609             let test_end = test_start.trees().collect();
610             assert_eq!(test_start, test_end)
611         })
612     }
613
614     #[test]
615     fn test_eq_0() {
616         with_default_globals(|| {
617             let test_res = string_to_ts("foo");
618             let test_eqs = string_to_ts("foo");
619             assert_eq!(test_res, test_eqs)
620         })
621     }
622
623     #[test]
624     fn test_eq_1() {
625         with_default_globals(|| {
626             let test_res = string_to_ts("::bar::baz");
627             let test_eqs = string_to_ts("::bar::baz");
628             assert_eq!(test_res, test_eqs)
629         })
630     }
631
632     #[test]
633     fn test_eq_3() {
634         with_default_globals(|| {
635             let test_res = string_to_ts("");
636             let test_eqs = string_to_ts("");
637             assert_eq!(test_res, test_eqs)
638         })
639     }
640
641     #[test]
642     fn test_diseq_0() {
643         with_default_globals(|| {
644             let test_res = string_to_ts("::bar::baz");
645             let test_eqs = string_to_ts("bar::baz");
646             assert_eq!(test_res == test_eqs, false)
647         })
648     }
649
650     #[test]
651     fn test_diseq_1() {
652         with_default_globals(|| {
653             let test_res = string_to_ts("(bar,baz)");
654             let test_eqs = string_to_ts("bar,baz");
655             assert_eq!(test_res == test_eqs, false)
656         })
657     }
658
659     #[test]
660     fn test_is_empty() {
661         with_default_globals(|| {
662             let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
663             let test1: TokenStream =
664                 TokenTree::token(token::Ident(Name::intern("a"), false), sp(0, 1)).into();
665             let test2 = string_to_ts("foo(bar::baz)");
666
667             assert_eq!(test0.is_empty(), true);
668             assert_eq!(test1.is_empty(), false);
669             assert_eq!(test2.is_empty(), false);
670         })
671     }
672
673     #[test]
674     fn test_dotdotdot() {
675         with_default_globals(|| {
676             let mut builder = TokenStreamBuilder::new();
677             builder.push(TokenTree::token(token::Dot, sp(0, 1)).joint());
678             builder.push(TokenTree::token(token::Dot, sp(1, 2)).joint());
679             builder.push(TokenTree::token(token::Dot, sp(2, 3)));
680             let stream = builder.build();
681             assert!(stream.eq_unspanned(&string_to_ts("...")));
682             assert_eq!(stream.trees().count(), 1);
683         })
684     }
685 }