Skip to content

Commit 7e0b909

Browse files
committed
Implement support for PEP 515.
1 parent f4ff7de commit 7e0b909

File tree

12 files changed

+124
-30
lines changed

12 files changed

+124
-30
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ Most numbers are just regular decimal constants, but any time that octal,
151151
binary, hexadecimal or complex numbers are used it's worth noting that they are
152152
of a special type. Highlighting of Python 2 'L' integers is also supported.
153153

154+
Underscores in numeric literals are also supported (PEP 515, introduced in
155+
Python 3.6):
156+
157+
```python
158+
100_000_000_000 0b_1110_0101 0x_FF_12_A0_99
159+
```
160+
154161

155162
### Python 3.5 features
156163

grammars/MagicPython.cson

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,25 +580,31 @@ repository:
580580
match: '''
581581
(?x)
582582
(?<! \\w)(?:
583-
[0-9]*\\.[0-9]+ ([eE][+-]?[0-9]+)?
584-
| [0-9]+\\. ([eE][+-]?[0-9]+)?
585-
| [0-9]+ ([eE][+-]?[0-9]+)
583+
(?:
584+
\\.[0-9](?: _?[0-9] )*
585+
|
586+
[0-9](?: _?[0-9] )* \\. [0-9](?: _?[0-9] )*
587+
|
588+
[0-9](?: _?[0-9] )* \\.
589+
) (?: [eE][+-]?[0-9](?: _?[0-9] )* )?
590+
|
591+
[0-9](?: _?[0-9] )* (?: [eE][+-]?[0-9](?: _?[0-9] )* )
586592
)([jJ])?\\b
587593
588594
'''
589595
captures:
590-
"4":
596+
"1":
591597
name: "storage.type.imaginary.number.python"
592598
"number-dec":
593599
name: "constant.numeric.dec.python"
594600
match: '''
595601
(?x)
596602
(?<![\\w\\.])(?:
597-
[1-9][0-9]*
603+
[1-9](?: _?[0-9] )*
598604
|
599605
0+
600606
|
601-
[0-9]+ ([jJ])
607+
[0-9](?: _?[0-9] )* ([jJ])
602608
|
603609
0 ([0-9]+)(?![eE\\.])
604610
)\\b
@@ -616,7 +622,7 @@ repository:
616622
match: '''
617623
(?x)
618624
(?<![\\w\\.])
619-
(0[xX]) [0-9a-fA-F]+
625+
(0[xX]) (_?[0-9a-fA-F])+
620626
\\b
621627
622628
'''
@@ -628,7 +634,7 @@ repository:
628634
match: '''
629635
(?x)
630636
(?<![\\w\\.])
631-
(0[oO]) [0-7]+
637+
(0[oO]) (_?[0-7])+
632638
\\b
633639
634640
'''
@@ -640,7 +646,7 @@ repository:
640646
match: '''
641647
(?x)
642648
(?<![\\w\\.])
643-
(0[bB]) [01]+
649+
(0[bB]) (_?[01])+
644650
\\b
645651
646652
'''

grammars/MagicPython.tmLanguage

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -902,14 +902,20 @@
902902
<key>match</key>
903903
<string>(?x)
904904
(?&lt;! \w)(?:
905-
[0-9]*\.[0-9]+ ([eE][+-]?[0-9]+)?
906-
| [0-9]+\. ([eE][+-]?[0-9]+)?
907-
| [0-9]+ ([eE][+-]?[0-9]+)
905+
(?:
906+
\.[0-9](?: _?[0-9] )*
907+
|
908+
[0-9](?: _?[0-9] )* \. [0-9](?: _?[0-9] )*
909+
|
910+
[0-9](?: _?[0-9] )* \.
911+
) (?: [eE][+-]?[0-9](?: _?[0-9] )* )?
912+
|
913+
[0-9](?: _?[0-9] )* (?: [eE][+-]?[0-9](?: _?[0-9] )* )
908914
)([jJ])?\b
909915
</string>
910916
<key>captures</key>
911917
<dict>
912-
<key>4</key>
918+
<key>1</key>
913919
<dict>
914920
<key>name</key>
915921
<string>storage.type.imaginary.number.python</string>
@@ -923,11 +929,11 @@
923929
<key>match</key>
924930
<string>(?x)
925931
(?&lt;![\w\.])(?:
926-
[1-9][0-9]*
932+
[1-9](?: _?[0-9] )*
927933
|
928934
0+
929935
|
930-
[0-9]+ ([jJ])
936+
[0-9](?: _?[0-9] )* ([jJ])
931937
|
932938
0 ([0-9]+)(?![eE\.])
933939
)\b
@@ -958,7 +964,7 @@
958964
<key>match</key>
959965
<string>(?x)
960966
(?&lt;![\w\.])
961-
(0[xX]) [0-9a-fA-F]+
967+
(0[xX]) (_?[0-9a-fA-F])+
962968
\b
963969
</string>
964970
<key>captures</key>
@@ -977,7 +983,7 @@
977983
<key>match</key>
978984
<string>(?x)
979985
(?&lt;![\w\.])
980-
(0[oO]) [0-7]+
986+
(0[oO]) (_?[0-7])+
981987
\b
982988
</string>
983989
<key>captures</key>
@@ -996,7 +1002,7 @@
9961002
<key>match</key>
9971003
<string>(?x)
9981004
(?&lt;![\w\.])
999-
(0[bB]) [01]+
1005+
(0[bB]) (_?[01])+
10001006
\b
10011007
</string>
10021008
<key>captures</key>

grammars/src/MagicPython.syntax.yaml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,29 @@ repository:
414414
match: |
415415
(?x)
416416
(?<! \w)(?:
417-
[0-9]*\.[0-9]+ ([eE][+-]?[0-9]+)?
418-
| [0-9]+\. ([eE][+-]?[0-9]+)?
419-
| [0-9]+ ([eE][+-]?[0-9]+)
417+
(?:
418+
\.[0-9](?: _?[0-9] )*
419+
|
420+
[0-9](?: _?[0-9] )* \. [0-9](?: _?[0-9] )*
421+
|
422+
[0-9](?: _?[0-9] )* \.
423+
) (?: [eE][+-]?[0-9](?: _?[0-9] )* )?
424+
|
425+
[0-9](?: _?[0-9] )* (?: [eE][+-]?[0-9](?: _?[0-9] )* )
420426
)([jJ])?\b
421427
captures:
422-
'4': {name: storage.type.imaginary.number.python}
428+
'1': {name: storage.type.imaginary.number.python}
423429

424430
number-dec:
425431
name: constant.numeric.dec.python
426432
match: |
427433
(?x)
428434
(?<![\w\.])(?:
429-
[1-9][0-9]*
435+
[1-9](?: _?[0-9] )*
430436
|
431437
0+
432438
|
433-
[0-9]+ ([jJ])
439+
[0-9](?: _?[0-9] )* ([jJ])
434440
|
435441
0 ([0-9]+)(?![eE\.])
436442
)\b
@@ -444,7 +450,7 @@ repository:
444450
match: |
445451
(?x)
446452
(?<![\w\.])
447-
(0[xX]) [0-9a-fA-F]+
453+
(0[xX]) (_?[0-9a-fA-F])+
448454
\b
449455
captures:
450456
'1': {name: storage.type.number.python}
@@ -454,7 +460,7 @@ repository:
454460
match: |
455461
(?x)
456462
(?<![\w\.])
457-
(0[oO]) [0-7]+
463+
(0[oO]) (_?[0-7])+
458464
\b
459465
captures:
460466
'1': {name: storage.type.number.python}
@@ -464,7 +470,7 @@ repository:
464470
match: |
465471
(?x)
466472
(?<![\w\.])
467-
(0[bB]) [01]+
473+
(0[bB]) (_?[01])+
468474
\b
469475
captures:
470476
'1': {name: storage.type.number.python}

misc/example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async def coroutine(db:aio_db.DatabaseConnection) -> List[str]:
4040

4141
# NOTE Numbers with leading zeros are invalid in Python 3,
4242
# use 0o...
43-
answer = func(0xdeadbeef + 0b0010001 + 0123 + 0o123 +
43+
answer = func(0xdeadbeef + 0b00100001 + 0123 + 0o123 +
44+
1_005_123 + # PEP 515
4445
# complex numbers
4546
.10e12 + 2j) @ mat
4647

File renamed without changes.

test/numbers/complex2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
3.141_592j
2+
10_000.j
3+
10_000j
4+
.001_123j
5+
1e10_000j
6+
3.141_592e-10_000j
7+
8+
9+
10+
3.141_592 : constant.numeric.float.python, source.python
11+
j : constant.numeric.float.python, source.python, storage.type.imaginary.number.python
12+
10_000. : constant.numeric.float.python, source.python
13+
j : constant.numeric.float.python, source.python, storage.type.imaginary.number.python
14+
10_000 : constant.numeric.dec.python, source.python
15+
j : constant.numeric.dec.python, source.python, storage.type.imaginary.number.python
16+
.001_123 : constant.numeric.float.python, source.python
17+
j : constant.numeric.float.python, source.python, storage.type.imaginary.number.python
18+
1e10_000 : constant.numeric.float.python, source.python
19+
j : constant.numeric.float.python, source.python, storage.type.imaginary.number.python
20+
3.141_592e-10_000 : constant.numeric.float.python, source.python
21+
j : constant.numeric.float.python, source.python, storage.type.imaginary.number.python

test/numbers/dec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
123
22
000
3+
123_456
4+
1_2_3_4_5_6
35

46

57

68
123 : constant.numeric.dec.python, source.python
79
000 : constant.numeric.dec.python, source.python
10+
123_456 : constant.numeric.dec.python, source.python
11+
1_2_3_4_5_6 : constant.numeric.dec.python, source.python

test/numbers/floats2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
1_234.567_890
2+
0.456_789
3+
000.000_1
4+
.012_34
5+
1_234e5_000
6+
1_234e-5_000
7+
000_123e-000_5
8+
1_234.567_8e+5_000
9+
0.456_78e-5_000
10+
11+
12+
13+
1_234.567_890 : constant.numeric.float.python, source.python
14+
0.456_789 : constant.numeric.float.python, source.python
15+
000.000_1 : constant.numeric.float.python, source.python
16+
.012_34 : constant.numeric.float.python, source.python
17+
1_234e5_000 : constant.numeric.float.python, source.python
18+
1_234e-5_000 : constant.numeric.float.python, source.python
19+
000_123e-000_5 : constant.numeric.float.python, source.python
20+
1_234.567_8e+5_000 : constant.numeric.float.python, source.python
21+
0.456_78e-5_000 : constant.numeric.float.python, source.python

test/numbers/invalid.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
123f
44
123d
55
123A
6+
123__456
7+
123_
68

79

810

@@ -12,3 +14,5 @@
1214
123f : invalid.illegal.name.python, source.python
1315
123d : invalid.illegal.name.python, source.python
1416
123A : invalid.illegal.name.python, source.python
17+
123__456 : invalid.illegal.name.python, source.python
18+
123_ : invalid.illegal.name.python, source.python

0 commit comments

Comments
 (0)