diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index 57aff68727..5078858c6d 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -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 => { diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index b51ef525ee..3a7e1f3bfb 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -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 { 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()), )); }