Commits

Bas Bossink committed 1ded56e

First draft of the PowerShell lexer, starting from the BashLexer as example.

-Added keyword, default cmdlets, aliases, operators, prefererence variable, automatic variables.

Comments (0)

Files changed (1)

pygments/lexers/other.py

             (r'([0-8]{3}|n|r|t|b|f|\\|\(|\)|)', String.Escape, '#pop'),
         ],
     }
+
+class PowerShellLexer(RegexLexer):
+    """
+    Lexer for PowerShell scripts.
+
+    """
+
+    name = 'PowerShell'
+    aliases = ['ps1', 'posh']
+    filenames = ['*.ps1', '*.psm1']
+    mimetypes = ['text/plain']
+    flags = re.IGNORECASE | re.MULTILINE
+
+    tokens = {
+        'root': [
+            include('basic'),
+            (r'\$\(', Keyword, 'paren'),
+            (r'`', String.Backtick, 'backticks'),
+            include('data'),
+        ],
+        'basic': [
+            (r'\b(begin|catch|data|do|dynamicparam|else|elseif|end|'
+             r'exit|filter|finally|for|foreach|from|function|if|in|param'
+             r'process|return|switch|throw|trap|try|until|while)\s*\b',
+             Keyword),
+            (r'\b(Add-Computer|Add-Content|Add-History|Add-Member|Add-PSSnapin|'
+             r'Add-Type|Checkpoint-Computer|Clear-Content|Clear-EventLog|'
+             r'Clear-History|Clear-Item|Clear-ItemProperty|Clear-Variable|'
+             r'Compare-Object|Complete-Transaction|Connect-WSMan|'
+             r'ConvertFrom-Csv|ConvertFrom-SecureString|ConvertFrom-StringData|'
+             r'Convert-Path|ConvertTo-Csv|ConvertTo-Html|ConvertTo-SecureString|'
+             r'ConvertTo-Xml|Copy-Item|Copy-ItemProperty|Debug-Process|'
+             r'Disable-ComputerRestore|Disable-PSBreakpoint|'
+             r'Disable-PSSessionConfiguration|Disable-WSManCredSSP|'
+             r'Disconnect-WSMan|Enable-ComputerRestore|Enable-PSBreakpoint|'
+             r'Enable-PSRemoting|Enable-PSSessionConfiguration|'
+             r'Enable-WSManCredSSP|Enter-PSSession|Exit-PSSession|Export-Alias|'
+             r'Export-Clixml|Export-Console|Export-Counter|Export-Csv|'
+             r'Export-FormatData|Export-ModuleMember|Export-PSSession|'
+             r'ForEach-Object|Format-Custom|Format-List|Format-Table|'
+             r'Format-Wide|Get-Acl|Get-Alias|Get-AuthenticodeSignature|'
+             r'Get-ChildItem|Get-Command|Get-ComputerRestorePoint|Get-Content|'
+             r'Get-Counter|Get-Credential|Get-Culture|Get-Date|Get-Event|'
+             r'Get-EventLog|Get-EventSubscriber|Get-ExecutionPolicy|'
+             r'Get-FormatData|Get-Help|Get-History|Get-Host|Get-HotFix|Get-Item|'
+             r'Get-ItemProperty|Get-Job|Get-Location|Get-Member|Get-Module|'
+             r'Get-PfxCertificate|Get-Process|Get-PSBreakpoint|Get-PSCallStack|'
+             r'Get-PSDrive|Get-PSProvider|Get-PSSession|'
+             r'Get-PSSessionConfiguration|Get-PSSnapin|Get-Random|Get-Service|'
+             r'Get-TraceSource|Get-Transaction|Get-UICulture|Get-Unique|'
+             r'Get-Variable|Get-WinEvent|Get-WmiObject|Get-WSManCredSSP|'
+             r'Get-WSManInstance|Group-Object|Import-Alias|Import-Clixml|'
+             r'Import-Counter|Import-Csv|Import-LocalizedData|Import-Module|'
+             r'Import-PSSession|Invoke-Command|Invoke-Expression|Invoke-History|'
+             r'Invoke-Item|Invoke-WmiMethod|Invoke-WSManAction|Join-Path|'
+             r'Limit-EventLog|Measure-Command|Measure-Object|Move-Item|'
+             r'Move-ItemProperty|New-Alias|New-Event|New-EventLog|New-Item|'
+             r'New-ItemProperty|New-Module|New-ModuleManifest|New-Object|'
+             r'New-PSDrive|New-PSSession|New-PSSessionOption|New-Service|'
+             r'New-TimeSpan|New-Variable|New-WebServiceProxy|New-WSManInstance|'
+             r'New-WSManSessionOption|Out-Default|Out-File|Out-GridView|Out-Host|'
+             r'Out-Null|Out-Printer|Out-String|Pop-Location|Push-Location|'
+             r'Read-Host|Receive-Job|Register-EngineEvent|Register-ObjectEvent|'
+             r'Register-PSSessionConfiguration|Register-WmiEvent|Remove-Computer|'
+             r'Remove-Event|Remove-EventLog|Remove-Item|Remove-ItemProperty|'
+             r'Remove-Job|Remove-Module|Remove-PSBreakpoint|Remove-PSDrive|'
+             r'Remove-PSSession|Remove-PSSnapin|Remove-Variable|Remove-WmiObject|'
+             r'Remove-WSManInstance|Rename-Item|Rename-ItemProperty|'
+             r'Reset-ComputerMachinePassword|Resolve-Path|Restart-Computer|'
+             r'Restart-Service|Restore-Computer|Resume-Service|Select-Object|'
+             r'Select-String|Select-Xml|Send-MailMessage|Set-Acl|Set-Alias|'
+             r'Set-AuthenticodeSignature|Set-Content|Set-Date|'
+             r'Set-ExecutionPolicy|Set-Item|Set-ItemProperty|Set-Location|'
+             r'Set-PSBreakpoint|Set-PSDebug|Set-PSSessionConfiguration|'
+             r'Set-Service|Set-StrictMode|Set-TraceSource|Set-Variable|'
+             r'Set-WmiInstance|Set-WSManInstance|Set-WSManQuickConfig|'
+             r'Show-EventLog|Sort-Object|Split-Path|Start-Job|Start-Process|'
+             r'Start-Service|Start-Sleep|Start-Transaction|Start-Transcript|'
+             r'Stop-Computer|Stop-Job|Stop-Process|Stop-Service|Stop-Transcript|'
+             r'Suspend-Service|Tee-Object|Test-ComputerSecureChannel|'
+             r'Test-Connection|Test-ModuleManifest|Test-Path|Test-WSMan|'
+             r'Trace-Command|Undo-Transaction|Unregister-Event|'
+             r'Unregister-PSSessionConfiguration|Update-FormatData|Update-List|'
+             r'Update-TypeData|Use-Transaction|Wait-Event|Wait-Job|Wait-Process|'
+             r'Where-Object|Write-Debug|Write-Error|Write-EventLog|Write-Host|'
+             r'Write-Output|Write-Progress|Write-Verbose|Write-Warning)\s*\b',
+             Name.Builtin), #v2.0 cmdlets
+            (r'\b(%|?|ac|asnp|cat|chdir|clc|clhy|cli|clp|clv|compare|copy|cp|'
+             r'cpi|cpp|cvpa|dbp|del|diff|dir|ebp|echo|epal|epcsv|epsn|erase|'
+             r'etsn|exsn|fc|fl|foreach|ft|fw|gal|gbp|gc|gci|gcm|gcs|gdr|ghy|gi|'
+             r'gjb|gl|gm|gmo|gp|gps|group|gsn|gsnp|gsv|gu|gv|gwmi|h|history|icm|'
+             r'iex|ihy|ii|ipal|ipcsv|ipmo|ipsn|iwmi|kill|lp|ls|measure|mi|mount|'
+             r'move|mp|mv|nal|ndr|ni|nmo|nsn|nv|ogv|oh|popd|ps|pushd|pwd|r|rbp|'
+             r'rcjb|rd|rdr|ren|ri|rjb|rm|rmdir|rmo|rni|rnp|rp|rsn|rsnp|rv|rvpa|'
+             r'rwmi|sajb|sal|saps|sasv|sbp|sc|select|set|si|sl|sleep|sls|sort|'
+             r'sp|spjb|spps|spsv|start|sv|swmi|tee|type|where|wjb|write)\s*\b',
+             Name.Builtin), #standard aliases
+            (r'(\b(\$_\$\$|\$\?|\$\^|\$Args|\$ConsoleFileName|\$Error|\$Event|'
+             r'\$EventSubscriber|\$ExecutionContext|\$False|\$ForEach|\$Home|'
+             r'\$Host|\$Input|\$LastExitCode|\$MyInvocation\$NestedPromptLevel|'
+             r'\$NULL|\$PID|\$Profile|\$PSBoundParameters|\$PSCmdlet|\$PsCulture|'
+             r'\$PsDebugContext|\$PsHome|\$PSScriptRoot|\$PSUICulture|'
+             r'\$PsVerionTable|\$Pwd|\$Sender|\$ShellID|\$SourceArgs|\$This|'
+             r'\$True|\$ConfirmPreference|\$DebugPreference|\$ErrorActionPreference|'
+             r'\$ErrorView|\$FormatEnumerationLimit|\$LogCommandHealthEvent|'
+             r'\$LogCommandLifecycleEvent|\$LogEngineHealthEvent|'
+             r'\$LogEngineLifecycleEvent|\$LogProviderLifecycleEvent|'
+             r'\$LogProviderHealthEvent|\$MaximumAliasCount|\$MaximumDriveCount|'
+             r'\$MaximumErrorCount|\$MaximumFunctionCount|\$MaximumHistoryCount|'
+             r'\$MaximumVariableCount|\$OFS|\$OutputEncoding|'
+             r'\$ProgressPreference|\$PSEmailServer|\$PSSessionApplicationName|'
+             r'\$PSSessionConfigurationName|\$PSSessionOption|\$VerbosePreference|'
+             r'\$WarningPreference|\$WhatIfPreference)\s*\b)', 
+             Name.Builtin), #automatic  & preference variables
+
+            (r'#.*?\n', Comment),
+            (r'(\b\$\w+)(\s*)(=|\+=|-=|\*=|/=|%=)', bygroups(Name.Variable, Text, Operator)),
+            (r'\+|-|\*|/|%|..', Operator),
+            (r'\b(-eq|-ne|-gt|-lt|-le|-ge|-like|-notlike|-match|-notmach|'
+             r'-notmatch|-contains|-notcontains|-replace|-band|-bor|-bxor)\s*\b', Operator),
+            (r'[\(\)\[\];,.<>/\{\}]', Punctuation),            
+        ],
+        'data': [
+            (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
+            (r"(?s)\$?'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
+            (r';', Text),
+            (r'\s+', Text),
+            (r'[^=\s\n\[\]{}()$"\'`\\<]+', Text),
+            (r'\d+(?= |\Z)', Number),
+            (r'\$#?(\w+|.)', Name.Variable),
+            (r'<', Text),
+        ],
+        'paren': [
+            (r'\)', Keyword, '#pop'),
+            include('root'),
+        ],
+        'backticks': [
+            (r'`', String.Backtick, '#pop'),
+            include('root'),
+        ],
+    }