ThirdParty/Pygments/pygments/lexers/shell.py

changeset 4172
4f20dba37ab6
parent 3145
a9de05d4a22f
child 4697
c2e9bf425554
equal deleted inserted replaced
4170:8bc578136279 4172:4f20dba37ab6
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-2013 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11
12 from __future__ import unicode_literals
13 11
14 import re 12 import re
15 13
16 from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, include 14 from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, include
17 from pygments.token import Punctuation, \ 15 from pygments.token import Punctuation, \
27 25
28 class BashLexer(RegexLexer): 26 class BashLexer(RegexLexer):
29 """ 27 """
30 Lexer for (ba|k|)sh shell scripts. 28 Lexer for (ba|k|)sh shell scripts.
31 29
32 *New in Pygments 0.6.* 30 .. versionadded:: 0.6
33 """ 31 """
34 32
35 name = 'Bash' 33 name = 'Bash'
36 aliases = ['bash', 'sh', 'ksh'] 34 aliases = ['bash', 'sh', 'ksh', 'shell']
37 filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass', 35 filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass',
38 '.bashrc', 'bashrc', '.bash_*', 'bash_*'] 36 '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'PKGBUILD']
39 mimetypes = ['application/x-sh', 'application/x-shellscript'] 37 mimetypes = ['application/x-sh', 'application/x-shellscript']
40 38
41 tokens = { 39 tokens = {
42 'root': [ 40 'root': [
43 include('basic'), 41 include('basic'),
42 (r'`', String.Backtick, 'backticks'),
43 include('data'),
44 include('interp'),
45 ],
46 'interp': [
44 (r'\$\(\(', Keyword, 'math'), 47 (r'\$\(\(', Keyword, 'math'),
45 (r'\$\(', Keyword, 'paren'), 48 (r'\$\(', Keyword, 'paren'),
46 (r'\${#?', Keyword, 'curly'), 49 (r'\$\{#?', String.Interpol, 'curly'),
47 (r'`', String.Backtick, 'backticks'), 50 (r'\$#?(\w+|.)', Name.Variable),
48 include('data'),
49 ], 51 ],
50 'basic': [ 52 'basic': [
51 (r'\b(if|fi|else|while|do|done|for|then|return|function|case|' 53 (r'\b(if|fi|else|while|do|done|for|then|return|function|case|'
52 r'select|continue|until|esac|elif)\s*\b', 54 r'select|continue|until|esac|elif)(\s*)\b',
53 Keyword), 55 bygroups(Keyword, Text)),
54 (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|' 56 (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|'
55 r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|' 57 r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|'
56 r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|' 58 r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|'
57 r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|' 59 r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|'
58 r'shopt|source|suspend|test|time|times|trap|true|type|typeset|' 60 r'shopt|source|suspend|test|time|times|trap|true|type|typeset|'
65 (r'<<<', Operator), # here-string 67 (r'<<<', Operator), # here-string
66 (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String), 68 (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
67 (r'&&|\|\|', Operator), 69 (r'&&|\|\|', Operator),
68 ], 70 ],
69 'data': [ 71 'data': [
70 (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double), 72 (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"', String.Double),
71 (r"(?s)\$?'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single), 73 (r'"', String.Double, 'string'),
72 (r';', Text), 74 (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
75 (r"(?s)'.*?'", String.Single),
76 (r';', Punctuation),
77 (r'&', Punctuation),
78 (r'\|', Punctuation),
73 (r'\s+', Text), 79 (r'\s+', Text),
74 (r'[^=\s\[\]{}()$"\'`\\<]+', Text),
75 (r'\d+(?= |\Z)', Number), 80 (r'\d+(?= |\Z)', Number),
76 (r'\$#?(\w+|.)', Name.Variable), 81 (r'[^=\s\[\]{}()$"\'`\\<&|;]+', Text),
77 (r'<', Text), 82 (r'<', Text),
78 ], 83 ],
84 'string': [
85 (r'"', String.Double, '#pop'),
86 (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+', String.Double),
87 include('interp'),
88 ],
79 'curly': [ 89 'curly': [
80 (r'}', Keyword, '#pop'), 90 (r'\}', String.Interpol, '#pop'),
81 (r':-', Keyword), 91 (r':-', Keyword),
82 (r'[a-zA-Z0-9_]+', Name.Variable), 92 (r'\w+', Name.Variable),
83 (r'[^}:"\'`$]+', Punctuation), 93 (r'[^}:"\'`$\\]+', Punctuation),
84 (r':', Punctuation), 94 (r':', Punctuation),
85 include('root'), 95 include('root'),
86 ], 96 ],
87 'paren': [ 97 'paren': [
88 (r'\)', Keyword, '#pop'), 98 (r'\)', Keyword, '#pop'),
89 include('root'), 99 include('root'),
90 ], 100 ],
91 'math': [ 101 'math': [
92 (r'\)\)', Keyword, '#pop'), 102 (r'\)\)', Keyword, '#pop'),
93 (r'[-+*/%^|&]|\*\*|\|\|', Operator), 103 (r'[-+*/%^|&]|\*\*|\|\|', Operator),
104 (r'\d+#\d+', Number),
105 (r'\d+#(?! )', Number),
94 (r'\d+', Number), 106 (r'\d+', Number),
95 include('root'), 107 include('root'),
96 ], 108 ],
97 'backticks': [ 109 'backticks': [
98 (r'`', String.Backtick, '#pop'), 110 (r'`', String.Backtick, '#pop'),
99 include('root'), 111 include('root'),
100 ], 112 ],
101 } 113 }
102 114
103 def analyse_text(text): 115 def analyse_text(text):
104 return shebang_matches(text, r'(ba|z|)sh') 116 if shebang_matches(text, r'(ba|z|)sh'):
117 return 1
118 if text.startswith('$ '):
119 return 0.2
105 120
106 121
107 class BashSessionLexer(Lexer): 122 class BashSessionLexer(Lexer):
108 """ 123 """
109 Lexer for simplistic shell sessions. 124 Lexer for simplistic shell sessions.
110 125
111 *New in Pygments 1.1.* 126 .. versionadded:: 1.1
112 """ 127 """
113 128
114 name = 'Bash Session' 129 name = 'Bash Session'
115 aliases = ['console'] 130 aliases = ['console']
116 filenames = ['*.sh-session'] 131 filenames = ['*.sh-session']
157 172
158 class ShellSessionLexer(Lexer): 173 class ShellSessionLexer(Lexer):
159 """ 174 """
160 Lexer for shell sessions that works with different command prompts 175 Lexer for shell sessions that works with different command prompts
161 176
162 *New in Pygments 1.6.* 177 .. versionadded:: 1.6
163 """ 178 """
164 179
165 name = 'Shell Session' 180 name = 'Shell Session'
166 aliases = ['shell-session'] 181 aliases = ['shell-session']
167 filenames = ['*.shell-session'] 182 filenames = ['*.shell-session']
174 curcode = '' 189 curcode = ''
175 insertions = [] 190 insertions = []
176 191
177 for match in line_re.finditer(text): 192 for match in line_re.finditer(text):
178 line = match.group() 193 line = match.group()
179 m = re.match(r'^((?:\[?\S+@[^$#%]+)[$#%])(.*\n?)', line) 194 m = re.match(r'^((?:\[?\S+@[^$#%]+\]?\s*)[$#%])(.*\n?)', line)
180 if m: 195 if m:
181 # To support output lexers (say diff output), the output 196 # To support output lexers (say diff output), the output
182 # needs to be broken by prompts whenever the output lexer 197 # needs to be broken by prompts whenever the output lexer
183 # changes. 198 # changes.
184 if not insertions: 199 if not insertions:
203 218
204 class BatchLexer(RegexLexer): 219 class BatchLexer(RegexLexer):
205 """ 220 """
206 Lexer for the DOS/Windows Batch file format. 221 Lexer for the DOS/Windows Batch file format.
207 222
208 *New in Pygments 0.7.* 223 .. versionadded:: 0.7
209 """ 224 """
210 name = 'Batchfile' 225 name = 'Batchfile'
211 aliases = ['bat'] 226 aliases = ['bat', 'batch', 'dosbatch', 'winbatch']
212 filenames = ['*.bat', '*.cmd'] 227 filenames = ['*.bat', '*.cmd']
213 mimetypes = ['application/x-dos-batch'] 228 mimetypes = ['application/x-dos-batch']
214 229
215 flags = re.MULTILINE | re.IGNORECASE 230 flags = re.MULTILINE | re.IGNORECASE
216 231
223 (r"'.*?'", String.Single), 238 (r"'.*?'", String.Single),
224 # If made more specific, make sure you still allow expansions 239 # If made more specific, make sure you still allow expansions
225 # like %~$VAR:zlt 240 # like %~$VAR:zlt
226 (r'%%?[~$:\w]+%?', Name.Variable), 241 (r'%%?[~$:\w]+%?', Name.Variable),
227 (r'::.*', Comment), # Technically :: only works at BOL 242 (r'::.*', Comment), # Technically :: only works at BOL
228 (r'(set)(\s+)(\w+)', bygroups(Keyword, Text, Name.Variable)), 243 (r'\b(set)(\s+)(\w+)', bygroups(Keyword, Text, Name.Variable)),
229 (r'(call)(\s+)(:\w+)', bygroups(Keyword, Text, Name.Label)), 244 (r'\b(call)(\s+)(:\w+)', bygroups(Keyword, Text, Name.Label)),
230 (r'(goto)(\s+)(\w+)', bygroups(Keyword, Text, Name.Label)), 245 (r'\b(goto)(\s+)(\w+)', bygroups(Keyword, Text, Name.Label)),
231 (r'\b(set|call|echo|on|off|endlocal|for|do|goto|if|pause|' 246 (r'\b(set|call|echo|on|off|endlocal|for|do|goto|if|pause|'
232 r'setlocal|shift|errorlevel|exist|defined|cmdextversion|' 247 r'setlocal|shift|errorlevel|exist|defined|cmdextversion|'
233 r'errorlevel|else|cd|md|del|deltree|cls|choice)\b', Keyword), 248 r'errorlevel|else|cd|md|del|deltree|cls|choice)\b', Keyword),
234 (r'\b(equ|neq|lss|leq|gtr|geq)\b', Operator), 249 (r'\b(equ|neq|lss|leq|gtr|geq)\b', Operator),
235 include('basic'), 250 include('basic'),
259 274
260 class TcshLexer(RegexLexer): 275 class TcshLexer(RegexLexer):
261 """ 276 """
262 Lexer for tcsh scripts. 277 Lexer for tcsh scripts.
263 278
264 *New in Pygments 0.10.* 279 .. versionadded:: 0.10
265 """ 280 """
266 281
267 name = 'Tcsh' 282 name = 'Tcsh'
268 aliases = ['tcsh', 'csh'] 283 aliases = ['tcsh', 'csh']
269 filenames = ['*.tcsh', '*.csh'] 284 filenames = ['*.tcsh', '*.csh']
271 286
272 tokens = { 287 tokens = {
273 'root': [ 288 'root': [
274 include('basic'), 289 include('basic'),
275 (r'\$\(', Keyword, 'paren'), 290 (r'\$\(', Keyword, 'paren'),
276 (r'\${#?', Keyword, 'curly'), 291 (r'\$\{#?', Keyword, 'curly'),
277 (r'`', String.Backtick, 'backticks'), 292 (r'`', String.Backtick, 'backticks'),
278 include('data'), 293 include('data'),
279 ], 294 ],
280 'basic': [ 295 'basic': [
281 (r'\b(if|endif|else|while|then|foreach|case|default|' 296 (r'\b(if|endif|else|while|then|foreach|case|default|'
289 r'set|shift|sched|setenv|setpath|settc|setty|setxvers|shift|' 304 r'set|shift|sched|setenv|setpath|settc|setty|setxvers|shift|'
290 r'source|stop|suspend|source|suspend|telltc|time|' 305 r'source|stop|suspend|source|suspend|telltc|time|'
291 r'umask|unalias|uncomplete|unhash|universe|unlimit|unset|unsetenv|' 306 r'umask|unalias|uncomplete|unhash|universe|unlimit|unset|unsetenv|'
292 r'ver|wait|warp|watchlog|where|which)\s*\b', 307 r'ver|wait|warp|watchlog|where|which)\s*\b',
293 Name.Builtin), 308 Name.Builtin),
294 (r'#.*\n', Comment), 309 (r'#.*', Comment),
295 (r'\\[\w\W]', String.Escape), 310 (r'\\[\w\W]', String.Escape),
296 (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)), 311 (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
297 (r'[\[\]{}()=]+', Operator), 312 (r'[\[\]{}()=]+', Operator),
298 (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String), 313 (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
314 (r';', Punctuation),
299 ], 315 ],
300 'data': [ 316 'data': [
301 (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double), 317 (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
302 (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single), 318 (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
303 (r'\s+', Text), 319 (r'\s+', Text),
304 (r'[^=\s\[\]{}()$"\'`\\]+', Text), 320 (r'[^=\s\[\]{}()$"\'`\\;#]+', Text),
305 (r'\d+(?= |\Z)', Number), 321 (r'\d+(?= |\Z)', Number),
306 (r'\$#?(\w+|.)', Name.Variable), 322 (r'\$#?(\w+|.)', Name.Variable),
307 ], 323 ],
308 'curly': [ 324 'curly': [
309 (r'}', Keyword, '#pop'), 325 (r'\}', Keyword, '#pop'),
310 (r':-', Keyword), 326 (r':-', Keyword),
311 (r'[a-zA-Z0-9_]+', Name.Variable), 327 (r'\w+', Name.Variable),
312 (r'[^}:"\'`$]+', Punctuation), 328 (r'[^}:"\'`$]+', Punctuation),
313 (r':', Punctuation), 329 (r':', Punctuation),
314 include('root'), 330 include('root'),
315 ], 331 ],
316 'paren': [ 332 'paren': [
326 342
327 class PowerShellLexer(RegexLexer): 343 class PowerShellLexer(RegexLexer):
328 """ 344 """
329 For Windows PowerShell code. 345 For Windows PowerShell code.
330 346
331 *New in Pygments 1.5.* 347 .. versionadded:: 1.5
332 """ 348 """
333 name = 'PowerShell' 349 name = 'PowerShell'
334 aliases = ['powershell', 'posh', 'ps1'] 350 aliases = ['powershell', 'posh', 'ps1', 'psm1']
335 filenames = ['*.ps1'] 351 filenames = ['*.ps1','*.psm1']
336 mimetypes = ['text/x-powershell'] 352 mimetypes = ['text/x-powershell']
337 353
338 flags = re.DOTALL | re.IGNORECASE | re.MULTILINE 354 flags = re.DOTALL | re.IGNORECASE | re.MULTILINE
339 355
340 keywords = ( 356 keywords = (
342 'validatecount until trap switch return ref process param parameter in ' 358 'validatecount until trap switch return ref process param parameter in '
343 'if global: function foreach for finally filter end elseif else ' 359 'if global: function foreach for finally filter end elseif else '
344 'dynamicparam do default continue cmdletbinding break begin alias \\? ' 360 'dynamicparam do default continue cmdletbinding break begin alias \\? '
345 '% #script #private #local #global mandatory parametersetname position ' 361 '% #script #private #local #global mandatory parametersetname position '
346 'valuefrompipeline valuefrompipelinebypropertyname ' 362 'valuefrompipeline valuefrompipelinebypropertyname '
347 'valuefromremainingarguments helpmessage try catch').split() 363 'valuefromremainingarguments helpmessage try catch throw').split()
348 364
349 operators = ( 365 operators = (
350 'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle ' 366 'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle '
351 'clike clt cmatch cne cnotcontains cnotlike cnotmatch contains ' 367 'clike clt cmatch cne cnotcontains cnotlike cnotmatch contains '
352 'creplace eq exact f file ge gt icontains ieq ige igt ile ilike ilt ' 368 'creplace eq exact f file ge gt icontains ieq ige igt ile ilike ilt '
368 'forwardhelptargetname functionality inputs link ' 384 'forwardhelptargetname functionality inputs link '
369 'notes outputs parameter remotehelprunspace role synopsis').split() 385 'notes outputs parameter remotehelprunspace role synopsis').split()
370 386
371 tokens = { 387 tokens = {
372 'root': [ 388 'root': [
389 # we need to count pairs of parentheses for correct highlight
390 # of '$(...)' blocks in strings
391 (r'\(', Punctuation, 'child'),
373 (r'\s+', Text), 392 (r'\s+', Text),
374 (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp), 393 (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp),
375 bygroups(Comment, String.Doc, Comment)), 394 bygroups(Comment, String.Doc, Comment)),
376 (r'#[^\n]*?$', Comment), 395 (r'#[^\n]*?$', Comment),
377 (r'(&lt;|<)#', Comment.Multiline, 'multline'), 396 (r'(&lt;|<)#', Comment.Multiline, 'multline'),
378 (r'@"\n.*?\n"@', String.Heredoc), 397 (r'@"\n', String.Heredoc, 'heredoc-double'),
379 (r"@'\n.*?\n'@", String.Heredoc), 398 (r"@'\n.*?\n'@", String.Heredoc),
380 # escaped syntax 399 # escaped syntax
381 (r'`[\'"$@-]', Punctuation), 400 (r'`[\'"$@-]', Punctuation),
382 (r'"', String.Double, 'string'), 401 (r'"', String.Double, 'string'),
383 (r"'([^']|'')*'", String.Single), 402 (r"'([^']|'')*'", String.Single),
384 (r'(\$|@@|@)((global|script|private|env):)?[a-z0-9_]+', 403 (r'(\$|@@|@)((global|script|private|env):)?\w+',
385 Name.Variable), 404 Name.Variable),
386 (r'(%s)\b' % '|'.join(keywords), Keyword), 405 (r'(%s)\b' % '|'.join(keywords), Keyword),
387 (r'-(%s)\b' % '|'.join(operators), Operator), 406 (r'-(%s)\b' % '|'.join(operators), Operator),
388 (r'(%s)-[a-z_][a-z0-9_]*\b' % '|'.join(verbs), Name.Builtin), 407 (r'(%s)-[a-z_]\w*\b' % '|'.join(verbs), Name.Builtin),
389 (r'\[[a-z_\[][a-z0-9_. `,\[\]]*\]', Name.Constant), # .net [type]s 408 (r'\[[a-z_\[][\w. `,\[\]]*\]', Name.Constant), # .net [type]s
390 (r'-[a-z_][a-z0-9_]*', Name), 409 (r'-[a-z_]\w*', Name),
391 (r'\w+', Name), 410 (r'\w+', Name),
392 (r'[.,{}\[\]$()=+*/\\&%!~?^`|<>-]', Punctuation), 411 (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation),
412 ],
413 'child': [
414 (r'\)', Punctuation, '#pop'),
415 include('root'),
393 ], 416 ],
394 'multline': [ 417 'multline': [
395 (r'[^#&.]+', Comment.Multiline), 418 (r'[^#&.]+', Comment.Multiline),
396 (r'#(>|&gt;)', Comment.Multiline, '#pop'), 419 (r'#(>|&gt;)', Comment.Multiline, '#pop'),
397 (r'\.(%s)' % '|'.join(commenthelp), String.Doc), 420 (r'\.(%s)' % '|'.join(commenthelp), String.Doc),
398 (r'[#&.]', Comment.Multiline), 421 (r'[#&.]', Comment.Multiline),
399 ], 422 ],
400 'string': [ 423 'string': [
424 (r"`[0abfnrtv'\"$`]", String.Escape),
401 (r'[^$`"]+', String.Double), 425 (r'[^$`"]+', String.Double),
402 (r'\$\(', String.Interpol, 'interpol'), 426 (r'\$\(', Punctuation, 'child'),
403 (r'`"|""', String.Double), 427 (r'""', String.Double),
404 (r'[`$]', String.Double), 428 (r'[`$]', String.Double),
405 (r'"', String.Double, '#pop'), 429 (r'"', String.Double, '#pop'),
406 ], 430 ],
407 'interpol': [ 431 'heredoc-double': [
408 (r'[^$)]+', String.Interpol), 432 (r'\n"@', String.Heredoc, '#pop'),
409 (r'\$\(', String.Interpol, '#push'), 433 (r'\$\(', Punctuation, 'child'),
410 (r'\)', String.Interpol, '#pop'), 434 (r'[^@\n]+"]', String.Heredoc),
435 (r".", String.Heredoc),
411 ] 436 ]
412 } 437 }

eric ide

mercurial