summaryrefslogtreecommitdiff
path: root/pygments
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2012-02-05 12:12:42 +0100
committerGeorg Brandl <georg@python.org>2012-02-05 12:12:42 +0100
commit853ead03f3dea69a76d34e9bb5a85d4df881ed59 (patch)
tree33a34de0fcd85fed440925e319325152ebb5fc42 /pygments
parentb89ed802bc78e49a2923a656742861c24a49e2c6 (diff)
downloadpygments-853ead03f3dea69a76d34e9bb5a85d4df881ed59.tar.gz
Closes #654: add PowerShell lexer.
Diffstat (limited to 'pygments')
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/shell.py89
2 files changed, 89 insertions, 1 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 180037f7..3ac9229e 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -176,6 +176,7 @@ LEXERS = {
'PostgresConsoleLexer': ('pygments.lexers.sql', 'PostgreSQL console (psql)', ('psql', 'postgresql-console', 'postgres-console'), (), ('text/x-postgresql-psql',)),
'PostgresLexer': ('pygments.lexers.sql', 'PostgreSQL SQL dialect', ('postgresql', 'postgres'), (), ('text/x-postgresql',)),
'PovrayLexer': ('pygments.lexers.other', 'POVRay', ('pov',), ('*.pov', '*.inc'), ('text/x-povray',)),
+ 'PowerShellLexer': ('pygments.lexers.shell', 'PowerShell', ('powershell', 'posh'), ('*.ps1',), ('text/x-powershell',)),
'PrologLexer': ('pygments.lexers.compiled', 'Prolog', ('prolog',), ('*.prolog', '*.pro', '*.pl'), ('text/x-prolog',)),
'PropertiesLexer': ('pygments.lexers.text', 'Properties', ('properties',), ('*.properties',), ('text/x-java-properties',)),
'ProtoBufLexer': ('pygments.lexers.other', 'Protocol Buffer', ('protobuf',), ('*.proto',), ()),
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py
index e2fa5191..51c4ec3c 100644
--- a/pygments/lexers/shell.py
+++ b/pygments/lexers/shell.py
@@ -17,7 +17,8 @@ from pygments.token import Punctuation, \
from pygments.util import shebang_matches
-__all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer']
+__all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer',
+ 'PowerShellLexer']
line_re = re.compile('.*?\n')
@@ -271,3 +272,89 @@ class TcshLexer(RegexLexer):
include('root'),
],
}
+
+
+class PowerShellLexer(RegexLexer):
+ """
+ For Windows PowerShell code.
+
+ *New in Pygments 1.5.*
+ """
+ name = 'PowerShell'
+ aliases = ['powershell', 'posh', 'ps1']
+ filenames = ['*.ps1']
+ mimetypes = ['text/x-powershell']
+
+ flags = re.DOTALL | re.IGNORECASE | re.MULTILINE
+
+ keywords = (
+ 'while validateset validaterange validatepattern validatelength '
+ 'validatecount until trap switch return ref process param parameter in '
+ 'if global: function foreach for finally filter end elseif else '
+ 'dynamicparam do default continue cmdletbinding break begin alias \\? '
+ '% #script #private #local #global mandatory parametersetname position '
+ 'valuefrompipeline valuefrompipelinebypropertyname '
+ 'valuefromremainingarguments helpmessage try catch').split()
+
+ operators = (
+ 'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle '
+ 'clike clt cmatch cne cnotcontains cnotlike cnotmatch contains '
+ 'creplace eq exact f file ge gt icontains ieq ige igt ile ilike ilt '
+ 'imatch ine inotcontains inotlike inotmatch ireplace is isnot le like '
+ 'lt match ne not notcontains notlike notmatch or regex replace '
+ 'wildcard').split()
+
+ verbs = (
+ 'write where wait use update unregister undo trace test tee take '
+ 'suspend stop start split sort skip show set send select scroll resume '
+ 'restore restart resolve resize reset rename remove register receive '
+ 'read push pop ping out new move measure limit join invoke import '
+ 'group get format foreach export expand exit enter enable disconnect '
+ 'disable debug cxnew copy convertto convertfrom convert connect '
+ 'complete compare clear checkpoint aggregate add').split()
+
+ commenthelp = (
+ 'component description example externalhelp forwardhelpcategory '
+ 'forwardhelptargetname forwardhelptargetname functionality inputs link '
+ 'notes outputs parameter remotehelprunspace role synopsis').split()
+
+ tokens = {
+ 'root': [
+ (r'\s+', Text),
+ (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp),
+ bygroups(Comment, String.Doc, Comment)),
+ (r'#[^\n]*?$', Comment),
+ (r'(&lt;|<)#', Comment.Multiline, 'multline'),
+ (r'@"\n.*?\n"@', String.Heredoc),
+ (r"@'\n.*?\n'@", String.Heredoc),
+ (r'"', String.Double, 'string'),
+ (r"'([^']|'')*'", String.Single),
+ (r'(\$|@@|@)((global|script|private|env):)?[a-z0-9_]+',
+ Name.Variable),
+ (r'(%s)\b' % '|'.join(keywords), Keyword),
+ (r'-(%s)\b' % '|'.join(operators), Operator),
+ (r'(%s)-[a-z_][a-z0-9_]*\b' % '|'.join(verbs), Name.Builtin),
+ (r'\[[a-z_\[][a-z0-9_. `,\[\]]*\]', Name.Constant), # .net [type]s
+ (r'-[a-z_][a-z0-9_]*', Name),
+ (r'\w+', Name),
+ (r'[.,{}\[\]$()=+*/\\&%!~?^`|<>-]', Punctuation),
+ ],
+ 'multline': [
+ (r'[^#&.]+', Comment.Multiline),
+ (r'#(>|&gt;)', Comment.Multiline, '#pop'),
+ (r'\.(%s)' % '|'.join(commenthelp), String.Doc),
+ (r'[#&.]', Comment.Multiline),
+ ],
+ 'string': [
+ (r'[^$`"]+', String.Double),
+ (r'\$\(', String.Interpol, 'interpol'),
+ (r'`"|""', String.Double),
+ (r'[`$]', String.Double),
+ (r'"', String.Double, '#pop'),
+ ],
+ 'interpol': [
+ (r'[^$)]+', String.Interpol),
+ (r'\$\(', String.Interpol, '#push'),
+ (r'\)', String.Interpol, '#pop'),
+ ]
+ }