1 | /*
|
---|
2 | Language: PowerShell
|
---|
3 | Description: PowerShell is a task-based command-line shell and scripting language built on .NET.
|
---|
4 | Author: David Mohundro <david@mohundro.com>
|
---|
5 | Contributors: Nicholas Blumhardt <nblumhardt@nblumhardt.com>, Victor Zhou <OiCMudkips@users.noreply.github.com>, Nicolas Le Gall <contact@nlegall.fr>
|
---|
6 | Website: https://docs.microsoft.com/en-us/powershell/
|
---|
7 | */
|
---|
8 |
|
---|
9 | function powershell(hljs) {
|
---|
10 | const TYPES = [
|
---|
11 | "string",
|
---|
12 | "char",
|
---|
13 | "byte",
|
---|
14 | "int",
|
---|
15 | "long",
|
---|
16 | "bool",
|
---|
17 | "decimal",
|
---|
18 | "single",
|
---|
19 | "double",
|
---|
20 | "DateTime",
|
---|
21 | "xml",
|
---|
22 | "array",
|
---|
23 | "hashtable",
|
---|
24 | "void"
|
---|
25 | ];
|
---|
26 |
|
---|
27 | // https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands
|
---|
28 | const VALID_VERBS =
|
---|
29 | 'Add|Clear|Close|Copy|Enter|Exit|Find|Format|Get|Hide|Join|Lock|' +
|
---|
30 | 'Move|New|Open|Optimize|Pop|Push|Redo|Remove|Rename|Reset|Resize|' +
|
---|
31 | 'Search|Select|Set|Show|Skip|Split|Step|Switch|Undo|Unlock|' +
|
---|
32 | 'Watch|Backup|Checkpoint|Compare|Compress|Convert|ConvertFrom|' +
|
---|
33 | 'ConvertTo|Dismount|Edit|Expand|Export|Group|Import|Initialize|' +
|
---|
34 | 'Limit|Merge|Mount|Out|Publish|Restore|Save|Sync|Unpublish|Update|' +
|
---|
35 | 'Approve|Assert|Build|Complete|Confirm|Deny|Deploy|Disable|Enable|Install|Invoke|' +
|
---|
36 | 'Register|Request|Restart|Resume|Start|Stop|Submit|Suspend|Uninstall|' +
|
---|
37 | 'Unregister|Wait|Debug|Measure|Ping|Repair|Resolve|Test|Trace|Connect|' +
|
---|
38 | 'Disconnect|Read|Receive|Send|Write|Block|Grant|Protect|Revoke|Unblock|' +
|
---|
39 | 'Unprotect|Use|ForEach|Sort|Tee|Where';
|
---|
40 |
|
---|
41 | const COMPARISON_OPERATORS =
|
---|
42 | '-and|-as|-band|-bnot|-bor|-bxor|-casesensitive|-ccontains|-ceq|-cge|-cgt|' +
|
---|
43 | '-cle|-clike|-clt|-cmatch|-cne|-cnotcontains|-cnotlike|-cnotmatch|-contains|' +
|
---|
44 | '-creplace|-csplit|-eq|-exact|-f|-file|-ge|-gt|-icontains|-ieq|-ige|-igt|' +
|
---|
45 | '-ile|-ilike|-ilt|-imatch|-in|-ine|-inotcontains|-inotlike|-inotmatch|' +
|
---|
46 | '-ireplace|-is|-isnot|-isplit|-join|-le|-like|-lt|-match|-ne|-not|' +
|
---|
47 | '-notcontains|-notin|-notlike|-notmatch|-or|-regex|-replace|-shl|-shr|' +
|
---|
48 | '-split|-wildcard|-xor';
|
---|
49 |
|
---|
50 | const KEYWORDS = {
|
---|
51 | $pattern: /-?[A-z\.\-]+\b/,
|
---|
52 | keyword:
|
---|
53 | 'if else foreach return do while until elseif begin for trap data dynamicparam ' +
|
---|
54 | 'end break throw param continue finally in switch exit filter try process catch ' +
|
---|
55 | 'hidden static parameter',
|
---|
56 | // "echo" relevance has been set to 0 to avoid auto-detect conflicts with shell transcripts
|
---|
57 | built_in:
|
---|
58 | 'ac asnp cat cd CFS chdir clc clear clhy cli clp cls clv cnsn compare copy cp ' +
|
---|
59 | 'cpi cpp curl cvpa dbp del diff dir dnsn ebp echo|0 epal epcsv epsn erase etsn exsn fc fhx ' +
|
---|
60 | 'fl ft fw gal gbp gc gcb gci gcm gcs gdr gerr ghy gi gin gjb gl gm gmo gp gps gpv group ' +
|
---|
61 | 'gsn gsnp gsv gtz gu gv gwmi h history icm iex ihy ii ipal ipcsv ipmo ipsn irm ise iwmi ' +
|
---|
62 | 'iwr kill lp ls man md measure mi mount move mp mv nal ndr ni nmo npssc nsn nv ogv oh ' +
|
---|
63 | 'popd ps pushd pwd r rbp rcjb rcsn rd rdr ren ri rjb rm rmdir rmo rni rnp rp rsn rsnp ' +
|
---|
64 | 'rujb rv rvpa rwmi sajb sal saps sasv sbp sc scb select set shcm si sl sleep sls sort sp ' +
|
---|
65 | 'spjb spps spsv start stz sujb sv swmi tee trcm type wget where wjb write'
|
---|
66 | // TODO: 'validate[A-Z]+' can't work in keywords
|
---|
67 | };
|
---|
68 |
|
---|
69 | const TITLE_NAME_RE = /\w[\w\d]*((-)[\w\d]+)*/;
|
---|
70 |
|
---|
71 | const BACKTICK_ESCAPE = {
|
---|
72 | begin: '`[\\s\\S]',
|
---|
73 | relevance: 0
|
---|
74 | };
|
---|
75 |
|
---|
76 | const VAR = {
|
---|
77 | className: 'variable',
|
---|
78 | variants: [
|
---|
79 | {
|
---|
80 | begin: /\$\B/
|
---|
81 | },
|
---|
82 | {
|
---|
83 | className: 'keyword',
|
---|
84 | begin: /\$this/
|
---|
85 | },
|
---|
86 | {
|
---|
87 | begin: /\$[\w\d][\w\d_:]*/
|
---|
88 | }
|
---|
89 | ]
|
---|
90 | };
|
---|
91 |
|
---|
92 | const LITERAL = {
|
---|
93 | className: 'literal',
|
---|
94 | begin: /\$(null|true|false)\b/
|
---|
95 | };
|
---|
96 |
|
---|
97 | const QUOTE_STRING = {
|
---|
98 | className: "string",
|
---|
99 | variants: [
|
---|
100 | {
|
---|
101 | begin: /"/,
|
---|
102 | end: /"/
|
---|
103 | },
|
---|
104 | {
|
---|
105 | begin: /@"/,
|
---|
106 | end: /^"@/
|
---|
107 | }
|
---|
108 | ],
|
---|
109 | contains: [
|
---|
110 | BACKTICK_ESCAPE,
|
---|
111 | VAR,
|
---|
112 | {
|
---|
113 | className: 'variable',
|
---|
114 | begin: /\$[A-z]/,
|
---|
115 | end: /[^A-z]/
|
---|
116 | }
|
---|
117 | ]
|
---|
118 | };
|
---|
119 |
|
---|
120 | const APOS_STRING = {
|
---|
121 | className: 'string',
|
---|
122 | variants: [
|
---|
123 | {
|
---|
124 | begin: /'/,
|
---|
125 | end: /'/
|
---|
126 | },
|
---|
127 | {
|
---|
128 | begin: /@'/,
|
---|
129 | end: /^'@/
|
---|
130 | }
|
---|
131 | ]
|
---|
132 | };
|
---|
133 |
|
---|
134 | const PS_HELPTAGS = {
|
---|
135 | className: "doctag",
|
---|
136 | variants: [
|
---|
137 | /* no paramater help tags */
|
---|
138 | {
|
---|
139 | begin: /\.(synopsis|description|example|inputs|outputs|notes|link|component|role|functionality)/
|
---|
140 | },
|
---|
141 | /* one parameter help tags */
|
---|
142 | {
|
---|
143 | begin: /\.(parameter|forwardhelptargetname|forwardhelpcategory|remotehelprunspace|externalhelp)\s+\S+/
|
---|
144 | }
|
---|
145 | ]
|
---|
146 | };
|
---|
147 |
|
---|
148 | const PS_COMMENT = hljs.inherit(
|
---|
149 | hljs.COMMENT(null, null),
|
---|
150 | {
|
---|
151 | variants: [
|
---|
152 | /* single-line comment */
|
---|
153 | {
|
---|
154 | begin: /#/,
|
---|
155 | end: /$/
|
---|
156 | },
|
---|
157 | /* multi-line comment */
|
---|
158 | {
|
---|
159 | begin: /<#/,
|
---|
160 | end: /#>/
|
---|
161 | }
|
---|
162 | ],
|
---|
163 | contains: [ PS_HELPTAGS ]
|
---|
164 | }
|
---|
165 | );
|
---|
166 |
|
---|
167 | const CMDLETS = {
|
---|
168 | className: 'built_in',
|
---|
169 | variants: [
|
---|
170 | {
|
---|
171 | begin: '('.concat(VALID_VERBS, ')+(-)[\\w\\d]+')
|
---|
172 | }
|
---|
173 | ]
|
---|
174 | };
|
---|
175 |
|
---|
176 | const PS_CLASS = {
|
---|
177 | className: 'class',
|
---|
178 | beginKeywords: 'class enum',
|
---|
179 | end: /\s*[{]/,
|
---|
180 | excludeEnd: true,
|
---|
181 | relevance: 0,
|
---|
182 | contains: [ hljs.TITLE_MODE ]
|
---|
183 | };
|
---|
184 |
|
---|
185 | const PS_FUNCTION = {
|
---|
186 | className: 'function',
|
---|
187 | begin: /function\s+/,
|
---|
188 | end: /\s*\{|$/,
|
---|
189 | excludeEnd: true,
|
---|
190 | returnBegin: true,
|
---|
191 | relevance: 0,
|
---|
192 | contains: [
|
---|
193 | {
|
---|
194 | begin: "function",
|
---|
195 | relevance: 0,
|
---|
196 | className: "keyword"
|
---|
197 | },
|
---|
198 | {
|
---|
199 | className: "title",
|
---|
200 | begin: TITLE_NAME_RE,
|
---|
201 | relevance: 0
|
---|
202 | },
|
---|
203 | {
|
---|
204 | begin: /\(/,
|
---|
205 | end: /\)/,
|
---|
206 | className: "params",
|
---|
207 | relevance: 0,
|
---|
208 | contains: [ VAR ]
|
---|
209 | }
|
---|
210 | // CMDLETS
|
---|
211 | ]
|
---|
212 | };
|
---|
213 |
|
---|
214 | // Using statment, plus type, plus assembly name.
|
---|
215 | const PS_USING = {
|
---|
216 | begin: /using\s/,
|
---|
217 | end: /$/,
|
---|
218 | returnBegin: true,
|
---|
219 | contains: [
|
---|
220 | QUOTE_STRING,
|
---|
221 | APOS_STRING,
|
---|
222 | {
|
---|
223 | className: 'keyword',
|
---|
224 | begin: /(using|assembly|command|module|namespace|type)/
|
---|
225 | }
|
---|
226 | ]
|
---|
227 | };
|
---|
228 |
|
---|
229 | // Comperison operators & function named parameters.
|
---|
230 | const PS_ARGUMENTS = {
|
---|
231 | variants: [
|
---|
232 | // PS literals are pretty verbose so it's a good idea to accent them a bit.
|
---|
233 | {
|
---|
234 | className: 'operator',
|
---|
235 | begin: '('.concat(COMPARISON_OPERATORS, ')\\b')
|
---|
236 | },
|
---|
237 | {
|
---|
238 | className: 'literal',
|
---|
239 | begin: /(-)[\w\d]+/,
|
---|
240 | relevance: 0
|
---|
241 | }
|
---|
242 | ]
|
---|
243 | };
|
---|
244 |
|
---|
245 | const HASH_SIGNS = {
|
---|
246 | className: 'selector-tag',
|
---|
247 | begin: /@\B/,
|
---|
248 | relevance: 0
|
---|
249 | };
|
---|
250 |
|
---|
251 | // It's a very general rule so I'll narrow it a bit with some strict boundaries
|
---|
252 | // to avoid any possible false-positive collisions!
|
---|
253 | const PS_METHODS = {
|
---|
254 | className: 'function',
|
---|
255 | begin: /\[.*\]\s*[\w]+[ ]??\(/,
|
---|
256 | end: /$/,
|
---|
257 | returnBegin: true,
|
---|
258 | relevance: 0,
|
---|
259 | contains: [
|
---|
260 | {
|
---|
261 | className: 'keyword',
|
---|
262 | begin: '('.concat(
|
---|
263 | KEYWORDS.keyword.toString().replace(/\s/g, '|'
|
---|
264 | ), ')\\b'),
|
---|
265 | endsParent: true,
|
---|
266 | relevance: 0
|
---|
267 | },
|
---|
268 | hljs.inherit(hljs.TITLE_MODE, {
|
---|
269 | endsParent: true
|
---|
270 | })
|
---|
271 | ]
|
---|
272 | };
|
---|
273 |
|
---|
274 | const GENTLEMANS_SET = [
|
---|
275 | // STATIC_MEMBER,
|
---|
276 | PS_METHODS,
|
---|
277 | PS_COMMENT,
|
---|
278 | BACKTICK_ESCAPE,
|
---|
279 | hljs.NUMBER_MODE,
|
---|
280 | QUOTE_STRING,
|
---|
281 | APOS_STRING,
|
---|
282 | // PS_NEW_OBJECT_TYPE,
|
---|
283 | CMDLETS,
|
---|
284 | VAR,
|
---|
285 | LITERAL,
|
---|
286 | HASH_SIGNS
|
---|
287 | ];
|
---|
288 |
|
---|
289 | const PS_TYPE = {
|
---|
290 | begin: /\[/,
|
---|
291 | end: /\]/,
|
---|
292 | excludeBegin: true,
|
---|
293 | excludeEnd: true,
|
---|
294 | relevance: 0,
|
---|
295 | contains: [].concat(
|
---|
296 | 'self',
|
---|
297 | GENTLEMANS_SET,
|
---|
298 | {
|
---|
299 | begin: "(" + TYPES.join("|") + ")",
|
---|
300 | className: "built_in",
|
---|
301 | relevance: 0
|
---|
302 | },
|
---|
303 | {
|
---|
304 | className: 'type',
|
---|
305 | begin: /[\.\w\d]+/,
|
---|
306 | relevance: 0
|
---|
307 | }
|
---|
308 | )
|
---|
309 | };
|
---|
310 |
|
---|
311 | PS_METHODS.contains.unshift(PS_TYPE);
|
---|
312 |
|
---|
313 | return {
|
---|
314 | name: 'PowerShell',
|
---|
315 | aliases: [
|
---|
316 | "ps",
|
---|
317 | "ps1"
|
---|
318 | ],
|
---|
319 | case_insensitive: true,
|
---|
320 | keywords: KEYWORDS,
|
---|
321 | contains: GENTLEMANS_SET.concat(
|
---|
322 | PS_CLASS,
|
---|
323 | PS_FUNCTION,
|
---|
324 | PS_USING,
|
---|
325 | PS_ARGUMENTS,
|
---|
326 | PS_TYPE
|
---|
327 | )
|
---|
328 | };
|
---|
329 | }
|
---|
330 |
|
---|
331 | module.exports = powershell;
|
---|