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'] |
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'), |
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': [ |
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'(<|<)#', Comment.Multiline, 'multline'), |
396 (r'(<|<)#', 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'#(>|>)', Comment.Multiline, '#pop'), |
419 (r'#(>|>)', 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 } |