Skip to content

Commit e3eeca8

Browse files
committed
Add Python 3.8 features.
Add walrus operator `:=`. Add positional only args separator `/`. Add `=` to f-string formatting. Fixes #189
1 parent 2802ded commit e3eeca8

File tree

11 files changed

+439
-18
lines changed

11 files changed

+439
-18
lines changed

Diff for: grammars/MagicPython.cson

+26-6
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ repository:
619619
620620
| (!= | == | >= | <= | < | >) (?# 5)
621621
622+
| (:=) (?# 6)
623+
622624
'''
623625
captures:
624626
"1":
@@ -631,6 +633,8 @@ repository:
631633
name: "keyword.operator.arithmetic.python"
632634
"5":
633635
name: "keyword.operator.comparison.python"
636+
"6":
637+
name: "keyword.operator.assignment.python"
634638
punctuation:
635639
patterns: [
636640
{
@@ -1255,6 +1259,10 @@ repository:
12551259
name: "punctuation.section.function.lambda.begin.python"
12561260
contentName: "meta.function.lambda.parameters.python"
12571261
patterns: [
1262+
{
1263+
name: "keyword.operator.positional.parameter.python"
1264+
match: "/"
1265+
}
12581266
{
12591267
name: "keyword.operator.unpacking.parameter.python"
12601268
match: "(\\*\\*|\\*)"
@@ -1402,6 +1410,10 @@ repository:
14021410
"1":
14031411
name: "punctuation.definition.parameters.end.python"
14041412
patterns: [
1413+
{
1414+
name: "keyword.operator.positional.parameter.python"
1415+
match: "/"
1416+
}
14051417
{
14061418
name: "keyword.operator.unpacking.parameter.python"
14071419
match: "(\\*\\*|\\*)"
@@ -4720,12 +4732,16 @@ repository:
47204732
patterns: [
47214733
{
47224734
name: "storage.type.format.python"
4723-
match: "(![rsa])(?=})"
4735+
match: "(=(![rsa])?)(?=})"
4736+
}
4737+
{
4738+
name: "storage.type.format.python"
4739+
match: "(=?![rsa])(?=})"
47244740
}
47254741
{
47264742
match: '''
47274743
(?x)
4728-
(![rsa])?
4744+
( (?: =?) (?: ![rsa])? )
47294745
( : \\w? [<>=^]? [-+ ]? \\#?
47304746
\\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )(?=})
47314747
@@ -4741,7 +4757,7 @@ repository:
47414757
}
47424758
]
47434759
"fstring-terminator-single-tail":
4744-
begin: "(![rsa])?(:)(?=.*?{)"
4760+
begin: "((?:=?)(?:![rsa])?)(:)(?=.*?{)"
47454761
end: "(?=})|(?=\\n)"
47464762
beginCaptures:
47474763
"1":
@@ -4944,12 +4960,16 @@ repository:
49444960
patterns: [
49454961
{
49464962
name: "storage.type.format.python"
4947-
match: "(![rsa])(?=})"
4963+
match: "(=(![rsa])?)(?=})"
4964+
}
4965+
{
4966+
name: "storage.type.format.python"
4967+
match: "(=?![rsa])(?=})"
49484968
}
49494969
{
49504970
match: '''
49514971
(?x)
4952-
(![rsa])?
4972+
( (?: =?) (?: ![rsa])? )
49534973
( : \\w? [<>=^]? [-+ ]? \\#?
49544974
\\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )(?=})
49554975
@@ -4965,7 +4985,7 @@ repository:
49654985
}
49664986
]
49674987
"fstring-terminator-multi-tail":
4968-
begin: "(![rsa])?(:)(?=.*?{)"
4988+
begin: "((?:=?)(?:![rsa])?)(:)(?=.*?{)"
49694989
end: "(?=})"
49704990
beginCaptures:
49714991
"1":

Diff for: grammars/MagicPython.tmLanguage

+37-6
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ E.g. "arr[idx](args)"
933933
| (\*\* | \* | \+ | - | % | // | / | @) (?# 4)
934934
935935
| (!= | == | &gt;= | &lt;= | &lt; | &gt;) (?# 5)
936+
937+
| (:=) (?# 6)
936938
</string>
937939
<key>captures</key>
938940
<dict>
@@ -961,6 +963,11 @@ E.g. "arr[idx](args)"
961963
<key>name</key>
962964
<string>keyword.operator.comparison.python</string>
963965
</dict>
966+
<key>6</key>
967+
<dict>
968+
<key>name</key>
969+
<string>keyword.operator.assignment.python</string>
970+
</dict>
964971
</dict>
965972
</dict>
966973
<key>punctuation</key>
@@ -1936,6 +1943,12 @@ E.g. "arr[idx](args)"
19361943
<string>meta.function.lambda.parameters.python</string>
19371944
<key>patterns</key>
19381945
<array>
1946+
<dict>
1947+
<key>name</key>
1948+
<string>keyword.operator.positional.parameter.python</string>
1949+
<key>match</key>
1950+
<string>/</string>
1951+
</dict>
19391952
<dict>
19401953
<key>name</key>
19411954
<string>keyword.operator.unpacking.parameter.python</string>
@@ -2184,6 +2197,12 @@ correctly identify the "in" as a control flow keyword.
21842197
</dict>
21852198
<key>patterns</key>
21862199
<array>
2200+
<dict>
2201+
<key>name</key>
2202+
<string>keyword.operator.positional.parameter.python</string>
2203+
<key>match</key>
2204+
<string>/</string>
2205+
</dict>
21872206
<dict>
21882207
<key>name</key>
21892208
<string>keyword.operator.unpacking.parameter.python</string>
@@ -8259,12 +8278,18 @@ indirectly through syntactic constructs
82598278
<key>name</key>
82608279
<string>storage.type.format.python</string>
82618280
<key>match</key>
8262-
<string>(![rsa])(?=})</string>
8281+
<string>(=(![rsa])?)(?=})</string>
8282+
</dict>
8283+
<dict>
8284+
<key>name</key>
8285+
<string>storage.type.format.python</string>
8286+
<key>match</key>
8287+
<string>(=?![rsa])(?=})</string>
82638288
</dict>
82648289
<dict>
82658290
<key>match</key>
82668291
<string>(?x)
8267-
(![rsa])?
8292+
( (?: =?) (?: ![rsa])? )
82688293
( : \w? [&lt;&gt;=^]? [-+ ]? \#?
82698294
\d* ,? (\.\d+)? [bcdeEfFgGnosxX%]? )(?=})
82708295
</string>
@@ -8291,7 +8316,7 @@ indirectly through syntactic constructs
82918316
<key>fstring-terminator-single-tail</key>
82928317
<dict>
82938318
<key>begin</key>
8294-
<string>(![rsa])?(:)(?=.*?{)</string>
8319+
<string>((?:=?)(?:![rsa])?)(:)(?=.*?{)</string>
82958320
<key>end</key>
82968321
<string>(?=})|(?=\n)</string>
82978322
<key>beginCaptures</key>
@@ -8631,12 +8656,18 @@ indirectly through syntactic constructs
86318656
<key>name</key>
86328657
<string>storage.type.format.python</string>
86338658
<key>match</key>
8634-
<string>(![rsa])(?=})</string>
8659+
<string>(=(![rsa])?)(?=})</string>
8660+
</dict>
8661+
<dict>
8662+
<key>name</key>
8663+
<string>storage.type.format.python</string>
8664+
<key>match</key>
8665+
<string>(=?![rsa])(?=})</string>
86358666
</dict>
86368667
<dict>
86378668
<key>match</key>
86388669
<string>(?x)
8639-
(![rsa])?
8670+
( (?: =?) (?: ![rsa])? )
86408671
( : \w? [&lt;&gt;=^]? [-+ ]? \#?
86418672
\d* ,? (\.\d+)? [bcdeEfFgGnosxX%]? )(?=})
86428673
</string>
@@ -8663,7 +8694,7 @@ indirectly through syntactic constructs
86638694
<key>fstring-terminator-multi-tail</key>
86648695
<dict>
86658696
<key>begin</key>
8666-
<string>(![rsa])?(:)(?=.*?{)</string>
8697+
<string>((?:=?)(?:![rsa])?)(:)(?=.*?{)</string>
86678698
<key>end</key>
86688699
<string>(?=})</string>
86698700
<key>beginCaptures</key>

Diff for: grammars/src/MagicPython.syntax.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,15 @@ repository:
582582
583583
| (!= | == | >= | <= | < | >) (?# 5)
584584
585+
| (:=) (?# 6)
586+
585587
captures:
586588
'1': {name: keyword.operator.logical.python}
587589
'2': {name: keyword.control.flow.python}
588590
'3': {name: keyword.operator.bitwise.python}
589591
'4': {name: keyword.operator.arithmetic.python}
590592
'5': {name: keyword.operator.comparison.python}
593+
'6': {name: keyword.operator.assignment.python}
591594

592595
punctuation:
593596
patterns:
@@ -1002,6 +1005,8 @@ repository:
10021005

10031006
contentName: meta.function.lambda.parameters.python
10041007
patterns:
1008+
- name: keyword.operator.positional.parameter.python
1009+
match: /
10051010
- name: keyword.operator.unpacking.parameter.python
10061011
match: (\*\*|\*)
10071012
- include: '#lambda-nested-incomplete'
@@ -1096,6 +1101,8 @@ repository:
10961101
'1': {name: punctuation.definition.parameters.end.python}
10971102

10981103
patterns:
1104+
- name: keyword.operator.positional.parameter.python
1105+
match: /
10991106
- name: keyword.operator.unpacking.parameter.python
11001107
match: (\*\*|\*)
11011108
- include: '#lambda-incomplete'

Diff for: grammars/src/pyfstring.inc.syntax.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ fstring-${line}-brace:
127127
fstring-terminator-${line}:
128128
patterns:
129129
- name: storage.type.format.python
130-
match: (![rsa])(?=})
130+
match: (=(![rsa])?)(?=})
131+
- name: storage.type.format.python
132+
match: (=?![rsa])(?=})
131133
- match: |
132134
(?x)
133-
(![rsa])?
135+
( (?: =?) (?: ![rsa])? )
134136
( : \w? [<>=^]? [-+ ]? \#?
135137
\d* ,? (\.\d+)? [bcdeEfFgGnosxX%]? )(?=})
136138
captures:
@@ -148,7 +150,7 @@ fstring-terminator-${line}:
148150
- include: '#fstring-terminator-${line}-tail'
149151

150152
fstring-terminator-${line}-tail:
151-
begin: (![rsa])?(:)(?=.*?{)
153+
begin: ((?:=?)(?:![rsa])?)(:)(?=.*?{)
152154
end: (?=})${fguard}
153155
beginCaptures:
154156
'1': {name: storage.type.format.python}

Diff for: misc/example.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22

33

4-
def showcase():
4+
def showcase(a, b, /, c, d, *, e, f):
55
"""Some code to showcase the syntax.
66
77
Docstrings are recognized and have an additional scope.
@@ -21,7 +21,7 @@ async def coroutine(db:aio_db.DatabaseConnection) -> List[str]:
2121

2222
async with db.transaction():
2323
result = await db.query(...)
24-
print(f'Result: {result!r}')
24+
print(f'Result: {result!r} {a=} {b=!r}')
2525

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

@@ -41,8 +41,14 @@ async def coroutine(db:aio_db.DatabaseConnection) -> List[str]:
4141
# NOTE Numbers with leading zeros are invalid in Python 3,
4242
# use 0o...
4343
answer = func(0xdeadbeef + 0b00100001 + 0123 + 0o123 +
44-
1_005_123 + # PEP 515
44+
1_005_123 + # PEP 515
4545
# complex numbers
4646
.10e12 + 2j) @ mat
4747

48+
# walrus operator
49+
filtered_data = [y for x in data if (y := f(x)) is not None]
50+
51+
# position-only params
52+
bar = lambda q, w, /, e, r: (q + w + e + r)
53+
4854
return R'''No escapes '\' in this \one'''

Diff for: misc/scopes

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ keyword.operator.lookahead.regexp
5656
keyword.operator.lookbehind.negative.regexp
5757
keyword.operator.lookbehind.regexp
5858
keyword.operator.negation.regexp
59+
keyword.operator.positional.parameter.python
5960
keyword.operator.python
6061
keyword.operator.quantifier.regexp
6162
keyword.operator.unpacking.arguments.python

0 commit comments

Comments
 (0)