-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to Python F-strings and string prefixes (#1642)
This PR adds support for [string interpolation](https://www.python.org/dev/peps/pep-0498/) (aka. f-strings) and makes the [string prefixes](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals) part of the string. Resolves #1636. ### Known issues Assumes that strings inside the interpolation expression are 'nice'. So strings with unfortunate numbers of curley braces will cause incorrect highlighting: E.g.: `f"{'}'}"`.
- Loading branch information
1 parent
5b6ad70
commit a69c2b6
Showing
5 changed files
with
188 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
147 changes: 147 additions & 0 deletions
147
tests/languages/python/string-interpolation_feature.test
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,147 @@ | ||
f'The value is {value}.' | ||
|
||
f"The value is {'4'}." | ||
|
||
f'input={value!s:#06x}' | ||
|
||
f'{{{4*10}}}' | ||
|
||
fr'x={4*10}\n' | ||
|
||
f'''{x | ||
+1}''' | ||
|
||
f'mapping is { {a:b for (a, b) in ((1, 2), (3, 4))} }' | ||
|
||
f'{(lambda x: x*2)(3)}' | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["string-interpolation", [ | ||
["string", "f'The value is "], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
"value", | ||
["punctuation", "}"] | ||
]], | ||
["string", ".'"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f\"The value is "], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
["string", "'4'"], | ||
["punctuation", "}"] | ||
]], | ||
["string", ".\""] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f'input="], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
"value", | ||
["conversion-option", "!s"], | ||
["punctuation", ":"], | ||
["format-spec", "#06x"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "'"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f'{{"], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
["number", "4"], | ||
["operator", "*"], | ||
["number", "10"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "}}'"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "fr'x="], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
["number", "4"], | ||
["operator", "*"], | ||
["number", "10"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "\\n'"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f'''"], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
"x\r\n", | ||
["operator", "+"], | ||
["number", "1"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "'''"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f'mapping is "], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
["punctuation", "{"], | ||
"a", | ||
["punctuation", ":"], | ||
"b ", | ||
["keyword", "for"], | ||
["punctuation", "("], | ||
"a", | ||
["punctuation", ","], | ||
" b", | ||
["punctuation", ")"], | ||
["keyword", "in"], | ||
["punctuation", "("], | ||
["punctuation", "("], | ||
["number", "1"], | ||
["punctuation", ","], | ||
["number", "2"], | ||
["punctuation", ")"], | ||
["punctuation", ","], | ||
["punctuation", "("], | ||
["number", "3"], | ||
["punctuation", ","], | ||
["number", "4"], | ||
["punctuation", ")"], | ||
["punctuation", ")"], | ||
["punctuation", "}"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "'"] | ||
]], | ||
|
||
["string-interpolation", [ | ||
["string", "f'"], | ||
["interpolation", [ | ||
["punctuation", "{"], | ||
["punctuation", "("], | ||
["keyword", "lambda"], | ||
" x", | ||
["punctuation", ":"], | ||
" x", | ||
["operator", "*"], | ||
["number", "2"], | ||
["punctuation", ")"], | ||
["punctuation", "("], | ||
["number", "3"], | ||
["punctuation", ")"], | ||
["punctuation", "}"] | ||
]], | ||
["string", "'"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for string interpolation. |
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