shell.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. """
  2. pygments.lexers.shell
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for various shells.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, \
  10. include, default, this, using, words
  11. from pygments.token import Punctuation, \
  12. Text, Comment, Operator, Keyword, Name, String, Number, Generic
  13. from pygments.util import shebang_matches
  14. __all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer',
  15. 'SlurmBashLexer', 'MSDOSSessionLexer', 'PowerShellLexer',
  16. 'PowerShellSessionLexer', 'TcshSessionLexer', 'FishShellLexer',
  17. 'ExeclineLexer']
  18. line_re = re.compile('.*?\n')
  19. class BashLexer(RegexLexer):
  20. """
  21. Lexer for (ba|k|z|)sh shell scripts.
  22. .. versionadded:: 0.6
  23. """
  24. name = 'Bash'
  25. aliases = ['bash', 'sh', 'ksh', 'zsh', 'shell']
  26. filenames = ['*.sh', '*.ksh', '*.bash', '*.ebuild', '*.eclass',
  27. '*.exheres-0', '*.exlib', '*.zsh',
  28. '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'zshrc', '.zshrc',
  29. 'PKGBUILD']
  30. mimetypes = ['application/x-sh', 'application/x-shellscript', 'text/x-shellscript']
  31. tokens = {
  32. 'root': [
  33. include('basic'),
  34. (r'`', String.Backtick, 'backticks'),
  35. include('data'),
  36. include('interp'),
  37. ],
  38. 'interp': [
  39. (r'\$\(\(', Keyword, 'math'),
  40. (r'\$\(', Keyword, 'paren'),
  41. (r'\$\{#?', String.Interpol, 'curly'),
  42. (r'\$[a-zA-Z_]\w*', Name.Variable), # user variable
  43. (r'\$(?:\d+|[#$?!_*@-])', Name.Variable), # builtin
  44. (r'\$', Text),
  45. ],
  46. 'basic': [
  47. (r'\b(if|fi|else|while|in|do|done|for|then|return|function|case|'
  48. r'select|continue|until|esac|elif)(\s*)\b',
  49. bygroups(Keyword, Text)),
  50. (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|'
  51. r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|'
  52. r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|'
  53. r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|'
  54. r'shopt|source|suspend|test|time|times|trap|true|type|typeset|'
  55. r'ulimit|umask|unalias|unset|wait)(?=[\s)`])',
  56. Name.Builtin),
  57. (r'\A#!.+\n', Comment.Hashbang),
  58. (r'#.*\n', Comment.Single),
  59. (r'\\[\w\W]', String.Escape),
  60. (r'(\b\w+)(\s*)(\+?=)', bygroups(Name.Variable, Text, Operator)),
  61. (r'[\[\]{}()=]', Operator),
  62. (r'<<<', Operator), # here-string
  63. (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
  64. (r'&&|\|\|', Operator),
  65. ],
  66. 'data': [
  67. (r'(?s)\$?"(\\.|[^"\\$])*"', String.Double),
  68. (r'"', String.Double, 'string'),
  69. (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
  70. (r"(?s)'.*?'", String.Single),
  71. (r';', Punctuation),
  72. (r'&', Punctuation),
  73. (r'\|', Punctuation),
  74. (r'\s+', Text),
  75. (r'\d+\b', Number),
  76. (r'[^=\s\[\]{}()$"\'`\\<&|;]+', Text),
  77. (r'<', Text),
  78. ],
  79. 'string': [
  80. (r'"', String.Double, '#pop'),
  81. (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+', String.Double),
  82. include('interp'),
  83. ],
  84. 'curly': [
  85. (r'\}', String.Interpol, '#pop'),
  86. (r':-', Keyword),
  87. (r'\w+', Name.Variable),
  88. (r'[^}:"\'`$\\]+', Punctuation),
  89. (r':', Punctuation),
  90. include('root'),
  91. ],
  92. 'paren': [
  93. (r'\)', Keyword, '#pop'),
  94. include('root'),
  95. ],
  96. 'math': [
  97. (r'\)\)', Keyword, '#pop'),
  98. (r'[-+*/%^|&]|\*\*|\|\|', Operator),
  99. (r'\d+#\d+', Number),
  100. (r'\d+#(?! )', Number),
  101. (r'\d+', Number),
  102. include('root'),
  103. ],
  104. 'backticks': [
  105. (r'`', String.Backtick, '#pop'),
  106. include('root'),
  107. ],
  108. }
  109. def analyse_text(text):
  110. if shebang_matches(text, r'(ba|z|)sh'):
  111. return 1
  112. if text.startswith('$ '):
  113. return 0.2
  114. class SlurmBashLexer(BashLexer):
  115. """
  116. Lexer for (ba|k|z|)sh Slurm scripts.
  117. .. versionadded:: 2.4
  118. """
  119. name = 'Slurm'
  120. aliases = ['slurm', 'sbatch']
  121. filenames = ['*.sl']
  122. mimetypes = []
  123. EXTRA_KEYWORDS = {'srun'}
  124. def get_tokens_unprocessed(self, text):
  125. for index, token, value in BashLexer.get_tokens_unprocessed(self, text):
  126. if token is Text and value in self.EXTRA_KEYWORDS:
  127. yield index, Name.Builtin, value
  128. elif token is Comment.Single and 'SBATCH' in value:
  129. yield index, Keyword.Pseudo, value
  130. else:
  131. yield index, token, value
  132. class ShellSessionBaseLexer(Lexer):
  133. """
  134. Base lexer for shell sessions.
  135. .. versionadded:: 2.1
  136. """
  137. _venv = re.compile(r'^(\([^)]*\))(\s*)')
  138. def get_tokens_unprocessed(self, text):
  139. innerlexer = self._innerLexerCls(**self.options)
  140. pos = 0
  141. curcode = ''
  142. insertions = []
  143. backslash_continuation = False
  144. for match in line_re.finditer(text):
  145. line = match.group()
  146. venv_match = self._venv.match(line)
  147. if venv_match:
  148. venv = venv_match.group(1)
  149. venv_whitespace = venv_match.group(2)
  150. insertions.append((len(curcode),
  151. [(0, Generic.Prompt.VirtualEnv, venv)]))
  152. if venv_whitespace:
  153. insertions.append((len(curcode),
  154. [(0, Text, venv_whitespace)]))
  155. line = line[venv_match.end():]
  156. m = self._ps1rgx.match(line)
  157. if m:
  158. # To support output lexers (say diff output), the output
  159. # needs to be broken by prompts whenever the output lexer
  160. # changes.
  161. if not insertions:
  162. pos = match.start()
  163. insertions.append((len(curcode),
  164. [(0, Generic.Prompt, m.group(1))]))
  165. curcode += m.group(2)
  166. backslash_continuation = curcode.endswith('\\\n')
  167. elif line.startswith(self._ps2) and backslash_continuation:
  168. insertions.append((len(curcode),
  169. [(0, Generic.Prompt, line[:len(self._ps2)])]))
  170. curcode += line[len(self._ps2):]
  171. backslash_continuation = curcode.endswith('\\\n')
  172. else:
  173. if insertions:
  174. toks = innerlexer.get_tokens_unprocessed(curcode)
  175. for i, t, v in do_insertions(insertions, toks):
  176. yield pos+i, t, v
  177. yield match.start(), Generic.Output, line
  178. insertions = []
  179. curcode = ''
  180. if insertions:
  181. for i, t, v in do_insertions(insertions,
  182. innerlexer.get_tokens_unprocessed(curcode)):
  183. yield pos+i, t, v
  184. class BashSessionLexer(ShellSessionBaseLexer):
  185. """
  186. Lexer for Bash shell sessions, i.e. command lines, including a
  187. prompt, interspersed with output.
  188. .. versionadded:: 1.1
  189. """
  190. name = 'Bash Session'
  191. aliases = ['console', 'shell-session']
  192. filenames = ['*.sh-session', '*.shell-session']
  193. mimetypes = ['application/x-shell-session', 'application/x-sh-session']
  194. _innerLexerCls = BashLexer
  195. _ps1rgx = re.compile(
  196. r'^((?:(?:\[.*?\])|(?:\(\S+\))?(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \
  197. r'?|\[\S+[@:][^\n]+\].+))\s*[$#%]\s*)(.*\n?)')
  198. _ps2 = '> '
  199. class BatchLexer(RegexLexer):
  200. """
  201. Lexer for the DOS/Windows Batch file format.
  202. .. versionadded:: 0.7
  203. """
  204. name = 'Batchfile'
  205. aliases = ['batch', 'bat', 'dosbatch', 'winbatch']
  206. filenames = ['*.bat', '*.cmd']
  207. mimetypes = ['application/x-dos-batch']
  208. flags = re.MULTILINE | re.IGNORECASE
  209. _nl = r'\n\x1a'
  210. _punct = r'&<>|'
  211. _ws = r'\t\v\f\r ,;=\xa0'
  212. _nlws = r'\s\x1a\xa0,;='
  213. _space = r'(?:(?:(?:\^[%s])?[%s])+)' % (_nl, _ws)
  214. _keyword_terminator = (r'(?=(?:\^[%s]?)?[%s+./:[\\\]]|[%s%s(])' %
  215. (_nl, _ws, _nl, _punct))
  216. _token_terminator = r'(?=\^?[%s]|[%s%s])' % (_ws, _punct, _nl)
  217. _start_label = r'((?:(?<=^[^:])|^[^:]?)[%s]*)(:)' % _ws
  218. _label = r'(?:(?:[^%s%s+:^]|\^[%s]?[\w\W])*)' % (_nlws, _punct, _nl)
  219. _label_compound = r'(?:(?:[^%s%s+:^)]|\^[%s]?[^)])*)' % (_nlws, _punct, _nl)
  220. _number = r'(?:-?(?:0[0-7]+|0x[\da-f]+|\d+)%s)' % _token_terminator
  221. _opword = r'(?:equ|geq|gtr|leq|lss|neq)'
  222. _string = r'(?:"[^%s"]*(?:"|(?=[%s])))' % (_nl, _nl)
  223. _variable = (r'(?:(?:%%(?:\*|(?:~[a-z]*(?:\$[^:]+:)?)?\d|'
  224. r'[^%%:%s]+(?::(?:~(?:-?\d+)?(?:,(?:-?\d+)?)?|(?:[^%%%s^]|'
  225. r'\^[^%%%s])[^=%s]*=(?:[^%%%s^]|\^[^%%%s])*)?)?%%))|'
  226. r'(?:\^?![^!:%s]+(?::(?:~(?:-?\d+)?(?:,(?:-?\d+)?)?|(?:'
  227. r'[^!%s^]|\^[^!%s])[^=%s]*=(?:[^!%s^]|\^[^!%s])*)?)?\^?!))' %
  228. (_nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl, _nl))
  229. _core_token = r'(?:(?:(?:\^[%s]?)?[^"%s%s])+)' % (_nl, _nlws, _punct)
  230. _core_token_compound = r'(?:(?:(?:\^[%s]?)?[^"%s%s)])+)' % (_nl, _nlws, _punct)
  231. _token = r'(?:[%s]+|%s)' % (_punct, _core_token)
  232. _token_compound = r'(?:[%s]+|%s)' % (_punct, _core_token_compound)
  233. _stoken = (r'(?:[%s]+|(?:%s|%s|%s)+)' %
  234. (_punct, _string, _variable, _core_token))
  235. def _make_begin_state(compound, _core_token=_core_token,
  236. _core_token_compound=_core_token_compound,
  237. _keyword_terminator=_keyword_terminator,
  238. _nl=_nl, _punct=_punct, _string=_string,
  239. _space=_space, _start_label=_start_label,
  240. _stoken=_stoken, _token_terminator=_token_terminator,
  241. _variable=_variable, _ws=_ws):
  242. rest = '(?:%s|%s|[^"%%%s%s%s])*' % (_string, _variable, _nl, _punct,
  243. ')' if compound else '')
  244. rest_of_line = r'(?:(?:[^%s^]|\^[%s]?[\w\W])*)' % (_nl, _nl)
  245. rest_of_line_compound = r'(?:(?:[^%s^)]|\^[%s]?[^)])*)' % (_nl, _nl)
  246. set_space = r'((?:(?:\^[%s]?)?[^\S\n])*)' % _nl
  247. suffix = ''
  248. if compound:
  249. _keyword_terminator = r'(?:(?=\))|%s)' % _keyword_terminator
  250. _token_terminator = r'(?:(?=\))|%s)' % _token_terminator
  251. suffix = '/compound'
  252. return [
  253. ((r'\)', Punctuation, '#pop') if compound else
  254. (r'\)((?=\()|%s)%s' % (_token_terminator, rest_of_line),
  255. Comment.Single)),
  256. (r'(?=%s)' % _start_label, Text, 'follow%s' % suffix),
  257. (_space, using(this, state='text')),
  258. include('redirect%s' % suffix),
  259. (r'[%s]+' % _nl, Text),
  260. (r'\(', Punctuation, 'root/compound'),
  261. (r'@+', Punctuation),
  262. (r'((?:for|if|rem)(?:(?=(?:\^[%s]?)?/)|(?:(?!\^)|'
  263. r'(?<=m))(?:(?=\()|%s)))(%s?%s?(?:\^[%s]?)?/(?:\^[%s]?)?\?)' %
  264. (_nl, _token_terminator, _space,
  265. _core_token_compound if compound else _core_token, _nl, _nl),
  266. bygroups(Keyword, using(this, state='text')),
  267. 'follow%s' % suffix),
  268. (r'(goto%s)(%s(?:\^[%s]?)?/(?:\^[%s]?)?\?%s)' %
  269. (_keyword_terminator, rest, _nl, _nl, rest),
  270. bygroups(Keyword, using(this, state='text')),
  271. 'follow%s' % suffix),
  272. (words(('assoc', 'break', 'cd', 'chdir', 'cls', 'color', 'copy',
  273. 'date', 'del', 'dir', 'dpath', 'echo', 'endlocal', 'erase',
  274. 'exit', 'ftype', 'keys', 'md', 'mkdir', 'mklink', 'move',
  275. 'path', 'pause', 'popd', 'prompt', 'pushd', 'rd', 'ren',
  276. 'rename', 'rmdir', 'setlocal', 'shift', 'start', 'time',
  277. 'title', 'type', 'ver', 'verify', 'vol'),
  278. suffix=_keyword_terminator), Keyword, 'follow%s' % suffix),
  279. (r'(call)(%s?)(:)' % _space,
  280. bygroups(Keyword, using(this, state='text'), Punctuation),
  281. 'call%s' % suffix),
  282. (r'call%s' % _keyword_terminator, Keyword),
  283. (r'(for%s(?!\^))(%s)(/f%s)' %
  284. (_token_terminator, _space, _token_terminator),
  285. bygroups(Keyword, using(this, state='text'), Keyword),
  286. ('for/f', 'for')),
  287. (r'(for%s(?!\^))(%s)(/l%s)' %
  288. (_token_terminator, _space, _token_terminator),
  289. bygroups(Keyword, using(this, state='text'), Keyword),
  290. ('for/l', 'for')),
  291. (r'for%s(?!\^)' % _token_terminator, Keyword, ('for2', 'for')),
  292. (r'(goto%s)(%s?)(:?)' % (_keyword_terminator, _space),
  293. bygroups(Keyword, using(this, state='text'), Punctuation),
  294. 'label%s' % suffix),
  295. (r'(if(?:(?=\()|%s)(?!\^))(%s?)((?:/i%s)?)(%s?)((?:not%s)?)(%s?)' %
  296. (_token_terminator, _space, _token_terminator, _space,
  297. _token_terminator, _space),
  298. bygroups(Keyword, using(this, state='text'), Keyword,
  299. using(this, state='text'), Keyword,
  300. using(this, state='text')), ('(?', 'if')),
  301. (r'rem(((?=\()|%s)%s?%s?.*|%s%s)' %
  302. (_token_terminator, _space, _stoken, _keyword_terminator,
  303. rest_of_line_compound if compound else rest_of_line),
  304. Comment.Single, 'follow%s' % suffix),
  305. (r'(set%s)%s(/a)' % (_keyword_terminator, set_space),
  306. bygroups(Keyword, using(this, state='text'), Keyword),
  307. 'arithmetic%s' % suffix),
  308. (r'(set%s)%s((?:/p)?)%s((?:(?:(?:\^[%s]?)?[^"%s%s^=%s]|'
  309. r'\^[%s]?[^"=])+)?)((?:(?:\^[%s]?)?=)?)' %
  310. (_keyword_terminator, set_space, set_space, _nl, _nl, _punct,
  311. ')' if compound else '', _nl, _nl),
  312. bygroups(Keyword, using(this, state='text'), Keyword,
  313. using(this, state='text'), using(this, state='variable'),
  314. Punctuation),
  315. 'follow%s' % suffix),
  316. default('follow%s' % suffix)
  317. ]
  318. def _make_follow_state(compound, _label=_label,
  319. _label_compound=_label_compound, _nl=_nl,
  320. _space=_space, _start_label=_start_label,
  321. _token=_token, _token_compound=_token_compound,
  322. _ws=_ws):
  323. suffix = '/compound' if compound else ''
  324. state = []
  325. if compound:
  326. state.append((r'(?=\))', Text, '#pop'))
  327. state += [
  328. (r'%s([%s]*)(%s)(.*)' %
  329. (_start_label, _ws, _label_compound if compound else _label),
  330. bygroups(Text, Punctuation, Text, Name.Label, Comment.Single)),
  331. include('redirect%s' % suffix),
  332. (r'(?=[%s])' % _nl, Text, '#pop'),
  333. (r'\|\|?|&&?', Punctuation, '#pop'),
  334. include('text')
  335. ]
  336. return state
  337. def _make_arithmetic_state(compound, _nl=_nl, _punct=_punct,
  338. _string=_string, _variable=_variable,
  339. _ws=_ws, _nlws=_nlws):
  340. op = r'=+\-*/!~'
  341. state = []
  342. if compound:
  343. state.append((r'(?=\))', Text, '#pop'))
  344. state += [
  345. (r'0[0-7]+', Number.Oct),
  346. (r'0x[\da-f]+', Number.Hex),
  347. (r'\d+', Number.Integer),
  348. (r'[(),]+', Punctuation),
  349. (r'([%s]|%%|\^\^)+' % op, Operator),
  350. (r'(%s|%s|(\^[%s]?)?[^()%s%%\^"%s%s]|\^[%s]?%s)+' %
  351. (_string, _variable, _nl, op, _nlws, _punct, _nlws,
  352. r'[^)]' if compound else r'[\w\W]'),
  353. using(this, state='variable')),
  354. (r'(?=[\x00|&])', Text, '#pop'),
  355. include('follow')
  356. ]
  357. return state
  358. def _make_call_state(compound, _label=_label,
  359. _label_compound=_label_compound):
  360. state = []
  361. if compound:
  362. state.append((r'(?=\))', Text, '#pop'))
  363. state.append((r'(:?)(%s)' % (_label_compound if compound else _label),
  364. bygroups(Punctuation, Name.Label), '#pop'))
  365. return state
  366. def _make_label_state(compound, _label=_label,
  367. _label_compound=_label_compound, _nl=_nl,
  368. _punct=_punct, _string=_string, _variable=_variable):
  369. state = []
  370. if compound:
  371. state.append((r'(?=\))', Text, '#pop'))
  372. state.append((r'(%s?)((?:%s|%s|\^[%s]?%s|[^"%%^%s%s%s])*)' %
  373. (_label_compound if compound else _label, _string,
  374. _variable, _nl, r'[^)]' if compound else r'[\w\W]', _nl,
  375. _punct, r')' if compound else ''),
  376. bygroups(Name.Label, Comment.Single), '#pop'))
  377. return state
  378. def _make_redirect_state(compound,
  379. _core_token_compound=_core_token_compound,
  380. _nl=_nl, _punct=_punct, _stoken=_stoken,
  381. _string=_string, _space=_space,
  382. _variable=_variable, _nlws=_nlws):
  383. stoken_compound = (r'(?:[%s]+|(?:%s|%s|%s)+)' %
  384. (_punct, _string, _variable, _core_token_compound))
  385. return [
  386. (r'((?:(?<=[%s])\d)?)(>>?&|<&)([%s]*)(\d)' %
  387. (_nlws, _nlws),
  388. bygroups(Number.Integer, Punctuation, Text, Number.Integer)),
  389. (r'((?:(?<=[%s])(?<!\^[%s])\d)?)(>>?|<)(%s?%s)' %
  390. (_nlws, _nl, _space, stoken_compound if compound else _stoken),
  391. bygroups(Number.Integer, Punctuation, using(this, state='text')))
  392. ]
  393. tokens = {
  394. 'root': _make_begin_state(False),
  395. 'follow': _make_follow_state(False),
  396. 'arithmetic': _make_arithmetic_state(False),
  397. 'call': _make_call_state(False),
  398. 'label': _make_label_state(False),
  399. 'redirect': _make_redirect_state(False),
  400. 'root/compound': _make_begin_state(True),
  401. 'follow/compound': _make_follow_state(True),
  402. 'arithmetic/compound': _make_arithmetic_state(True),
  403. 'call/compound': _make_call_state(True),
  404. 'label/compound': _make_label_state(True),
  405. 'redirect/compound': _make_redirect_state(True),
  406. 'variable-or-escape': [
  407. (_variable, Name.Variable),
  408. (r'%%%%|\^[%s]?(\^!|[\w\W])' % _nl, String.Escape)
  409. ],
  410. 'string': [
  411. (r'"', String.Double, '#pop'),
  412. (_variable, Name.Variable),
  413. (r'\^!|%%', String.Escape),
  414. (r'[^"%%^%s]+|[%%^]' % _nl, String.Double),
  415. default('#pop')
  416. ],
  417. 'sqstring': [
  418. include('variable-or-escape'),
  419. (r'[^%]+|%', String.Single)
  420. ],
  421. 'bqstring': [
  422. include('variable-or-escape'),
  423. (r'[^%]+|%', String.Backtick)
  424. ],
  425. 'text': [
  426. (r'"', String.Double, 'string'),
  427. include('variable-or-escape'),
  428. (r'[^"%%^%s%s\d)]+|.' % (_nlws, _punct), Text)
  429. ],
  430. 'variable': [
  431. (r'"', String.Double, 'string'),
  432. include('variable-or-escape'),
  433. (r'[^"%%^%s]+|.' % _nl, Name.Variable)
  434. ],
  435. 'for': [
  436. (r'(%s)(in)(%s)(\()' % (_space, _space),
  437. bygroups(using(this, state='text'), Keyword,
  438. using(this, state='text'), Punctuation), '#pop'),
  439. include('follow')
  440. ],
  441. 'for2': [
  442. (r'\)', Punctuation),
  443. (r'(%s)(do%s)' % (_space, _token_terminator),
  444. bygroups(using(this, state='text'), Keyword), '#pop'),
  445. (r'[%s]+' % _nl, Text),
  446. include('follow')
  447. ],
  448. 'for/f': [
  449. (r'(")((?:%s|[^"])*?")([%s]*)(\))' % (_variable, _nlws),
  450. bygroups(String.Double, using(this, state='string'), Text,
  451. Punctuation)),
  452. (r'"', String.Double, ('#pop', 'for2', 'string')),
  453. (r"('(?:%%%%|%s|[\w\W])*?')([%s]*)(\))" % (_variable, _nlws),
  454. bygroups(using(this, state='sqstring'), Text, Punctuation)),
  455. (r'(`(?:%%%%|%s|[\w\W])*?`)([%s]*)(\))' % (_variable, _nlws),
  456. bygroups(using(this, state='bqstring'), Text, Punctuation)),
  457. include('for2')
  458. ],
  459. 'for/l': [
  460. (r'-?\d+', Number.Integer),
  461. include('for2')
  462. ],
  463. 'if': [
  464. (r'((?:cmdextversion|errorlevel)%s)(%s)(\d+)' %
  465. (_token_terminator, _space),
  466. bygroups(Keyword, using(this, state='text'),
  467. Number.Integer), '#pop'),
  468. (r'(defined%s)(%s)(%s)' % (_token_terminator, _space, _stoken),
  469. bygroups(Keyword, using(this, state='text'),
  470. using(this, state='variable')), '#pop'),
  471. (r'(exist%s)(%s%s)' % (_token_terminator, _space, _stoken),
  472. bygroups(Keyword, using(this, state='text')), '#pop'),
  473. (r'(%s%s)(%s)(%s%s)' % (_number, _space, _opword, _space, _number),
  474. bygroups(using(this, state='arithmetic'), Operator.Word,
  475. using(this, state='arithmetic')), '#pop'),
  476. (_stoken, using(this, state='text'), ('#pop', 'if2')),
  477. ],
  478. 'if2': [
  479. (r'(%s?)(==)(%s?%s)' % (_space, _space, _stoken),
  480. bygroups(using(this, state='text'), Operator,
  481. using(this, state='text')), '#pop'),
  482. (r'(%s)(%s)(%s%s)' % (_space, _opword, _space, _stoken),
  483. bygroups(using(this, state='text'), Operator.Word,
  484. using(this, state='text')), '#pop')
  485. ],
  486. '(?': [
  487. (_space, using(this, state='text')),
  488. (r'\(', Punctuation, ('#pop', 'else?', 'root/compound')),
  489. default('#pop')
  490. ],
  491. 'else?': [
  492. (_space, using(this, state='text')),
  493. (r'else%s' % _token_terminator, Keyword, '#pop'),
  494. default('#pop')
  495. ]
  496. }
  497. class MSDOSSessionLexer(ShellSessionBaseLexer):
  498. """
  499. Lexer for MS DOS shell sessions, i.e. command lines, including a
  500. prompt, interspersed with output.
  501. .. versionadded:: 2.1
  502. """
  503. name = 'MSDOS Session'
  504. aliases = ['doscon']
  505. filenames = []
  506. mimetypes = []
  507. _innerLexerCls = BatchLexer
  508. _ps1rgx = re.compile(r'^([^>]*>)(.*\n?)')
  509. _ps2 = 'More? '
  510. class TcshLexer(RegexLexer):
  511. """
  512. Lexer for tcsh scripts.
  513. .. versionadded:: 0.10
  514. """
  515. name = 'Tcsh'
  516. aliases = ['tcsh', 'csh']
  517. filenames = ['*.tcsh', '*.csh']
  518. mimetypes = ['application/x-csh']
  519. tokens = {
  520. 'root': [
  521. include('basic'),
  522. (r'\$\(', Keyword, 'paren'),
  523. (r'\$\{#?', Keyword, 'curly'),
  524. (r'`', String.Backtick, 'backticks'),
  525. include('data'),
  526. ],
  527. 'basic': [
  528. (r'\b(if|endif|else|while|then|foreach|case|default|'
  529. r'continue|goto|breaksw|end|switch|endsw)\s*\b',
  530. Keyword),
  531. (r'\b(alias|alloc|bg|bindkey|break|builtins|bye|caller|cd|chdir|'
  532. r'complete|dirs|echo|echotc|eval|exec|exit|fg|filetest|getxvers|'
  533. r'glob|getspath|hashstat|history|hup|inlib|jobs|kill|'
  534. r'limit|log|login|logout|ls-F|migrate|newgrp|nice|nohup|notify|'
  535. r'onintr|popd|printenv|pushd|rehash|repeat|rootnode|popd|pushd|'
  536. r'set|shift|sched|setenv|setpath|settc|setty|setxvers|shift|'
  537. r'source|stop|suspend|source|suspend|telltc|time|'
  538. r'umask|unalias|uncomplete|unhash|universe|unlimit|unset|unsetenv|'
  539. r'ver|wait|warp|watchlog|where|which)\s*\b',
  540. Name.Builtin),
  541. (r'#.*', Comment),
  542. (r'\\[\w\W]', String.Escape),
  543. (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
  544. (r'[\[\]{}()=]+', Operator),
  545. (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
  546. (r';', Punctuation),
  547. ],
  548. 'data': [
  549. (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
  550. (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
  551. (r'\s+', Text),
  552. (r'[^=\s\[\]{}()$"\'`\\;#]+', Text),
  553. (r'\d+(?= |\Z)', Number),
  554. (r'\$#?(\w+|.)', Name.Variable),
  555. ],
  556. 'curly': [
  557. (r'\}', Keyword, '#pop'),
  558. (r':-', Keyword),
  559. (r'\w+', Name.Variable),
  560. (r'[^}:"\'`$]+', Punctuation),
  561. (r':', Punctuation),
  562. include('root'),
  563. ],
  564. 'paren': [
  565. (r'\)', Keyword, '#pop'),
  566. include('root'),
  567. ],
  568. 'backticks': [
  569. (r'`', String.Backtick, '#pop'),
  570. include('root'),
  571. ],
  572. }
  573. class TcshSessionLexer(ShellSessionBaseLexer):
  574. """
  575. Lexer for Tcsh sessions, i.e. command lines, including a
  576. prompt, interspersed with output.
  577. .. versionadded:: 2.1
  578. """
  579. name = 'Tcsh Session'
  580. aliases = ['tcshcon']
  581. filenames = []
  582. mimetypes = []
  583. _innerLexerCls = TcshLexer
  584. _ps1rgx = re.compile(r'^([^>]+>)(.*\n?)')
  585. _ps2 = '? '
  586. class PowerShellLexer(RegexLexer):
  587. """
  588. For Windows PowerShell code.
  589. .. versionadded:: 1.5
  590. """
  591. name = 'PowerShell'
  592. aliases = ['powershell', 'posh', 'ps1', 'psm1']
  593. filenames = ['*.ps1', '*.psm1']
  594. mimetypes = ['text/x-powershell']
  595. flags = re.DOTALL | re.IGNORECASE | re.MULTILINE
  596. keywords = (
  597. 'while validateset validaterange validatepattern validatelength '
  598. 'validatecount until trap switch return ref process param parameter in '
  599. 'if global: function foreach for finally filter end elseif else '
  600. 'dynamicparam do default continue cmdletbinding break begin alias \\? '
  601. '% #script #private #local #global mandatory parametersetname position '
  602. 'valuefrompipeline valuefrompipelinebypropertyname '
  603. 'valuefromremainingarguments helpmessage try catch throw').split()
  604. operators = (
  605. 'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle '
  606. 'clike clt cmatch cne cnotcontains cnotlike cnotmatch contains '
  607. 'creplace eq exact f file ge gt icontains ieq ige igt ile ilike ilt '
  608. 'imatch ine inotcontains inotlike inotmatch ireplace is isnot le like '
  609. 'lt match ne not notcontains notlike notmatch or regex replace '
  610. 'wildcard').split()
  611. verbs = (
  612. 'write where watch wait use update unregister unpublish unprotect '
  613. 'unlock uninstall undo unblock trace test tee take sync switch '
  614. 'suspend submit stop step start split sort skip show set send select '
  615. 'search scroll save revoke resume restore restart resolve resize '
  616. 'reset request repair rename remove register redo receive read push '
  617. 'publish protect pop ping out optimize open new move mount merge '
  618. 'measure lock limit join invoke install initialize import hide group '
  619. 'grant get format foreach find export expand exit enter enable edit '
  620. 'dismount disconnect disable deny debug cxnew copy convertto '
  621. 'convertfrom convert connect confirm compress complete compare close '
  622. 'clear checkpoint block backup assert approve aggregate add').split()
  623. aliases_ = (
  624. 'ac asnp cat cd cfs chdir clc clear clhy cli clp cls clv cnsn '
  625. 'compare copy cp cpi cpp curl cvpa dbp del diff dir dnsn ebp echo epal '
  626. 'epcsv epsn erase etsn exsn fc fhx fl foreach ft fw gal gbp gc gci gcm '
  627. 'gcs gdr ghy gi gjb gl gm gmo gp gps gpv group gsn gsnp gsv gu gv gwmi '
  628. 'h history icm iex ihy ii ipal ipcsv ipmo ipsn irm ise iwmi iwr kill lp '
  629. 'ls man md measure mi mount move mp mv nal ndr ni nmo npssc nsn nv ogv '
  630. 'oh popd ps pushd pwd r rbp rcjb rcsn rd rdr ren ri rjb rm rmdir rmo '
  631. 'rni rnp rp rsn rsnp rujb rv rvpa rwmi sajb sal saps sasv sbp sc select '
  632. 'set shcm si sl sleep sls sort sp spjb spps spsv start sujb sv swmi tee '
  633. 'trcm type wget where wjb write').split()
  634. commenthelp = (
  635. 'component description example externalhelp forwardhelpcategory '
  636. 'forwardhelptargetname functionality inputs link '
  637. 'notes outputs parameter remotehelprunspace role synopsis').split()
  638. tokens = {
  639. 'root': [
  640. # we need to count pairs of parentheses for correct highlight
  641. # of '$(...)' blocks in strings
  642. (r'\(', Punctuation, 'child'),
  643. (r'\s+', Text),
  644. (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp),
  645. bygroups(Comment, String.Doc, Comment)),
  646. (r'#[^\n]*?$', Comment),
  647. (r'(&lt;|<)#', Comment.Multiline, 'multline'),
  648. (r'@"\n', String.Heredoc, 'heredoc-double'),
  649. (r"@'\n.*?\n'@", String.Heredoc),
  650. # escaped syntax
  651. (r'`[\'"$@-]', Punctuation),
  652. (r'"', String.Double, 'string'),
  653. (r"'([^']|'')*'", String.Single),
  654. (r'(\$|@@|@)((global|script|private|env):)?\w+',
  655. Name.Variable),
  656. (r'(%s)\b' % '|'.join(keywords), Keyword),
  657. (r'-(%s)\b' % '|'.join(operators), Operator),
  658. (r'(%s)-[a-z_]\w*\b' % '|'.join(verbs), Name.Builtin),
  659. (r'(%s)\s' % '|'.join(aliases_), Name.Builtin),
  660. (r'\[[a-z_\[][\w. `,\[\]]*\]', Name.Constant), # .net [type]s
  661. (r'-[a-z_]\w*', Name),
  662. (r'\w+', Name),
  663. (r'[.,;:@{}\[\]$()=+*/\\&%!~?^`|<>-]', Punctuation),
  664. ],
  665. 'child': [
  666. (r'\)', Punctuation, '#pop'),
  667. include('root'),
  668. ],
  669. 'multline': [
  670. (r'[^#&.]+', Comment.Multiline),
  671. (r'#(>|&gt;)', Comment.Multiline, '#pop'),
  672. (r'\.(%s)' % '|'.join(commenthelp), String.Doc),
  673. (r'[#&.]', Comment.Multiline),
  674. ],
  675. 'string': [
  676. (r"`[0abfnrtv'\"$`]", String.Escape),
  677. (r'[^$`"]+', String.Double),
  678. (r'\$\(', Punctuation, 'child'),
  679. (r'""', String.Double),
  680. (r'[`$]', String.Double),
  681. (r'"', String.Double, '#pop'),
  682. ],
  683. 'heredoc-double': [
  684. (r'\n"@', String.Heredoc, '#pop'),
  685. (r'\$\(', Punctuation, 'child'),
  686. (r'[^@\n]+"]', String.Heredoc),
  687. (r".", String.Heredoc),
  688. ]
  689. }
  690. class PowerShellSessionLexer(ShellSessionBaseLexer):
  691. """
  692. Lexer for PowerShell sessions, i.e. command lines, including a
  693. prompt, interspersed with output.
  694. .. versionadded:: 2.1
  695. """
  696. name = 'PowerShell Session'
  697. aliases = ['ps1con']
  698. filenames = []
  699. mimetypes = []
  700. _innerLexerCls = PowerShellLexer
  701. _ps1rgx = re.compile(r'^((?:\[[^]]+\]: )?PS[^>]*> ?)(.*\n?)')
  702. _ps2 = '>> '
  703. class FishShellLexer(RegexLexer):
  704. """
  705. Lexer for Fish shell scripts.
  706. .. versionadded:: 2.1
  707. """
  708. name = 'Fish'
  709. aliases = ['fish', 'fishshell']
  710. filenames = ['*.fish', '*.load']
  711. mimetypes = ['application/x-fish']
  712. tokens = {
  713. 'root': [
  714. include('basic'),
  715. include('data'),
  716. include('interp'),
  717. ],
  718. 'interp': [
  719. (r'\$\(\(', Keyword, 'math'),
  720. (r'\(', Keyword, 'paren'),
  721. (r'\$#?(\w+|.)', Name.Variable),
  722. ],
  723. 'basic': [
  724. (r'\b(begin|end|if|else|while|break|for|in|return|function|block|'
  725. r'case|continue|switch|not|and|or|set|echo|exit|pwd|true|false|'
  726. r'cd|count|test)(\s*)\b',
  727. bygroups(Keyword, Text)),
  728. (r'\b(alias|bg|bind|breakpoint|builtin|command|commandline|'
  729. r'complete|contains|dirh|dirs|emit|eval|exec|fg|fish|fish_config|'
  730. r'fish_indent|fish_pager|fish_prompt|fish_right_prompt|'
  731. r'fish_update_completions|fishd|funced|funcsave|functions|help|'
  732. r'history|isatty|jobs|math|mimedb|nextd|open|popd|prevd|psub|'
  733. r'pushd|random|read|set_color|source|status|trap|type|ulimit|'
  734. r'umask|vared|fc|getopts|hash|kill|printf|time|wait)\s*\b(?!\.)',
  735. Name.Builtin),
  736. (r'#.*\n', Comment),
  737. (r'\\[\w\W]', String.Escape),
  738. (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
  739. (r'[\[\]()=]', Operator),
  740. (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
  741. ],
  742. 'data': [
  743. (r'(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"', String.Double),
  744. (r'"', String.Double, 'string'),
  745. (r"(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
  746. (r"(?s)'.*?'", String.Single),
  747. (r';', Punctuation),
  748. (r'&|\||\^|<|>', Operator),
  749. (r'\s+', Text),
  750. (r'\d+(?= |\Z)', Number),
  751. (r'[^=\s\[\]{}()$"\'`\\<&|;]+', Text),
  752. ],
  753. 'string': [
  754. (r'"', String.Double, '#pop'),
  755. (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+', String.Double),
  756. include('interp'),
  757. ],
  758. 'paren': [
  759. (r'\)', Keyword, '#pop'),
  760. include('root'),
  761. ],
  762. 'math': [
  763. (r'\)\)', Keyword, '#pop'),
  764. (r'[-+*/%^|&]|\*\*|\|\|', Operator),
  765. (r'\d+#\d+', Number),
  766. (r'\d+#(?! )', Number),
  767. (r'\d+', Number),
  768. include('root'),
  769. ],
  770. }
  771. class ExeclineLexer(RegexLexer):
  772. """
  773. Lexer for Laurent Bercot's execline language
  774. (https://skarnet.org/software/execline).
  775. .. versionadded:: 2.7
  776. """
  777. name = 'execline'
  778. aliases = ['execline']
  779. filenames = ['*.exec']
  780. tokens = {
  781. 'root': [
  782. include('basic'),
  783. include('data'),
  784. include('interp')
  785. ],
  786. 'interp': [
  787. (r'\$\{', String.Interpol, 'curly'),
  788. (r'\$[\w@#]+', Name.Variable), # user variable
  789. (r'\$', Text),
  790. ],
  791. 'basic': [
  792. (r'\b(background|backtick|cd|define|dollarat|elgetopt|'
  793. r'elgetpositionals|elglob|emptyenv|envfile|exec|execlineb|'
  794. r'exit|export|fdblock|fdclose|fdmove|fdreserve|fdswap|'
  795. r'forbacktickx|foreground|forstdin|forx|getcwd|getpid|heredoc|'
  796. r'homeof|if|ifelse|ifte|ifthenelse|importas|loopwhilex|'
  797. r'multidefine|multisubstitute|pipeline|piperw|posix-cd|'
  798. r'redirfd|runblock|shift|trap|tryexec|umask|unexport|wait|'
  799. r'withstdinas)\b', Name.Builtin),
  800. (r'\A#!.+\n', Comment.Hashbang),
  801. (r'#.*\n', Comment.Single),
  802. (r'[{}]', Operator)
  803. ],
  804. 'data': [
  805. (r'(?s)"(\\.|[^"\\$])*"', String.Double),
  806. (r'"', String.Double, 'string'),
  807. (r'\s+', Text),
  808. (r'[^\s{}$"\\]+', Text)
  809. ],
  810. 'string': [
  811. (r'"', String.Double, '#pop'),
  812. (r'(?s)(\\\\|\\.|[^"\\$])+', String.Double),
  813. include('interp'),
  814. ],
  815. 'curly': [
  816. (r'\}', String.Interpol, '#pop'),
  817. (r'[\w#@]+', Name.Variable),
  818. include('root')
  819. ]
  820. }
  821. def analyse_text(text):
  822. if shebang_matches(text, r'execlineb'):
  823. return 1