@@ -363,112 +363,103 @@ impl<'a> StringReader<'a> {
363
363
fn cook_lexer_literal (
364
364
& self ,
365
365
start : BytePos ,
366
- suffix_start : BytePos ,
366
+ end : BytePos ,
367
367
kind : rustc_lexer:: LiteralKind ,
368
368
) -> ( token:: LitKind , Symbol ) {
369
- // prefix means `"` or `br"` or `r###"`, ...
370
- let ( lit_kind, mode, prefix_len, postfix_len) = match kind {
369
+ match kind {
371
370
rustc_lexer:: LiteralKind :: Char { terminated } => {
372
371
if !terminated {
373
372
self . sess . span_diagnostic . span_fatal_with_code (
374
- self . mk_sp ( start, suffix_start ) ,
373
+ self . mk_sp ( start, end ) ,
375
374
"unterminated character literal" ,
376
375
error_code ! ( E0762 ) ,
377
376
)
378
377
}
379
- ( token:: Char , Mode :: Char , 1 , 1 ) // ' '
378
+ self . cook_quoted ( token:: Char , Mode :: Char , start , end , 1 , 1 ) // ' '
380
379
}
381
380
rustc_lexer:: LiteralKind :: Byte { terminated } => {
382
381
if !terminated {
383
382
self . sess . span_diagnostic . span_fatal_with_code (
384
- self . mk_sp ( start + BytePos ( 1 ) , suffix_start ) ,
383
+ self . mk_sp ( start + BytePos ( 1 ) , end ) ,
385
384
"unterminated byte constant" ,
386
385
error_code ! ( E0763 ) ,
387
386
)
388
387
}
389
- ( token:: Byte , Mode :: Byte , 2 , 1 ) // b' '
388
+ self . cook_quoted ( token:: Byte , Mode :: Byte , start , end , 2 , 1 ) // b' '
390
389
}
391
390
rustc_lexer:: LiteralKind :: Str { terminated } => {
392
391
if !terminated {
393
392
self . sess . span_diagnostic . span_fatal_with_code (
394
- self . mk_sp ( start, suffix_start ) ,
393
+ self . mk_sp ( start, end ) ,
395
394
"unterminated double quote string" ,
396
395
error_code ! ( E0765 ) ,
397
396
)
398
397
}
399
- ( token:: Str , Mode :: Str , 1 , 1 ) // " "
398
+ self . cook_quoted ( token:: Str , Mode :: Str , start , end , 1 , 1 ) // " "
400
399
}
401
400
rustc_lexer:: LiteralKind :: ByteStr { terminated } => {
402
401
if !terminated {
403
402
self . sess . span_diagnostic . span_fatal_with_code (
404
- self . mk_sp ( start + BytePos ( 1 ) , suffix_start ) ,
403
+ self . mk_sp ( start + BytePos ( 1 ) , end ) ,
405
404
"unterminated double quote byte string" ,
406
405
error_code ! ( E0766 ) ,
407
406
)
408
407
}
409
- ( token:: ByteStr , Mode :: ByteStr , 2 , 1 ) // b" "
408
+ self . cook_quoted ( token:: ByteStr , Mode :: ByteStr , start , end , 2 , 1 ) // b" "
410
409
}
411
410
rustc_lexer:: LiteralKind :: RawStr { n_hashes } => {
412
411
if let Some ( n_hashes) = n_hashes {
413
412
let n = u32:: from ( n_hashes) ;
414
- ( token:: StrRaw ( n_hashes) , Mode :: RawStr , 2 + n, 1 + n) // r##" "##
413
+ let kind = token:: StrRaw ( n_hashes) ;
414
+ self . cook_quoted ( kind, Mode :: RawStr , start, end, 2 + n, 1 + n) // r##" "##
415
415
} else {
416
416
self . report_raw_str_error ( start, 1 ) ;
417
417
}
418
418
}
419
419
rustc_lexer:: LiteralKind :: RawByteStr { n_hashes } => {
420
420
if let Some ( n_hashes) = n_hashes {
421
421
let n = u32:: from ( n_hashes) ;
422
- ( token:: ByteStrRaw ( n_hashes) , Mode :: RawByteStr , 3 + n, 1 + n) // br##" "##
422
+ let kind = token:: ByteStrRaw ( n_hashes) ;
423
+ self . cook_quoted ( kind, Mode :: RawByteStr , start, end, 3 + n, 1 + n) // br##" "##
423
424
} else {
424
425
self . report_raw_str_error ( start, 2 ) ;
425
426
}
426
427
}
427
428
rustc_lexer:: LiteralKind :: Int { base, empty_int } => {
428
- return if empty_int {
429
+ if empty_int {
429
430
self . sess
430
431
. span_diagnostic
431
432
. struct_span_err_with_code (
432
- self . mk_sp ( start, suffix_start ) ,
433
+ self . mk_sp ( start, end ) ,
433
434
"no valid digits found for number" ,
434
435
error_code ! ( E0768 ) ,
435
436
)
436
437
. emit ( ) ;
437
438
( token:: Integer , sym:: integer ( 0 ) )
438
439
} else {
439
- self . validate_int_literal ( base, start, suffix_start ) ;
440
- ( token:: Integer , self . symbol_from_to ( start, suffix_start ) )
441
- } ;
440
+ self . validate_int_literal ( base, start, end ) ;
441
+ ( token:: Integer , self . symbol_from_to ( start, end ) )
442
+ }
442
443
}
443
444
rustc_lexer:: LiteralKind :: Float { base, empty_exponent } => {
444
445
if empty_exponent {
445
446
self . err_span_ ( start, self . pos , "expected at least one digit in exponent" ) ;
446
447
}
447
-
448
448
match base {
449
- Base :: Hexadecimal => self . err_span_ (
450
- start,
451
- suffix_start,
452
- "hexadecimal float literal is not supported" ,
453
- ) ,
449
+ Base :: Hexadecimal => {
450
+ self . err_span_ ( start, end, "hexadecimal float literal is not supported" )
451
+ }
454
452
Base :: Octal => {
455
- self . err_span_ ( start, suffix_start , "octal float literal is not supported" )
453
+ self . err_span_ ( start, end , "octal float literal is not supported" )
456
454
}
457
455
Base :: Binary => {
458
- self . err_span_ ( start, suffix_start , "binary float literal is not supported" )
456
+ self . err_span_ ( start, end , "binary float literal is not supported" )
459
457
}
460
- _ => ( ) ,
458
+ _ => { }
461
459
}
462
-
463
- let id = self . symbol_from_to ( start, suffix_start) ;
464
- return ( token:: Float , id) ;
460
+ ( token:: Float , self . symbol_from_to ( start, end) )
465
461
}
466
- } ;
467
- let content_start = start + BytePos ( prefix_len) ;
468
- let content_end = suffix_start - BytePos ( postfix_len) ;
469
- let id = self . symbol_from_to ( content_start, content_end) ;
470
- self . validate_literal_escape ( mode, content_start, content_end, prefix_len, postfix_len) ;
471
- ( lit_kind, id)
462
+ }
472
463
}
473
464
474
465
#[ inline]
@@ -659,20 +650,22 @@ impl<'a> StringReader<'a> {
659
650
)
660
651
}
661
652
662
- fn validate_literal_escape (
653
+ fn cook_quoted (
663
654
& self ,
655
+ kind : token:: LitKind ,
664
656
mode : Mode ,
665
- content_start : BytePos ,
666
- content_end : BytePos ,
657
+ start : BytePos ,
658
+ end : BytePos ,
667
659
prefix_len : u32 ,
668
660
postfix_len : u32 ,
669
- ) {
661
+ ) -> ( token:: LitKind , Symbol ) {
662
+ let content_start = start + BytePos ( prefix_len) ;
663
+ let content_end = end - BytePos ( postfix_len) ;
670
664
let lit_content = self . str_from_to ( content_start, content_end) ;
671
665
unescape:: unescape_literal ( lit_content, mode, & mut |range, result| {
672
666
// Here we only check for errors. The actual unescaping is done later.
673
667
if let Err ( err) = result {
674
- let span_with_quotes = self
675
- . mk_sp ( content_start - BytePos ( prefix_len) , content_end + BytePos ( postfix_len) ) ;
668
+ let span_with_quotes = self . mk_sp ( start, end) ;
676
669
let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
677
670
let lo = content_start + BytePos ( start) ;
678
671
let hi = lo + BytePos ( end - start) ;
@@ -688,6 +681,7 @@ impl<'a> StringReader<'a> {
688
681
) ;
689
682
}
690
683
} ) ;
684
+ ( kind, Symbol :: intern ( lit_content) )
691
685
}
692
686
693
687
fn validate_int_literal ( & self , base : Base , content_start : BytePos , content_end : BytePos ) {
0 commit comments