@@ -157,9 +157,16 @@ pub struct RawToken {
157157#[ derive( Copy , Clone ) ]
158158pub struct Token < ' a > {
159159 raw : & ' a RawToken ,
160- i : & ' a SourceMap ,
160+ pub ( crate ) sm : & ' a SourceMap ,
161+ pub ( crate ) idx : usize ,
161162 offset : u32 ,
162- idx : u32 ,
163+ }
164+
165+ impl < ' a > Token < ' a > {
166+ /// The sourcemap this token is linked to.
167+ pub fn sourcemap ( & self ) -> & ' a SourceMap {
168+ self . sm
169+ }
163170}
164171
165172impl < ' a > PartialEq for Token < ' a > {
@@ -241,7 +248,7 @@ impl<'a> Token<'a> {
241248 if self . raw . src_id == !0 {
242249 None
243250 } else {
244- self . i . get_source ( self . raw . src_id )
251+ self . sm . get_source ( self . raw . src_id )
245252 }
246253 }
247254
@@ -255,7 +262,7 @@ impl<'a> Token<'a> {
255262 if self . raw . name_id == !0 {
256263 None
257264 } else {
258- self . i . get_name ( self . raw . name_id )
265+ self . sm . get_name ( self . raw . name_id )
259266 }
260267 }
261268
@@ -287,7 +294,7 @@ impl<'a> Token<'a> {
287294
288295 /// Returns the referenced source view.
289296 pub fn get_source_view ( & self ) -> Option < & SourceView > {
290- self . i . get_source_view ( self . get_src_id ( ) )
297+ self . sm . get_source_view ( self . get_src_id ( ) )
291298 }
292299
293300 /// If true, this token is a range token.
@@ -298,18 +305,10 @@ impl<'a> Token<'a> {
298305 }
299306}
300307
301- pub fn idx_from_token ( token : & Token < ' _ > ) -> u32 {
302- token. idx
303- }
304-
305- pub fn sourcemap_from_token < ' a > ( token : & Token < ' a > ) -> & ' a SourceMap {
306- token. i
307- }
308-
309308/// Iterates over all tokens in a sourcemap
310309pub struct TokenIter < ' a > {
311310 i : & ' a SourceMap ,
312- next_idx : u32 ,
311+ next_idx : usize ,
313312}
314313
315314impl < ' a > TokenIter < ' a > {
@@ -390,23 +389,6 @@ impl<'a> Iterator for NameIter<'a> {
390389 }
391390}
392391
393- /// Iterates over all index items in a sourcemap
394- pub struct IndexIter < ' a > {
395- i : & ' a SourceMap ,
396- next_idx : usize ,
397- }
398-
399- impl < ' a > Iterator for IndexIter < ' a > {
400- type Item = ( u32 , u32 , u32 ) ;
401-
402- fn next ( & mut self ) -> Option < ( u32 , u32 , u32 ) > {
403- self . i . index . get ( self . next_idx ) . map ( |idx| {
404- self . next_idx += 1 ;
405- * idx
406- } )
407- }
408- }
409-
410392impl < ' a > fmt:: Debug for Token < ' a > {
411393 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
412394 write ! ( f, "<Token {self:#}>" )
@@ -481,7 +463,6 @@ pub struct SourceMapIndex {
481463pub struct SourceMap {
482464 pub ( crate ) file : Option < Arc < str > > ,
483465 pub ( crate ) tokens : Vec < RawToken > ,
484- pub ( crate ) index : Vec < ( u32 , u32 , u32 ) > ,
485466 pub ( crate ) names : Vec < Arc < str > > ,
486467 pub ( crate ) source_root : Option < Arc < str > > ,
487468 pub ( crate ) sources : Vec < Arc < str > > ,
@@ -596,21 +577,15 @@ impl SourceMap {
596577 /// - `sources_content` optional source contents
597578 pub fn new (
598579 file : Option < Arc < str > > ,
599- tokens : Vec < RawToken > ,
580+ mut tokens : Vec < RawToken > ,
600581 names : Vec < Arc < str > > ,
601582 sources : Vec < Arc < str > > ,
602583 sources_content : Option < Vec < Option < Arc < str > > > > ,
603584 ) -> SourceMap {
604- let mut index: Vec < _ > = tokens
605- . iter ( )
606- . enumerate ( )
607- . map ( |( idx, token) | ( token. dst_line , token. dst_col , idx as u32 ) )
608- . collect ( ) ;
609- index. sort_unstable ( ) ;
585+ tokens. sort_unstable_by_key ( |t| ( t. dst_line , t. dst_col ) ) ;
610586 SourceMap {
611587 file,
612588 tokens,
613- index,
614589 names,
615590 source_root : None ,
616591 sources,
@@ -681,10 +656,10 @@ impl SourceMap {
681656 }
682657
683658 /// Looks up a token by its index.
684- pub fn get_token ( & self , idx : u32 ) -> Option < Token < ' _ > > {
685- self . tokens . get ( idx as usize ) . map ( |raw| Token {
659+ pub fn get_token ( & self , idx : usize ) -> Option < Token < ' _ > > {
660+ self . tokens . get ( idx) . map ( |raw| Token {
686661 raw,
687- i : self ,
662+ sm : self ,
688663 idx,
689664 offset : 0 ,
690665 } )
@@ -705,9 +680,15 @@ impl SourceMap {
705680
706681 /// Looks up the closest token to a given 0-indexed line and column.
707682 pub fn lookup_token ( & self , line : u32 , col : u32 ) -> Option < Token < ' _ > > {
708- let ii = greatest_lower_bound ( & self . index , & ( line, col) , |ii| ( ii. 0 , ii. 1 ) ) ?;
683+ let ( idx, raw) =
684+ greatest_lower_bound ( & self . tokens , & ( line, col) , |t| ( t. dst_line , t. dst_col ) ) ?;
709685
710- let mut token = self . get_token ( ii. 2 ) ?;
686+ let mut token = Token {
687+ raw,
688+ sm : self ,
689+ idx,
690+ offset : 0 ,
691+ } ;
711692
712693 if token. is_range ( ) {
713694 token. offset = col - token. get_dst_col ( ) ;
@@ -827,19 +808,6 @@ impl SourceMap {
827808 self . names . clear ( ) ;
828809 }
829810
830- /// Returns the number of items in the index
831- pub fn get_index_size ( & self ) -> usize {
832- self . index . len ( )
833- }
834-
835- /// Returns the number of items in the index
836- pub fn index_iter ( & self ) -> IndexIter < ' _ > {
837- IndexIter {
838- i : self ,
839- next_idx : 0 ,
840- }
841- }
842-
843811 /// This rewrites the sourcemap according to the provided rewrite
844812 /// options.
845813 ///
@@ -998,7 +966,6 @@ impl SourceMap {
998966 // the `self` tokens and `src_line/col` for the `adjustment` tokens.
999967 let self_tokens = std:: mem:: take ( & mut self . tokens ) ;
1000968 let original_ranges = create_ranges ( self_tokens, |t| ( t. dst_line , t. dst_col ) ) ;
1001- self . index . clear ( ) ;
1002969 let adjustment_ranges =
1003970 create_ranges ( adjustment. tokens . clone ( ) , |t| ( t. src_line , t. src_col ) ) ;
1004971
@@ -1059,14 +1026,8 @@ impl SourceMap {
10591026 }
10601027 }
10611028
1062- // Regenerate the index
1063- self . index . extend (
1064- self . tokens
1065- . iter ( )
1066- . enumerate ( )
1067- . map ( |( idx, token) | ( token. dst_line , token. dst_col , idx as u32 ) ) ,
1068- ) ;
1069- self . index . sort_unstable ( ) ;
1029+ self . tokens
1030+ . sort_unstable_by_key ( |t| ( t. dst_line , t. dst_col ) ) ;
10701031 }
10711032}
10721033
@@ -1190,7 +1151,7 @@ impl SourceMapIndex {
11901151 /// If a sourcemap is encountered that is not embedded but just
11911152 /// externally referenced it is silently skipped.
11921153 pub fn lookup_token ( & self , line : u32 , col : u32 ) -> Option < Token < ' _ > > {
1193- let section =
1154+ let ( _section_idx , section) =
11941155 greatest_lower_bound ( & self . sections , & ( line, col) , SourceMapSection :: get_offset) ?;
11951156 let map = section. get_sourcemap ( ) ?;
11961157 let ( off_line, off_col) = section. get_offset ( ) ;
0 commit comments