Skip to content

Commit

Permalink
Make raw f-strings consistent.
Browse files Browse the repository at this point in the history
Any f-string allows variable interpolation, that includes a raw
f-string. Make all raw prefixes (`r` or `R`) behave the same way in
combination with `f` prefix.

Fixes #186
  • Loading branch information
vpetrovykh committed Oct 14, 2022
1 parent db32a0d commit 5b5f388
Show file tree
Hide file tree
Showing 19 changed files with 546 additions and 3,434 deletions.
910 changes: 2 additions & 908 deletions grammars/MagicPython.cson

Large diffs are not rendered by default.

1,788 changes: 2 additions & 1,786 deletions grammars/MagicPython.tmLanguage

Large diffs are not rendered by default.

80 changes: 0 additions & 80 deletions grammars/src/MagicPython.syntax.yaml
Expand Up @@ -103,82 +103,6 @@ repository:
bquote: '(""")'
equote: '(""")'

- file: 'regexp.inc.syntax.yaml'
vars:
prefix: 'single-one-'
basename: 'fregexp'
marker: "|(?=\\')"
nested: ''
guard: "|((?=(?<!\\\\)\\n))"

- file: 'regexp.inc.syntax.yaml'
vars:
prefix: 'single-three-'
basename: 'fregexp'
marker: "|(?=\\'\\'\\')"
nested: "- include: '#comments-string-single-three'"
guard: ""

- file: 'regexp.inc.syntax.yaml'
vars:
prefix: 'double-one-'
basename: 'fregexp'
marker: '|(?=")'
nested: ''
guard: "|((?=(?<!\\\\)\\n))"

- file: 'regexp.inc.syntax.yaml'
vars:
prefix: 'double-three-'
basename: 'fregexp'
marker: '|(?=""")'
nested: "- include: '#comments-string-double-three'"
guard: ""

- file: 'regexp.wrapper.syntax.yaml'
vars:
scopename: string.regexp.quoted.single.python
string.interpolated.python
prefix: 'single-one-'
basename: 'fregexp'
nested: ''
modifier: '[fF]'
bquote: "(\\')"
equote: "(\\')|(?<!\\\\)(\\n)"

- file: 'regexp.wrapper.syntax.yaml'
vars:
scopename: string.regexp.quoted.multi.python
string.interpolated.python
prefix: 'single-three-'
basename: 'fregexp'
nested: "- include: '#comments-string-single-three'"
modifier: '[fF]'
bquote: "(\\'\\'\\')"
equote: "(\\'\\'\\')"

- file: 'regexp.wrapper.syntax.yaml'
vars:
scopename: string.regexp.quoted.single.python
string.interpolated.python
prefix: 'double-one-'
basename: 'fregexp'
nested: ''
modifier: '[fF]'
bquote: '(")'
equote: '(")|(?<!\\)(\n)'

- file: 'regexp.wrapper.syntax.yaml'
vars:
scopename: string.regexp.quoted.multi.python
string.interpolated.python
prefix: 'double-three-'
basename: 'fregexp'
nested: "- include: '#comments-string-double-three'"
modifier: '[fF]'
bquote: '(""")'
equote: '(""")'

- file: 'pystring.inc.syntax.yaml'
vars:
line: single
Expand Down Expand Up @@ -700,10 +624,6 @@ repository:
- include: '#regexp-double-three-line'
- include: '#regexp-single-one-line'
- include: '#regexp-double-one-line'
- include: '#fregexp-single-three-line'
- include: '#fregexp-double-three-line'
- include: '#fregexp-single-one-line'
- include: '#fregexp-double-one-line'

string:
patterns:
Expand Down
2 changes: 1 addition & 1 deletion grammars/src/pyfstring.inc.syntax.yaml
Expand Up @@ -51,7 +51,7 @@ fstring-normf-quoted-${line}-line:

fstring-raw-quoted-${line}-line:
name: meta.fstring.python
begin: (\b(?:[R][fF]|[fF][R]))(${marker})
begin: (\b(?:[rR][fF]|[fF][rR]))(${marker})
end: (\2)${guard}
beginCaptures:
'1': {name: storage.type.string.python
Expand Down
2 changes: 2 additions & 0 deletions misc/example.py
Expand Up @@ -22,6 +22,8 @@ async def coroutine(db:aio_db.DatabaseConnection) -> List[str]:
async with db.transaction():
result = await db.query(...)
print(f'Result: {result!r} {a=} {b=!r}')
print(Rf'data: {c=}')
print(rf'data: {c=}')

mapping = None # type: Dict[int, Any] # PEP 484

Expand Down
744 changes: 335 additions & 409 deletions test/atom-spec/python-spec.js

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions test/fstrings/empty2.py
Expand Up @@ -7,20 +7,22 @@



rf : source.python, storage.type.string.python, string.interpolated.python, string.regexp.quoted.single.python
" : punctuation.definition.string.begin.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
{ : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
} : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
: source.python, string.interpolated.python, string.regexp.quoted.single.python
{ : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
: invalid.illegal.brace.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
} : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
" : punctuation.definition.string.end.python, source.python, string.interpolated.python, string.regexp.quoted.single.python
rf : source.python, storage.type.string.python, string.interpolated.python, string.regexp.quoted.multi.python
""" : punctuation.definition.string.begin.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
{ : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
} : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
{ : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
: invalid.illegal.brace.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
} : constant.character.format.placeholder.other.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
""" : punctuation.definition.string.end.python, source.python, string.interpolated.python, string.regexp.quoted.multi.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
: meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
: invalid.illegal.brace.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.multi.python
""" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.multi.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
: meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
: invalid.illegal.brace.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
: meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
""" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
40 changes: 40 additions & 0 deletions test/fstrings/fraw1.py
@@ -0,0 +1,40 @@
a = fr'[a-z]'
a = Fr'[a-z]'
a = rf'[a-z]'
a = rF'[a-z]'




a : source.python
: source.python
= : keyword.operator.assignment.python, source.python
: source.python
fr : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
[a-z] : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
a : source.python
: source.python
= : keyword.operator.assignment.python, source.python
: source.python
Fr : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
[a-z] : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
a : source.python
: source.python
= : keyword.operator.assignment.python, source.python
: source.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
[a-z] : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
a : source.python
: source.python
= : keyword.operator.assignment.python, source.python
: source.python
rF : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
[a-z] : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
36 changes: 36 additions & 0 deletions test/fstrings/fraw2.py
@@ -0,0 +1,36 @@
rf'fo{{2}}'
rf"fo{{2}}"
rf'''fo{{2}}'''
rf"""fo{{2}}"""




rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
{{ : constant.character.escape.python, meta.fstring.python, source.python
2 : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
}} : constant.character.escape.python, meta.fstring.python, source.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
{{ : constant.character.escape.python, meta.fstring.python, source.python
2 : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
}} : constant.character.escape.python, meta.fstring.python, source.python
" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.multi.python
''' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.multi.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
{{ : constant.character.escape.python, meta.fstring.python, source.python
2 : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
}} : constant.character.escape.python, meta.fstring.python, source.python
''' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.multi.python
""" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.multi.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
{{ : constant.character.escape.python, meta.fstring.python, source.python
2 : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
}} : constant.character.escape.python, meta.fstring.python, source.python
""" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
36 changes: 36 additions & 0 deletions test/fstrings/fraw3.py
@@ -0,0 +1,36 @@
rf'fo{2}'
rf"fo{2}"
rf'''fo{2}'''
rf"""fo{2}"""




rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
2 : constant.numeric.dec.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.single.python
" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.single.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.single.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
2 : constant.numeric.dec.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.single.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.multi.python
''' : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.multi.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
2 : constant.numeric.dec.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
''' : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
rf : meta.fstring.python, source.python, storage.type.string.python, string.interpolated.python, string.quoted.raw.multi.python
""" : meta.fstring.python, punctuation.definition.string.begin.python, source.python, string.quoted.raw.multi.python
fo : meta.fstring.python, source.python, string.interpolated.python, string.quoted.raw.multi.python
{ : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
2 : constant.numeric.dec.python, meta.fstring.python, source.python
} : constant.character.format.placeholder.other.python, meta.fstring.python, source.python
""" : meta.fstring.python, punctuation.definition.string.end.python, source.python, string.interpolated.python, string.quoted.raw.multi.python

0 comments on commit 5b5f388

Please sign in to comment.