Skip to content

Commit

Permalink
Incorrect f-string parsing (#112)
Browse files Browse the repository at this point in the history
* Fix parse_fstring

* Add test

* Push char

* Remove unused import
  • Loading branch information
yt2b authored Jan 23, 2024
1 parent 8731e9f commit 9ce55ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 0 additions & 2 deletions ast/src/ranged.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::text_size::{TextRange, TextSize};

pub use crate::builtin::*;

pub trait Ranged {
fn range(&self) -> TextRange;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('\\');
} else {
content.push_str(&self.parse_escaped_char()?);
}
}
_ => {
content.push(ch);
Expand Down Expand Up @@ -956,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'";
Expand Down

0 comments on commit 9ce55ae

Please sign in to comment.