From 9d3dd49a94bc95ee9c67438d720b1b55de8b8436 Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Sun, 1 Oct 2023 20:26:38 +0100 Subject: [PATCH] allow multiline expressions in f-strings --- native/libcst/src/tokenizer/core/mod.rs | 6 ++++-- native/libcst/src/tokenizer/core/string_types.rs | 2 +- native/libcst/tests/fixtures/super_strings.py | 12 +++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/native/libcst/src/tokenizer/core/mod.rs b/native/libcst/src/tokenizer/core/mod.rs index 2365eaa36..52c3112bd 100644 --- a/native/libcst/src/tokenizer/core/mod.rs +++ b/native/libcst/src/tokenizer/core/mod.rs @@ -363,7 +363,8 @@ impl<'t> TokState<'t> { self.text_pos.next(); self.at_bol = true; if self.split_fstring - && !self.fstring_stack.iter().all(|node| node.allow_multiline()) + && self.fstring_stack.last().map(|node| node.allow_multiline()) + == Some(false) { Err(TokError::UnterminatedString) } else if self.blank_line || !self.paren_stack.is_empty() { @@ -895,7 +896,8 @@ impl<'t> TokState<'t> { is_in_format_spec: bool, is_raw_string: bool, ) -> Result, TokError<'t>> { - let allow_multiline = self.fstring_stack.iter().all(|node| node.allow_multiline()); + let allow_multiline = + self.fstring_stack.last().map(|node| node.allow_multiline()) == Some(true); let mut in_named_unicode: bool = false; let mut ok_result = Ok(None); // value to return if we reach the end and don't error out 'outer: loop { diff --git a/native/libcst/src/tokenizer/core/string_types.rs b/native/libcst/src/tokenizer/core/string_types.rs index d14d13f5b..b04ccb397 100644 --- a/native/libcst/src/tokenizer/core/string_types.rs +++ b/native/libcst/src/tokenizer/core/string_types.rs @@ -105,7 +105,7 @@ impl FStringNode { } pub fn allow_multiline(&self) -> bool { - self.quote_size == StringQuoteSize::Triple + self.quote_size == StringQuoteSize::Triple || self.is_in_expr() } pub fn is_in_expr(&self) -> bool { diff --git a/native/libcst/tests/fixtures/super_strings.py b/native/libcst/tests/fixtures/super_strings.py index 824572793..5650adc4d 100644 --- a/native/libcst/tests/fixtures/super_strings.py +++ b/native/libcst/tests/fixtures/super_strings.py @@ -27,8 +27,18 @@ print(f"{self.ERASE_CURRENT_LINE}{self._human_seconds(elapsed_time)} {percent:.{self.pretty_precision}f}% complete, {self.estimate_completion(elapsed_time, finished, left)} estimated for {left} files to go...") +f"{"\n".join()}" + +f"___{ + x +}___" + +f"___{( + x +)}___" + f'\{{\}}' f"regexp_like(path, '.*\{file_type}$')" f"\lfoo" -f"{_:{_:}{a}}" \ No newline at end of file +f"{_:{_:}{a}}"