Skip to content

Commit b6cf834

Browse files
author
Michael Lyons
committed
Overhaul numbers and option flags
1 parent 6bebbf5 commit b6cf834

File tree

3 files changed

+231
-122
lines changed

3 files changed

+231
-122
lines changed

PowerShell.sublime-syntax

Lines changed: 104 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ file_extensions:
1010
- psd1
1111

1212
variables:
13+
dec_digits: (?:[\d_]*\d)
14+
dec_exponent: (?:[eE][-+]?{{dec_digits}})
15+
float_suffix: '(?i:[fdm])'
16+
integer_suffix: '(?i:ul?|lu?)'
1317
bytes_unit: (?i:[kmgtp]b)
1418

1519
contexts:
@@ -64,30 +68,51 @@ contexts:
6468
# This should only be relevant inside a class but will require a rework of how classes are matched. This is a temp fix.
6569
scope: storage.modifier.powershell
6670
- match: \b((?i:class)|%|\?)(?:\s)+((?:\p{L}|\d|_|-|)+)\b
67-
# capture should be entity.name.type, but it doesn't provide a good color in the default schema.
6871
captures:
6972
1: storage.type.class.powershell
7073
2: meta.class.powershell entity.name.class.powershell
71-
- match: \B-(?i:as)\b
74+
- match: \B(-)(?i:as)\b
7275
scope: keyword.operator.cast.powershell
73-
- match: \B-(?i:[ic]?(?:eq|ne|[gl][te]))(?!\p{L})
76+
captures:
77+
1: punctuation.definition.keyword.powershell
78+
- match: \B(-)(?i:[ic]?(?:eq|ne|[gl][te]))(?!\p{L})
7479
scope: keyword.operator.comparison.powershell
75-
- match: \B-(?i:[ic]?(?:not)?(?:like|match|contains|in))(?!\p{L})
80+
captures:
81+
1: punctuation.definition.keyword.powershell
82+
- match: \B(-)(?i:[ic]?(?:not)?(?:like|match|contains|in))(?!\p{L})
7683
scope: keyword.operator.logical.powershell
77-
- match: \B-(?i:join|split|replace)(?!\p{L})|!
84+
captures:
85+
1: punctuation.definition.keyword.powershell
86+
- match: \B(-)(?i:join|split|replace)(?!\p{L})|!
7887
scope: keyword.operator.string.powershell
79-
- match: \B-(?i:is(?:not)?)\b
88+
captures:
89+
1: punctuation.definition.keyword.powershell
90+
- match: \B(-)(?i:is(?:not)?)\b
8091
scope: keyword.operator.logical.powershell
81-
- match: \B-(?i:and|or|not|xor)(?!\p{L})|!
92+
captures:
93+
1: punctuation.definition.keyword.powershell
94+
- match: \B(-)(?i:and|or|not|xor)(?!\p{L})|!
8295
scope: keyword.operator.logical.powershell
83-
- match: \B-(?i:band|bor|bnot|bxor|sh[lr])(?!\p{L})
96+
captures:
97+
1: punctuation.definition.keyword.powershell
98+
- match: \B(-)(?i:band|bor|bnot|bxor|sh[lr])(?!\p{L})
8499
scope: keyword.operator.bitwise.powershell
85-
- match: \B-(?i:f)(?!\p{L})
100+
captures:
101+
1: punctuation.definition.keyword.powershell
102+
- match: \B(-)(?i:f)(?!\p{L})
86103
scope: keyword.operator.string-format.powershell
104+
captures:
105+
1: punctuation.definition.keyword.powershell
106+
- match: \B([-/])[\p{L}](?:[\w-]*\w)?
107+
scope: variable.parameter.option.powershell
108+
captures:
109+
1: punctuation.definition.parameter.powershell
87110
- match: '[+/*%-]?='
88111
scope: keyword.operator.assignment.powershell
89112
- match: (?:\+\+|--)(?![ \t]*\d)
90113
scope: keyword.operator.assignment.powershell
114+
- match: '[+-](?=\.?\d)'
115+
scope: keyword.operator.unary.powershell
91116
- match: '[+/*%-]'
92117
scope: keyword.operator.arithmetic.powershell
93118
- match: \|\||&&
@@ -102,7 +127,7 @@ contexts:
102127
scope: keyword.operator.logical.pipe.powershell
103128
- match: '&|\B\.(?= )'
104129
scope: keyword.operator.other.powershell
105-
- match: (?<!\s|^)\.\.(?=\-?\d|\(|\$)
130+
- match: \.\.(?=\-?\d|\(|\$)
106131
# This is very imprecise. Is there a syntax for 'must come after...'?
107132
scope: keyword.operator.range.powershell
108133

@@ -111,17 +136,20 @@ contexts:
111136
- include: comment-line
112137

113138
redirection:
114-
- match: ([2-6])(>&)(1)|([1-6])(>)|([1-6])(>>)
139+
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection
140+
- match: ([2-6*])(>&)(1)
115141
captures:
116142
1: constant.numeric.decimal.file-descriptor.powershell
117143
2: keyword.operator.redirection.powershell
118144
3: constant.numeric.decimal.file-descriptor.powershell
119-
4: constant.numeric.decimal.file-descriptor.powershell
120-
5: keyword.operator.redirection.powershell
121-
6: constant.numeric.decimal.file-descriptor.powershell
122-
7: keyword.operator.redirection.powershell
123-
- match: '>>?|<<?|>\|'
145+
- match: ([1-6*])(>>?)
146+
captures:
147+
1: constant.numeric.decimal.file-descriptor.powershell
148+
2: keyword.operator.redirection.powershell
149+
- match: '>>?'
124150
scope: keyword.operator.redirection.powershell
151+
# - match: <+
152+
# scope: invalid.illegal.powershell
125153

126154
requires-directive:
127155
- match: (?<=#)(?i:requires)\s
@@ -349,35 +377,44 @@ contexts:
349377
- include: interpolated-string-content
350378

351379
numeric-constant:
352-
- match: ((?:[-+]|\b)0(?i:x[0-9a-f_]+)(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
380+
- match: \b((0[xX])[\h_]*\h({{integer_suffix}})?)({{bytes_unit}})?\b
353381
captures:
354382
1: constant.numeric.integer.hexadecimal.powershell
355-
2: keyword.other.powershell
356-
- match: ((?:[-+]|\b)(?:[0-9_]+)?(\.)[0-9_]+(?:(?i:e)[0-9]+)?(?i:[fdm])?)({{bytes_unit}}?)\b
357-
captures:
358-
1: constant.numeric.float.decimal.powershell
359-
2: punctuation.separator.decimal.powershell
360-
3: keyword.other.unit.powershell
361-
- match: ((?:[-+]|\b)0[bB][01_]+(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
383+
2: punctuation.definition.numeric.base.powershell
384+
3: storage.type.numeric.powershell
385+
4: keyword.other.unit.powershell
386+
- match: \b((0[bB])[01_]*[01]({{integer_suffix}})?)({{bytes_unit}})?\b
362387
captures:
363388
1: constant.numeric.integer.binary.powershell
364-
2: keyword.other.powershell
365-
- match: ((?:[-+]|\b)[0-9_]+[eE](?:[0-9_])?+(?i:[fdm])?)({{bytes_unit}}?)\b
389+
2: punctuation.definition.numeric.base.powershell
390+
3: storage.type.numeric.powershell
391+
4: keyword.other.unit.powershell
392+
- match: |-
393+
(?x:
394+
\b(
395+
{{dec_digits}}
396+
(?:
397+
(?:
398+
(?:(\.(?!\.)){{dec_digits}}?){{dec_exponent}}?
399+
|{{dec_exponent}}
400+
)
401+
({{float_suffix}})?
402+
|({{float_suffix}})
403+
)
404+
)
405+
({{bytes_unit}})?
406+
)\b
366407
captures:
367-
1: constant.numeric.integer.powershell
368-
2: keyword.other.powershell
369-
- match: ((?:[-+]|\b)[0-9_]+\.[eE](?:[0-9_])?+(?i:[fdm])?)({{bytes_unit}}?)\b
370-
captures:
371-
1: constant.numeric.integer.powershell
372-
2: keyword.other.powershell
373-
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:[fdm]))({{bytes_unit}}?)\b
374-
captures:
375-
1: constant.numeric.integer.powershell
376-
2: keyword.other.powershell
377-
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
408+
1: constant.numeric.float.decimal.powershell
409+
2: punctuation.separator.decimal.powershell
410+
3: storage.type.numeric.powershell
411+
4: storage.type.numeric.powershell
412+
5: keyword.other.unit.powershell
413+
- match: \b((?:0|[1-9]{{dec_digits}}?)({{integer_suffix}})?)({{bytes_unit}})?\b
378414
captures:
379-
1: constant.numeric.integer.powershell
380-
2: keyword.other.powershell
415+
1: constant.numeric.integer.decimal.powershell
416+
2: storage.type.numeric.powershell
417+
3: keyword.other.unit.powershell
381418

382419
script-block:
383420
- match: \{
@@ -410,7 +447,22 @@ contexts:
410447
1: support.constant.variable.powershell
411448
2: punctuation.definition.variable.powershell
412449
3: variable.other.member.powershell
413-
- match: ((\$)(?i:[$^?_]|Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs|SourceEventArgs|StackTrace|Switch|This))((?:\.(?:\p{L}|\d|_)+)*\b)?\b
450+
- match: |-
451+
(?x:
452+
(
453+
(\$)
454+
(?:
455+
[$^?_]
456+
|(?i:Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach
457+
|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile
458+
|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem
459+
|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs
460+
|SourceEventArgs|StackTrace|Switch|This
461+
)\b
462+
)
463+
)
464+
((?:\.(?:\p{L}|\d|_)+)*\b)?
465+
)
414466
captures:
415467
1: variable.language.powershell
416468
2: punctuation.definition.variable.powershell
@@ -459,7 +511,18 @@ contexts:
459511
scope: support.constant.variable.powershell
460512
captures:
461513
1: punctuation.definition.variable.powershell
462-
- match: (\$)(?i:[$^?_]|Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs|SourceEventArgs|StackTrace|Switch|This)\b
514+
- match: |-
515+
(?x:
516+
(\$)
517+
(?:
518+
[$^?_]
519+
|(?i:Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach
520+
|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile
521+
|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem
522+
|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs
523+
|SourceEventArgs|StackTrace|Switch|This)\b
524+
)
525+
)
463526
scope: variable.language.powershell
464527
captures:
465528
1: punctuation.definition.variable.powershell

Tests/syntax_text_Function.ps1 renamed to Tests/syntax_test_Function.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
2+
23
using module Microsoft.Management
34
# <- keyword.control.using
45
# ^ keyword.other

0 commit comments

Comments
 (0)