From bc16c9fd8d48e3db05f2239520e18a2fea9c3a41 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sat, 20 Jan 2024 15:27:41 +0900 Subject: [PATCH 1/4] Fix parse_fstring --- parser/src/string.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/parser/src/string.rs b/parser/src/string.rs index 2201343..3f5f1a2 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -503,7 +503,11 @@ impl<'a> StringParser<'a> { } '\\' if !self.kind.is_raw() => { self.next_char(); - content.push_str(&self.parse_escaped_char()?); + if let Some('{' | '}') = self.peek() { + content.push_str("\\"); + } else { + content.push_str(&self.parse_escaped_char()?); + } } _ => { content.push(ch); From 29c98d4a2f11f9b8df83cc60422fd1945649e219 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sat, 20 Jan 2024 15:30:38 +0900 Subject: [PATCH 2/4] Add test --- ...ng__tests__parse_fstring_escaped_brackets.snap | 15 +++++++++++++++ parser/src/string.rs | 7 +++++++ 2 files changed, 22 insertions(+) create mode 100644 parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap diff --git a/parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap b/parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap new file mode 100644 index 0000000..7e458fd --- /dev/null +++ b/parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap @@ -0,0 +1,15 @@ +--- +source: parser/src/string.rs +expression: parse_ast +--- +[ + Constant( + ExprConstant { + range: 0..10, + value: Str( + "\\{x\\}", + ), + kind: None, + }, + ), +] diff --git a/parser/src/string.rs b/parser/src/string.rs index 3f5f1a2..750eaa7 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -960,6 +960,13 @@ mod tests { insta::assert_debug_snapshot!(parse_ast); } + #[test] + fn test_parse_fstring_escaped_brackets() { + let source = "\\{{x\\}}"; + let parse_ast = parse_fstring(source).unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } + #[test] fn test_parse_string_concat() { let source = "'Hello ' 'world'"; From e247b89c1a7598b2981e9f3abfeb5d4874f733be Mon Sep 17 00:00:00 2001 From: yt2b Date: Sat, 20 Jan 2024 15:46:15 +0900 Subject: [PATCH 3/4] Push char --- parser/src/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/src/string.rs b/parser/src/string.rs index 750eaa7..2914aa2 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -504,7 +504,7 @@ impl<'a> StringParser<'a> { '\\' if !self.kind.is_raw() => { self.next_char(); if let Some('{' | '}') = self.peek() { - content.push_str("\\"); + content.push('\\'); } else { content.push_str(&self.parse_escaped_char()?); } From c78f92f713de6dcc427b75e7320738c420dc15b8 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sat, 20 Jan 2024 15:54:47 +0900 Subject: [PATCH 4/4] Remove unused import --- ast/src/ranged.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ast/src/ranged.rs b/ast/src/ranged.rs index f01c15a..b50e22a 100644 --- a/ast/src/ranged.rs +++ b/ast/src/ranged.rs @@ -1,7 +1,5 @@ use crate::text_size::{TextRange, TextSize}; -pub use crate::builtin::*; - pub trait Ranged { fn range(&self) -> TextRange;