Skip to content

Commit 1714444

Browse files
committed
Add support for fstrings.
1 parent 8377808 commit 1714444

15 files changed

+879
-2
lines changed

grammars/MagicPython.cson

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,12 @@ repository:
795795
{
796796
include: "#string-raw-bin-quoted-single-line"
797797
}
798+
{
799+
include: "#fstring-quoted-multi-line"
800+
}
801+
{
802+
include: "#fstring-quoted-single-line"
803+
}
798804
]
799805
"string-unicode-guts":
800806
patterns: [
@@ -843,6 +849,22 @@ repository:
843849
include: "#string-formatting"
844850
}
845851
]
852+
"fstring-guts":
853+
patterns: [
854+
{
855+
include: "#escape-sequence-unicode"
856+
}
857+
{
858+
include: "#escape-sequence"
859+
}
860+
{
861+
include: "#string-line-continuation"
862+
}
863+
{
864+
name: "constant.character.escape.python"
865+
match: "(\\\\[{}])"
866+
}
867+
]
846868
"escape-sequence-unicode":
847869
patterns: [
848870
{
@@ -3335,3 +3357,141 @@ repository:
33353357
include: "#string-formatting"
33363358
}
33373359
]
3360+
"fstring-quoted-single-line":
3361+
begin: "(\\b[fF])((['\"]))"
3362+
end: "(\\2)|((?<!\\\\)\\n)"
3363+
beginCaptures:
3364+
"1":
3365+
name: "string.quoted.single.python storage.type.string.python"
3366+
"2":
3367+
name: "string.quoted.single.python punctuation.definition.string.begin.python"
3368+
endCaptures:
3369+
"1":
3370+
name: "string.quoted.single.python punctuation.definition.string.end.python"
3371+
"2":
3372+
name: "invalid.illegal.newline.python"
3373+
patterns: [
3374+
{
3375+
include: "#fstring-guts"
3376+
}
3377+
{
3378+
include: "#fstring-single-brace"
3379+
}
3380+
{
3381+
name: "string.quoted.single.python"
3382+
match: '''
3383+
(?x)
3384+
(.*?)
3385+
(
3386+
(?# .* and .*? in multi-line match need special handling of
3387+
newlines otherwise SublimeText and Atom will match slightly
3388+
differently.
3389+
3390+
The guard for newlines has to be separate from the
3391+
lookahead because of special $ matching rule.)
3392+
($\\n?)
3393+
|
3394+
(?=[\\\\\\{]|(['"])|((?<!\\\\)\\n))
3395+
)
3396+
3397+
'''
3398+
}
3399+
]
3400+
"fstring-single-brace":
3401+
comment: "value interpolation using { ... }"
3402+
begin: "(\\{)"
3403+
end: '''
3404+
(?x)
3405+
(![rsa])?
3406+
( : \\w? [<>=^]? [-+ ]? \\#?
3407+
\\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )?
3408+
(\\})
3409+
3410+
'''
3411+
beginCaptures:
3412+
"1":
3413+
name: "constant.character.format.placeholder.other.python"
3414+
endCaptures:
3415+
"1":
3416+
name: "storage.type.format.python"
3417+
"2":
3418+
name: "support.other.format.python"
3419+
"4":
3420+
name: "constant.character.format.placeholder.other.python"
3421+
patterns: [
3422+
{
3423+
include: "#fstring-terminator"
3424+
}
3425+
{
3426+
include: "#expression"
3427+
}
3428+
]
3429+
"fstring-quoted-multi-line":
3430+
begin: "(\\b[fF])('''|\"\"\")"
3431+
end: "(\\2)"
3432+
beginCaptures:
3433+
"1":
3434+
name: "string.quoted.multi.python storage.type.string.python"
3435+
"2":
3436+
name: "string.quoted.multi.python punctuation.definition.string.begin.python"
3437+
endCaptures:
3438+
"1":
3439+
name: "string.quoted.multi.python punctuation.definition.string.end.python"
3440+
"2":
3441+
name: "invalid.illegal.newline.python"
3442+
patterns: [
3443+
{
3444+
include: "#fstring-guts"
3445+
}
3446+
{
3447+
include: "#fstring-multi-brace"
3448+
}
3449+
{
3450+
name: "string.quoted.multi.python"
3451+
match: '''
3452+
(?x)
3453+
(.*?)
3454+
(
3455+
(?# .* and .*? in multi-line match need special handling of
3456+
newlines otherwise SublimeText and Atom will match slightly
3457+
differently.
3458+
3459+
The guard for newlines has to be separate from the
3460+
lookahead because of special $ matching rule.)
3461+
($\\n?)
3462+
|
3463+
(?=[\\\\\\{]|\'''|""")
3464+
)
3465+
3466+
'''
3467+
}
3468+
]
3469+
"fstring-multi-brace":
3470+
comment: "value interpolation using { ... }"
3471+
begin: "(\\{)"
3472+
end: '''
3473+
(?x)
3474+
(![rsa])?
3475+
( : \\w? [<>=^]? [-+ ]? \\#?
3476+
\\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )?
3477+
(\\})
3478+
3479+
'''
3480+
beginCaptures:
3481+
"1":
3482+
name: "constant.character.format.placeholder.other.python"
3483+
endCaptures:
3484+
"1":
3485+
name: "storage.type.format.python"
3486+
"2":
3487+
name: "support.other.format.python"
3488+
"4":
3489+
name: "constant.character.format.placeholder.other.python"
3490+
patterns: [
3491+
{
3492+
include: "#fstring-terminator"
3493+
}
3494+
{
3495+
include: "#expression"
3496+
}
3497+
]

0 commit comments

Comments
 (0)