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 """ |
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 ], |