Incorrect f-string parsing (#112)

* Fix parse_fstring

* Add test

* Push char

* Remove unused import
This commit is contained in:
yt2b 2024-01-23 17:20:32 +09:00 committed by GitHub
parent 8731e9fc50
commit 9ce55aefde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 3 deletions

View file

@ -1,7 +1,5 @@
use crate::text_size::{TextRange, TextSize};
pub use crate::builtin::*;
pub trait Ranged {
fn range(&self) -> TextRange;

View file

@ -0,0 +1,15 @@
---
source: parser/src/string.rs
expression: parse_ast
---
[
Constant(
ExprConstant {
range: 0..10,
value: Str(
"\\{x\\}",
),
kind: None,
},
),
]

View file

@ -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);
@ -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'";