Skip to content

Commit 18cc63d

Browse files
Unified validate_{byte,str,raw_str,raw_byte_str}_escape methods into one method validate_literal_escape with a mode argument.
This enables simplifying the `match` in `cook_lexer_literal()` and it eliminates 90 lines of repetition :)
1 parent 1be5d1e commit 18cc63d

File tree

1 file changed

+30
-122
lines changed
  • src/librustc_parse/lexer

1 file changed

+30
-122
lines changed

src/librustc_parse/lexer/mod.rs

Lines changed: 30 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod tokentrees;
1515
mod unescape_error_reporting;
1616
mod unicode_chars;
1717

18+
use rustc_lexer::unescape::Mode;
1819
use unescape_error_reporting::{emit_unescape_error, push_escaped_char};
1920

2021
#[derive(Clone, Debug)]
@@ -326,38 +327,27 @@ impl<'a> StringReader<'a> {
326327
suffix_start: BytePos,
327328
kind: rustc_lexer::LiteralKind,
328329
) -> (token::LitKind, Symbol) {
329-
match kind {
330+
// prefix means `"` or `br"` or `r###"`, ...
331+
let (lit_kind, mode, prefix_len, postfix_len) = match kind {
330332
rustc_lexer::LiteralKind::Char { terminated } => {
331333
if !terminated {
332334
self.fatal_span_(start, suffix_start, "unterminated character literal").raise()
333335
}
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) // ' '
339337
}
340338
rustc_lexer::LiteralKind::Byte { terminated } => {
341339
if !terminated {
342340
self.fatal_span_(start + BytePos(1), suffix_start, "unterminated byte constant")
343341
.raise()
344342
}
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' '
350344
}
351345
rustc_lexer::LiteralKind::Str { terminated } => {
352346
if !terminated {
353347
self.fatal_span_(start, suffix_start, "unterminated double quote string")
354348
.raise()
355349
}
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) // " "
361351
}
362352
rustc_lexer::LiteralKind::ByteStr { terminated } => {
363353
if !terminated {
@@ -368,42 +358,28 @@ impl<'a> StringReader<'a> {
368358
)
369359
.raise()
370360
}
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" "
376362
}
377363
rustc_lexer::LiteralKind::RawStr(unvalidated_raw_str) => {
378364
let valid_raw_str = self.validate_and_report_errors(start, unvalidated_raw_str);
379365
let n_hashes = valid_raw_str.num_hashes();
380366
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##" "##
387368
}
388369
rustc_lexer::LiteralKind::RawByteStr(unvalidated_raw_str) => {
389370
let validated_raw_str = self.validate_and_report_errors(start, unvalidated_raw_str);
390371
let n_hashes = validated_raw_str.num_hashes();
391372
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##" "##
398374
}
399375
rustc_lexer::LiteralKind::Int { base, empty_int } => {
400-
if empty_int {
376+
return if empty_int {
401377
self.err_span_(start, suffix_start, "no valid digits found for number");
402378
(token::Integer, sym::integer(0))
403379
} else {
404380
self.validate_int_literal(base, start, suffix_start);
405381
(token::Integer, self.symbol_from_to(start, suffix_start))
406-
}
382+
};
407383
}
408384
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
409385
if empty_exponent {
@@ -431,9 +407,14 @@ impl<'a> StringReader<'a> {
431407
}
432408

433409
let id = self.symbol_from_to(start, suffix_start);
434-
(token::Float, id)
410+
return (token::Float, id);
435411
}
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);
437418
}
438419

439420
#[inline]
@@ -555,96 +536,23 @@ impl<'a> StringReader<'a> {
555536
.raise();
556537
}
557538

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));
590546
emit_unescape_error(
591547
&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,
595551
range,
596552
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+
);
646554
}
647-
})
555+
});
648556
}
649557

650558
fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {

0 commit comments

Comments
 (0)