-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix curly brace escape handling in f-strings (#7331)
## Summary This PR fixes the escape handling of curly braces inside a f-string. There are 2 main changes: ### Lexer The lexer change was actually a bug. Instead of breaking as soon as we find a curly brace after the `\` character, we'll continue and let the next iteration handle it in the curly brace branch. This fixes the following case: ```python f"\{{foo}}" # ^ use the curly brace branch to handle this character instead of breaking ``` ### Parser We can encounter a `\` as the last character in a `FStringMiddle` token which is valid in this context[^1]. For example, ```python f"\{foo} \{bar:\}" # ^ ^^ ^ # The marked characters are part of 3 different `FStringMiddle` token ``` Here, the `FStringMiddle` token content will be `"\"` and `" \"` which is invalid in a regular string literal. However, it's valid here because it's a substring of a f-string. Even though curly braces cannot be escaped, it's a valid syntax. [^1]: Refer to point 3 in https://peps.python.org/pep-0701/#rejected-ideas ## Test Plan Verified that existing test cases are passing and add new test cases for the lexer and parser.
- Loading branch information
1 parent
a1da9aa
commit 9942683
Showing
5 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
..._python_parser/src/snapshots/ruff_python_parser__lexer__tests__fstring_escape_braces.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
source: crates/ruff_python_parser/src/lexer.rs | ||
expression: lex_source(source) | ||
--- | ||
[ | ||
( | ||
FStringStart, | ||
0..2, | ||
), | ||
( | ||
FStringMiddle { | ||
value: "\\", | ||
is_raw: false, | ||
}, | ||
2..3, | ||
), | ||
( | ||
Lbrace, | ||
3..4, | ||
), | ||
( | ||
Name { | ||
name: "foo", | ||
}, | ||
4..7, | ||
), | ||
( | ||
Rbrace, | ||
7..8, | ||
), | ||
( | ||
FStringEnd, | ||
8..9, | ||
), | ||
( | ||
FStringStart, | ||
10..12, | ||
), | ||
( | ||
FStringMiddle { | ||
value: "\\\\", | ||
is_raw: false, | ||
}, | ||
12..14, | ||
), | ||
( | ||
Lbrace, | ||
14..15, | ||
), | ||
( | ||
Name { | ||
name: "foo", | ||
}, | ||
15..18, | ||
), | ||
( | ||
Rbrace, | ||
18..19, | ||
), | ||
( | ||
FStringEnd, | ||
19..20, | ||
), | ||
( | ||
FStringStart, | ||
21..23, | ||
), | ||
( | ||
FStringMiddle { | ||
value: "\\{foo}", | ||
is_raw: false, | ||
}, | ||
23..31, | ||
), | ||
( | ||
FStringEnd, | ||
31..32, | ||
), | ||
( | ||
FStringStart, | ||
33..35, | ||
), | ||
( | ||
FStringMiddle { | ||
value: "\\\\{foo}", | ||
is_raw: false, | ||
}, | ||
35..44, | ||
), | ||
( | ||
FStringEnd, | ||
44..45, | ||
), | ||
( | ||
Newline, | ||
45..45, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters