Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Named Unicode Characters in f-strings #424

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions libcst/_nodes/tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,34 @@ class AtomTest(CSTNodeTest):
"parser": _parse_expression_force_38,
"expected_position": None,
},
{
"node": cst.FormattedString(
parts=(
cst.FormattedStringExpression(
cst.Yield(
value=cst.Integer("1"),
whitespace_after_yield=cst.SimpleWhitespace(" "),
),
),
),
),
"code": 'f"{yield 1}"',
"parser": _parse_expression_force_38,
"expected_position": None,
},
{
"node": cst.FormattedString(
parts=(
cst.FormattedStringText("\\N{X Y}"),
cst.FormattedStringExpression(
cst.Name(value="Z"),
),
),
),
"code": 'f"\\N{X Y}{Z}"',
"parser": parse_expression,
"expected_position": None,
},
# Validate parens
{
"node": cst.FormattedString(
Expand Down
6 changes: 3 additions & 3 deletions libcst/_parser/conversions/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,13 +1038,13 @@ def convert_fstring_equality(

@with_production(
"fstring_expr",
"'{' testlist_comp_tuple [ fstring_equality ] [ fstring_conversion ] [ fstring_format_spec ] '}'",
"'{' (testlist_comp_tuple | yield_expr) [ fstring_equality ] [ fstring_conversion ] [ fstring_format_spec ] '}'",
version=">=3.8",
)
@with_production(
"fstring_expr",
"'{' testlist_comp_tuple [ fstring_conversion ] [ fstring_format_spec ] '}'",
version="<=3.7",
"'{' (testlist_comp_tuple | yield_expr) [ fstring_conversion ] [ fstring_format_spec ] '}'",
version="<3.8",
)
def convert_fstring_expr(
config: ParserConfig, children: typing.Sequence[typing.Any]
Expand Down
11 changes: 9 additions & 2 deletions libcst/_parser/parso/python/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ def _get_token_collection(version_info: PythonVersionInfo) -> TokenCollection:
return result


fstring_string_single_line = _compile(r"(?:\{\{|\}\}|\\(?:\r\n?|\n)|[^{}\r\n])+")
fstring_string_multi_line = _compile(r"(?:[^{}]+|\{\{|\}\})+")
unicode_character_name = r"[A-Za-z0-9\-]+(?: [A-Za-z0-9\-]+)*"
fstring_string_single_line = _compile(
r"(?:\{\{|\}\}|\\N\{"
+ unicode_character_name
+ r"\}|\\(?:\r\n?|\n)|\\[^\r\nN]|[^{}\r\n\\])+"
)
fstring_string_multi_line = _compile(
r"(?:\{\{|\}\}|\\N\{" + unicode_character_name + r"\}|\\[^N]|[^{}\\])+"
)
fstring_format_spec_single_line = _compile(r"(?:\\(?:\r\n?|\n)|[^{}\r\n])+")
fstring_format_spec_multi_line = _compile(r"[^{}]+")

Expand Down