提交 | 用户 | 时间
|
e7c126
|
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others |
H |
2 |
// Distributed under an MIT license: https://codemirror.net/LICENSE |
|
3 |
|
|
4 |
(function(mod) { |
|
5 |
'use strict'; |
|
6 |
if (typeof exports == 'object' && typeof module == 'object') // CommonJS |
|
7 |
mod(require('../../lib/codemirror')); |
|
8 |
else if (typeof define == 'function' && define.amd) // AMD |
|
9 |
define(['../../lib/codemirror'], mod); |
|
10 |
else // Plain browser env |
|
11 |
mod(window.CodeMirror); |
|
12 |
})(function(CodeMirror) { |
|
13 |
'use strict'; |
|
14 |
|
|
15 |
CodeMirror.defineMode('powershell', function() { |
|
16 |
function buildRegexp(patterns, options) { |
|
17 |
options = options || {}; |
|
18 |
var prefix = options.prefix !== undefined ? options.prefix : '^'; |
|
19 |
var suffix = options.suffix !== undefined ? options.suffix : '\\b'; |
|
20 |
|
|
21 |
for (var i = 0; i < patterns.length; i++) { |
|
22 |
if (patterns[i] instanceof RegExp) { |
|
23 |
patterns[i] = patterns[i].source; |
|
24 |
} |
|
25 |
else { |
|
26 |
patterns[i] = patterns[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
return new RegExp(prefix + '(' + patterns.join('|') + ')' + suffix, 'i'); |
|
31 |
} |
|
32 |
|
|
33 |
var notCharacterOrDash = '(?=[^A-Za-z\\d\\-_]|$)'; |
|
34 |
var varNames = /[\w\-:]/ |
|
35 |
var keywords = buildRegexp([ |
|
36 |
/begin|break|catch|continue|data|default|do|dynamicparam/, |
|
37 |
/else|elseif|end|exit|filter|finally|for|foreach|from|function|if|in/, |
|
38 |
/param|process|return|switch|throw|trap|try|until|where|while/ |
|
39 |
], { suffix: notCharacterOrDash }); |
|
40 |
|
|
41 |
var punctuation = /[\[\]{},;`\.]|@[({]/; |
|
42 |
var wordOperators = buildRegexp([ |
|
43 |
'f', |
|
44 |
/b?not/, |
|
45 |
/[ic]?split/, 'join', |
|
46 |
/is(not)?/, 'as', |
|
47 |
/[ic]?(eq|ne|[gl][te])/, |
|
48 |
/[ic]?(not)?(like|match|contains)/, |
|
49 |
/[ic]?replace/, |
|
50 |
/b?(and|or|xor)/ |
|
51 |
], { prefix: '-' }); |
|
52 |
var symbolOperators = /[+\-*\/%]=|\+\+|--|\.\.|[+\-*&^%:=!|\/]|<(?!#)|(?!#)>/; |
|
53 |
var operators = buildRegexp([wordOperators, symbolOperators], { suffix: '' }); |
|
54 |
|
|
55 |
var numbers = /^((0x[\da-f]+)|((\d+\.\d+|\d\.|\.\d+|\d+)(e[\+\-]?\d+)?))[ld]?([kmgtp]b)?/i; |
|
56 |
|
|
57 |
var identifiers = /^[A-Za-z\_][A-Za-z\-\_\d]*\b/; |
|
58 |
|
|
59 |
var symbolBuiltins = /[A-Z]:|%|\?/i; |
|
60 |
var namedBuiltins = buildRegexp([ |
|
61 |
/Add-(Computer|Content|History|Member|PSSnapin|Type)/, |
|
62 |
/Checkpoint-Computer/, |
|
63 |
/Clear-(Content|EventLog|History|Host|Item(Property)?|Variable)/, |
|
64 |
/Compare-Object/, |
|
65 |
/Complete-Transaction/, |
|
66 |
/Connect-PSSession/, |
|
67 |
/ConvertFrom-(Csv|Json|SecureString|StringData)/, |
|
68 |
/Convert-Path/, |
|
69 |
/ConvertTo-(Csv|Html|Json|SecureString|Xml)/, |
|
70 |
/Copy-Item(Property)?/, |
|
71 |
/Debug-Process/, |
|
72 |
/Disable-(ComputerRestore|PSBreakpoint|PSRemoting|PSSessionConfiguration)/, |
|
73 |
/Disconnect-PSSession/, |
|
74 |
/Enable-(ComputerRestore|PSBreakpoint|PSRemoting|PSSessionConfiguration)/, |
|
75 |
/(Enter|Exit)-PSSession/, |
|
76 |
/Export-(Alias|Clixml|Console|Counter|Csv|FormatData|ModuleMember|PSSession)/, |
|
77 |
/ForEach-Object/, |
|
78 |
/Format-(Custom|List|Table|Wide)/, |
|
79 |
new RegExp('Get-(Acl|Alias|AuthenticodeSignature|ChildItem|Command|ComputerRestorePoint|Content|ControlPanelItem|Counter|Credential' |
|
80 |
+ '|Culture|Date|Event|EventLog|EventSubscriber|ExecutionPolicy|FormatData|Help|History|Host|HotFix|Item|ItemProperty|Job' |
|
81 |
+ '|Location|Member|Module|PfxCertificate|Process|PSBreakpoint|PSCallStack|PSDrive|PSProvider|PSSession|PSSessionConfiguration' |
|
82 |
+ '|PSSnapin|Random|Service|TraceSource|Transaction|TypeData|UICulture|Unique|Variable|Verb|WinEvent|WmiObject)'), |
|
83 |
/Group-Object/, |
|
84 |
/Import-(Alias|Clixml|Counter|Csv|LocalizedData|Module|PSSession)/, |
|
85 |
/ImportSystemModules/, |
|
86 |
/Invoke-(Command|Expression|History|Item|RestMethod|WebRequest|WmiMethod)/, |
|
87 |
/Join-Path/, |
|
88 |
/Limit-EventLog/, |
|
89 |
/Measure-(Command|Object)/, |
|
90 |
/Move-Item(Property)?/, |
|
91 |
new RegExp('New-(Alias|Event|EventLog|Item(Property)?|Module|ModuleManifest|Object|PSDrive|PSSession|PSSessionConfigurationFile' |
|
92 |
+ '|PSSessionOption|PSTransportOption|Service|TimeSpan|Variable|WebServiceProxy|WinEvent)'), |
|
93 |
/Out-(Default|File|GridView|Host|Null|Printer|String)/, |
|
94 |
/Pause/, |
|
95 |
/(Pop|Push)-Location/, |
|
96 |
/Read-Host/, |
|
97 |
/Receive-(Job|PSSession)/, |
|
98 |
/Register-(EngineEvent|ObjectEvent|PSSessionConfiguration|WmiEvent)/, |
|
99 |
/Remove-(Computer|Event|EventLog|Item(Property)?|Job|Module|PSBreakpoint|PSDrive|PSSession|PSSnapin|TypeData|Variable|WmiObject)/, |
|
100 |
/Rename-(Computer|Item(Property)?)/, |
|
101 |
/Reset-ComputerMachinePassword/, |
|
102 |
/Resolve-Path/, |
|
103 |
/Restart-(Computer|Service)/, |
|
104 |
/Restore-Computer/, |
|
105 |
/Resume-(Job|Service)/, |
|
106 |
/Save-Help/, |
|
107 |
/Select-(Object|String|Xml)/, |
|
108 |
/Send-MailMessage/, |
|
109 |
new RegExp('Set-(Acl|Alias|AuthenticodeSignature|Content|Date|ExecutionPolicy|Item(Property)?|Location|PSBreakpoint|PSDebug' + |
|
110 |
'|PSSessionConfiguration|Service|StrictMode|TraceSource|Variable|WmiInstance)'), |
|
111 |
/Show-(Command|ControlPanelItem|EventLog)/, |
|
112 |
/Sort-Object/, |
|
113 |
/Split-Path/, |
|
114 |
/Start-(Job|Process|Service|Sleep|Transaction|Transcript)/, |
|
115 |
/Stop-(Computer|Job|Process|Service|Transcript)/, |
|
116 |
/Suspend-(Job|Service)/, |
|
117 |
/TabExpansion2/, |
|
118 |
/Tee-Object/, |
|
119 |
/Test-(ComputerSecureChannel|Connection|ModuleManifest|Path|PSSessionConfigurationFile)/, |
|
120 |
/Trace-Command/, |
|
121 |
/Unblock-File/, |
|
122 |
/Undo-Transaction/, |
|
123 |
/Unregister-(Event|PSSessionConfiguration)/, |
|
124 |
/Update-(FormatData|Help|List|TypeData)/, |
|
125 |
/Use-Transaction/, |
|
126 |
/Wait-(Event|Job|Process)/, |
|
127 |
/Where-Object/, |
|
128 |
/Write-(Debug|Error|EventLog|Host|Output|Progress|Verbose|Warning)/, |
|
129 |
/cd|help|mkdir|more|oss|prompt/, |
|
130 |
/ac|asnp|cat|cd|chdir|clc|clear|clhy|cli|clp|cls|clv|cnsn|compare|copy|cp|cpi|cpp|cvpa|dbp|del|diff|dir|dnsn|ebp/, |
|
131 |
/echo|epal|epcsv|epsn|erase|etsn|exsn|fc|fl|foreach|ft|fw|gal|gbp|gc|gci|gcm|gcs|gdr|ghy|gi|gjb|gl|gm|gmo|gp|gps/, |
|
132 |
/group|gsn|gsnp|gsv|gu|gv|gwmi|h|history|icm|iex|ihy|ii|ipal|ipcsv|ipmo|ipsn|irm|ise|iwmi|iwr|kill|lp|ls|man|md/, |
|
133 |
/measure|mi|mount|move|mp|mv|nal|ndr|ni|nmo|npssc|nsn|nv|ogv|oh|popd|ps|pushd|pwd|r|rbp|rcjb|rcsn|rd|rdr|ren|ri/, |
|
134 |
/rjb|rm|rmdir|rmo|rni|rnp|rp|rsn|rsnp|rujb|rv|rvpa|rwmi|sajb|sal|saps|sasv|sbp|sc|select|set|shcm|si|sl|sleep|sls/, |
|
135 |
/sort|sp|spjb|spps|spsv|start|sujb|sv|swmi|tee|trcm|type|where|wjb|write/ |
|
136 |
], { prefix: '', suffix: '' }); |
|
137 |
var variableBuiltins = buildRegexp([ |
|
138 |
/[$?^_]|Args|ConfirmPreference|ConsoleFileName|DebugPreference|Error|ErrorActionPreference|ErrorView|ExecutionContext/, |
|
139 |
/FormatEnumerationLimit|Home|Host|Input|MaximumAliasCount|MaximumDriveCount|MaximumErrorCount|MaximumFunctionCount/, |
|
140 |
/MaximumHistoryCount|MaximumVariableCount|MyInvocation|NestedPromptLevel|OutputEncoding|Pid|Profile|ProgressPreference/, |
|
141 |
/PSBoundParameters|PSCommandPath|PSCulture|PSDefaultParameterValues|PSEmailServer|PSHome|PSScriptRoot|PSSessionApplicationName/, |
|
142 |
/PSSessionConfigurationName|PSSessionOption|PSUICulture|PSVersionTable|Pwd|ShellId|StackTrace|VerbosePreference/, |
|
143 |
/WarningPreference|WhatIfPreference/, |
|
144 |
|
|
145 |
/Event|EventArgs|EventSubscriber|Sender/, |
|
146 |
/Matches|Ofs|ForEach|LastExitCode|PSCmdlet|PSItem|PSSenderInfo|This/, |
|
147 |
/true|false|null/ |
|
148 |
], { prefix: '\\$', suffix: '' }); |
|
149 |
|
|
150 |
var builtins = buildRegexp([symbolBuiltins, namedBuiltins, variableBuiltins], { suffix: notCharacterOrDash }); |
|
151 |
|
|
152 |
var grammar = { |
|
153 |
keyword: keywords, |
|
154 |
number: numbers, |
|
155 |
operator: operators, |
|
156 |
builtin: builtins, |
|
157 |
punctuation: punctuation, |
|
158 |
identifier: identifiers |
|
159 |
}; |
|
160 |
|
|
161 |
// tokenizers |
|
162 |
function tokenBase(stream, state) { |
|
163 |
// Handle Comments |
|
164 |
//var ch = stream.peek(); |
|
165 |
|
|
166 |
var parent = state.returnStack[state.returnStack.length - 1]; |
|
167 |
if (parent && parent.shouldReturnFrom(state)) { |
|
168 |
state.tokenize = parent.tokenize; |
|
169 |
state.returnStack.pop(); |
|
170 |
return state.tokenize(stream, state); |
|
171 |
} |
|
172 |
|
|
173 |
if (stream.eatSpace()) { |
|
174 |
return null; |
|
175 |
} |
|
176 |
|
|
177 |
if (stream.eat('(')) { |
|
178 |
state.bracketNesting += 1; |
|
179 |
return 'punctuation'; |
|
180 |
} |
|
181 |
|
|
182 |
if (stream.eat(')')) { |
|
183 |
state.bracketNesting -= 1; |
|
184 |
return 'punctuation'; |
|
185 |
} |
|
186 |
|
|
187 |
for (var key in grammar) { |
|
188 |
if (stream.match(grammar[key])) { |
|
189 |
return key; |
|
190 |
} |
|
191 |
} |
|
192 |
|
|
193 |
var ch = stream.next(); |
|
194 |
|
|
195 |
// single-quote string |
|
196 |
if (ch === "'") { |
|
197 |
return tokenSingleQuoteString(stream, state); |
|
198 |
} |
|
199 |
|
|
200 |
if (ch === '$') { |
|
201 |
return tokenVariable(stream, state); |
|
202 |
} |
|
203 |
|
|
204 |
// double-quote string |
|
205 |
if (ch === '"') { |
|
206 |
return tokenDoubleQuoteString(stream, state); |
|
207 |
} |
|
208 |
|
|
209 |
if (ch === '<' && stream.eat('#')) { |
|
210 |
state.tokenize = tokenComment; |
|
211 |
return tokenComment(stream, state); |
|
212 |
} |
|
213 |
|
|
214 |
if (ch === '#') { |
|
215 |
stream.skipToEnd(); |
|
216 |
return 'comment'; |
|
217 |
} |
|
218 |
|
|
219 |
if (ch === '@') { |
|
220 |
var quoteMatch = stream.eat(/["']/); |
|
221 |
if (quoteMatch && stream.eol()) { |
|
222 |
state.tokenize = tokenMultiString; |
|
223 |
state.startQuote = quoteMatch[0]; |
|
224 |
return tokenMultiString(stream, state); |
|
225 |
} else if (stream.eol()) { |
|
226 |
return 'error'; |
|
227 |
} else if (stream.peek().match(/[({]/)) { |
|
228 |
return 'punctuation'; |
|
229 |
} else if (stream.peek().match(varNames)) { |
|
230 |
// splatted variable |
|
231 |
return tokenVariable(stream, state); |
|
232 |
} |
|
233 |
} |
|
234 |
return 'error'; |
|
235 |
} |
|
236 |
|
|
237 |
function tokenSingleQuoteString(stream, state) { |
|
238 |
var ch; |
|
239 |
while ((ch = stream.peek()) != null) { |
|
240 |
stream.next(); |
|
241 |
|
|
242 |
if (ch === "'" && !stream.eat("'")) { |
|
243 |
state.tokenize = tokenBase; |
|
244 |
return 'string'; |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
return 'error'; |
|
249 |
} |
|
250 |
|
|
251 |
function tokenDoubleQuoteString(stream, state) { |
|
252 |
var ch; |
|
253 |
while ((ch = stream.peek()) != null) { |
|
254 |
if (ch === '$') { |
|
255 |
state.tokenize = tokenStringInterpolation; |
|
256 |
return 'string'; |
|
257 |
} |
|
258 |
|
|
259 |
stream.next(); |
|
260 |
if (ch === '`') { |
|
261 |
stream.next(); |
|
262 |
continue; |
|
263 |
} |
|
264 |
|
|
265 |
if (ch === '"' && !stream.eat('"')) { |
|
266 |
state.tokenize = tokenBase; |
|
267 |
return 'string'; |
|
268 |
} |
|
269 |
} |
|
270 |
|
|
271 |
return 'error'; |
|
272 |
} |
|
273 |
|
|
274 |
function tokenStringInterpolation(stream, state) { |
|
275 |
return tokenInterpolation(stream, state, tokenDoubleQuoteString); |
|
276 |
} |
|
277 |
|
|
278 |
function tokenMultiStringReturn(stream, state) { |
|
279 |
state.tokenize = tokenMultiString; |
|
280 |
state.startQuote = '"' |
|
281 |
return tokenMultiString(stream, state); |
|
282 |
} |
|
283 |
|
|
284 |
function tokenHereStringInterpolation(stream, state) { |
|
285 |
return tokenInterpolation(stream, state, tokenMultiStringReturn); |
|
286 |
} |
|
287 |
|
|
288 |
function tokenInterpolation(stream, state, parentTokenize) { |
|
289 |
if (stream.match('$(')) { |
|
290 |
var savedBracketNesting = state.bracketNesting; |
|
291 |
state.returnStack.push({ |
|
292 |
/*jshint loopfunc:true */ |
|
293 |
shouldReturnFrom: function(state) { |
|
294 |
return state.bracketNesting === savedBracketNesting; |
|
295 |
}, |
|
296 |
tokenize: parentTokenize |
|
297 |
}); |
|
298 |
state.tokenize = tokenBase; |
|
299 |
state.bracketNesting += 1; |
|
300 |
return 'punctuation'; |
|
301 |
} else { |
|
302 |
stream.next(); |
|
303 |
state.returnStack.push({ |
|
304 |
shouldReturnFrom: function() { return true; }, |
|
305 |
tokenize: parentTokenize |
|
306 |
}); |
|
307 |
state.tokenize = tokenVariable; |
|
308 |
return state.tokenize(stream, state); |
|
309 |
} |
|
310 |
} |
|
311 |
|
|
312 |
function tokenComment(stream, state) { |
|
313 |
var maybeEnd = false, ch; |
|
314 |
while ((ch = stream.next()) != null) { |
|
315 |
if (maybeEnd && ch == '>') { |
|
316 |
state.tokenize = tokenBase; |
|
317 |
break; |
|
318 |
} |
|
319 |
maybeEnd = (ch === '#'); |
|
320 |
} |
|
321 |
return 'comment'; |
|
322 |
} |
|
323 |
|
|
324 |
function tokenVariable(stream, state) { |
|
325 |
var ch = stream.peek(); |
|
326 |
if (stream.eat('{')) { |
|
327 |
state.tokenize = tokenVariableWithBraces; |
|
328 |
return tokenVariableWithBraces(stream, state); |
|
329 |
} else if (ch != undefined && ch.match(varNames)) { |
|
330 |
stream.eatWhile(varNames); |
|
331 |
state.tokenize = tokenBase; |
|
332 |
return 'variable-2'; |
|
333 |
} else { |
|
334 |
state.tokenize = tokenBase; |
|
335 |
return 'error'; |
|
336 |
} |
|
337 |
} |
|
338 |
|
|
339 |
function tokenVariableWithBraces(stream, state) { |
|
340 |
var ch; |
|
341 |
while ((ch = stream.next()) != null) { |
|
342 |
if (ch === '}') { |
|
343 |
state.tokenize = tokenBase; |
|
344 |
break; |
|
345 |
} |
|
346 |
} |
|
347 |
return 'variable-2'; |
|
348 |
} |
|
349 |
|
|
350 |
function tokenMultiString(stream, state) { |
|
351 |
var quote = state.startQuote; |
|
352 |
if (stream.sol() && stream.match(new RegExp(quote + '@'))) { |
|
353 |
state.tokenize = tokenBase; |
|
354 |
} |
|
355 |
else if (quote === '"') { |
|
356 |
while (!stream.eol()) { |
|
357 |
var ch = stream.peek(); |
|
358 |
if (ch === '$') { |
|
359 |
state.tokenize = tokenHereStringInterpolation; |
|
360 |
return 'string'; |
|
361 |
} |
|
362 |
|
|
363 |
stream.next(); |
|
364 |
if (ch === '`') { |
|
365 |
stream.next(); |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
else { |
|
370 |
stream.skipToEnd(); |
|
371 |
} |
|
372 |
|
|
373 |
return 'string'; |
|
374 |
} |
|
375 |
|
|
376 |
var external = { |
|
377 |
startState: function() { |
|
378 |
return { |
|
379 |
returnStack: [], |
|
380 |
bracketNesting: 0, |
|
381 |
tokenize: tokenBase |
|
382 |
}; |
|
383 |
}, |
|
384 |
|
|
385 |
token: function(stream, state) { |
|
386 |
return state.tokenize(stream, state); |
|
387 |
}, |
|
388 |
|
|
389 |
blockCommentStart: '<#', |
|
390 |
blockCommentEnd: '#>', |
|
391 |
lineComment: '#', |
|
392 |
fold: 'brace' |
|
393 |
}; |
|
394 |
return external; |
|
395 |
}); |
|
396 |
|
|
397 |
CodeMirror.defineMIME('application/x-powershell', 'powershell'); |
|
398 |
}); |