powershell.js 8.0 KB

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