eric6/ThirdParty/Pygments/pygments/lexers/shell.py

changeset 7547
21b0534faebc
parent 6942
2602857055c5
child 7701
25f42e208e08
equal deleted inserted replaced
7546:bf5f777260a6 7547:21b0534faebc
3 pygments.lexers.shell 3 pygments.lexers.shell
4 ~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for various shells. 6 Lexers for various shells.
7 7
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import re 12 import re
13 13
17 Text, Comment, Operator, Keyword, Name, String, Number, Generic 17 Text, Comment, Operator, Keyword, Name, String, Number, Generic
18 from pygments.util import shebang_matches 18 from pygments.util import shebang_matches
19 19
20 20
21 __all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer', 21 __all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer',
22 'MSDOSSessionLexer', 'PowerShellLexer', 22 'SlurmBashLexer', 'MSDOSSessionLexer', 'PowerShellLexer',
23 'PowerShellSessionLexer', 'TcshSessionLexer', 'FishShellLexer'] 23 'PowerShellSessionLexer', 'TcshSessionLexer', 'FishShellLexer']
24 24
25 line_re = re.compile('.*?\n') 25 line_re = re.compile('.*?\n')
26 26
27 27
36 aliases = ['bash', 'sh', 'ksh', 'zsh', 'shell'] 36 aliases = ['bash', 'sh', 'ksh', 'zsh', 'shell']
37 filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass', 37 filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass',
38 '*.exheres-0', '*.exlib', '*.zsh', 38 '*.exheres-0', '*.exlib', '*.zsh',
39 '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'zshrc', '.zshrc', 39 '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'zshrc', '.zshrc',
40 'PKGBUILD'] 40 'PKGBUILD']
41 mimetypes = ['application/x-sh', 'application/x-shellscript'] 41 mimetypes = ['application/x-sh', 'application/x-shellscript', 'text/x-shellscript']
42 42
43 tokens = { 43 tokens = {
44 'root': [ 44 'root': [
45 include('basic'), 45 include('basic'),
46 (r'`', String.Backtick, 'backticks'), 46 (r'`', String.Backtick, 'backticks'),
74 (r'<<<', Operator), # here-string 74 (r'<<<', Operator), # here-string
75 (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String), 75 (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
76 (r'&&|\|\|', Operator), 76 (r'&&|\|\|', Operator),
77 ], 77 ],
78 'data': [ 78 'data': [
79 (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"', String.Double), 79 (r'(?s)\$?"(\\.|[^"\\$])*"', String.Double),
80 (r'"', String.Double, 'string'), 80 (r'"', String.Double, 'string'),
81 (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single), 81 (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
82 (r"(?s)'.*?'", String.Single), 82 (r"(?s)'.*?'", String.Single),
83 (r';', Punctuation), 83 (r';', Punctuation),
84 (r'&', Punctuation), 84 (r'&', Punctuation),
124 return 1 124 return 1
125 if text.startswith('$ '): 125 if text.startswith('$ '):
126 return 0.2 126 return 0.2
127 127
128 128
129 class SlurmBashLexer(BashLexer):
130 """
131 Lexer for (ba|k|z|)sh Slurm scripts.
132
133 .. versionadded:: 2.4
134 """
135
136 name = 'Slurm'
137 aliases = ['slurm', 'sbatch']
138 filenames = ['*.sl']
139 mimetypes = []
140 EXTRA_KEYWORDS = {'srun'}
141
142 def get_tokens_unprocessed(self, text):
143 for index, token, value in BashLexer.get_tokens_unprocessed(self, text):
144 if token is Text and value in self.EXTRA_KEYWORDS:
145 yield index, Name.Builtin, value
146 elif token is Comment.Single and 'SBATCH' in value:
147 yield index, Keyword.Pseudo, value
148 else:
149 yield index, token, value
150
129 class ShellSessionBaseLexer(Lexer): 151 class ShellSessionBaseLexer(Lexer):
130 """ 152 """
131 Base lexer for simplistic shell sessions. 153 Base lexer for simplistic shell sessions.
132 154
133 .. versionadded:: 2.1 155 .. versionadded:: 2.1
134 """ 156 """
157
158 _venv = re.compile(r'^(\([^)]*\))(\s*)')
159
135 def get_tokens_unprocessed(self, text): 160 def get_tokens_unprocessed(self, text):
136 innerlexer = self._innerLexerCls(**self.options) 161 innerlexer = self._innerLexerCls(**self.options)
137 162
138 pos = 0 163 pos = 0
139 curcode = '' 164 curcode = ''
140 insertions = [] 165 insertions = []
141 backslash_continuation = False 166 backslash_continuation = False
142 167
143 for match in line_re.finditer(text): 168 for match in line_re.finditer(text):
144 line = match.group() 169 line = match.group()
145 m = re.match(self._ps1rgx, line)
146 if backslash_continuation: 170 if backslash_continuation:
147 curcode += line 171 curcode += line
148 backslash_continuation = curcode.endswith('\\\n') 172 backslash_continuation = curcode.endswith('\\\n')
149 elif m: 173 continue
174
175 venv_match = self._venv.match(line)
176 if venv_match:
177 venv = venv_match.group(1)
178 venv_whitespace = venv_match.group(2)
179 insertions.append((len(curcode),
180 [(0, Generic.Prompt.VirtualEnv, venv)]))
181 if venv_whitespace:
182 insertions.append((len(curcode),
183 [(0, Text, venv_whitespace)]))
184 line = line[venv_match.end():]
185
186 m = self._ps1rgx.match(line)
187 if m:
150 # To support output lexers (say diff output), the output 188 # To support output lexers (say diff output), the output
151 # needs to be broken by prompts whenever the output lexer 189 # needs to be broken by prompts whenever the output lexer
152 # changes. 190 # changes.
153 if not insertions: 191 if not insertions:
154 pos = match.start() 192 pos = match.start()
187 aliases = ['console', 'shell-session'] 225 aliases = ['console', 'shell-session']
188 filenames = ['*.sh-session', '*.shell-session'] 226 filenames = ['*.sh-session', '*.shell-session']
189 mimetypes = ['application/x-shell-session', 'application/x-sh-session'] 227 mimetypes = ['application/x-shell-session', 'application/x-sh-session']
190 228
191 _innerLexerCls = BashLexer 229 _innerLexerCls = BashLexer
192 _ps1rgx = \ 230 _ps1rgx = re.compile(
193 r'^((?:(?:\[.*?\])|(?:\(\S+\))?(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \ 231 r'^((?:(?:\[.*?\])|(?:\(\S+\))?(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \
194 r'?|\[\S+[@:][^\n]+\].+))\s*[$#%])(.*\n?)' 232 r'?|\[\S+[@:][^\n]+\].+))\s*[$#%])(.*\n?)')
195 _ps2 = '>' 233 _ps2 = '>'
196 234
197 235
198 class BatchLexer(RegexLexer): 236 class BatchLexer(RegexLexer):
199 """ 237 """
516 aliases = ['doscon'] 554 aliases = ['doscon']
517 filenames = [] 555 filenames = []
518 mimetypes = [] 556 mimetypes = []
519 557
520 _innerLexerCls = BatchLexer 558 _innerLexerCls = BatchLexer
521 _ps1rgx = r'^([^>]+>)(.*\n?)' 559 _ps1rgx = re.compile(r'^([^>]*>)(.*\n?)')
522 _ps2 = 'More? ' 560 _ps2 = 'More? '
523 561
524 562
525 class TcshLexer(RegexLexer): 563 class TcshLexer(RegexLexer):
526 """ 564 """
601 aliases = ['tcshcon'] 639 aliases = ['tcshcon']
602 filenames = [] 640 filenames = []
603 mimetypes = [] 641 mimetypes = []
604 642
605 _innerLexerCls = TcshLexer 643 _innerLexerCls = TcshLexer
606 _ps1rgx = r'^([^>]+>)(.*\n?)' 644 _ps1rgx = re.compile(r'^([^>]+>)(.*\n?)')
607 _ps2 = '? ' 645 _ps2 = '? '
608 646
609 647
610 class PowerShellLexer(RegexLexer): 648 class PowerShellLexer(RegexLexer):
611 """ 649 """
648 'grant get format foreach find export expand exit enter enable edit ' 686 'grant get format foreach find export expand exit enter enable edit '
649 'dismount disconnect disable deny debug cxnew copy convertto ' 687 'dismount disconnect disable deny debug cxnew copy convertto '
650 'convertfrom convert connect confirm compress complete compare close ' 688 'convertfrom convert connect confirm compress complete compare close '
651 'clear checkpoint block backup assert approve aggregate add').split() 689 'clear checkpoint block backup assert approve aggregate add').split()
652 690
653 aliases = ( 691 aliases_ = (
654 'ac asnp cat cd cfs chdir clc clear clhy cli clp cls clv cnsn ' 692 'ac asnp cat cd cfs chdir clc clear clhy cli clp cls clv cnsn '
655 'compare copy cp cpi cpp curl cvpa dbp del diff dir dnsn ebp echo epal ' 693 'compare copy cp cpi cpp curl cvpa dbp del diff dir dnsn ebp echo epal '
656 'epcsv epsn erase etsn exsn fc fhx fl foreach ft fw gal gbp gc gci gcm ' 694 'epcsv epsn erase etsn exsn fc fhx fl foreach ft fw gal gbp gc gci gcm '
657 'gcs gdr ghy gi gjb gl gm gmo gp gps gpv group gsn gsnp gsv gu gv gwmi ' 695 'gcs gdr ghy gi gjb gl gm gmo gp gps gpv group gsn gsnp gsv gu gv gwmi '
658 'h history icm iex ihy ii ipal ipcsv ipmo ipsn irm ise iwmi iwr kill lp ' 696 'h history icm iex ihy ii ipal ipcsv ipmo ipsn irm ise iwmi iwr kill lp '
686 (r'(\$|@@|@)((global|script|private|env):)?\w+', 724 (r'(\$|@@|@)((global|script|private|env):)?\w+',
687 Name.Variable), 725 Name.Variable),
688 (r'(%s)\b' % '|'.join(keywords), Keyword), 726 (r'(%s)\b' % '|'.join(keywords), Keyword),
689 (r'-(%s)\b' % '|'.join(operators), Operator), 727 (r'-(%s)\b' % '|'.join(operators), Operator),
690 (r'(%s)-[a-z_]\w*\b' % '|'.join(verbs), Name.Builtin), 728 (r'(%s)-[a-z_]\w*\b' % '|'.join(verbs), Name.Builtin),
691 (r'(%s)\s' % '|'.join(aliases), Name.Builtin), 729 (r'(%s)\s' % '|'.join(aliases_), Name.Builtin),
692 (r'\[[a-z_\[][\w. `,\[\]]*\]', Name.Constant), # .net [type]s 730 (r'\[[a-z_\[][\w. `,\[\]]*\]', Name.Constant), # .net [type]s
693 (r'-[a-z_]\w*', Name), 731 (r'-[a-z_]\w*', Name),
694 (r'\w+', Name), 732 (r'\w+', Name),
695 (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation), 733 (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation),
696 ], 734 ],
732 aliases = ['ps1con'] 770 aliases = ['ps1con']
733 filenames = [] 771 filenames = []
734 mimetypes = [] 772 mimetypes = []
735 773
736 _innerLexerCls = PowerShellLexer 774 _innerLexerCls = PowerShellLexer
737 _ps1rgx = r'^(PS [^>]+> )(.*\n?)' 775 _ps1rgx = re.compile(r'^(PS [^>]+> )(.*\n?)')
738 _ps2 = '>> ' 776 _ps2 = '>> '
739 777
740 778
741 class FishShellLexer(RegexLexer): 779 class FishShellLexer(RegexLexer):
742 """ 780 """

eric ide

mercurial