summaryrefslogtreecommitdiff
path: root/pygments/lexers/shell.py
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/lexers/shell.py')
-rw-r--r--pygments/lexers/shell.py646
1 files changed, 509 insertions, 137 deletions
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py
index b95faf93..adb7744c 100644
--- a/pygments/lexers/shell.py
+++ b/pygments/lexers/shell.py
@@ -5,20 +5,22 @@
Lexers for various shells.
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
+ :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import re
-from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, include
+from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, \
+ include, default, this, using, words
from pygments.token import Punctuation, \
Text, Comment, Operator, Keyword, Name, String, Number, Generic
from pygments.util import shebang_matches
__all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer',
- 'PowerShellLexer', 'ShellSessionLexer']
+ 'MSDOSSessionLexer', 'PowerShellLexer',
+ 'PowerShellSessionLexer', 'TcshSessionLexer', 'FishShellLexer']
line_re = re.compile('.*?\n')
@@ -27,28 +29,34 @@ class BashLexer(RegexLexer):
"""
Lexer for (ba|k|)sh shell scripts.
- *New in Pygments 0.6.*
+ .. versionadded:: 0.6
"""
name = 'Bash'
- aliases = ['bash', 'sh', 'ksh']
+ aliases = ['bash', 'sh', 'ksh', 'shell']
filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass',
- '.bashrc', 'bashrc', '.bash_*', 'bash_*']
+ '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'PKGBUILD']
mimetypes = ['application/x-sh', 'application/x-shellscript']
tokens = {
'root': [
include('basic'),
- (r'\$\(\(', Keyword, 'math'),
- (r'\$\(', Keyword, 'paren'),
- (r'\${#?', Keyword, 'curly'),
(r'`', String.Backtick, 'backticks'),
include('data'),
+ include('interp'),
+ ],
+ 'interp': [
+ (r'\$\(\(', Keyword, 'math'),
+ (r'\$\(', Keyword, 'paren'),
+ (r'\$\{#?', String.Interpol, 'curly'),
+ (r'\$[a-fA-F_][a-fA-F0-9_]*', Name.Variable), # user variable
+ (r'\$(?:\d+|[#$?!_*@-])', Name.Variable), # builtin
+ (r'\$', Text),
],
'basic': [
(r'\b(if|fi|else|while|do|done|for|then|return|function|case|'
- r'select|continue|until|esac|elif)\s*\b',
- Keyword),
+ r'select|continue|until|esac|elif)(\s*)\b',
+ bygroups(Keyword, Text)),
(r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|'
r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|'
r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|'
@@ -56,7 +64,8 @@ class BashLexer(RegexLexer):
r'shopt|source|suspend|test|time|times|trap|true|type|typeset|'
r'ulimit|umask|unalias|unset|wait)\s*\b(?!\.)',
Name.Builtin),
- (r'#.*\n', Comment),
+ (r'\A#!.+\n', Comment.Hashbang),
+ (r'#.*\n', Comment.Single),
(r'\\[\w\W]', String.Escape),
(r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
(r'[\[\]{}()=]', Operator),
@@ -65,20 +74,28 @@ class BashLexer(RegexLexer):
(r'&&|\|\|', Operator),
],
'data': [
- (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
- (r"(?s)\$?'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
- (r';', Text),
+ (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"', String.Double),
+ (r'"', String.Double, 'string'),
+ (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
+ (r"(?s)'.*?'", String.Single),
+ (r';', Punctuation),
+ (r'&', Punctuation),
+ (r'\|', Punctuation),
(r'\s+', Text),
- (r'[^=\s\[\]{}()$"\'`\\<]+', Text),
(r'\d+(?= |\Z)', Number),
- (r'\$#?(\w+|.)', Name.Variable),
+ (r'[^=\s\[\]{}()$"\'`\\<&|;]+', Text),
(r'<', Text),
],
+ 'string': [
+ (r'"', String.Double, '#pop'),
+ (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+', String.Double),
+ include('interp'),
+ ],
'curly': [
- (r'}', Keyword, '#pop'),
+ (r'\}', String.Interpol, '#pop'),
(r':-', Keyword),
- (r'[a-zA-Z0-9_]+', Name.Variable),
- (r'[^}:"\'`$]+', Punctuation),
+ (r'\w+', Name.Variable),
+ (r'[^}:"\'`$\\]+', Punctuation),
(r':', Punctuation),
include('root'),
],
@@ -89,6 +106,8 @@ class BashLexer(RegexLexer):
'math': [
(r'\)\)', Keyword, '#pop'),
(r'[-+*/%^|&]|\*\*|\|\|', Operator),
+ (r'\d+#\d+', Number),
+ (r'\d+#(?! )', Number),
(r'\d+', Number),
include('root'),
],
@@ -99,23 +118,20 @@ class BashLexer(RegexLexer):
}
def analyse_text(text):
- return shebang_matches(text, r'(ba|z|)sh')
+ if shebang_matches(text, r'(ba|z|)sh'):
+ return 1
+ if text.startswith('$ '):
+ return 0.2
-class BashSessionLexer(Lexer):
+class ShellSessionBaseLexer(Lexer):
"""
- Lexer for simplistic shell sessions.
+ Base lexer for simplistic shell sessions.
- *New in Pygments 1.1.*
+ .. versionadded:: 2.1
"""
-
- name = 'Bash Session'
- aliases = ['console']
- filenames = ['*.sh-session']
- mimetypes = ['application/x-shell-session']
-
def get_tokens_unprocessed(self, text):
- bashlexer = BashLexer(**self.options)
+ innerlexer = self._innerLexerCls(**self.options)
pos = 0
curcode = ''
@@ -123,8 +139,7 @@ class BashSessionLexer(Lexer):
for match in line_re.finditer(text):
line = match.group()
- m = re.match(r'^((?:\(\S+\))?(?:|sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)'
- r'?|\[\S+[@:][^\n]+\].+)[$#%])(.*\n?)' , line)
+ m = re.match(self._ps1rgx, line)
if m:
# To support output lexers (say diff output), the output
# needs to be broken by prompts whenever the output lexer
@@ -135,13 +150,13 @@ class BashSessionLexer(Lexer):
insertions.append((len(curcode),
[(0, Generic.Prompt, m.group(1))]))
curcode += m.group(2)
- elif line.startswith('>'):
+ elif line.startswith(self._ps2):
insertions.append((len(curcode),
- [(0, Generic.Prompt, line[:1])]))
- curcode += line[1:]
+ [(0, Generic.Prompt, line[:len(self._ps2)])]))
+ curcode += line[len(self._ps2):]
else:
if insertions:
- toks = bashlexer.get_tokens_unprocessed(curcode)
+ toks = innerlexer.get_tokens_unprocessed(curcode)
for i, t, v in do_insertions(insertions, toks):
yield pos+i, t, v
yield match.start(), Generic.Output, line
@@ -149,117 +164,358 @@ class BashSessionLexer(Lexer):
curcode = ''
if insertions:
for i, t, v in do_insertions(insertions,
- bashlexer.get_tokens_unprocessed(curcode)):
+ innerlexer.get_tokens_unprocessed(curcode)):
yield pos+i, t, v
-class ShellSessionLexer(Lexer):
+class BashSessionLexer(ShellSessionBaseLexer):
"""
- Lexer for shell sessions that works with different command prompts
+ Lexer for simplistic shell sessions.
- *New in Pygments 1.6.*
+ .. versionadded:: 1.1
"""
- name = 'Shell Session'
- aliases = ['shell-session']
- filenames = ['*.shell-session']
- mimetypes = ['application/x-sh-session']
-
- def get_tokens_unprocessed(self, text):
- bashlexer = BashLexer(**self.options)
-
- pos = 0
- curcode = ''
- insertions = []
-
- for match in line_re.finditer(text):
- line = match.group()
- m = re.match(r'^((?:\[?\S+@[^$#%]+)[$#%])(.*\n?)', line)
- if m:
- # To support output lexers (say diff output), the output
- # needs to be broken by prompts whenever the output lexer
- # changes.
- if not insertions:
- pos = match.start()
+ name = 'Bash Session'
+ aliases = ['console', 'shell-session']
+ filenames = ['*.sh-session', '*.shell-session']
+ mimetypes = ['application/x-shell-session', 'application/x-sh-session']
- insertions.append((len(curcode),
- [(0, Generic.Prompt, m.group(1))]))
- curcode += m.group(2)
- else:
- if insertions:
- toks = bashlexer.get_tokens_unprocessed(curcode)
- for i, t, v in do_insertions(insertions, toks):
- yield pos+i, t, v
- yield match.start(), Generic.Output, line
- insertions = []
- curcode = ''
- if insertions:
- for i, t, v in do_insertions(insertions,
- bashlexer.get_tokens_unprocessed(curcode)):
- yield pos+i, t, v
+ _innerLexerCls = BashLexer
+ _ps1rgx = \
+ r'^((?:(?:\[.*?\])|(?:\(\S+\))?(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \
+ r'?|\[\S+[@:][^\n]+\].+))\s*[$#%])(.*\n?)'
+ _ps2 = '>'
class BatchLexer(RegexLexer):
"""
Lexer for the DOS/Windows Batch file format.
- *New in Pygments 0.7.*
+ .. versionadded:: 0.7
"""
name = 'Batchfile'
- aliases = ['bat']
+ aliases = ['bat', 'batch', 'dosbatch', 'winbatch']
filenames = ['*.bat', '*.cmd']
mimetypes = ['application/x-dos-batch']
flags = re.MULTILINE | re.IGNORECASE
+ _nl = r'\n\x1a'
+ _punct = r'&<>|'
+ _ws = r'\t\v\f\r ,;=\xa0'
+ _space = r'(?:(?:(?:\^[%s])?[%s])+)' % (_nl, _ws)
+ _keyword_terminator = (r'(?=(?:\^[%s]?)?[%s+./:[\\\]]|[%s%s(])' %
+ (_nl, _ws, _nl, _punct))
+ _token_terminator = r'(?=\^?[%s]|[%s%s])' % (_ws, _punct, _nl)
+ _start_label = r'((?:(?<=^[^:])|^[^:]?)[%s]*)(:)' % _ws
+ _label = r'(?:(?:[^%s%s%s+:^]|\^[%s]?[\w\W])*)' % (_nl, _punct, _ws, _nl)
+ _label_compound = (r'(?:(?:[^%s%s%s+:^)]|\^[%s]?[^)])*)' %
+ (_nl, _punct, _ws, _nl))
+ _number = r'(?:-?(?:0[0-7]+|0x[\da-f]+|\d+)%s)' % _token_terminator
+ _opword = r'(?:equ|geq|gtr|leq|lss|neq)'
+ _string = r'(?:"[^%s"]*"?)' % _nl
+ _variable = (r'(?:(?:%%(?:\*|(?:~[a-z]*(?:\$[^:]+:)?)?\d|'
+ r'[^%%:%s]+(?::(?:~(?:-?\d+)?(?:,(?:-?\d+)?)?|(?:[^%%%s^]|'
+ r'\^[^%%%s])[^=%s]*=(?:[^%%%s^]|\^[^%%%s])*)?)?%%))|'
+ r'(?:\^?![^!:%s]+(?::(?:~(?:-?\d+)?(?:,(?:-?\d+)?)?|(?:'
+ r'[^!%s^]|\^[^!%s])[^=%s]*=(?:[^!%s^]|\^[^!%s])*)?)?\^?!))' %
+ (_nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl))
+ _core_token = r'(?:(?:(?:\^[%s]?)?[^%s%s%s])+)' % (_nl, _nl, _punct, _ws)
+ _core_token_compound = r'(?:(?:(?:\^[%s]?)?[^%s%s%s)])+)' % (_nl, _nl,
+ _punct, _ws)
+ _token = r'(?:[%s]+|%s)' % (_punct, _core_token)
+ _token_compound = r'(?:[%s]+|%s)' % (_punct, _core_token_compound)
+ _stoken = (r'(?:[%s]+|(?:%s|%s|%s)+)' %
+ (_punct, _string, _variable, _core_token))
+
+ def _make_begin_state(compound, _core_token=_core_token,
+ _core_token_compound=_core_token_compound,
+ _keyword_terminator=_keyword_terminator,
+ _nl=_nl, _punct=_punct, _string=_string,
+ _space=_space, _start_label=_start_label,
+ _stoken=_stoken, _token_terminator=_token_terminator,
+ _variable=_variable, _ws=_ws):
+ rest = '(?:%s|%s|[^"%%%s%s%s])*' % (_string, _variable, _nl, _punct,
+ ')' if compound else '')
+ rest_of_line = r'(?:(?:[^%s^]|\^[%s]?[\w\W])*)' % (_nl, _nl)
+ rest_of_line_compound = r'(?:(?:[^%s^)]|\^[%s]?[^)])*)' % (_nl, _nl)
+ set_space = r'((?:(?:\^[%s]?)?[^\S\n])*)' % _nl
+ suffix = ''
+ if compound:
+ _keyword_terminator = r'(?:(?=\))|%s)' % _keyword_terminator
+ _token_terminator = r'(?:(?=\))|%s)' % _token_terminator
+ suffix = '/compound'
+ return [
+ ((r'\)', Punctuation, '#pop') if compound else
+ (r'\)((?=\()|%s)%s' % (_token_terminator, rest_of_line),
+ Comment.Single)),
+ (r'(?=%s)' % _start_label, Text, 'follow%s' % suffix),
+ (_space, using(this, state='text')),
+ include('redirect%s' % suffix),
+ (r'[%s]+' % _nl, Text),
+ (r'\(', Punctuation, 'root/compound'),
+ (r'@+', Punctuation),
+ (r'((?:for|if|rem)(?:(?=(?:\^[%s]?)?/)|(?:(?!\^)|'
+ r'(?<=m))(?:(?=\()|%s)))(%s?%s?(?:\^[%s]?)?/(?:\^[%s]?)?\?)' %
+ (_nl, _token_terminator, _space,
+ _core_token_compound if compound else _core_token, _nl, _nl),
+ bygroups(Keyword, using(this, state='text')),
+ 'follow%s' % suffix),
+ (r'(goto%s)(%s(?:\^[%s]?)?/(?:\^[%s]?)?\?%s)' %
+ (_keyword_terminator, rest, _nl, _nl, rest),
+ bygroups(Keyword, using(this, state='text')),
+ 'follow%s' % suffix),
+ (words(('assoc', 'break', 'cd', 'chdir', 'cls', 'color', 'copy',
+ 'date', 'del', 'dir', 'dpath', 'echo', 'endlocal', 'erase',
+ 'exit', 'ftype', 'keys', 'md', 'mkdir', 'mklink', 'move',
+ 'path', 'pause', 'popd', 'prompt', 'pushd', 'rd', 'ren',
+ 'rename', 'rmdir', 'setlocal', 'shift', 'start', 'time',
+ 'title', 'type', 'ver', 'verify', 'vol'),
+ suffix=_keyword_terminator), Keyword, 'follow%s' % suffix),
+ (r'(call)(%s?)(:)' % _space,
+ bygroups(Keyword, using(this, state='text'), Punctuation),
+ 'call%s' % suffix),
+ (r'call%s' % _keyword_terminator, Keyword),
+ (r'(for%s(?!\^))(%s)(/f%s)' %
+ (_token_terminator, _space, _token_terminator),
+ bygroups(Keyword, using(this, state='text'), Keyword),
+ ('for/f', 'for')),
+ (r'(for%s(?!\^))(%s)(/l%s)' %
+ (_token_terminator, _space, _token_terminator),
+ bygroups(Keyword, using(this, state='text'), Keyword),
+ ('for/l', 'for')),
+ (r'for%s(?!\^)' % _token_terminator, Keyword, ('for2', 'for')),
+ (r'(goto%s)(%s?)(:?)' % (_keyword_terminator, _space),
+ bygroups(Keyword, using(this, state='text'), Punctuation),
+ 'label%s' % suffix),
+ (r'(if(?:(?=\()|%s)(?!\^))(%s?)((?:/i%s)?)(%s?)((?:not%s)?)(%s?)' %
+ (_token_terminator, _space, _token_terminator, _space,
+ _token_terminator, _space),
+ bygroups(Keyword, using(this, state='text'), Keyword,
+ using(this, state='text'), Keyword,
+ using(this, state='text')), ('(?', 'if')),
+ (r'rem(((?=\()|%s)%s?%s?.*|%s%s)' %
+ (_token_terminator, _space, _stoken, _keyword_terminator,
+ rest_of_line_compound if compound else rest_of_line),
+ Comment.Single, 'follow%s' % suffix),
+ (r'(set%s)%s(/a)' % (_keyword_terminator, set_space),
+ bygroups(Keyword, using(this, state='text'), Keyword),
+ 'arithmetic%s' % suffix),
+ (r'(set%s)%s((?:/p)?)%s((?:(?:(?:\^[%s]?)?[^"%s%s^=%s]|'
+ r'\^[%s]?[^"=])+)?)((?:(?:\^[%s]?)?=)?)' %
+ (_keyword_terminator, set_space, set_space, _nl, _nl, _punct,
+ ')' if compound else '', _nl, _nl),
+ bygroups(Keyword, using(this, state='text'), Keyword,
+ using(this, state='text'), using(this, state='variable'),
+ Punctuation),
+ 'follow%s' % suffix),
+ default('follow%s' % suffix)
+ ]
+
+ def _make_follow_state(compound, _label=_label,
+ _label_compound=_label_compound, _nl=_nl,
+ _space=_space, _start_label=_start_label,
+ _token=_token, _token_compound=_token_compound,
+ _ws=_ws):
+ suffix = '/compound' if compound else ''
+ state = []
+ if compound:
+ state.append((r'(?=\))', Text, '#pop'))
+ state += [
+ (r'%s([%s]*)(%s)(.*)' %
+ (_start_label, _ws, _label_compound if compound else _label),
+ bygroups(Text, Punctuation, Text, Name.Label, Comment.Single)),
+ include('redirect%s' % suffix),
+ (r'(?=[%s])' % _nl, Text, '#pop'),
+ (r'\|\|?|&&?', Punctuation, '#pop'),
+ include('text')
+ ]
+ return state
+
+ def _make_arithmetic_state(compound, _nl=_nl, _punct=_punct,
+ _string=_string, _variable=_variable, _ws=_ws):
+ op = r'=+\-*/!~'
+ state = []
+ if compound:
+ state.append((r'(?=\))', Text, '#pop'))
+ state += [
+ (r'0[0-7]+', Number.Oct),
+ (r'0x[\da-f]+', Number.Hex),
+ (r'\d+', Number.Integer),
+ (r'[(),]+', Punctuation),
+ (r'([%s]|%%|\^\^)+' % op, Operator),
+ (r'(%s|%s|(\^[%s]?)?[^()%s%%^"%s%s%s]|\^[%s%s]?%s)+' %
+ (_string, _variable, _nl, op, _nl, _punct, _ws, _nl, _ws,
+ r'[^)]' if compound else r'[\w\W]'),
+ using(this, state='variable')),
+ (r'(?=[\x00|&])', Text, '#pop'),
+ include('follow')
+ ]
+ return state
+
+ def _make_call_state(compound, _label=_label,
+ _label_compound=_label_compound):
+ state = []
+ if compound:
+ state.append((r'(?=\))', Text, '#pop'))
+ state.append((r'(:?)(%s)' % (_label_compound if compound else _label),
+ bygroups(Punctuation, Name.Label), '#pop'))
+ return state
+
+ def _make_label_state(compound, _label=_label,
+ _label_compound=_label_compound, _nl=_nl,
+ _punct=_punct, _string=_string, _variable=_variable):
+ state = []
+ if compound:
+ state.append((r'(?=\))', Text, '#pop'))
+ state.append((r'(%s?)((?:%s|%s|\^[%s]?%s|[^"%%^%s%s%s])*)' %
+ (_label_compound if compound else _label, _string,
+ _variable, _nl, r'[^)]' if compound else r'[\w\W]', _nl,
+ _punct, r')' if compound else ''),
+ bygroups(Name.Label, Comment.Single), '#pop'))
+ return state
+
+ def _make_redirect_state(compound,
+ _core_token_compound=_core_token_compound,
+ _nl=_nl, _punct=_punct, _stoken=_stoken,
+ _string=_string, _space=_space,
+ _variable=_variable, _ws=_ws):
+ stoken_compound = (r'(?:[%s]+|(?:%s|%s|%s)+)' %
+ (_punct, _string, _variable, _core_token_compound))
+ return [
+ (r'((?:(?<=[%s%s])\d)?)(>>?&|<&)([%s%s]*)(\d)' %
+ (_nl, _ws, _nl, _ws),
+ bygroups(Number.Integer, Punctuation, Text, Number.Integer)),
+ (r'((?:(?<=[%s%s])(?<!\^[%s])\d)?)(>>?|<)(%s?%s)' %
+ (_nl, _ws, _nl, _space, stoken_compound if compound else _stoken),
+ bygroups(Number.Integer, Punctuation, using(this, state='text')))
+ ]
+
tokens = {
- 'root': [
- # Lines can start with @ to prevent echo
- (r'^\s*@', Punctuation),
- (r'^(\s*)(rem\s.*)$', bygroups(Text, Comment)),
- (r'".*?"', String.Double),
- (r"'.*?'", String.Single),
- # If made more specific, make sure you still allow expansions
- # like %~$VAR:zlt
- (r'%%?[~$:\w]+%?', Name.Variable),
- (r'::.*', Comment), # Technically :: only works at BOL
- (r'(set)(\s+)(\w+)', bygroups(Keyword, Text, Name.Variable)),
- (r'(call)(\s+)(:\w+)', bygroups(Keyword, Text, Name.Label)),
- (r'(goto)(\s+)(\w+)', bygroups(Keyword, Text, Name.Label)),
- (r'\b(set|call|echo|on|off|endlocal|for|do|goto|if|pause|'
- r'setlocal|shift|errorlevel|exist|defined|cmdextversion|'
- r'errorlevel|else|cd|md|del|deltree|cls|choice)\b', Keyword),
- (r'\b(equ|neq|lss|leq|gtr|geq)\b', Operator),
- include('basic'),
- (r'.', Text),
+ 'root': _make_begin_state(False),
+ 'follow': _make_follow_state(False),
+ 'arithmetic': _make_arithmetic_state(False),
+ 'call': _make_call_state(False),
+ 'label': _make_label_state(False),
+ 'redirect': _make_redirect_state(False),
+ 'root/compound': _make_begin_state(True),
+ 'follow/compound': _make_follow_state(True),
+ 'arithmetic/compound': _make_arithmetic_state(True),
+ 'call/compound': _make_call_state(True),
+ 'label/compound': _make_label_state(True),
+ 'redirect/compound': _make_redirect_state(True),
+ 'variable-or-escape': [
+ (_variable, Name.Variable),
+ (r'%%%%|\^[%s]?(\^!|[\w\W])' % _nl, String.Escape)
],
- 'echo': [
- # Escapes only valid within echo args?
- (r'\^\^|\^<|\^>|\^\|', String.Escape),
- (r'\n', Text, '#pop'),
- include('basic'),
- (r'[^\'"^]+', Text),
+ 'string': [
+ (r'"', String.Double, '#pop'),
+ (_variable, Name.Variable),
+ (r'\^!|%%', String.Escape),
+ (r'[^"%%^%s]+|[%%^]' % _nl, String.Double),
+ default('#pop')
],
- 'basic': [
- (r'".*?"', String.Double),
- (r"'.*?'", String.Single),
- (r'`.*?`', String.Backtick),
- (r'-?\d+', Number),
- (r',', Punctuation),
- (r'=', Operator),
- (r'/\S+', Name),
- (r':\w+', Name.Label),
- (r'\w:\w+', Text),
- (r'([<>|])(\s*)(\w+)', bygroups(Punctuation, Text, Name)),
+ 'sqstring': [
+ include('variable-or-escape'),
+ (r'[^%]+|%', String.Single)
+ ],
+ 'bqstring': [
+ include('variable-or-escape'),
+ (r'[^%]+|%', String.Backtick)
+ ],
+ 'text': [
+ (r'"', String.Double, 'string'),
+ include('variable-or-escape'),
+ (r'[^"%%^%s%s%s\d)]+|.' % (_nl, _punct, _ws), Text)
+ ],
+ 'variable': [
+ (r'"', String.Double, 'string'),
+ include('variable-or-escape'),
+ (r'[^"%%^%s]+|.' % _nl, Name.Variable)
+ ],
+ 'for': [
+ (r'(%s)(in)(%s)(\()' % (_space, _space),
+ bygroups(using(this, state='text'), Keyword,
+ using(this, state='text'), Punctuation), '#pop'),
+ include('follow')
+ ],
+ 'for2': [
+ (r'\)', Punctuation),
+ (r'(%s)(do%s)' % (_space, _token_terminator),
+ bygroups(using(this, state='text'), Keyword), '#pop'),
+ (r'[%s]+' % _nl, Text),
+ include('follow')
+ ],
+ 'for/f': [
+ (r'(")((?:%s|[^"])*?")([%s%s]*)(\))' % (_variable, _nl, _ws),
+ bygroups(String.Double, using(this, state='string'), Text,
+ Punctuation)),
+ (r'"', String.Double, ('#pop', 'for2', 'string')),
+ (r"('(?:%s|[\w\W])*?')([%s%s]*)(\))" % (_variable, _nl, _ws),
+ bygroups(using(this, state='sqstring'), Text, Punctuation)),
+ (r'(`(?:%s|[\w\W])*?`)([%s%s]*)(\))' % (_variable, _nl, _ws),
+ bygroups(using(this, state='bqstring'), Text, Punctuation)),
+ include('for2')
+ ],
+ 'for/l': [
+ (r'-?\d+', Number.Integer),
+ include('for2')
],
+ 'if': [
+ (r'((?:cmdextversion|errorlevel)%s)(%s)(\d+)' %
+ (_token_terminator, _space),
+ bygroups(Keyword, using(this, state='text'),
+ Number.Integer), '#pop'),
+ (r'(defined%s)(%s)(%s)' % (_token_terminator, _space, _stoken),
+ bygroups(Keyword, using(this, state='text'),
+ using(this, state='variable')), '#pop'),
+ (r'(exist%s)(%s%s)' % (_token_terminator, _space, _stoken),
+ bygroups(Keyword, using(this, state='text')), '#pop'),
+ (r'(%s%s?)(==)(%s?%s)' % (_stoken, _space, _space, _stoken),
+ bygroups(using(this, state='text'), Operator,
+ using(this, state='text')), '#pop'),
+ (r'(%s%s)(%s)(%s%s)' % (_number, _space, _opword, _space, _number),
+ bygroups(using(this, state='arithmetic'), Operator.Word,
+ using(this, state='arithmetic')), '#pop'),
+ (r'(%s%s)(%s)(%s%s)' % (_stoken, _space, _opword, _space, _stoken),
+ bygroups(using(this, state='text'), Operator.Word,
+ using(this, state='text')), '#pop')
+ ],
+ '(?': [
+ (_space, using(this, state='text')),
+ (r'\(', Punctuation, ('#pop', 'else?', 'root/compound')),
+ default('#pop')
+ ],
+ 'else?': [
+ (_space, using(this, state='text')),
+ (r'else%s' % _token_terminator, Keyword, '#pop'),
+ default('#pop')
+ ]
}
+class MSDOSSessionLexer(ShellSessionBaseLexer):
+ """
+ Lexer for simplistic MSDOS sessions.
+
+ .. versionadded:: 2.1
+ """
+
+ name = 'MSDOS Session'
+ aliases = ['doscon']
+ filenames = []
+ mimetypes = []
+
+ _innerLexerCls = BatchLexer
+ _ps1rgx = r'^([^>]+>)(.*\n?)'
+ _ps2 = 'More? '
+
+
class TcshLexer(RegexLexer):
"""
Lexer for tcsh scripts.
- *New in Pygments 0.10.*
+ .. versionadded:: 0.10
"""
name = 'Tcsh'
@@ -271,7 +527,7 @@ class TcshLexer(RegexLexer):
'root': [
include('basic'),
(r'\$\(', Keyword, 'paren'),
- (r'\${#?', Keyword, 'curly'),
+ (r'\$\{#?', Keyword, 'curly'),
(r'`', String.Backtick, 'backticks'),
include('data'),
],
@@ -289,24 +545,25 @@ class TcshLexer(RegexLexer):
r'umask|unalias|uncomplete|unhash|universe|unlimit|unset|unsetenv|'
r'ver|wait|warp|watchlog|where|which)\s*\b',
Name.Builtin),
- (r'#.*\n', Comment),
+ (r'#.*', Comment),
(r'\\[\w\W]', String.Escape),
(r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
(r'[\[\]{}()=]+', Operator),
(r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
+ (r';', Punctuation),
],
'data': [
(r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
(r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
(r'\s+', Text),
- (r'[^=\s\[\]{}()$"\'`\\]+', Text),
+ (r'[^=\s\[\]{}()$"\'`\\;#]+', Text),
(r'\d+(?= |\Z)', Number),
(r'\$#?(\w+|.)', Name.Variable),
],
'curly': [
- (r'}', Keyword, '#pop'),
+ (r'\}', Keyword, '#pop'),
(r':-', Keyword),
- (r'[a-zA-Z0-9_]+', Name.Variable),
+ (r'\w+', Name.Variable),
(r'[^}:"\'`$]+', Punctuation),
(r':', Punctuation),
include('root'),
@@ -321,16 +578,32 @@ class TcshLexer(RegexLexer):
],
}
+class TcshSessionLexer(ShellSessionBaseLexer):
+ """
+ Lexer for Tcsh sessions.
+
+ .. versionadded:: 2.1
+ """
+
+ name = 'Tcsh Session'
+ aliases = ['tcshcon']
+ filenames = []
+ mimetypes = []
+
+ _innerLexerCls = TcshLexer
+ _ps1rgx = r'^([^>]+>)(.*\n?)'
+ _ps2 = '? '
+
class PowerShellLexer(RegexLexer):
"""
For Windows PowerShell code.
- *New in Pygments 1.5.*
+ .. versionadded:: 1.5
"""
name = 'PowerShell'
- aliases = ['powershell', 'posh', 'ps1']
- filenames = ['*.ps1']
+ aliases = ['powershell', 'posh', 'ps1', 'psm1']
+ filenames = ['*.ps1','*.psm1']
mimetypes = ['text/x-powershell']
flags = re.DOTALL | re.IGNORECASE | re.MULTILINE
@@ -342,7 +615,7 @@ class PowerShellLexer(RegexLexer):
'dynamicparam do default continue cmdletbinding break begin alias \\? '
'% #script #private #local #global mandatory parametersetname position '
'valuefrompipeline valuefrompipelinebypropertyname '
- 'valuefromremainingarguments helpmessage try catch').split()
+ 'valuefromremainingarguments helpmessage try catch throw').split()
operators = (
'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle '
@@ -368,26 +641,33 @@ class PowerShellLexer(RegexLexer):
tokens = {
'root': [
+ # we need to count pairs of parentheses for correct highlight
+ # of '$(...)' blocks in strings
+ (r'\(', Punctuation, 'child'),
(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', String.Heredoc, 'heredoc-double'),
(r"@'\n.*?\n'@", String.Heredoc),
# escaped syntax
(r'`[\'"$@-]', Punctuation),
(r'"', String.Double, 'string'),
(r"'([^']|'')*'", String.Single),
- (r'(\$|@@|@)((global|script|private|env):)?[a-z0-9_]+',
+ (r'(\$|@@|@)((global|script|private|env):)?\w+',
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'(%s)-[a-z_]\w*\b' % '|'.join(verbs), Name.Builtin),
+ (r'\[[a-z_\[][\w. `,\[\]]*\]', Name.Constant), # .net [type]s
+ (r'-[a-z_]\w*', Name),
(r'\w+', Name),
- (r'[.,{}\[\]$()=+*/\\&%!~?^`|<>-]', Punctuation),
+ (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation),
+ ],
+ 'child': [
+ (r'\)', Punctuation, '#pop'),
+ include('root'),
],
'multline': [
(r'[^#&.]+', Comment.Multiline),
@@ -396,15 +676,107 @@ class PowerShellLexer(RegexLexer):
(r'[#&.]', Comment.Multiline),
],
'string': [
+ (r"`[0abfnrtv'\"$`]", String.Escape),
(r'[^$`"]+', String.Double),
- (r'\$\(', String.Interpol, 'interpol'),
- (r'`"|""', String.Double),
+ (r'\$\(', Punctuation, 'child'),
+ (r'""', String.Double),
(r'[`$]', String.Double),
(r'"', String.Double, '#pop'),
],
- 'interpol': [
- (r'[^$)]+', String.Interpol),
- (r'\$\(', String.Interpol, '#push'),
- (r'\)', String.Interpol, '#pop'),
+ 'heredoc-double': [
+ (r'\n"@', String.Heredoc, '#pop'),
+ (r'\$\(', Punctuation, 'child'),
+ (r'[^@\n]+"]', String.Heredoc),
+ (r".", String.Heredoc),
]
}
+
+
+class FishShellLexer(RegexLexer):
+ """
+ Lexer for Fish shell scripts.
+
+ .. versionadded:: 2.1
+ """
+
+ name = 'Fish'
+ aliases = ['fish', 'fishshell']
+ filenames = ['*.fish', '*.load']
+ mimetypes = ['application/x-fish']
+
+ tokens = {
+ 'root': [
+ include('basic'),
+ include('data'),
+ include('interp'),
+ ],
+ 'interp': [
+ (r'\$\(\(', Keyword, 'math'),
+ (r'\(', Keyword, 'paren'),
+ (r'\$#?(\w+|.)', Name.Variable),
+ ],
+ 'basic': [
+ (r'\b(begin|end|if|else|while|break|for|in|return|function|block|'
+ r'case|continue|switch|not|and|or|set|echo|exit|pwd|true|false|'
+ r'cd|count|test)(\s*)\b',
+ bygroups(Keyword, Text)),
+ (r'\b(alias|bg|bind|breakpoint|builtin|command|commandline|'
+ r'complete|contains|dirh|dirs|emit|eval|exec|fg|fish|fish_config|'
+ r'fish_indent|fish_pager|fish_prompt|fish_right_prompt|'
+ r'fish_update_completions|fishd|funced|funcsave|functions|help|'
+ r'history|isatty|jobs|math|mimedb|nextd|open|popd|prevd|psub|'
+ r'pushd|random|read|set_color|source|status|trap|type|ulimit|'
+ r'umask|vared|fc|getopts|hash|kill|printf|time|wait)\s*\b(?!\.)',
+ Name.Builtin),
+ (r'#.*\n', Comment),
+ (r'\\[\w\W]', String.Escape),
+ (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
+ (r'[\[\]()=]', Operator),
+ (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
+ ],
+ 'data': [
+ (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"', String.Double),
+ (r'"', String.Double, 'string'),
+ (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
+ (r"(?s)'.*?'", String.Single),
+ (r';', Punctuation),
+ (r'&|\||\^|<|>', Operator),
+ (r'\s+', Text),
+ (r'\d+(?= |\Z)', Number),
+ (r'[^=\s\[\]{}()$"\'`\\<&|;]+', Text),
+ ],
+ 'string': [
+ (r'"', String.Double, '#pop'),
+ (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+', String.Double),
+ include('interp'),
+ ],
+ 'paren': [
+ (r'\)', Keyword, '#pop'),
+ include('root'),
+ ],
+ 'math': [
+ (r'\)\)', Keyword, '#pop'),
+ (r'[-+*/%^|&]|\*\*|\|\|', Operator),
+ (r'\d+#\d+', Number),
+ (r'\d+#(?! )', Number),
+ (r'\d+', Number),
+ include('root'),
+ ],
+ }
+
+
+class PowerShellSessionLexer(ShellSessionBaseLexer):
+ """
+ Lexer for simplistic Windows PowerShell sessions.
+
+ .. versionadded:: 2.1
+ """
+
+ name = 'PowerShell Session'
+ aliases = ['ps1con']
+ filenames = []
+ mimetypes = []
+
+ _innerLexerCls = PowerShellLexer
+ _ps1rgx = r'^(PS [^>]+> )(.*\n?)'
+ _ps2 = '>> '