add diagnosis messages for chars and byte literal errors

This commit is contained in:
Pol Valletbó 2023-10-11 12:19:00 +02:00
parent 8a2331450a
commit 1fe6ac87e9
9 changed files with 337 additions and 12 deletions

View file

@ -9,8 +9,11 @@
//! include info about comments and whitespace.
use rustc_dependencies::lexer as rustc_lexer;
use std::ops;
use rustc_lexer::unescape::{Mode, EscapeError};
use crate::{
SyntaxKind::{self, *},
T,
@ -254,13 +257,28 @@ impl<'a> Converter<'a> {
rustc_lexer::LiteralKind::Char { terminated } => {
if !terminated {
err = "Missing trailing `'` symbol to terminate the character literal";
} else {
let text = &self.res.text[self.offset + 1..][..len - 1];
let i = text.rfind('\'').unwrap();
let text = &text[..i];
if let Err(e) = rustc_lexer::unescape::unescape_char(text) {
err = error_to_diagnostic_message(e, Mode::Char);
}
}
CHAR
}
rustc_lexer::LiteralKind::Byte { terminated } => {
if !terminated {
err = "Missing trailing `'` symbol to terminate the byte literal";
} else {
let text = &self.res.text[self.offset + 2..][..len - 2];
let i = text.rfind('\'').unwrap();
let text = &text[..i];
if let Err(e) = rustc_lexer::unescape::unescape_char(text) {
err = error_to_diagnostic_message(e, Mode::Byte);
}
}
BYTE
}
rustc_lexer::LiteralKind::Str { terminated } => {
@ -305,3 +323,40 @@ impl<'a> Converter<'a> {
self.push(syntax_kind, len, err);
}
}
fn error_to_diagnostic_message(error: EscapeError, mode: Mode) -> &'static str {
match error {
EscapeError::ZeroChars => "empty character literal",
EscapeError::MoreThanOneChar => "character literal may only contain one codepoint",
EscapeError::LoneSlash => "",
EscapeError::InvalidEscape if mode == Mode::Byte || mode == Mode::ByteStr => {
"unknown byte escape"
}
EscapeError::InvalidEscape => "unknown character escape",
EscapeError::BareCarriageReturn => "",
EscapeError::BareCarriageReturnInRawString => "",
EscapeError::EscapeOnlyChar if mode == Mode::Byte => "byte constant must be escaped",
EscapeError::EscapeOnlyChar => "character constant must be escaped",
EscapeError::TooShortHexEscape => "numeric character escape is too short",
EscapeError::InvalidCharInHexEscape => "invalid character in numeric character escape",
EscapeError::OutOfRangeHexEscape => "out of range hex escape",
EscapeError::NoBraceInUnicodeEscape => "incorrect unicode escape sequence",
EscapeError::InvalidCharInUnicodeEscape => "invalid character in unicode escape",
EscapeError::EmptyUnicodeEscape => "empty unicode escape",
EscapeError::UnclosedUnicodeEscape => "unterminated unicode escape",
EscapeError::LeadingUnderscoreUnicodeEscape => "invalid start of unicode escape",
EscapeError::OverlongUnicodeEscape => "overlong unicode escape",
EscapeError::LoneSurrogateUnicodeEscape => "invalid unicode character escape",
EscapeError::OutOfRangeUnicodeEscape => "invalid unicode character escape",
EscapeError::UnicodeEscapeInByte => "unicode escape in byte string",
EscapeError::NonAsciiCharInByte if mode == Mode::Byte => {
"non-ASCII character in byte literal"
}
EscapeError::NonAsciiCharInByte if mode == Mode::ByteStr => {
"non-ASCII character in byte string literal"
}
EscapeError::NonAsciiCharInByte => "non-ASCII character in raw byte string literal",
EscapeError::UnskippedWhitespaceWarning => "",
EscapeError::MultipleSkippedLinesWarning => "",
}
}