Skip to content

Commit d30c668

Browse files
committed
make cook generic
1 parent 6d905a8 commit d30c668

File tree

1 file changed

+27
-37
lines changed
  • compiler/rustc_parse/src/lexer

1 file changed

+27
-37
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+27-37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Range;
2+
13
use crate::errors;
24
use crate::lexer::unicode_chars::UNICODE_ARRAY;
35
use crate::make_unclosed_delims_error;
@@ -6,7 +8,7 @@ use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
68
use rustc_ast::tokenstream::TokenStream;
79
use rustc_ast::util::unicode::contains_text_flow_control_chars;
810
use rustc_errors::{error_code, Applicability, Diagnostic, DiagnosticBuilder, StashKey};
9-
use rustc_lexer::unescape::{self, Mode};
11+
use rustc_lexer::unescape::{self, EscapeError, Mode};
1012
use rustc_lexer::Cursor;
1113
use rustc_lexer::{Base, DocStyle, RawStrError};
1214
use rustc_session::lint::builtin::{
@@ -670,20 +672,21 @@ impl<'a> StringReader<'a> {
670672
self.sess.emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
671673
}
672674

673-
fn cook_quoted(
675+
fn cook_common(
674676
&self,
675677
kind: token::LitKind,
676678
mode: Mode,
677679
start: BytePos,
678680
end: BytePos,
679681
prefix_len: u32,
680682
postfix_len: u32,
683+
unescape: fn(&str, Mode, &mut dyn FnMut(Range<usize>, Result<(), EscapeError>)),
681684
) -> (token::LitKind, Symbol) {
682685
let mut has_fatal_err = false;
683686
let content_start = start + BytePos(prefix_len);
684687
let content_end = end - BytePos(postfix_len);
685688
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| {
687690
// Here we only check for errors. The actual unescaping is done later.
688691
if let Err(err) = result {
689692
let span_with_quotes = self.mk_sp(start, end);
@@ -715,7 +718,7 @@ impl<'a> StringReader<'a> {
715718
}
716719
}
717720

718-
fn cook_c_string(
721+
fn cook_quoted(
719722
&self,
720723
kind: token::LitKind,
721724
mode: Mode,
@@ -724,40 +727,27 @@ impl<'a> StringReader<'a> {
724727
prefix_len: u32,
725728
postfix_len: u32,
726729
) -> (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+
}
753736

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+
})
761751
}
762752
}
763753

0 commit comments

Comments
 (0)