Skip to content

Commit

Permalink
Support PEP 498, Literal String Interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
odcinek committed Aug 18, 2017
1 parent 08c7561 commit eeabbc2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
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

0 comments on commit eeabbc2

Please sign in to comment.