13
13
//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
14
14
//! ownership of the original.
15
15
16
- use crate :: token:: { self , DelimToken , Token , TokenKind } ;
16
+ use crate :: token:: { self , DelimToken , Spacing , Token , TokenKind } ;
17
17
18
18
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
19
19
use rustc_data_structures:: sync:: Lrc ;
@@ -82,10 +82,6 @@ impl TokenTree {
82
82
}
83
83
}
84
84
85
- pub fn joint ( self ) -> TokenStream {
86
- TokenStream :: new ( vec ! [ ( self , Joint ) ] )
87
- }
88
-
89
85
pub fn token ( kind : TokenKind , span : Span ) -> TokenTree {
90
86
TokenTree :: Token ( Token :: new ( kind, span) )
91
87
}
@@ -125,22 +121,12 @@ where
125
121
/// instead of a representation of the abstract syntax tree.
126
122
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
127
123
#[ derive( Clone , Debug , Default , RustcEncodable , RustcDecodable ) ]
128
- pub struct TokenStream ( pub Lrc < Vec < TreeAndJoint > > ) ;
129
-
130
- pub type TreeAndJoint = ( TokenTree , IsJoint ) ;
124
+ pub struct TokenStream ( pub Lrc < Vec < TokenTree > > ) ;
131
125
132
126
// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
133
127
#[ cfg( target_arch = "x86_64" ) ]
134
128
rustc_data_structures:: static_assert_size!( TokenStream , 8 ) ;
135
129
136
- #[ derive( Clone , Copy , Debug , PartialEq , RustcEncodable , RustcDecodable ) ]
137
- pub enum IsJoint {
138
- Joint ,
139
- NonJoint ,
140
- }
141
-
142
- use IsJoint :: * ;
143
-
144
130
impl TokenStream {
145
131
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
146
132
/// separating the two arguments with a comma for diagnostic suggestions.
@@ -151,22 +137,22 @@ impl TokenStream {
151
137
while let Some ( ( pos, ts) ) = iter. next ( ) {
152
138
if let Some ( ( _, next) ) = iter. peek ( ) {
153
139
let sp = match ( & ts, & next) {
154
- ( _, ( TokenTree :: Token ( Token { kind : token:: Comma , .. } ) , _ ) ) => continue ,
140
+ ( _, TokenTree :: Token ( Token { kind : token:: Comma , .. } ) ) => continue ,
155
141
(
156
- ( TokenTree :: Token ( token_left) , NonJoint ) ,
157
- ( TokenTree :: Token ( token_right) , _ ) ,
142
+ TokenTree :: Token ( token_left @ Token { spacing : Spacing :: Alone , .. } ) ,
143
+ TokenTree :: Token ( token_right) ,
158
144
) if ( ( token_left. is_ident ( ) && !token_left. is_reserved_ident ( ) )
159
145
|| token_left. is_lit ( ) )
160
146
&& ( ( token_right. is_ident ( ) && !token_right. is_reserved_ident ( ) )
161
147
|| token_right. is_lit ( ) ) =>
162
148
{
163
149
token_left. span
164
150
}
165
- ( ( TokenTree :: Delimited ( sp, ..) , NonJoint ) , _) => sp. entire ( ) ,
151
+ ( TokenTree :: Delimited ( sp, ..) , _) => sp. entire ( ) ,
166
152
_ => continue ,
167
153
} ;
168
154
let sp = sp. shrink_to_hi ( ) ;
169
- let comma = ( TokenTree :: token ( token:: Comma , sp) , NonJoint ) ;
155
+ let comma = TokenTree :: token ( token:: Comma , sp) ;
170
156
suggestion = Some ( ( pos, comma, sp) ) ;
171
157
}
172
158
}
@@ -184,19 +170,13 @@ impl TokenStream {
184
170
185
171
impl From < TokenTree > for TokenStream {
186
172
fn from ( tree : TokenTree ) -> TokenStream {
187
- TokenStream :: new ( vec ! [ ( tree, NonJoint ) ] )
188
- }
189
- }
190
-
191
- impl From < TokenTree > for TreeAndJoint {
192
- fn from ( tree : TokenTree ) -> TreeAndJoint {
193
- ( tree, NonJoint )
173
+ TokenStream :: new ( vec ! [ ( tree) ] )
194
174
}
195
175
}
196
176
197
177
impl iter:: FromIterator < TokenTree > for TokenStream {
198
178
fn from_iter < I : IntoIterator < Item = TokenTree > > ( iter : I ) -> Self {
199
- TokenStream :: new ( iter. into_iter ( ) . map ( Into :: into) . collect :: < Vec < TreeAndJoint > > ( ) )
179
+ TokenStream :: new ( iter. into_iter ( ) . map ( Into :: into) . collect :: < Vec < TokenTree > > ( ) )
200
180
}
201
181
}
202
182
@@ -209,7 +189,7 @@ impl PartialEq<TokenStream> for TokenStream {
209
189
}
210
190
211
191
impl TokenStream {
212
- pub fn new ( streams : Vec < TreeAndJoint > ) -> TokenStream {
192
+ pub fn new ( streams : Vec < TokenTree > ) -> TokenStream {
213
193
TokenStream ( Lrc :: new ( streams) )
214
194
}
215
195
@@ -224,8 +204,8 @@ impl TokenStream {
224
204
pub fn span ( & self ) -> Option < Span > {
225
205
match & * * self . 0 {
226
206
[ ] => None ,
227
- [ ( tt , _ ) ] => Some ( tt. span ( ) ) ,
228
- [ ( tt_start, _ ) , .., ( tt_end, _ ) ] => Some ( tt_start. span ( ) . to ( tt_end. span ( ) ) ) ,
207
+ [ tt ] => Some ( tt. span ( ) ) ,
208
+ [ tt_start, .., tt_end] => Some ( tt_start. span ( ) . to ( tt_end. span ( ) ) ) ,
229
209
}
230
210
}
231
211
@@ -290,18 +270,12 @@ impl TokenStream {
290
270
291
271
pub fn map_enumerated < F : FnMut ( usize , TokenTree ) -> TokenTree > ( self , mut f : F ) -> TokenStream {
292
272
TokenStream ( Lrc :: new (
293
- self . 0
294
- . iter ( )
295
- . enumerate ( )
296
- . map ( |( i, ( tree, is_joint) ) | ( f ( i, tree. clone ( ) ) , * is_joint) )
297
- . collect ( ) ,
273
+ self . 0 . iter ( ) . enumerate ( ) . map ( |( i, tree) | f ( i, tree. clone ( ) ) ) . collect ( ) ,
298
274
) )
299
275
}
300
276
301
277
pub fn map < F : FnMut ( TokenTree ) -> TokenTree > ( self , mut f : F ) -> TokenStream {
302
- TokenStream ( Lrc :: new (
303
- self . 0 . iter ( ) . map ( |( tree, is_joint) | ( f ( tree. clone ( ) ) , * is_joint) ) . collect ( ) ,
304
- ) )
278
+ TokenStream ( Lrc :: new ( self . 0 . iter ( ) . map ( |tree| f ( tree. clone ( ) ) ) . collect ( ) ) )
305
279
}
306
280
}
307
281
@@ -320,11 +294,11 @@ impl TokenStreamBuilder {
320
294
// If `self` is not empty and the last tree within the last stream is a
321
295
// token tree marked with `Joint`...
322
296
if let Some ( TokenStream ( ref mut last_stream_lrc) ) = self . 0 . last_mut ( ) {
323
- if let Some ( ( TokenTree :: Token ( last_token) , Joint ) ) = last_stream_lrc. last ( ) {
297
+ if let Some ( TokenTree :: Token ( last_token) ) = last_stream_lrc. last ( ) {
324
298
// ...and `stream` is not empty and the first tree within it is
325
299
// a token tree...
326
300
let TokenStream ( ref mut stream_lrc) = stream;
327
- if let Some ( ( TokenTree :: Token ( token) , is_joint ) ) = stream_lrc. first ( ) {
301
+ if let Some ( TokenTree :: Token ( token) ) = stream_lrc. first ( ) {
328
302
// ...and the two tokens can be glued together...
329
303
if let Some ( glued_tok) = last_token. glue ( & token) {
330
304
// ...then do so, by overwriting the last token
@@ -337,8 +311,7 @@ impl TokenStreamBuilder {
337
311
// Overwrite the last token tree with the merged
338
312
// token.
339
313
let last_vec_mut = Lrc :: make_mut ( last_stream_lrc) ;
340
- * last_vec_mut. last_mut ( ) . unwrap ( ) =
341
- ( TokenTree :: Token ( glued_tok) , * is_joint) ;
314
+ * last_vec_mut. last_mut ( ) . unwrap ( ) = TokenTree :: Token ( glued_tok) ;
342
315
343
316
// Remove the first token tree from `stream`. (This
344
317
// is almost always the only tree in `stream`.)
@@ -375,23 +348,19 @@ impl Iterator for Cursor {
375
348
type Item = TokenTree ;
376
349
377
350
fn next ( & mut self ) -> Option < TokenTree > {
378
- self . next_with_joint ( ) . map ( |( tree, _) | tree)
379
- }
380
- }
381
-
382
- impl Cursor {
383
- fn new ( stream : TokenStream ) -> Self {
384
- Cursor { stream, index : 0 }
385
- }
386
-
387
- pub fn next_with_joint ( & mut self ) -> Option < TreeAndJoint > {
388
351
if self . index < self . stream . len ( ) {
389
352
self . index += 1 ;
390
353
Some ( self . stream . 0 [ self . index - 1 ] . clone ( ) )
391
354
} else {
392
355
None
393
356
}
394
357
}
358
+ }
359
+
360
+ impl Cursor {
361
+ fn new ( stream : TokenStream ) -> Self {
362
+ Cursor { stream, index : 0 }
363
+ }
395
364
396
365
pub fn append ( & mut self , new_stream : TokenStream ) {
397
366
if new_stream. is_empty ( ) {
@@ -404,7 +373,7 @@ impl Cursor {
404
373
}
405
374
406
375
pub fn look_ahead ( & self , n : usize ) -> Option < TokenTree > {
407
- self . stream . 0 [ self . index ..] . get ( n) . map ( | ( tree , _ ) | tree . clone ( ) )
376
+ self . stream . 0 [ self . index ..] . get ( n) . cloned ( )
408
377
}
409
378
}
410
379
0 commit comments