@@ -15,6 +15,7 @@ mod tokentrees;
15
15
mod unescape_error_reporting;
16
16
mod unicode_chars;
17
17
18
+ use rustc_lexer:: unescape:: Mode ;
18
19
use unescape_error_reporting:: { emit_unescape_error, push_escaped_char} ;
19
20
20
21
#[ derive( Clone , Debug ) ]
@@ -326,38 +327,27 @@ impl<'a> StringReader<'a> {
326
327
suffix_start : BytePos ,
327
328
kind : rustc_lexer:: LiteralKind ,
328
329
) -> ( token:: LitKind , Symbol ) {
329
- match kind {
330
+ // prefix means `"` or `br"` or `r###"`, ...
331
+ let ( lit_kind, mode, prefix_len, postfix_len) = match kind {
330
332
rustc_lexer:: LiteralKind :: Char { terminated } => {
331
333
if !terminated {
332
334
self . fatal_span_ ( start, suffix_start, "unterminated character literal" ) . raise ( )
333
335
}
334
- let content_start = start + BytePos ( 1 ) ;
335
- let content_end = suffix_start - BytePos ( 1 ) ;
336
- self . validate_char_escape ( content_start, content_end) ;
337
- let id = self . symbol_from_to ( content_start, content_end) ;
338
- ( token:: Char , id)
336
+ ( token:: Char , Mode :: Char , 1 , 1 ) // ' '
339
337
}
340
338
rustc_lexer:: LiteralKind :: Byte { terminated } => {
341
339
if !terminated {
342
340
self . fatal_span_ ( start + BytePos ( 1 ) , suffix_start, "unterminated byte constant" )
343
341
. raise ( )
344
342
}
345
- let content_start = start + BytePos ( 2 ) ;
346
- let content_end = suffix_start - BytePos ( 1 ) ;
347
- self . validate_byte_escape ( content_start, content_end) ;
348
- let id = self . symbol_from_to ( content_start, content_end) ;
349
- ( token:: Byte , id)
343
+ ( token:: Byte , Mode :: Byte , 2 , 1 ) // b' '
350
344
}
351
345
rustc_lexer:: LiteralKind :: Str { terminated } => {
352
346
if !terminated {
353
347
self . fatal_span_ ( start, suffix_start, "unterminated double quote string" )
354
348
. raise ( )
355
349
}
356
- let content_start = start + BytePos ( 1 ) ;
357
- let content_end = suffix_start - BytePos ( 1 ) ;
358
- self . validate_str_escape ( content_start, content_end) ;
359
- let id = self . symbol_from_to ( content_start, content_end) ;
360
- ( token:: Str , id)
350
+ ( token:: Str , Mode :: Str , 1 , 1 ) // " "
361
351
}
362
352
rustc_lexer:: LiteralKind :: ByteStr { terminated } => {
363
353
if !terminated {
@@ -368,42 +358,28 @@ impl<'a> StringReader<'a> {
368
358
)
369
359
. raise ( )
370
360
}
371
- let content_start = start + BytePos ( 2 ) ;
372
- let content_end = suffix_start - BytePos ( 1 ) ;
373
- self . validate_byte_str_escape ( content_start, content_end) ;
374
- let id = self . symbol_from_to ( content_start, content_end) ;
375
- ( token:: ByteStr , id)
361
+ ( token:: ByteStr , Mode :: ByteStr , 2 , 1 ) // b" "
376
362
}
377
363
rustc_lexer:: LiteralKind :: RawStr ( unvalidated_raw_str) => {
378
364
let valid_raw_str = self . validate_and_report_errors ( start, unvalidated_raw_str) ;
379
365
let n_hashes = valid_raw_str. num_hashes ( ) ;
380
366
let n = u32:: from ( n_hashes) ;
381
-
382
- let content_start = start + BytePos ( 2 + n) ;
383
- let content_end = suffix_start - BytePos ( 1 + n) ;
384
- self . validate_raw_str_escape ( content_start, content_end) ;
385
- let id = self . symbol_from_to ( content_start, content_end) ;
386
- ( token:: StrRaw ( n_hashes) , id)
367
+ ( token:: StrRaw ( n_hashes) , Mode :: RawStr , 2 + n, 1 + n) // r##" "##
387
368
}
388
369
rustc_lexer:: LiteralKind :: RawByteStr ( unvalidated_raw_str) => {
389
370
let validated_raw_str = self . validate_and_report_errors ( start, unvalidated_raw_str) ;
390
371
let n_hashes = validated_raw_str. num_hashes ( ) ;
391
372
let n = u32:: from ( n_hashes) ;
392
-
393
- let content_start = start + BytePos ( 3 + n) ;
394
- let content_end = suffix_start - BytePos ( 1 + n) ;
395
- self . validate_raw_byte_str_escape ( content_start, content_end) ;
396
- let id = self . symbol_from_to ( content_start, content_end) ;
397
- ( token:: ByteStrRaw ( n_hashes) , id)
373
+ ( token:: ByteStrRaw ( n_hashes) , Mode :: RawByteStr , 3 + n, 1 + n) // br##" "##
398
374
}
399
375
rustc_lexer:: LiteralKind :: Int { base, empty_int } => {
400
- if empty_int {
376
+ return if empty_int {
401
377
self . err_span_ ( start, suffix_start, "no valid digits found for number" ) ;
402
378
( token:: Integer , sym:: integer ( 0 ) )
403
379
} else {
404
380
self . validate_int_literal ( base, start, suffix_start) ;
405
381
( token:: Integer , self . symbol_from_to ( start, suffix_start) )
406
- }
382
+ } ;
407
383
}
408
384
rustc_lexer:: LiteralKind :: Float { base, empty_exponent } => {
409
385
if empty_exponent {
@@ -431,9 +407,14 @@ impl<'a> StringReader<'a> {
431
407
}
432
408
433
409
let id = self . symbol_from_to ( start, suffix_start) ;
434
- ( token:: Float , id)
410
+ return ( token:: Float , id) ;
435
411
}
436
- }
412
+ } ;
413
+ let content_start = start + BytePos ( prefix_len) ;
414
+ let content_end = suffix_start - BytePos ( postfix_len) ;
415
+ let id = self . symbol_from_to ( content_start, content_end) ;
416
+ self . validate_literal_escape ( mode, content_start, content_end) ;
417
+ return ( lit_kind, id) ;
437
418
}
438
419
439
420
#[ inline]
@@ -555,96 +536,23 @@ impl<'a> StringReader<'a> {
555
536
. raise ( ) ;
556
537
}
557
538
558
- fn validate_char_escape ( & self , content_start : BytePos , content_end : BytePos ) {
559
- let lit = self . str_from_to ( content_start, content_end) ;
560
- if let Err ( ( off, err) ) = unescape:: unescape_char ( lit) {
561
- emit_unescape_error (
562
- & self . sess . span_diagnostic ,
563
- lit,
564
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
565
- unescape:: Mode :: Char ,
566
- 0 ..off,
567
- err,
568
- )
569
- }
570
- }
571
-
572
- fn validate_byte_escape ( & self , content_start : BytePos , content_end : BytePos ) {
573
- let lit = self . str_from_to ( content_start, content_end) ;
574
- if let Err ( ( off, err) ) = unescape:: unescape_byte ( lit) {
575
- emit_unescape_error (
576
- & self . sess . span_diagnostic ,
577
- lit,
578
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
579
- unescape:: Mode :: Byte ,
580
- 0 ..off,
581
- err,
582
- )
583
- }
584
- }
585
-
586
- fn validate_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
587
- let lit = self . str_from_to ( content_start, content_end) ;
588
- unescape:: unescape_str ( lit, & mut |range, c| {
589
- if let Err ( err) = c {
539
+ fn validate_literal_escape ( & self , mode : Mode , content_start : BytePos , content_end : BytePos ) {
540
+ let lit_content = self . str_from_to ( content_start, content_end) ;
541
+ unescape:: unescape_literal ( lit_content, mode, & mut |range, result| {
542
+ // Here we only check for errors. The actual unescaping is done later.
543
+ if let Err ( err) = result {
544
+ let span_with_quotes =
545
+ self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ;
590
546
emit_unescape_error (
591
547
& self . sess . span_diagnostic ,
592
- lit ,
593
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
594
- unescape :: Mode :: Str ,
548
+ lit_content ,
549
+ span_with_quotes ,
550
+ mode ,
595
551
range,
596
552
err,
597
- )
598
- }
599
- } )
600
- }
601
-
602
- fn validate_raw_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
603
- let lit = self . str_from_to ( content_start, content_end) ;
604
- unescape:: unescape_raw_str ( lit, & mut |range, c| {
605
- if let Err ( err) = c {
606
- emit_unescape_error (
607
- & self . sess . span_diagnostic ,
608
- lit,
609
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
610
- unescape:: Mode :: Str ,
611
- range,
612
- err,
613
- )
614
- }
615
- } )
616
- }
617
-
618
- fn validate_raw_byte_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
619
- let lit = self . str_from_to ( content_start, content_end) ;
620
- unescape:: unescape_raw_byte_str ( lit, & mut |range, c| {
621
- if let Err ( err) = c {
622
- emit_unescape_error (
623
- & self . sess . span_diagnostic ,
624
- lit,
625
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
626
- unescape:: Mode :: ByteStr ,
627
- range,
628
- err,
629
- )
630
- }
631
- } )
632
- }
633
-
634
- fn validate_byte_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
635
- let lit = self . str_from_to ( content_start, content_end) ;
636
- unescape:: unescape_byte_str ( lit, & mut |range, c| {
637
- if let Err ( err) = c {
638
- emit_unescape_error (
639
- & self . sess . span_diagnostic ,
640
- lit,
641
- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
642
- unescape:: Mode :: ByteStr ,
643
- range,
644
- err,
645
- )
553
+ ) ;
646
554
}
647
- } )
555
+ } ) ;
648
556
}
649
557
650
558
fn validate_int_literal ( & self , base : Base , content_start : BytePos , content_end : BytePos ) {
0 commit comments