From 0f21f07f81c59ddb2865935f0d531977ac52ce80 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 14 Sep 2023 12:52:44 +0530 Subject: [PATCH] Allow `NUL` character in f-strings --- crates/ruff_python_parser/src/lexer.rs | 11 +++++++- ...__lexer__tests__fstring_with_nul_char.snap | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__fstring_with_nul_char.snap diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index ab41477d77951..b8abdc411e9fa 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -552,7 +552,10 @@ impl<'source> Lexer<'source> { loop { match self.cursor.first() { - EOF_CHAR => { + // The condition is to differentiate between the `NUL` (`\0`) character + // in the source code and the one returned by `self.cursor.first()` when + // we reach the end of the source code. + EOF_CHAR if self.cursor.is_eof() => { let error = if fstring.is_triple_quoted() { FStringErrorType::UnterminatedTripleQuotedString } else { @@ -2020,6 +2023,12 @@ f"{(lambda x:{x})}" assert_debug_snapshot!(lex_source(source)); } + #[test] + fn test_fstring_with_nul_char() { + let source = r"f'\0'"; + assert_debug_snapshot!(lex_source(source)); + } + fn lex_fstring_error(source: &str) -> FStringErrorType { match lex(source, Mode::Module).find_map(std::result::Result::err) { Some(err) => match err.error { diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__fstring_with_nul_char.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__fstring_with_nul_char.snap new file mode 100644 index 0000000000000..667d1897ecbcd --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__fstring_with_nul_char.snap @@ -0,0 +1,25 @@ +--- +source: crates/ruff_python_parser/src/lexer.rs +expression: lex_source(source) +--- +[ + ( + FStringStart, + 0..2, + ), + ( + FStringMiddle { + value: "\\0", + is_raw: false, + }, + 2..4, + ), + ( + FStringEnd, + 4..5, + ), + ( + Newline, + 5..5, + ), +]