1
+ use std:: ops:: Range ;
2
+
1
3
use crate :: errors;
2
4
use crate :: lexer:: unicode_chars:: UNICODE_ARRAY ;
3
5
use crate :: make_unclosed_delims_error;
@@ -6,7 +8,7 @@ use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
6
8
use rustc_ast:: tokenstream:: TokenStream ;
7
9
use rustc_ast:: util:: unicode:: contains_text_flow_control_chars;
8
10
use rustc_errors:: { error_code, Applicability , Diagnostic , DiagnosticBuilder , StashKey } ;
9
- use rustc_lexer:: unescape:: { self , Mode } ;
11
+ use rustc_lexer:: unescape:: { self , EscapeError , Mode } ;
10
12
use rustc_lexer:: Cursor ;
11
13
use rustc_lexer:: { Base , DocStyle , RawStrError } ;
12
14
use rustc_session:: lint:: builtin:: {
@@ -670,20 +672,21 @@ impl<'a> StringReader<'a> {
670
672
self . sess . emit_fatal ( errors:: TooManyHashes { span : self . mk_sp ( start, self . pos ) , num } ) ;
671
673
}
672
674
673
- fn cook_quoted (
675
+ fn cook_common (
674
676
& self ,
675
677
kind : token:: LitKind ,
676
678
mode : Mode ,
677
679
start : BytePos ,
678
680
end : BytePos ,
679
681
prefix_len : u32 ,
680
682
postfix_len : u32 ,
683
+ unescape : fn ( & str , Mode , & mut dyn FnMut ( Range < usize > , Result < ( ) , EscapeError > ) ) ,
681
684
) -> ( token:: LitKind , Symbol ) {
682
685
let mut has_fatal_err = false ;
683
686
let content_start = start + BytePos ( prefix_len) ;
684
687
let content_end = end - BytePos ( postfix_len) ;
685
688
let lit_content = self . str_from_to ( content_start, content_end) ;
686
- unescape:: unescape_literal ( lit_content, mode, & mut |range, result| {
689
+ unescape ( lit_content, mode, & mut |range, result| {
687
690
// Here we only check for errors. The actual unescaping is done later.
688
691
if let Err ( err) = result {
689
692
let span_with_quotes = self . mk_sp ( start, end) ;
@@ -715,7 +718,7 @@ impl<'a> StringReader<'a> {
715
718
}
716
719
}
717
720
718
- fn cook_c_string (
721
+ fn cook_quoted (
719
722
& self ,
720
723
kind : token:: LitKind ,
721
724
mode : Mode ,
@@ -724,40 +727,27 @@ impl<'a> StringReader<'a> {
724
727
prefix_len : u32 ,
725
728
postfix_len : u32 ,
726
729
) -> ( token:: LitKind , Symbol ) {
727
- let mut has_fatal_err = false ;
728
- let content_start = start + BytePos ( prefix_len) ;
729
- let content_end = end - BytePos ( postfix_len) ;
730
- let lit_content = self . str_from_to ( content_start, content_end) ;
731
- unescape:: unescape_c_string ( lit_content, mode, & mut |range, result| {
732
- // Here we only check for errors. The actual unescaping is done later.
733
- if let Err ( err) = result {
734
- let span_with_quotes = self . mk_sp ( start, end) ;
735
- let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
736
- let lo = content_start + BytePos ( start) ;
737
- let hi = lo + BytePos ( end - start) ;
738
- let span = self . mk_sp ( lo, hi) ;
739
- if err. is_fatal ( ) {
740
- has_fatal_err = true ;
741
- }
742
- emit_unescape_error (
743
- & self . sess . span_diagnostic ,
744
- lit_content,
745
- span_with_quotes,
746
- span,
747
- mode,
748
- range,
749
- err,
750
- ) ;
751
- }
752
- } ) ;
730
+ self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
731
+ unescape:: unescape_literal ( src, mode, & mut |span, result| {
732
+ callback ( span, result. map ( drop) )
733
+ } )
734
+ } )
735
+ }
753
736
754
- // We normally exclude the quotes for the symbol, but for errors we
755
- // include it because it results in clearer error messages.
756
- if !has_fatal_err {
757
- ( kind, Symbol :: intern ( lit_content) )
758
- } else {
759
- ( token:: Err , self . symbol_from_to ( start, end) )
760
- }
737
+ fn cook_c_string (
738
+ & self ,
739
+ kind : token:: LitKind ,
740
+ mode : Mode ,
741
+ start : BytePos ,
742
+ end : BytePos ,
743
+ prefix_len : u32 ,
744
+ postfix_len : u32 ,
745
+ ) -> ( token:: LitKind , Symbol ) {
746
+ self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
747
+ unescape:: unescape_c_string ( src, mode, & mut |span, result| {
748
+ callback ( span, result. map ( drop) )
749
+ } )
750
+ } )
761
751
}
762
752
}
763
753
0 commit comments