powershell.js 13 KB

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