1313//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
1414//! ownership of the original.
1515
16- use crate :: token:: { self , DelimToken , Token , TokenKind } ;
16+ use crate :: token:: { self , DelimToken , Spacing , Token , TokenKind } ;
1717
1818use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
1919use rustc_data_structures:: sync:: Lrc ;
@@ -82,10 +82,6 @@ impl TokenTree {
8282 }
8383 }
8484
85- pub fn joint ( self ) -> TokenStream {
86- TokenStream :: new ( vec ! [ ( self , Joint ) ] )
87- }
88-
8985 pub fn token ( kind : TokenKind , span : Span ) -> TokenTree {
9086 TokenTree :: Token ( Token :: new ( kind, span) )
9187 }
@@ -125,22 +121,12 @@ where
125121/// instead of a representation of the abstract syntax tree.
126122/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
127123#[ 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 > > ) ;
131125
132126// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
133127#[ cfg( target_arch = "x86_64" ) ]
134128rustc_data_structures:: static_assert_size!( TokenStream , 8 ) ;
135129
136- #[ derive( Clone , Copy , Debug , PartialEq , RustcEncodable , RustcDecodable ) ]
137- pub enum IsJoint {
138- Joint ,
139- NonJoint ,
140- }
141-
142- use IsJoint :: * ;
143-
144130impl TokenStream {
145131 /// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
146132 /// separating the two arguments with a comma for diagnostic suggestions.
@@ -151,22 +137,22 @@ impl TokenStream {
151137 while let Some ( ( pos, ts) ) = iter. next ( ) {
152138 if let Some ( ( _, next) ) = iter. peek ( ) {
153139 let sp = match ( & ts, & next) {
154- ( _, ( TokenTree :: Token ( Token { kind : token:: Comma , .. } ) , _ ) ) => continue ,
140+ ( _, TokenTree :: Token ( Token { kind : token:: Comma , .. } ) ) => continue ,
155141 (
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) ,
158144 ) if ( ( token_left. is_ident ( ) && !token_left. is_reserved_ident ( ) )
159145 || token_left. is_lit ( ) )
160146 && ( ( token_right. is_ident ( ) && !token_right. is_reserved_ident ( ) )
161147 || token_right. is_lit ( ) ) =>
162148 {
163149 token_left. span
164150 }
165- ( ( TokenTree :: Delimited ( sp, ..) , NonJoint ) , _) => sp. entire ( ) ,
151+ ( TokenTree :: Delimited ( sp, ..) , _) => sp. entire ( ) ,
166152 _ => continue ,
167153 } ;
168154 let sp = sp. shrink_to_hi ( ) ;
169- let comma = ( TokenTree :: token ( token:: Comma , sp) , NonJoint ) ;
155+ let comma = TokenTree :: token ( token:: Comma , sp) ;
170156 suggestion = Some ( ( pos, comma, sp) ) ;
171157 }
172158 }
@@ -184,19 +170,13 @@ impl TokenStream {
184170
185171impl From < TokenTree > for TokenStream {
186172 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) ] )
194174 }
195175}
196176
197177impl iter:: FromIterator < TokenTree > for TokenStream {
198178 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 > > ( ) )
200180 }
201181}
202182
@@ -209,7 +189,7 @@ impl PartialEq<TokenStream> for TokenStream {
209189}
210190
211191impl TokenStream {
212- pub fn new ( streams : Vec < TreeAndJoint > ) -> TokenStream {
192+ pub fn new ( streams : Vec < TokenTree > ) -> TokenStream {
213193 TokenStream ( Lrc :: new ( streams) )
214194 }
215195
@@ -224,8 +204,8 @@ impl TokenStream {
224204 pub fn span ( & self ) -> Option < Span > {
225205 match & * * self . 0 {
226206 [ ] => 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 ( ) ) ) ,
229209 }
230210 }
231211
@@ -290,18 +270,12 @@ impl TokenStream {
290270
291271 pub fn map_enumerated < F : FnMut ( usize , TokenTree ) -> TokenTree > ( self , mut f : F ) -> TokenStream {
292272 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 ( ) ,
298274 ) )
299275 }
300276
301277 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 ( ) ) )
305279 }
306280}
307281
@@ -320,11 +294,11 @@ impl TokenStreamBuilder {
320294 // If `self` is not empty and the last tree within the last stream is a
321295 // token tree marked with `Joint`...
322296 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 ( ) {
324298 // ...and `stream` is not empty and the first tree within it is
325299 // a token tree...
326300 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 ( ) {
328302 // ...and the two tokens can be glued together...
329303 if let Some ( glued_tok) = last_token. glue ( & token) {
330304 // ...then do so, by overwriting the last token
@@ -337,8 +311,7 @@ impl TokenStreamBuilder {
337311 // Overwrite the last token tree with the merged
338312 // token.
339313 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) ;
342315
343316 // Remove the first token tree from `stream`. (This
344317 // is almost always the only tree in `stream`.)
@@ -375,23 +348,19 @@ impl Iterator for Cursor {
375348 type Item = TokenTree ;
376349
377350 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 > {
388351 if self . index < self . stream . len ( ) {
389352 self . index += 1 ;
390353 Some ( self . stream . 0 [ self . index - 1 ] . clone ( ) )
391354 } else {
392355 None
393356 }
394357 }
358+ }
359+
360+ impl Cursor {
361+ fn new ( stream : TokenStream ) -> Self {
362+ Cursor { stream, index : 0 }
363+ }
395364
396365 pub fn append ( & mut self , new_stream : TokenStream ) {
397366 if new_stream. is_empty ( ) {
@@ -404,7 +373,7 @@ impl Cursor {
404373 }
405374
406375 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 ( )
408377 }
409378}
410379
0 commit comments