powershell.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. { begin: /\$\B/ },
  73. {
  74. className: 'keyword',
  75. begin: /\$this/
  76. },
  77. { begin: /\$[\w\d][\w\d_:]*/ }
  78. ]
  79. };
  80. const LITERAL = {
  81. className: 'literal',
  82. begin: /\$(null|true|false)\b/
  83. };
  84. const QUOTE_STRING = {
  85. className: "string",
  86. variants: [
  87. {
  88. begin: /"/,
  89. end: /"/
  90. },
  91. {
  92. begin: /@"/,
  93. end: /^"@/
  94. }
  95. ],
  96. contains: [
  97. BACKTICK_ESCAPE,
  98. VAR,
  99. {
  100. className: 'variable',
  101. begin: /\$[A-z]/,
  102. end: /[^A-z]/
  103. }
  104. ]
  105. };
  106. const APOS_STRING = {
  107. className: 'string',
  108. variants: [
  109. {
  110. begin: /'/,
  111. end: /'/
  112. },
  113. {
  114. begin: /@'/,
  115. end: /^'@/
  116. }
  117. ]
  118. };
  119. const PS_HELPTAGS = {
  120. className: "doctag",
  121. variants: [
  122. /* no paramater help tags */
  123. { begin: /\.(synopsis|description|example|inputs|outputs|notes|link|component|role|functionality)/ },
  124. /* one parameter help tags */
  125. { begin: /\.(parameter|forwardhelptargetname|forwardhelpcategory|remotehelprunspace|externalhelp)\s+\S+/ }
  126. ]
  127. };
  128. const PS_COMMENT = hljs.inherit(
  129. hljs.COMMENT(null, null),
  130. {
  131. variants: [
  132. /* single-line comment */
  133. {
  134. begin: /#/,
  135. end: /$/
  136. },
  137. /* multi-line comment */
  138. {
  139. begin: /<#/,
  140. end: /#>/
  141. }
  142. ],
  143. contains: [ PS_HELPTAGS ]
  144. }
  145. );
  146. const CMDLETS = {
  147. className: 'built_in',
  148. variants: [ { begin: '('.concat(VALID_VERBS, ')+(-)[\\w\\d]+') } ]
  149. };
  150. const PS_CLASS = {
  151. className: 'class',
  152. beginKeywords: 'class enum',
  153. end: /\s*[{]/,
  154. excludeEnd: true,
  155. relevance: 0,
  156. contains: [ hljs.TITLE_MODE ]
  157. };
  158. const PS_FUNCTION = {
  159. className: 'function',
  160. begin: /function\s+/,
  161. end: /\s*\{|$/,
  162. excludeEnd: true,
  163. returnBegin: true,
  164. relevance: 0,
  165. contains: [
  166. {
  167. begin: "function",
  168. relevance: 0,
  169. className: "keyword"
  170. },
  171. {
  172. className: "title",
  173. begin: TITLE_NAME_RE,
  174. relevance: 0
  175. },
  176. {
  177. begin: /\(/,
  178. end: /\)/,
  179. className: "params",
  180. relevance: 0,
  181. contains: [ VAR ]
  182. }
  183. // CMDLETS
  184. ]
  185. };
  186. // Using statment, plus type, plus assembly name.
  187. const PS_USING = {
  188. begin: /using\s/,
  189. end: /$/,
  190. returnBegin: true,
  191. contains: [
  192. QUOTE_STRING,
  193. APOS_STRING,
  194. {
  195. className: 'keyword',
  196. begin: /(using|assembly|command|module|namespace|type)/
  197. }
  198. ]
  199. };
  200. // Comperison operators & function named parameters.
  201. const PS_ARGUMENTS = { variants: [
  202. // PS literals are pretty verbose so it's a good idea to accent them a bit.
  203. {
  204. className: 'operator',
  205. begin: '('.concat(COMPARISON_OPERATORS, ')\\b')
  206. },
  207. {
  208. className: 'literal',
  209. begin: /(-){1,2}[\w\d-]+/,
  210. relevance: 0
  211. }
  212. ] };
  213. const HASH_SIGNS = {
  214. className: 'selector-tag',
  215. begin: /@\B/,
  216. relevance: 0
  217. };
  218. // It's a very general rule so I'll narrow it a bit with some strict boundaries
  219. // to avoid any possible false-positive collisions!
  220. const PS_METHODS = {
  221. className: 'function',
  222. begin: /\[.*\]\s*[\w]+[ ]??\(/,
  223. end: /$/,
  224. returnBegin: true,
  225. relevance: 0,
  226. contains: [
  227. {
  228. className: 'keyword',
  229. begin: '('.concat(
  230. KEYWORDS.keyword.toString().replace(/\s/g, '|'
  231. ), ')\\b'),
  232. endsParent: true,
  233. relevance: 0
  234. },
  235. hljs.inherit(hljs.TITLE_MODE, { endsParent: true })
  236. ]
  237. };
  238. const GENTLEMANS_SET = [
  239. // STATIC_MEMBER,
  240. PS_METHODS,
  241. PS_COMMENT,
  242. BACKTICK_ESCAPE,
  243. hljs.NUMBER_MODE,
  244. QUOTE_STRING,
  245. APOS_STRING,
  246. // PS_NEW_OBJECT_TYPE,
  247. CMDLETS,
  248. VAR,
  249. LITERAL,
  250. HASH_SIGNS
  251. ];
  252. const PS_TYPE = {
  253. begin: /\[/,
  254. end: /\]/,
  255. excludeBegin: true,
  256. excludeEnd: true,
  257. relevance: 0,
  258. contains: [].concat(
  259. 'self',
  260. GENTLEMANS_SET,
  261. {
  262. begin: "(" + TYPES.join("|") + ")",
  263. className: "built_in",
  264. relevance: 0
  265. },
  266. {
  267. className: 'type',
  268. begin: /[\.\w\d]+/,
  269. relevance: 0
  270. }
  271. )
  272. };
  273. PS_METHODS.contains.unshift(PS_TYPE);
  274. return {
  275. name: 'PowerShell',
  276. aliases: [
  277. "pwsh",
  278. "ps",
  279. "ps1"
  280. ],
  281. case_insensitive: true,
  282. keywords: KEYWORDS,
  283. contains: GENTLEMANS_SET.concat(
  284. PS_CLASS,
  285. PS_FUNCTION,
  286. PS_USING,
  287. PS_ARGUMENTS,
  288. PS_TYPE
  289. )
  290. };
  291. }
  292. module.exports = powershell;