本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
13KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. 'use strict';
  5. if (typeof exports == 'object' && typeof module == 'object') // CommonJS
  6. mod(require('../../lib/codemirror'));
  7. else if (typeof define == 'function' && define.amd) // AMD
  8. define(['../../lib/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.eol()) {
  201. return 'error';
  202. } else if (stream.peek().match(/[({]/)) {
  203. return 'punctuation';
  204. } else if (stream.peek().match(varNames)) {
  205. // splatted variable
  206. return tokenVariable(stream, state);
  207. }
  208. }
  209. return 'error';
  210. }
  211. function tokenSingleQuoteString(stream, state) {
  212. var ch;
  213. while ((ch = stream.peek()) != null) {
  214. stream.next();
  215. if (ch === "'" && !stream.eat("'")) {
  216. state.tokenize = tokenBase;
  217. return 'string';
  218. }
  219. }
  220. return 'error';
  221. }
  222. function tokenDoubleQuoteString(stream, state) {
  223. var ch;
  224. while ((ch = stream.peek()) != null) {
  225. if (ch === '$') {
  226. state.tokenize = tokenStringInterpolation;
  227. return 'string';
  228. }
  229. stream.next();
  230. if (ch === '`') {
  231. stream.next();
  232. continue;
  233. }
  234. if (ch === '"' && !stream.eat('"')) {
  235. state.tokenize = tokenBase;
  236. return 'string';
  237. }
  238. }
  239. return 'error';
  240. }
  241. function tokenStringInterpolation(stream, state) {
  242. return tokenInterpolation(stream, state, tokenDoubleQuoteString);
  243. }
  244. function tokenMultiStringReturn(stream, state) {
  245. state.tokenize = tokenMultiString;
  246. state.startQuote = '"'
  247. return tokenMultiString(stream, state);
  248. }
  249. function tokenHereStringInterpolation(stream, state) {
  250. return tokenInterpolation(stream, state, tokenMultiStringReturn);
  251. }
  252. function tokenInterpolation(stream, state, parentTokenize) {
  253. if (stream.match('$(')) {
  254. var savedBracketNesting = state.bracketNesting;
  255. state.returnStack.push({
  256. /*jshint loopfunc:true */
  257. shouldReturnFrom: function(state) {
  258. return state.bracketNesting === savedBracketNesting;
  259. },
  260. tokenize: parentTokenize
  261. });
  262. state.tokenize = tokenBase;
  263. state.bracketNesting += 1;
  264. return 'punctuation';
  265. } else {
  266. stream.next();
  267. state.returnStack.push({
  268. shouldReturnFrom: function() { return true; },
  269. tokenize: parentTokenize
  270. });
  271. state.tokenize = tokenVariable;
  272. return state.tokenize(stream, state);
  273. }
  274. }
  275. function tokenComment(stream, state) {
  276. var maybeEnd = false, ch;
  277. while ((ch = stream.next()) != null) {
  278. if (maybeEnd && ch == '>') {
  279. state.tokenize = tokenBase;
  280. break;
  281. }
  282. maybeEnd = (ch === '#');
  283. }
  284. return 'comment';
  285. }
  286. function tokenVariable(stream, state) {
  287. var ch = stream.peek();
  288. if (stream.eat('{')) {
  289. state.tokenize = tokenVariableWithBraces;
  290. return tokenVariableWithBraces(stream, state);
  291. } else if (ch != undefined && ch.match(varNames)) {
  292. stream.eatWhile(varNames);
  293. state.tokenize = tokenBase;
  294. return 'variable-2';
  295. } else {
  296. state.tokenize = tokenBase;
  297. return 'error';
  298. }
  299. }
  300. function tokenVariableWithBraces(stream, state) {
  301. var ch;
  302. while ((ch = stream.next()) != null) {
  303. if (ch === '}') {
  304. state.tokenize = tokenBase;
  305. break;
  306. }
  307. }
  308. return 'variable-2';
  309. }
  310. function tokenMultiString(stream, state) {
  311. var quote = state.startQuote;
  312. if (stream.sol() && stream.match(new RegExp(quote + '@'))) {
  313. state.tokenize = tokenBase;
  314. }
  315. else if (quote === '"') {
  316. while (!stream.eol()) {
  317. var ch = stream.peek();
  318. if (ch === '$') {
  319. state.tokenize = tokenHereStringInterpolation;
  320. return 'string';
  321. }
  322. stream.next();
  323. if (ch === '`') {
  324. stream.next();
  325. }
  326. }
  327. }
  328. else {
  329. stream.skipToEnd();
  330. }
  331. return 'string';
  332. }
  333. var external = {
  334. startState: function() {
  335. return {
  336. returnStack: [],
  337. bracketNesting: 0,
  338. tokenize: tokenBase
  339. };
  340. },
  341. token: function(stream, state) {
  342. return state.tokenize(stream, state);
  343. },
  344. blockCommentStart: '<#',
  345. blockCommentEnd: '#>',
  346. lineComment: '#',
  347. fold: 'brace'
  348. };
  349. return external;
  350. });
  351. CodeMirror.defineMIME('application/x-powershell', 'powershell');
  352. });
上海开阖软件有限公司 沪ICP备12045867号-1