powershell.js 7.9 KB

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