powershell_highlight_rules.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var PowershellHighlightRules = function() {
  6. var keywords = (
  7. "function|if|else|elseif|switch|while|default|for|do|until|break|continue|" +
  8. "foreach|return|filter|in|trap|throw|param|begin|process|end"
  9. );
  10. var builtinFunctions = (
  11. "Get-Alias|Import-Alias|New-Alias|Set-Alias|Get-AuthenticodeSignature|Set-AuthenticodeSignature|" +
  12. "Set-Location|Get-ChildItem|Clear-Item|Get-Command|Measure-Command|Trace-Command|" +
  13. "Add-Computer|Checkpoint-Computer|Remove-Computer|Restart-Computer|Restore-Computer|Stop-Computer|" +
  14. "Reset-ComputerMachinePassword|Test-ComputerSecureChannel|Add-Content|Get-Content|Set-Content|Clear-Content|" +
  15. "Get-Command|Invoke-Command|Enable-ComputerRestore|Disable-ComputerRestore|Get-ComputerRestorePoint|Test-Connection|" +
  16. "ConvertFrom-CSV|ConvertTo-CSV|ConvertTo-Html|ConvertTo-Xml|ConvertFrom-SecureString|ConvertTo-SecureString|" +
  17. "Copy-Item|Export-Counter|Get-Counter|Import-Counter|Get-Credential|Get-Culture|" +
  18. "Get-ChildItem|Get-Date|Set-Date|Remove-Item|Compare-Object|Get-Event|" +
  19. "Get-WinEvent|New-Event|Remove-Event|Unregister-Event|Wait-Event|Clear-EventLog|" +
  20. "Get-Eventlog|Limit-EventLog|New-Eventlog|Remove-EventLog|Show-EventLog|Write-EventLog|" +
  21. "Get-EventSubscriber|Register-EngineEvent|Register-ObjectEvent|Register-WmiEvent|Get-ExecutionPolicy|Set-ExecutionPolicy|" +
  22. "Export-Alias|Export-Clixml|Export-Console|Export-Csv|ForEach-Object|Format-Custom|" +
  23. "Format-List|Format-Table|Format-Wide|Export-FormatData|Get-FormatData|Get-Item|" +
  24. "Get-ChildItem|Get-Help|Add-History|Clear-History|Get-History|Invoke-History|" +
  25. "Get-Host|Read-Host|Write-Host|Get-HotFix|Import-Clixml|Import-Csv|" +
  26. "Invoke-Command|Invoke-Expression|Get-Item|Invoke-Item|New-Item|Remove-Item|" +
  27. "Set-Item|Clear-ItemProperty|Copy-ItemProperty|Get-ItemProperty|Move-ItemProperty|New-ItemProperty|" +
  28. "Remove-ItemProperty|Rename-ItemProperty|Set-ItemProperty|Get-Job|Receive-Job|Remove-Job|" +
  29. "Start-Job|Stop-Job|Wait-Job|Stop-Process|Update-List|Get-Location|" +
  30. "Pop-Location|Push-Location|Set-Location|Send-MailMessage|Add-Member|Get-Member|" +
  31. "Move-Item|Compare-Object|Group-Object|Measure-Object|New-Object|Select-Object|" +
  32. "Sort-Object|Where-Object|Out-Default|Out-File|Out-GridView|Out-Host|" +
  33. "Out-Null|Out-Printer|Out-String|Convert-Path|Join-Path|Resolve-Path|" +
  34. "Split-Path|Test-Path|Get-Pfxcertificate|Pop-Location|Push-Location|Get-Process|" +
  35. "Start-Process|Stop-Process|Wait-Process|Enable-PSBreakpoint|Disable-PSBreakpoint|Get-PSBreakpoint|" +
  36. "Set-PSBreakpoint|Remove-PSBreakpoint|Get-PSDrive|New-PSDrive|Remove-PSDrive|Get-PSProvider|" +
  37. "Set-PSdebug|Enter-PSSession|Exit-PSSession|Export-PSSession|Get-PSSession|Import-PSSession|" +
  38. "New-PSSession|Remove-PSSession|Disable-PSSessionConfiguration|Enable-PSSessionConfiguration|Get-PSSessionConfiguration|Register-PSSessionConfiguration|" +
  39. "Set-PSSessionConfiguration|Unregister-PSSessionConfiguration|New-PSSessionOption|Add-PsSnapIn|Get-PsSnapin|Remove-PSSnapin|" +
  40. "Get-Random|Read-Host|Remove-Item|Rename-Item|Rename-ItemProperty|Select-Object|" +
  41. "Select-XML|Send-MailMessage|Get-Service|New-Service|Restart-Service|Resume-Service|" +
  42. "Set-Service|Start-Service|Stop-Service|Suspend-Service|Sort-Object|Start-Sleep|" +
  43. "ConvertFrom-StringData|Select-String|Tee-Object|New-Timespan|Trace-Command|Get-Tracesource|" +
  44. "Set-Tracesource|Start-Transaction|Complete-Transaction|Get-Transaction|Use-Transaction|Undo-Transaction|" +
  45. "Start-Transcript|Stop-Transcript|Add-Type|Update-TypeData|Get-Uiculture|Get-Unique|" +
  46. "Update-Formatdata|Update-Typedata|Clear-Variable|Get-Variable|New-Variable|Remove-Variable|" +
  47. "Set-Variable|New-WebServiceProxy|Where-Object|Write-Debug|Write-Error|Write-Host|" +
  48. "Write-Output|Write-Progress|Write-Verbose|Write-Warning|Set-WmiInstance|Invoke-WmiMethod|" +
  49. "Get-WmiObject|Remove-WmiObject|Connect-WSMan|Disconnect-WSMan|Test-WSMan|Invoke-WSManAction|" +
  50. "Disable-WSManCredSSP|Enable-WSManCredSSP|Get-WSManCredSSP|New-WSManInstance|Get-WSManInstance|Set-WSManInstance|" +
  51. "Remove-WSManInstance|Set-WSManQuickConfig|New-WSManSessionOption"
  52. );
  53. var keywordMapper = this.createKeywordMapper({
  54. "support.function": builtinFunctions,
  55. "keyword": keywords
  56. }, "identifier");
  57. var binaryOperatorsRe = "eq|ne|ge|gt|lt|le|like|notlike|match|notmatch|replace|contains|notcontains|" +
  58. "ieq|ine|ige|igt|ile|ilt|ilike|inotlike|imatch|inotmatch|ireplace|icontains|inotcontains|" +
  59. "is|isnot|as|" +
  60. "and|or|band|bor|not";
  61. // regexp must not have capturing parentheses. Use (?:) instead.
  62. // regexps are ordered -> the first match is used
  63. this.$rules = {
  64. "start" : [
  65. {
  66. token : "comment",
  67. regex : "#.*$"
  68. }, {
  69. token : "comment.start",
  70. regex : "<#",
  71. next : "comment"
  72. }, {
  73. token : "string", // single line
  74. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  75. }, {
  76. token : "string", // single line
  77. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  78. }, {
  79. token : "constant.numeric", // hex
  80. regex : "0[xX][0-9a-fA-F]+\\b"
  81. }, {
  82. token : "constant.numeric", // float
  83. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  84. }, {
  85. token : "constant.language.boolean",
  86. regex : "[$](?:[Tt]rue|[Ff]alse)\\b"
  87. }, {
  88. token : "constant.language",
  89. regex : "[$][Nn]ull\\b"
  90. }, {
  91. token : "variable.instance",
  92. regex : "[$][a-zA-Z][a-zA-Z0-9_]*\\b"
  93. }, {
  94. token : keywordMapper,
  95. // TODO: Unicode escape sequences
  96. // TODO: Unicode identifiers
  97. regex : "[a-zA-Z_$][a-zA-Z0-9_$\\-]*\\b"
  98. }, {
  99. token : "keyword.operator",
  100. regex : "\\-(?:" + binaryOperatorsRe + ")"
  101. }, {
  102. token : "keyword.operator",
  103. regex : "&|\\*|\\+|\\-|\\=|\\+=|\\-="
  104. }, {
  105. token : "lparen",
  106. regex : "[[({]"
  107. }, {
  108. token : "rparen",
  109. regex : "[\\])}]"
  110. }, {
  111. token : "text",
  112. regex : "\\s+"
  113. }
  114. ],
  115. "comment" : [
  116. {
  117. token : "comment.end",
  118. regex : "#>",
  119. next : "start"
  120. }, {
  121. token : "doc.comment.tag",
  122. regex : "^\\.\\w+"
  123. }, {
  124. token : "comment",
  125. regex : "\\w+"
  126. }, {
  127. token : "comment",
  128. regex : "."
  129. }
  130. ]
  131. };
  132. };
  133. oop.inherits(PowershellHighlightRules, TextHighlightRules);
  134. exports.PowershellHighlightRules = PowershellHighlightRules;
  135. });