diff options
Diffstat (limited to 'pygments/lexers/shell.py')
-rw-r--r-- | pygments/lexers/shell.py | 138 |
1 files changed, 86 insertions, 52 deletions
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index 8d07d24c..0e481208 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -17,8 +17,9 @@ from pygments.token import Punctuation, \ from pygments.util import shebang_matches -__all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer', - 'PowerShellLexer', 'ShellSessionLexer', 'FishShellLexer'] +__all__ = ['BashLexer', 'ShellSessionBaseLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer', + 'MSDOSSessionLexer', 'PowerShellLexer', 'ShellSessionLexer', + 'PowerShellSessionLexer', 'TcshSessionLexer', 'FishShellLexer'] line_re = re.compile('.*?\n') @@ -122,20 +123,14 @@ class BashLexer(RegexLexer): return 0.2 -class BashSessionLexer(Lexer): +class ShellSessionBaseLexer(Lexer): """ - Lexer for simplistic shell sessions. + Base lexer for simplistic shell sessions. - .. versionadded:: 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 = '' @@ -143,8 +138,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 @@ -155,13 +149,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 @@ -169,11 +163,30 @@ 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 simplistic shell sessions. + + .. versionadded:: 1.1 + """ + + name = 'Bash Session' + aliases = ['console'] + filenames = ['*.sh-session'] + mimetypes = ['application/x-shell-session'] + + _innerLexerCls = BashLexer + _ps1rgx = \ + r'^((?:\(\S+\))?(?:|sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \ + r'?|\[\S+[@:][^\n]+\].+)[$#%])(.*\n?)' + _ps2 = '>' + + +class ShellSessionLexer(ShellSessionBaseLexer): """ Lexer for shell sessions that works with different command prompts @@ -185,38 +198,9 @@ class ShellSessionLexer(Lexer): 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+@[^$#%]+\]?\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() - - 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+@[^$#%]+\]?\s*)[$#%])(.*\n?)', + _ps2 = '>' class BatchLexer(RegexLexer): @@ -275,6 +259,23 @@ class BatchLexer(RegexLexer): } +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. @@ -342,6 +343,22 @@ 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): """ @@ -511,3 +528,20 @@ class FishShellLexer(RegexLexer): 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 = '>> ' |