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:
@@ -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 => {
|
||||
|
||||
@@ -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()),
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user