mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-28 06:14:56 +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};
|
use crate::text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
pub use crate::builtin::*;
|
|
||||||
|
|
||||||
pub trait Ranged {
|
pub trait Ranged {
|
||||||
fn range(&self) -> TextRange;
|
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,8 +503,12 @@ impl<'a> StringParser<'a> {
|
||||||
}
|
}
|
||||||
'\\' if !self.kind.is_raw() => {
|
'\\' if !self.kind.is_raw() => {
|
||||||
self.next_char();
|
self.next_char();
|
||||||
|
if let Some('{' | '}') = self.peek() {
|
||||||
|
content.push('\\');
|
||||||
|
} else {
|
||||||
content.push_str(&self.parse_escaped_char()?);
|
content.push_str(&self.parse_escaped_char()?);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
content.push(ch);
|
content.push(ch);
|
||||||
self.next_char();
|
self.next_char();
|
||||||
|
@ -956,6 +960,13 @@ mod tests {
|
||||||
insta::assert_debug_snapshot!(parse_ast);
|
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]
|
#[test]
|
||||||
fn test_parse_string_concat() {
|
fn test_parse_string_concat() {
|
||||||
let source = "'Hello ' 'world'";
|
let source = "'Hello ' 'world'";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue