powershell.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. module.exports = function(hljs){
  2. var TYPES =
  3. ["string", "char", "byte", "int", "long", "bool", "decimal", "single",
  4. "double", "DateTime", "xml", "array", "hashtable", "void"];
  5. // https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx
  6. var VALID_VERBS =
  7. 'Add|Clear|Close|Copy|Enter|Exit|Find|Format|Get|Hide|Join|Lock|' +
  8. 'Move|New|Open|Optimize|Pop|Push|Redo|Remove|Rename|Reset|Resize|' +
  9. 'Search|Select|Set|Show|Skip|Split|Step|Switch|Undo|Unlock|' +
  10. 'Watch|Backup|Checkpoint|Compare|Compress|Convert|ConvertFrom|' +
  11. 'ConvertTo|Dismount|Edit|Expand|Export|Group|Import|Initialize|' +
  12. 'Limit|Merge|New|Out|Publish|Restore|Save|Sync|Unpublish|Update|' +
  13. 'Approve|Assert|Complete|Confirm|Deny|Disable|Enable|Install|Invoke|Register|' +
  14. 'Request|Restart|Resume|Start|Stop|Submit|Suspend|Uninstall|' +
  15. 'Unregister|Wait|Debug|Measure|Ping|Repair|Resolve|Test|Trace|Connect|' +
  16. 'Disconnect|Read|Receive|Send|Write|Block|Grant|Protect|Revoke|Unblock|' +
  17. 'Unprotect|Use|ForEach|Sort|Tee|Where';
  18. var COMPARISON_OPERATORS =
  19. '-and|-as|-band|-bnot|-bor|-bxor|-casesensitive|-ccontains|-ceq|-cge|-cgt|' +
  20. '-cle|-clike|-clt|-cmatch|-cne|-cnotcontains|-cnotlike|-cnotmatch|-contains|' +
  21. '-creplace|-csplit|-eq|-exact|-f|-file|-ge|-gt|-icontains|-ieq|-ige|-igt|' +
  22. '-ile|-ilike|-ilt|-imatch|-in|-ine|-inotcontains|-inotlike|-inotmatch|' +
  23. '-ireplace|-is|-isnot|-isplit|-join|-le|-like|-lt|-match|-ne|-not|' +
  24. '-notcontains|-notin|-notlike|-notmatch|-or|-regex|-replace|-shl|-shr|' +
  25. '-split|-wildcard|-xor';
  26. var KEYWORDS = {
  27. keyword: 'if else foreach return do while until elseif begin for trap data dynamicparam ' +
  28. 'end break throw param continue finally in switch exit filter try process catch ' +
  29. 'hidden static parameter'
  30. // TODO: 'validate[A-Z]+' can't work in keywords
  31. };
  32. var TITLE_NAME_RE = /\w[\w\d]*((-)[\w\d]+)*/;
  33. var BACKTICK_ESCAPE = {
  34. begin: '`[\\s\\S]',
  35. relevance: 0
  36. };
  37. var VAR = {
  38. className: 'variable',
  39. variants: [
  40. { begin: /\$\B/ },
  41. { className: 'keyword', begin: /\$this/ },
  42. { begin: /\$[\w\d][\w\d_:]*/ }
  43. ]
  44. };
  45. var LITERAL = {
  46. className: 'literal',
  47. begin: /\$(null|true|false)\b/
  48. };
  49. var QUOTE_STRING = {
  50. className: "string",
  51. variants: [{ begin: /"/, end: /"/ }, { begin: /@"/, end: /^"@/ }],
  52. contains: [
  53. BACKTICK_ESCAPE,
  54. VAR,
  55. {
  56. className: 'variable',
  57. begin: /\$[A-z]/, end: /[^A-z]/
  58. }
  59. ]
  60. };
  61. var APOS_STRING = {
  62. className: 'string',
  63. variants: [
  64. { begin: /'/, end: /'/ },
  65. { begin: /@'/, end: /^'@/ }
  66. ]
  67. };
  68. var PS_HELPTAGS = {
  69. className: "doctag",
  70. variants: [
  71. /* no paramater help tags */
  72. {
  73. begin: /\.(synopsis|description|example|inputs|outputs|notes|link|component|role|functionality)/
  74. },
  75. /* one parameter help tags */
  76. { begin: /\.(parameter|forwardhelptargetname|forwardhelpcategory|remotehelprunspace|externalhelp)\s+\S+/ }
  77. ]
  78. };
  79. var PS_COMMENT = hljs.inherit(
  80. hljs.COMMENT(null, null),
  81. {
  82. variants: [
  83. /* single-line comment */
  84. { begin: /#/, end: /$/ },
  85. /* multi-line comment */
  86. { begin: /<#/, end: /#>/ }
  87. ],
  88. contains: [PS_HELPTAGS]
  89. }
  90. );
  91. var CMDLETS = {
  92. className: 'built_in',
  93. variants: [
  94. { begin: '('.concat(VALID_VERBS, ')+(-)[\\w\\d]+') }
  95. ]
  96. };
  97. var PS_CLASS = {
  98. className: 'class',
  99. beginKeywords: 'class enum', end: /\s*[{]/, excludeEnd: true,
  100. relevance: 0,
  101. contains: [hljs.TITLE_MODE]
  102. };
  103. var PS_FUNCTION = {
  104. className: 'function',
  105. begin: /function\s+/, end: /\s*\{|$/,
  106. excludeEnd: true,
  107. returnBegin: true,
  108. relevance: 0,
  109. contains: [
  110. { begin: "function", relevance: 0, className: "keyword" },
  111. { className: "title",
  112. begin: TITLE_NAME_RE, relevance:0 },
  113. { begin: /\(/, end: /\)/, className: "params",
  114. relevance: 0,
  115. contains: [VAR] }
  116. // CMDLETS
  117. ]
  118. };
  119. // Using statment, plus type, plus assembly name.
  120. var PS_USING = {
  121. begin: /using\s/, end: /$/,
  122. returnBegin: true,
  123. contains: [
  124. QUOTE_STRING,
  125. APOS_STRING,
  126. { className: 'keyword', begin: /(using|assembly|command|module|namespace|type)/ }
  127. ]
  128. };
  129. // Comperison operators & function named parameters.
  130. var PS_ARGUMENTS = {
  131. variants: [
  132. // PS literals are pretty verbose so it's a good idea to accent them a bit.
  133. { className: 'operator', begin: '('.concat(COMPARISON_OPERATORS, ')\\b') },
  134. { className: 'literal', begin: /(-)[\w\d]+/, relevance:0 }
  135. ]
  136. };
  137. var STATIC_MEMBER = {
  138. className: 'selector-tag',
  139. begin: /::\w+\b/, end: /$/,
  140. returnBegin: true,
  141. contains: [
  142. { className: 'attribute', begin: /\w+/, endsParent: true }
  143. ]
  144. };
  145. var HASH_SIGNS = {
  146. className: 'selector-tag',
  147. begin: /\@\B/,
  148. relevance: 0
  149. };
  150. var PS_NEW_OBJECT_TYPE = {
  151. className: 'built_in',
  152. begin: /New-Object\s+\w/, end: /$/,
  153. returnBegin: true,
  154. contains: [
  155. { begin: /New-Object\s+/, relevance: 0 },
  156. { className: 'meta', begin: /([\w\.])+/, endsParent: true }
  157. ]
  158. };
  159. // It's a very general rule so I'll narrow it a bit with some strict boundaries
  160. // to avoid any possible false-positive collisions!
  161. var PS_METHODS = {
  162. className: 'function',
  163. begin: /\[.*\]\s*[\w]+[ ]??\(/, end: /$/,
  164. returnBegin: true,
  165. relevance: 0,
  166. contains: [
  167. {
  168. className: 'keyword', begin: '('.concat(
  169. KEYWORDS.keyword.toString().replace(/\s/g, '|'
  170. ), ')\\b'),
  171. endsParent: true,
  172. relevance: 0
  173. },
  174. hljs.inherit(hljs.TITLE_MODE, { endsParent: true })
  175. ]
  176. };
  177. var GENTLEMANS_SET = [
  178. // STATIC_MEMBER,
  179. PS_METHODS,
  180. PS_COMMENT,
  181. BACKTICK_ESCAPE,
  182. hljs.NUMBER_MODE,
  183. QUOTE_STRING,
  184. APOS_STRING,
  185. // PS_NEW_OBJECT_TYPE,
  186. CMDLETS,
  187. VAR,
  188. LITERAL,
  189. HASH_SIGNS
  190. ];
  191. var PS_TYPE = {
  192. begin: /\[/, end: /\]/,
  193. excludeBegin: true,
  194. excludeEnd: true,
  195. relevance: 0,
  196. contains: [].concat(
  197. 'self',
  198. GENTLEMANS_SET,
  199. { begin: "(" + TYPES.join("|") + ")", className: "built_in", relevance:0 },
  200. { className: 'type', begin: /[\.\w\d]+/, relevance: 0 }
  201. )
  202. };
  203. PS_METHODS.contains.unshift(PS_TYPE)
  204. return {
  205. aliases: ["ps", "ps1"],
  206. lexemes: /-?[A-z\.\-]+/,
  207. case_insensitive: true,
  208. keywords: KEYWORDS,
  209. contains: GENTLEMANS_SET.concat(
  210. PS_CLASS,
  211. PS_FUNCTION,
  212. PS_USING,
  213. PS_ARGUMENTS,
  214. PS_TYPE
  215. )
  216. };
  217. };