Commits

Bas Bossink committed 3457db0

Add first test to check lexing function names correctly.

Comments (0)

Files changed (2)

pygments/lexers/other.py

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

tests/test_powershell_lexer.py

+# -*- coding: utf-8 -*-
+"""
+    Pygments PowerShell lexer tests
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+    :copyright: Copyright 2010 by Bas Bossink <bas.bossink@gmail.com>
+    :license: BSD, see LICENSE for details.
+"""
+
+import unittest
+from pygments.token import *
+from pygments.lexers.other import PowerShellLexer
+
+class PowerShellLexerTest(unittest.TestCase):
+    def test_function_name(self):
+        lx = PowerShellLexer()
+        sample = 'function Get-Image {}'
+        toks = list(lx.get_tokens_unprocessed(sample))
+        print toks
+        self.assertEquals(toks, [
+                (0, Keyword, 'function'), 
+                (8, Text, ' '),
+                (9, Name.Function, 'Get-Image'),
+                (18, Text, ' '),
+                (19, Punctuation, '{'),
+                (20, Punctuation, '}'),
+                ])