Introduce LexicalError::InvalidByteLiteral (#10328)

## Summary

This PR introduces a new `InvalidByteLiteral` lexical error type to
avoid repeating the message in multiple locations.
This commit is contained in:
Dhruv Manilawala
2024-03-12 14:00:21 +05:30
parent 94cc5f2e13
commit 98f6dcbb91
2 changed files with 8 additions and 15 deletions

View File

@@ -1456,6 +1456,8 @@ pub enum LexicalErrorType {
UnrecognizedToken { tok: char },
/// An f-string error containing the [`FStringErrorType`].
FStringError(FStringErrorType),
/// Invalid character encountered in a byte literal.
InvalidByteLiteral,
/// An unexpected character was encountered after a line continuation.
LineContinuationError,
/// An unexpected end of file was encountered.
@@ -1473,6 +1475,9 @@ impl std::fmt::Display for LexicalErrorType {
match self {
LexicalErrorType::StringError => write!(f, "Got unexpected string"),
LexicalErrorType::FStringError(error) => write!(f, "f-string: {error}"),
LexicalErrorType::InvalidByteLiteral => {
write!(f, "bytes can only contain ASCII literal characters")
}
LexicalErrorType::UnicodeError => write!(f, "Got unexpected unicode"),
LexicalErrorType::NestingError => write!(f, "Got unexpected nesting"),
LexicalErrorType::IndentationError => {

View File

@@ -217,11 +217,7 @@ impl StringParser {
_ => {
if self.kind.is_byte_string() && !first_char.is_ascii() {
return Err(LexicalError::new(
LexicalErrorType::OtherError(
"bytes can only contain ASCII literal characters"
.to_string()
.into_boxed_str(),
),
LexicalErrorType::InvalidByteLiteral,
self.range(self.get_pos()),
));
}
@@ -295,11 +291,7 @@ impl StringParser {
ch => {
if !ch.is_ascii() {
return Err(LexicalError::new(
LexicalErrorType::OtherError(
"bytes can only contain ASCII literal characters"
.to_string()
.into_boxed_str(),
),
LexicalErrorType::InvalidByteLiteral,
self.range(self.get_pos()),
));
}
@@ -328,11 +320,7 @@ impl StringParser {
fn parse_bytes(mut self) -> Result<StringType, LexicalError> {
if let Some(index) = self.source.as_bytes().find_non_ascii_byte() {
return Err(LexicalError::new(
LexicalErrorType::OtherError(
"bytes can only contain ASCII literal characters"
.to_string()
.into_boxed_str(),
),
LexicalErrorType::InvalidByteLiteral,
self.range(TextSize::try_from(index).unwrap()),
));
}