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 PEP 498, Literal String Interpolation #110

Merged
merged 1 commit into from
Sep 4, 2017
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
2 changes: 2 additions & 0 deletions baron/formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class UnExpectedSpaceToken(BaronError):
STRING = (
"STRING",
"RAW_STRING",
"INTERPOLATED_STRING",
"INTERPOLATED_RAW_STRING",
"UNICODE_STRING",
"UNICODE_RAW_STRING",
"BINARY_STRING",
Expand Down
2 changes: 2 additions & 0 deletions baron/grammator.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,12 @@ def strings_string(pack):
# TODO tests those other kind of strings
@pg.production("string : STRING")
@pg.production("string : RAW_STRING")
@pg.production("string : INTERPOLATED_STRING")
@pg.production("string : UNICODE_STRING")
@pg.production("string : BINARY_STRING")
@pg.production("string : UNICODE_RAW_STRING")
@pg.production("string : BINARY_RAW_STRING")
@pg.production("string : INTERPOLATED_RAW_STRING")
def string(pack):
(string_,) = pack
return [{
Expand Down
4 changes: 2 additions & 2 deletions baron/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def group_generator(sequence):
current += next(iterator)
if current in to_group_keys and matching_found(to_group, current, iterator.show_next()):
current += next(iterator)
if current in list('uUrRbB') and str(iterator.show_next()).startswith(('"', "'")):
if current in list('uUfFrRbB') and str(iterator.show_next()).startswith(('"', "'")):
current += next(iterator)
if str(current).lower() in ["ur", "br"] and str(iterator.show_next()).startswith(('"', "'")):
if str(current).lower() in ["ur", "br", "fr"] and str(iterator.show_next()).startswith(('"', "'")):
current += next(iterator)
if any([re.match(x, current) for x in (r'^\d+[eE]$', r'^\d+\.\d*[eE]$', r'^\.\d+[eE]$')]):
current += next(iterator)
Expand Down
2 changes: 2 additions & 0 deletions baron/inner_formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GroupingError(BaronError):
# TODO test everything bellow
"STRING",
"RAW_STRING",
"INTERPOLATED_STRING",
"INTERPOLATED_RAW_STRING",
"BINARY_STRING",
"BINARY_RAW_STRING",
"UNICODE_STRING",
Expand Down
2 changes: 2 additions & 0 deletions baron/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ class UnknowItem(BaronError):
(r'(\s|\\\n|\\\r\n)+', 'SPACE'),
(r'["\'](.|\n|\r)*["\']', 'STRING'),
(r'[uU]["\'](.|\n|\r)*["\']', 'UNICODE_STRING'),
(r'[fF]["\'](.|\n|\r)*["\']', 'INTERPOLATED_STRING'),
(r'[rR]["\'](.|\n|\r)*["\']', 'RAW_STRING'),
(r'[bB]["\'](.|\n|\r)*["\']', 'BINARY_STRING'),
(r'[uU][rR]["\'](.|\n|\r)*["\']', 'UNICODE_RAW_STRING'),
(r'[bB][rR]["\'](.|\n|\r)*["\']', 'BINARY_RAW_STRING'),
(r'[fF][rR]["\'](.|\n|\r)*["\']', 'INTERPOLATED_RAW_STRING'),
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ def test_strings():
"""
I don't this because python allow to write stuff like 'qsd' rb"qsd" u'pouet'
"""
for i in ('STRING', 'RAW_STRING', 'UNICODE_STRING', 'UNICODE_RAW_STRING', 'BINARY_STRING', 'BINARY_RAW_STRING'):
for i in ('STRING', 'RAW_STRING', 'UNICODE_STRING', 'INTERPOLATED_STRING', 'UNICODE_RAW_STRING', 'BINARY_STRING', 'INTERPOLATED_RAW_STRING', 'BINARY_RAW_STRING'):
group([
('SPACE', ' '),
(i, 'dummy'),
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,27 @@ def test_unicode_string():
match("U'''pouet pouet'''", "UNICODE_STRING")


def test_interpolated_string():
match("f'He said his name is {name!r}.'", 'INTERPOLATED_STRING')
match("f'The value is {value}.'", 'INTERPOLATED_STRING')
match('F"He said his name is {name!r}."', 'INTERPOLATED_STRING')
match('f"The value is {value}."', 'INTERPOLATED_STRING')
match("F'{date} was on a {date:%A}'", 'INTERPOLATED_STRING')
match("f'a={d[\"a\"]}'", 'INTERPOLATED_STRING')
match("F'{(lambda x: x*2)(3)}'", 'INTERPOLATED_STRING')


def test_interpolated_raw_string():
match("fr'He said his name is {name!r}.'", 'INTERPOLATED_RAW_STRING')
match("fr'The value is {value}.'", 'INTERPOLATED_RAW_STRING')
match('Fr"He said his name is {name!r}."', 'INTERPOLATED_RAW_STRING')
match('fR"The value is {value}."', 'INTERPOLATED_RAW_STRING')
match("FR'{date} was on a {date:%A}'", 'INTERPOLATED_RAW_STRING')
match("fr'a={d[\"a\"]}'", 'INTERPOLATED_RAW_STRING')
match("FR'{(lambda x: x*2)(3)}'", 'INTERPOLATED_RAW_STRING')
match("FR'{(lambda x: x*2)(3)}'", 'INTERPOLATED_RAW_STRING')


def test_raw_string():
match('r"pouet pouet"', 'RAW_STRING')
match("r'pouet pouet'", "RAW_STRING")
Expand Down