mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-07 13:15:21 +00:00
Incorrect f-string parsing (#112)
* Fix parse_fstring * Add test * Push char * Remove unused import
This commit is contained in:
parent
8731e9fc50
commit
9ce55aefde
3 changed files with 27 additions and 3 deletions
|
@ -1,7 +1,5 @@
|
|||
use crate::text_size::{TextRange, TextSize};
|
||||
|
||||
pub use crate::builtin::*;
|
||||
|
||||
pub trait Ranged {
|
||||
fn range(&self) -> TextRange;
|
||||
|
||||
|
|
15
parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap
generated
Normal file
15
parser/src/snapshots/rustpython_parser__string__tests__parse_fstring_escaped_brackets.snap
generated
Normal file
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
source: parser/src/string.rs
|
||||
expression: parse_ast
|
||||
---
|
||||
[
|
||||
Constant(
|
||||
ExprConstant {
|
||||
range: 0..10,
|
||||
value: Str(
|
||||
"\\{x\\}",
|
||||
),
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
]
|
|
@ -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'";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue