on 67fe804 help sign in
bat/assets/syntaxes/PowerShell.sublime-syntax
451 lines 21 kB history raw
1%YAML 1.2
2---
3# http://www.sublimetext.com/docs/3/syntax.html
4name: PowerShell
5file_extensions:
6 - ps1
7 - psm1
8 - psd1
9scope: source.powershell
10contexts:
11 main:
12 - match: "<#"
13 captures:
14 0: punctuation.definition.comment.block.begin.powershell
15 push:
16 - meta_scope: comment.block.powershell
17 - match: "#>"
18 captures:
19 0: punctuation.definition.comment.block.end.powershell
20 pop: true
21 - include: commentEmbeddedDocs
22 - match: '[2-6]>&1|>>|>|<<|<|>|>\||[1-6]>|[1-6]>>'
23 scope: keyword.operator.redirection.powershell
24 - include: commands
25 - include: commentLine
26 - include: variable
27 - include: interpolatedStringContent
28 - include: function
29 - include: attribute
30 - include: UsingDirective
31 - include: type
32 - include: hashtable
33 - include: doubleQuotedString
34 - include: scriptblock
35 - include: doubleQuotedStringEscapes
36 - match: (?<!')'
37 captures:
38 0: punctuation.definition.string.begin.powershell
39 push:
40 - meta_scope: string.quoted.single.powershell
41 - match: "'(?!')"
42 captures:
43 0: punctuation.definition.string.end.powershell
44 pop: true
45 - match: "''"
46 scope: constant.character.escape.powershell
47 - match: \@"(?=$)
48 push:
49 - meta_scope: string.quoted.double.heredoc.powershell
50 - match: ^"@
51 pop: true
52 - include: variableNoProperty
53 - include: doubleQuotedStringEscapes
54 - include: interpolation
55 - match: \@'(?=$)
56 push:
57 - meta_scope: string.quoted.single.heredoc.powershell
58 - match: ^'@
59 pop: true
60 - match: "''"
61 scope: constant.character.escape.powershell
62 - include: numericConstant
63 - match: (@)(\()
64 captures:
65 1: keyword.other.array.begin.powershell
66 2: punctuation.section.group.begin.powershell
67 push:
68 - meta_scope: meta.group.array-expression.powershell
69 - match: \)
70 captures:
71 0: punctuation.section.group.end.powershell
72 pop: true
73 - include: main
74 - match: (\$)(\()
75 comment: "TODO: move to repo; make recursive."
76 captures:
77 1: punctuation.definition.variable.powershell
78 2: punctuation.section.group.begin.powershell
79 push:
80 - meta_scope: meta.group.complex.subexpression.powershell
81 - match: \)
82 captures:
83 0: punctuation.section.group.end.powershell
84 pop: true
85 - include: main
86 - match: '(\b(([A-Za-z0-9\-_\.]+)\.(?i:exe|com|cmd|bat))\b)'
87 scope: support.function.powershell
88 - match: (?<!\w|-|\.)((?i:begin|break|catch|continue|data|default|define|do|dynamicparam|else|elseif|end|exit|finally|for|from|if|in|inlinescript|parallel|param|process|return|sequence|switch|throw|trap|try|until|var|while)|%|\?)(?!\w)
89 scope: keyword.control.powershell
90 - match: '(?<!\w|-|[^\)]\.)((?i:(foreach|where)(?!-object))|%|\?)(?!\w)'
91 scope: keyword.control.powershell
92 - match: (?<!\w)(--%)(?!\w)
93 comment: This should be moved to the repository at some point.
94 captures:
95 1: keyword.control.powershell
96 push:
97 - match: $
98 pop: true
99 - match: .+
100 scope: string.unquoted.powershell
101 - match: (?<!\w)((?i:hidden|static))(?!\w)
102 comment: This should only be relevant inside a class but will require a rework of how classes are matched. This is a temp fix.
103 scope: storage.modifier.powershell
104 - match: '(?<!\w|-)((?i:class)|%|\?)(?:\s)+((?:\p{L}|\d|_|-|)+)\b'
105 comment: capture should be entity.name.type, but it doesn't provide a good color in the default schema.
106 captures:
107 1: storage.type.powershell
108 2: entity.name.function
109 - match: (?<!\w)-(?i:is(?:not)?|as)\b
110 scope: keyword.operator.comparison.powershell
111 - match: '(?<!\w)-(?i:[ic]?(?:eq|ne|[gl][te]|(?:not)?(?:like|match|contains|in)|replace))(?!\p{L})'
112 scope: keyword.operator.comparison.powershell
113 - match: '(?<!\w)-(?i:join|split)(?!\p{L})|!'
114 scope: keyword.operator.unary.powershell
115 - match: '(?<!\w)-(?i:and|or|not|xor)(?!\p{L})|!'
116 scope: keyword.operator.logical.powershell
117 - match: '(?<!\w)-(?i:band|bor|bnot|bxor|shl|shr)(?!\p{L})'
118 scope: keyword.operator.bitwise.powershell
119 - match: '(?<!\w)-(?i:f)(?!\p{L})'
120 scope: keyword.operator.string-format.powershell
121 - match: "[+%*/-]?=|[+/*%-]"
122 scope: keyword.operator.assignment.powershell
123 - match: '\|{2}|&{2}|;'
124 scope: punctuation.terminator.statement.powershell
125 - match: '&|(?<!\w)\.(?= )|`|,|\|'
126 scope: keyword.operator.other.powershell
127 - match: (?<!\s|^)\.\.(?=\-?\d|\(|\$)
128 comment: This is very imprecise, is there a syntax for 'must come after...'
129 scope: keyword.operator.range.powershell
130 RequiresDirective:
131 - match: (?<=#)(?i:(requires))\s
132 captures:
133 0: keyword.control.requires.powershell
134 push:
135 - meta_scope: meta.requires.powershell
136 - match: $
137 pop: true
138 - match: \-(?i:Modules|PSSnapin|RunAsAdministrator|ShellId|Version)
139 scope: keyword.other.powershell
140 - match: '(?<!-)\b\p{L}+|\d+(?:\.\d+)*'
141 scope: variable.parameter.powershell
142 - include: hashtable
143 UsingDirective:
144 - match: (?<!\w)(?i:(using))\s+(?i:(namespace|module))\s+(?i:((?:\w+(?:\.)?)+))
145 captures:
146 1: keyword.control.using.powershell
147 2: keyword.other.powershell
148 3: variable.parameter.powershell
149 attribute:
150 - match: '(\[)\s*\b(?i)(cmdletbinding|alias|outputtype|parameter|validatenotnull|validatenotnullorempty|validatecount|validateset|allownull|allowemptycollection|allowemptystring|validatescript|validaterange|validatepattern|validatelength|supportswildcards)\b'
151 captures:
152 1: punctuation.section.bracket.begin.powershell
153 2: support.function.attribute.powershell
154 push:
155 - meta_scope: meta.attribute.powershell
156 - match: '(\])'
157 captures:
158 1: punctuation.section.bracket.end.powershell
159 pop: true
160 - match: \(
161 captures:
162 0: punctuation.section.group.begin.powershell
163 push:
164 - match: \)
165 captures:
166 0: punctuation.section.group.end.powershell
167 pop: true
168 - include: main
169 - match: (?i)\b(mandatory|valuefrompipeline|valuefrompipelinebypropertyname|valuefromremainingarguments|position|parametersetname|defaultparametersetname|supportsshouldprocess|supportspaging|positionalbinding|helpuri|confirmimpact|helpmessage)\b(?:\s+)?(=)?
170 captures:
171 1: variable.parameter.attribute.powershell
172 2: keyword.operator.assignment.powershell
173 commands:
174 - match: '(?:(\p{L}|\d|_|-|\\|\:)*\\)?\b(?i:Add|Approve|Assert|Backup|Block|Build|Checkpoint|Clear|Close|Compare|Complete|Compress|Confirm|Connect|Convert|ConvertFrom|ConvertTo|Copy|Debug|Deny|Deploy|Disable|Disconnect|Dismount|Edit|Enable|Enter|Exit|Expand|Export|Find|Format|Get|Grant|Group|Hide|Import|Initialize|Install|Invoke|Join|Limit|Lock|Measure|Merge|Mount|Move|New|Open|Optimize|Out|Ping|Pop|Protect|Publish|Push|Read|Receive|Redo|Register|Remove|Rename|Repair|Request|Reset|Resize|Resolve|Restart|Restore|Resume|Revoke|Save|Search|Select|Send|Set|Show|Skip|Split|Start|Step|Stop|Submit|Suspend|Switch|Sync|Test|Trace|Unblock|Undo|Uninstall|Unlock|Unprotect|Unpublish|Unregister|Update|Use|Wait|Watch|Write)\-.+?(?:\.(?i:exe|cmd|bat|ps1))?\b'
175 comment: "Verb-Noun pattern:"
176 scope: support.function.powershell
177 - match: (?<!\w)(?i:foreach-object)(?!\w)
178 comment: Builtin cmdlets with reserved verbs
179 scope: support.function.powershell
180 - match: (?<!\w)(?i:where-object)(?!\w)
181 comment: Builtin cmdlets with reserved verbs
182 scope: support.function.powershell
183 - match: (?<!\w)(?i:sort-object)(?!\w)
184 comment: Builtin cmdlets with reserved verbs
185 scope: support.function.powershell
186 - match: (?<!\w)(?i:tee-object)(?!\w)
187 comment: Builtin cmdlets with reserved verbs
188 scope: support.function.powershell
189 commentEmbeddedDocs:
190 - match: ^(?i:(?:\s?|#)+(\.)(COMPONENT|DESCRIPTION|EXAMPLE|EXTERNALHELP|FORWARDHELPCATEGORY|FORWARDHELPTARGETNAME|FUNCTIONALITY|INPUTS|LINK|NOTES|OUTPUTS|REMOTEHELPRUNSPACE|ROLE|SYNOPSIS))
191 scope: comment.documentation.embedded.powershell
192 captures:
193 1: constant.string.documentation.powershell
194 2: keyword.operator.documentation.powershell
195 - match: '(?i:\s?(\.)(PARAMETER|FORWARDHELPTARGETNAME|FORWARDHELPCATEGORY|REMOTEHELPRUNSPACE|EXTERNALHELP)\s+([a-z0-9-_]+))'
196 scope: comment.documentation.embedded.powershell
197 captures:
198 1: constant.string.documentation.powershell
199 2: keyword.operator.documentation.powershell
200 3: keyword.operator.documentation.powershell
201 commentLine:
202 - match: '(?<![`\\-])#'
203 captures:
204 0: punctuation.definition.comment.powershell
205 push:
206 - meta_scope: comment.line.powershell
207 - match: $\n?
208 captures:
209 0: punctuation.definition.comment.powershell
210 pop: true
211 - include: commentEmbeddedDocs
212 - include: RequiresDirective
213 doubleQuotedString:
214 - match: (?<!(?<!`)")"
215 captures:
216 0: punctuation.definition.string.begin.powershell
217 push:
218 - meta_scope: string.quoted.double.powershell
219 - match: '"(?!")'
220 captures:
221 0: punctuation.definition.string.end.powershell
222 pop: true
223 - match: '(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,64}\b'
224 - include: variableNoProperty
225 - include: doubleQuotedStringEscapes
226 - include: interpolation
227 - match: '`\s*$'
228 scope: keyword.other.powershell
229 doubleQuotedStringEscapes:
230 - match: '`[`0abefnrtv"''$]'
231 scope: constant.character.escape.powershell
232 - include: unicodeEscape
233 - match: '""'
234 scope: constant.character.escape.powershell
235 function:
236 - match: '^(?:\s*+)(?i)(function|filter|configuration|workflow)\s+(?:(global|local|script|private):)?((?:\p{L}|\d|_|-|\.)+)'
237 captures:
238 0: meta.function.powershell
239 1: storage.type.powershell
240 2: storage.modifier.scope.powershell
241 3: entity.name.function.powershell
242 push:
243 - match: '(?=\{|\()'
244 pop: true
245 - include: commentLine
246 hashtable:
247 - match: '(@)(\{)'
248 captures:
249 1: keyword.other.hashtable.begin.powershell
250 2: punctuation.section.braces.begin.powershell
251 push:
252 - meta_scope: meta.hashtable.powershell
253 - match: '(\})'
254 captures:
255 1: punctuation.section.braces.end.powershell
256 pop: true
257 - match: \b((?:\'|\")?)(\w+)((?:\'|\")?)(?:\s+)?(=)(?:\s+)?
258 scope: meta.hashtable.assignment.powershell
259 captures:
260 1: punctuation.definition.string.begin.powershell
261 2: variable.other.readwrite.powershell
262 3: punctuation.definition.string.end.powershell
263 4: keyword.operator.assignment.powershell
264 - include: scriptblock
265 - include: main
266 interpolatedStringContent:
267 - match: \(
268 captures:
269 0: punctuation.section.group.begin.powershell
270 push:
271 - meta_content_scope: interpolated.simple.source.powershell
272 - match: \)
273 captures:
274 0: punctuation.section.group.end.powershell
275 pop: true
276 - include: main
277 - include: interpolation
278 - include: interpolatedStringContent
279 interpolation:
280 - match: (\$)(\()
281 captures:
282 1: punctuation.definition.variable.powershell
283 2: punctuation.section.group.begin.powershell
284 push:
285 - meta_content_scope: interpolated.complex.source.powershell
286 - match: \)
287 captures:
288 0: punctuation.section.group.end.powershell
289 pop: true
290 - include: main
291 - include: interpolation
292 - include: interpolatedStringContent
293 numericConstant:
294 - match: '(?<!\w)([-+]?0(?:x|X)[0-9a-fA-F_]+(?:U|u|L|l|UL|Ul|uL|ul|LU|Lu|lU|lu)?)((?i:[kmgtp]b)?)\b'
295 captures:
296 1: constant.numeric.hex.powershell
297 2: keyword.other.powershell
298 - match: '(?<!\w)([-+]?(?:[0-9_]+)?\.[0-9_]+(?:(?:e|E)[0-9]+)?(?:F|f|D|d|M|m)?)((?i:[kmgtp]b)?)\b'
299 captures:
300 1: constant.numeric.integer.powershell
301 2: keyword.other.powershell
302 - match: '(?<!\w)([-+]?0(?:b|B)[01_]+(?:U|u|L|l|UL|Ul|uL|ul|LU|Lu|lU|lu)?)((?i:[kmgtp]b)?)\b'
303 captures:
304 1: constant.numeric.octal.powershell
305 2: keyword.other.powershell
306 - match: '(?<!\w)([-+]?[0-9_]+(?:e|E)(?:[0-9_])?+(?:F|f|D|d|M|m)?)((?i:[kmgtp]b)?)\b'
307 captures:
308 1: constant.numeric.integer.powershell
309 2: keyword.other.powershell
310 - match: '(?<!\w)([-+]?[0-9_]+\.(?:e|E)(?:[0-9_])?+(?:F|f|D|d|M|m)?)((?i:[kmgtp]b)?)\b'
311 captures:
312 1: constant.numeric.integer.powershell
313 2: keyword.other.powershell
314 - match: '(?<!\w)([-+]?[0-9_]+[\.]?(?:F|f|D|d|M|m))((?i:[kmgtp]b)?)\b'
315 captures:
316 1: constant.numeric.integer.powershell
317 2: keyword.other.powershell
318 - match: '(?<!\w)([-+]?[0-9_]+[\.]?(?:U|u|L|l|UL|Ul|uL|ul|LU|Lu|lU|lu)?)((?i:[kmgtp]b)?)\b'
319 captures:
320 1: constant.numeric.integer.powershell
321 2: keyword.other.powershell
322 scriptblock:
323 - match: '\{'
324 captures:
325 0: punctuation.section.braces.begin.powershell
326 push:
327 - meta_scope: meta.scriptblock.powershell
328 - match: '\}'
329 captures:
330 0: punctuation.section.braces.end.powershell
331 pop: true
332 - include: main
333 type:
334 - match: '\['
335 captures:
336 0: punctuation.section.bracket.begin.powershell
337 push:
338 - match: '\]'
339 captures:
340 0: punctuation.section.bracket.end.powershell
341 pop: true
342 - match: '(?!\d+|\.)(?:\p{L}|\p{N}|\.)+'
343 scope: storage.type.powershell
344 - include: main
345 unicodeEscape:
346 - match: '`u\{(?:(?:10)?([0-9a-fA-F]){1,4}|0?\g<1>{1,5})}'
347 scope: constant.character.escape.powershell
348 - match: '`u(?:\{[0-9a-fA-F]{,6}.)?'
349 scope: invalid.character.escape.powershell
350 variable:
351 - match: (\$)(?i:(False|Null|True))\b
352 comment: These are special constants.
353 captures:
354 0: constant.language.powershell
355 1: punctuation.definition.variable.powershell
356 - match: '(\$)(?i:(Error|ExecutionContext|Host|Home|PID|PsHome|PsVersionTable|ShellID))((?:\.(?:\p{L}|\d|_)+)*\b)?\b'
357 comment: These are the other built-in constants.
358 captures:
359 0: support.constant.variable.powershell
360 1: punctuation.definition.variable.powershell
361 3: variable.other.member.powershell
362 - 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)((?:\.(?:\p{L}|\d|_)+)*\b)?'
363 comment: Automatic variables are not constants, but they are read-only. In monokai (default) color schema support.variable doesn't have color, so we use constant.
364 captures:
365 0: support.variable.automatic.powershell
366 1: punctuation.definition.variable.powershell
367 3: variable.other.member.powershell
368 - match: '(\$)(?i:(ConfirmPreference|DebugPreference|ErrorActionPreference|ErrorView|FormatEnumerationLimit|InformationPreference|LogCommandHealthEvent|LogCommandLifecycleEvent|LogEngineHealthEvent|LogEngineLifecycleEvent|LogProviderHealthEvent|LogProviderLifecycleEvent|MaximumAliasCount|MaximumDriveCount|MaximumErrorCount|MaximumFunctionCount|MaximumHistoryCount|MaximumVariableCount|OFS|OutputEncoding|PSCulture|PSDebugContext|PSDefaultParameterValues|PSEmailServer|PSItem|PSModuleAutoLoadingPreference|PSModuleAutoloadingPreference|PSSenderInfo|PSSessionApplicationName|PSSessionConfigurationName|PSSessionOption|ProgressPreference|VerbosePreference|WarningPreference|WhatIfPreference))((?:\.(?:\p{L}|\d|_)+)*\b)?\b'
369 comment: Style preference variables as language variables so that they stand out.
370 captures:
371 0: variable.language.powershell
372 1: punctuation.definition.variable.powershell
373 3: variable.other.member.powershell
374 - match: '(?i:(\$|@)(global|local|private|script|using|workflow):((?:\p{L}|\d|_)+))((?:\.(?:\p{L}|\d|_)+)*\b)?'
375 captures:
376 0: variable.other.readwrite.powershell
377 1: punctuation.definition.variable.powershell
378 2: storage.modifier.scope.powershell
379 4: variable.other.member.powershell
380 - match: '(?i:(\$)(\{)(global|local|private|script|using|workflow):([^}]*[^}`])(\}))((?:\.(?:\p{L}|\d|_)+)*\b)?'
381 captures:
382 0: variable.other.readwrite.powershell
383 1: punctuation.definition.variable.powershell
384 2: punctuation.section.braces.begin.powershell
385 3: storage.modifier.scope.powershell
386 5: punctuation.section.braces.end.powershell
387 6: variable.other.member.powershell
388 - match: '(?i:(\$|@)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))((?:\.(?:\p{L}|\d|_)+)*\b)?'
389 captures:
390 0: variable.other.readwrite.powershell
391 1: punctuation.definition.variable.powershell
392 2: support.variable.drive.powershell
393 4: variable.other.member.powershell
394 - match: '(?i:(\$)(\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))((?:\.(?:\p{L}|\d|_)+)*\b)?'
395 captures:
396 0: variable.other.readwrite.powershell
397 1: punctuation.definition.variable.powershell
398 2: punctuation.section.braces.begin.powershell
399 3: support.variable.drive.powershell
400 5: punctuation.section.braces.end.powershell
401 6: variable.other.member.powershell
402 variableNoProperty:
403 - match: (\$)(?i:(False|Null|True))\b
404 comment: These are special constants.
405 captures:
406 0: constant.language.powershell
407 1: punctuation.definition.variable.powershell
408 - match: (\$)(?i:(Error|ExecutionContext|Host|Home|PID|PsHome|PsVersionTable|ShellID))\b
409 comment: These are the other built-in constants.
410 captures:
411 0: support.constant.variable.powershell
412 1: punctuation.definition.variable.powershell
413 3: variable.other.member.powershell
414 - 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)'
415 comment: Automatic variables are not constants, but they are read-only...
416 captures:
417 0: support.variable.automatic.powershell
418 1: punctuation.definition.variable.powershell
419 3: variable.other.member.powershell
420 - match: (\$)(?i:(ConfirmPreference|DebugPreference|ErrorActionPreference|ErrorView|FormatEnumerationLimit|InformationPreference|LogCommandHealthEvent|LogCommandLifecycleEvent|LogEngineHealthEvent|LogEngineLifecycleEvent|LogProviderHealthEvent|LogProviderLifecycleEvent|MaximumAliasCount|MaximumDriveCount|MaximumErrorCount|MaximumFunctionCount|MaximumHistoryCount|MaximumVariableCount|OFS|OutputEncoding|PSCulture|PSDebugContext|PSDefaultParameterValues|PSEmailServer|PSItem|PSModuleAutoLoadingPreference|PSModuleAutoloadingPreference|PSSenderInfo|PSSessionApplicationName|PSSessionConfigurationName|PSSessionOption|ProgressPreference|VerbosePreference|WarningPreference|WhatIfPreference))\b
421 comment: Style preference variables as language variables so that they stand out.
422 captures:
423 0: variable.language.powershell
424 1: punctuation.definition.variable.powershell
425 3: variable.other.member.powershell
426 - match: '(?i:(\$)(global|local|private|script|using|workflow):((?:\p{L}|\d|_)+))'
427 captures:
428 0: variable.other.readwrite.powershell
429 1: punctuation.definition.variable.powershell
430 2: storage.modifier.scope.powershell
431 4: variable.other.member.powershell
432 - match: '(?i:(\$)(\{)(global|local|private|script|using|workflow):([^}]*[^}`])(\}))'
433 captures:
434 0: variable.other.readwrite.powershell
435 1: punctuation.definition.variable.powershell
436 2: storage.modifier.scope.powershell
437 4: keyword.other.powershell
438 5: variable.other.member.powershell
439 - match: '(?i:(\$)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))'
440 captures:
441 0: variable.other.readwrite.powershell
442 1: punctuation.definition.variable.powershell
443 2: support.variable.drive.powershell
444 4: variable.other.member.powershell
445 - match: '(?i:(\$)(\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))'
446 captures:
447 0: variable.other.readwrite.powershell
448 1: punctuation.definition.variable.powershell
449 2: punctuation.section.braces.begin
450 3: support.variable.drive.powershell
451 5: punctuation.section.braces.end