@@ -141,6 +141,7 @@ impl FlatTree {
141
141
ident : Vec :: new ( ) ,
142
142
token_tree : Vec :: new ( ) ,
143
143
text : Vec :: new ( ) ,
144
+ version,
144
145
} ;
145
146
w. write ( subtree) ;
146
147
@@ -178,6 +179,7 @@ impl FlatTree {
178
179
ident : Vec :: new ( ) ,
179
180
token_tree : Vec :: new ( ) ,
180
181
text : Vec :: new ( ) ,
182
+ version,
181
183
} ;
182
184
w. write ( subtree) ;
183
185
@@ -228,6 +230,7 @@ impl FlatTree {
228
230
token_tree : self . token_tree ,
229
231
text : self . text ,
230
232
span_data_table,
233
+ version,
231
234
}
232
235
. read ( )
233
236
}
@@ -253,6 +256,7 @@ impl FlatTree {
253
256
token_tree : self . token_tree ,
254
257
text : self . text ,
255
258
span_data_table : & ( ) ,
259
+ version,
256
260
}
257
261
. read ( )
258
262
}
@@ -386,8 +390,9 @@ impl InternableSpan for Span {
386
390
387
391
struct Writer < ' a , ' span , S : InternableSpan > {
388
392
work : VecDeque < ( usize , & ' a tt:: Subtree < S > ) > ,
389
- string_table : FxHashMap < & ' a str , u32 > ,
393
+ string_table : FxHashMap < std :: borrow :: Cow < ' a , str > , u32 > ,
390
394
span_data_table : & ' span mut S :: Table ,
395
+ version : u32 ,
391
396
392
397
subtree : Vec < SubtreeRepr > ,
393
398
literal : Vec < LiteralRepr > ,
@@ -425,9 +430,15 @@ impl<'a, 'span, S: InternableSpan> Writer<'a, 'span, S> {
425
430
tt:: TokenTree :: Leaf ( leaf) => match leaf {
426
431
tt:: Leaf :: Literal ( lit) => {
427
432
let idx = self . literal . len ( ) as u32 ;
428
- let text = self . intern ( & lit. text ) ;
429
433
let id = self . token_id_of ( lit. span ) ;
430
- let suffix = lit. suffix . as_ref ( ) . map ( |s| self . intern ( s) ) . unwrap_or ( !0 ) ;
434
+ let ( text, suffix) = if self . version >= EXTENDED_LEAF_DATA {
435
+ (
436
+ self . intern ( & lit. text ) ,
437
+ lit. suffix . as_ref ( ) . map ( |s| self . intern ( s) ) . unwrap_or ( !0 ) ,
438
+ )
439
+ } else {
440
+ ( self . intern_owned ( format ! ( "{lit}" ) ) , !0 )
441
+ } ;
431
442
self . literal . push ( LiteralRepr {
432
443
id,
433
444
text,
@@ -456,13 +467,15 @@ impl<'a, 'span, S: InternableSpan> Writer<'a, 'span, S> {
456
467
}
457
468
tt:: Leaf :: Ident ( ident) => {
458
469
let idx = self . ident . len ( ) as u32 ;
459
- let text = self . intern ( & ident. text ) ;
460
470
let id = self . token_id_of ( ident. span ) ;
461
- self . ident . push ( IdentRepr {
462
- id,
463
- text,
464
- is_raw : ident. is_raw == tt:: IdentIsRaw :: Yes ,
465
- } ) ;
471
+ let text = if self . version >= EXTENDED_LEAF_DATA {
472
+ self . intern ( & ident. text )
473
+ } else if ident. is_raw . yes ( ) {
474
+ self . intern_owned ( format ! ( "r#{}" , ident. text, ) )
475
+ } else {
476
+ self . intern ( & ident. text )
477
+ } ;
478
+ self . ident . push ( IdentRepr { id, text, is_raw : ident. is_raw . yes ( ) } ) ;
466
479
idx << 2 | 0b11
467
480
}
468
481
} ,
@@ -484,15 +497,25 @@ impl<'a, 'span, S: InternableSpan> Writer<'a, 'span, S> {
484
497
485
498
pub ( crate ) fn intern ( & mut self , text : & ' a str ) -> u32 {
486
499
let table = & mut self . text ;
487
- * self . string_table . entry ( text) . or_insert_with ( || {
500
+ * self . string_table . entry ( text. into ( ) ) . or_insert_with ( || {
488
501
let idx = table. len ( ) ;
489
502
table. push ( text. to_owned ( ) ) ;
490
503
idx as u32
491
504
} )
492
505
}
506
+
507
+ pub ( crate ) fn intern_owned ( & mut self , text : String ) -> u32 {
508
+ let table = & mut self . text ;
509
+ * self . string_table . entry ( text. clone ( ) . into ( ) ) . or_insert_with ( || {
510
+ let idx = table. len ( ) ;
511
+ table. push ( text) ;
512
+ idx as u32
513
+ } )
514
+ }
493
515
}
494
516
495
517
struct Reader < ' span , S : InternableSpan > {
518
+ version : u32 ,
496
519
subtree : Vec < SubtreeRepr > ,
497
520
literal : Vec < LiteralRepr > ,
498
521
punct : Vec < PunctRepr > ,
@@ -528,30 +551,36 @@ impl<'span, S: InternableSpan> Reader<'span, S> {
528
551
0b01 => {
529
552
use tt:: LitKind :: * ;
530
553
let repr = & self . literal [ idx] ;
531
- tt:: Leaf :: Literal ( tt:: Literal {
532
- text : self . text [ repr. text as usize ] . as_str ( ) . into ( ) ,
533
- span : read_span ( repr. id ) ,
534
- kind : match u16:: to_le_bytes ( repr. kind ) {
535
- [ 0 , _] => Err ( ( ) ) ,
536
- [ 1 , _] => Byte ,
537
- [ 2 , _] => Char ,
538
- [ 3 , _] => Integer ,
539
- [ 4 , _] => Float ,
540
- [ 5 , _] => Str ,
541
- [ 6 , r] => StrRaw ( r) ,
542
- [ 7 , _] => ByteStr ,
543
- [ 8 , r] => ByteStrRaw ( r) ,
544
- [ 9 , _] => CStr ,
545
- [ 10 , r] => CStrRaw ( r) ,
546
- _ => unreachable ! ( ) ,
547
- } ,
548
- suffix : if repr. suffix != !0 {
549
- Some ( Box :: new (
550
- self . text [ repr. suffix as usize ] . as_str ( ) . into ( ) ,
551
- ) )
552
- } else {
553
- None
554
- } ,
554
+ let text = self . text [ repr. text as usize ] . as_str ( ) ;
555
+ let span = read_span ( repr. id ) ;
556
+ tt:: Leaf :: Literal ( if self . version >= EXTENDED_LEAF_DATA {
557
+ tt:: Literal {
558
+ text : text. into ( ) ,
559
+ span,
560
+ kind : match u16:: to_le_bytes ( repr. kind ) {
561
+ [ 0 , _] => Err ( ( ) ) ,
562
+ [ 1 , _] => Byte ,
563
+ [ 2 , _] => Char ,
564
+ [ 3 , _] => Integer ,
565
+ [ 4 , _] => Float ,
566
+ [ 5 , _] => Str ,
567
+ [ 6 , r] => StrRaw ( r) ,
568
+ [ 7 , _] => ByteStr ,
569
+ [ 8 , r] => ByteStrRaw ( r) ,
570
+ [ 9 , _] => CStr ,
571
+ [ 10 , r] => CStrRaw ( r) ,
572
+ _ => unreachable ! ( ) ,
573
+ } ,
574
+ suffix : if repr. suffix != !0 {
575
+ Some ( Box :: new (
576
+ self . text [ repr. suffix as usize ] . as_str ( ) . into ( ) ,
577
+ ) )
578
+ } else {
579
+ None
580
+ } ,
581
+ }
582
+ } else {
583
+ mbe:: token_to_literal ( text. into ( ) , span)
555
584
} )
556
585
. into ( )
557
586
}
@@ -566,14 +595,23 @@ impl<'span, S: InternableSpan> Reader<'span, S> {
566
595
}
567
596
0b11 => {
568
597
let repr = & self . ident [ idx] ;
598
+ let text = self . text [ repr. text as usize ] . as_str ( ) ;
599
+ let ( is_raw, text) = if self . version >= EXTENDED_LEAF_DATA {
600
+ (
601
+ if repr. is_raw {
602
+ tt:: IdentIsRaw :: Yes
603
+ } else {
604
+ tt:: IdentIsRaw :: No
605
+ } ,
606
+ text,
607
+ )
608
+ } else {
609
+ tt:: IdentIsRaw :: split_from_symbol ( & text)
610
+ } ;
569
611
tt:: Leaf :: Ident ( tt:: Ident {
570
- text : self . text [ repr . text as usize ] . as_str ( ) . into ( ) ,
612
+ text : text. into ( ) ,
571
613
span : read_span ( repr. id ) ,
572
- is_raw : if repr. is_raw {
573
- tt:: IdentIsRaw :: Yes
574
- } else {
575
- tt:: IdentIsRaw :: No
576
- } ,
614
+ is_raw,
577
615
} )
578
616
. into ( )
579
617
}
0 commit comments