Skip to content

Commit d963686

Browse files
committed
Refactor cook_lexer_literal.
It deals with eight cases: ints, floats, and the six quoted types (char/byte/strings). For ints and floats we have an early return, and the other six types fall through to the code at the end, which makes the function hard to read. This commit rearranges things to avoid the early returns.
1 parent a21c045 commit d963686

File tree

1 file changed

+36
-42
lines changed
  • compiler/rustc_parse/src/lexer

1 file changed

+36
-42
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+36-42
Original file line numberDiff line numberDiff line change
@@ -363,112 +363,103 @@ impl<'a> StringReader<'a> {
363363
fn cook_lexer_literal(
364364
&self,
365365
start: BytePos,
366-
suffix_start: BytePos,
366+
end: BytePos,
367367
kind: rustc_lexer::LiteralKind,
368368
) -> (token::LitKind, Symbol) {
369-
// prefix means `"` or `br"` or `r###"`, ...
370-
let (lit_kind, mode, prefix_len, postfix_len) = match kind {
369+
match kind {
371370
rustc_lexer::LiteralKind::Char { terminated } => {
372371
if !terminated {
373372
self.sess.span_diagnostic.span_fatal_with_code(
374-
self.mk_sp(start, suffix_start),
373+
self.mk_sp(start, end),
375374
"unterminated character literal",
376375
error_code!(E0762),
377376
)
378377
}
379-
(token::Char, Mode::Char, 1, 1) // ' '
378+
self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' '
380379
}
381380
rustc_lexer::LiteralKind::Byte { terminated } => {
382381
if !terminated {
383382
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),
385384
"unterminated byte constant",
386385
error_code!(E0763),
387386
)
388387
}
389-
(token::Byte, Mode::Byte, 2, 1) // b' '
388+
self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
390389
}
391390
rustc_lexer::LiteralKind::Str { terminated } => {
392391
if !terminated {
393392
self.sess.span_diagnostic.span_fatal_with_code(
394-
self.mk_sp(start, suffix_start),
393+
self.mk_sp(start, end),
395394
"unterminated double quote string",
396395
error_code!(E0765),
397396
)
398397
}
399-
(token::Str, Mode::Str, 1, 1) // " "
398+
self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " "
400399
}
401400
rustc_lexer::LiteralKind::ByteStr { terminated } => {
402401
if !terminated {
403402
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),
405404
"unterminated double quote byte string",
406405
error_code!(E0766),
407406
)
408407
}
409-
(token::ByteStr, Mode::ByteStr, 2, 1) // b" "
408+
self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
410409
}
411410
rustc_lexer::LiteralKind::RawStr { n_hashes } => {
412411
if let Some(n_hashes) = n_hashes {
413412
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##" "##
415415
} else {
416416
self.report_raw_str_error(start, 1);
417417
}
418418
}
419419
rustc_lexer::LiteralKind::RawByteStr { n_hashes } => {
420420
if let Some(n_hashes) = n_hashes {
421421
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##" "##
423424
} else {
424425
self.report_raw_str_error(start, 2);
425426
}
426427
}
427428
rustc_lexer::LiteralKind::Int { base, empty_int } => {
428-
return if empty_int {
429+
if empty_int {
429430
self.sess
430431
.span_diagnostic
431432
.struct_span_err_with_code(
432-
self.mk_sp(start, suffix_start),
433+
self.mk_sp(start, end),
433434
"no valid digits found for number",
434435
error_code!(E0768),
435436
)
436437
.emit();
437438
(token::Integer, sym::integer(0))
438439
} 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+
}
442443
}
443444
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
444445
if empty_exponent {
445446
self.err_span_(start, self.pos, "expected at least one digit in exponent");
446447
}
447-
448448
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+
}
454452
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")
456454
}
457455
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")
459457
}
460-
_ => (),
458+
_ => {}
461459
}
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))
465461
}
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+
}
472463
}
473464

474465
#[inline]
@@ -659,20 +650,22 @@ impl<'a> StringReader<'a> {
659650
)
660651
}
661652

662-
fn validate_literal_escape(
653+
fn cook_quoted(
663654
&self,
655+
kind: token::LitKind,
664656
mode: Mode,
665-
content_start: BytePos,
666-
content_end: BytePos,
657+
start: BytePos,
658+
end: BytePos,
667659
prefix_len: u32,
668660
postfix_len: u32,
669-
) {
661+
) -> (token::LitKind, Symbol) {
662+
let content_start = start + BytePos(prefix_len);
663+
let content_end = end - BytePos(postfix_len);
670664
let lit_content = self.str_from_to(content_start, content_end);
671665
unescape::unescape_literal(lit_content, mode, &mut |range, result| {
672666
// Here we only check for errors. The actual unescaping is done later.
673667
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);
676669
let (start, end) = (range.start as u32, range.end as u32);
677670
let lo = content_start + BytePos(start);
678671
let hi = lo + BytePos(end - start);
@@ -688,6 +681,7 @@ impl<'a> StringReader<'a> {
688681
);
689682
}
690683
});
684+
(kind, Symbol::intern(lit_content))
691685
}
692686

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

0 commit comments

Comments
 (0)