configs.py 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. """
  2. pygments.lexers.configs
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for configuration file formats.
  5. :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import ExtendedRegexLexer, RegexLexer, default, words, \
  10. bygroups, include, using, this, line_re
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation, Whitespace, Literal, Error, Generic
  13. from pygments.lexers.shell import BashLexer
  14. from pygments.lexers.data import JsonLexer
  15. __all__ = ['IniLexer', 'SystemdLexer', 'DesktopLexer', 'RegeditLexer', 'PropertiesLexer',
  16. 'KconfigLexer', 'Cfengine3Lexer', 'ApacheConfLexer', 'SquidConfLexer',
  17. 'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer',
  18. 'TerraformLexer', 'TermcapLexer', 'TerminfoLexer',
  19. 'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer',
  20. 'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer',
  21. 'CaddyfileLexer']
  22. class IniLexer(RegexLexer):
  23. """
  24. Lexer for configuration files in INI style.
  25. """
  26. name = 'INI'
  27. aliases = ['ini', 'cfg', 'dosini']
  28. filenames = [
  29. '*.ini', '*.cfg', '*.inf', '.editorconfig',
  30. ]
  31. mimetypes = ['text/x-ini', 'text/inf']
  32. url = 'https://en.wikipedia.org/wiki/INI_file'
  33. version_added = ''
  34. tokens = {
  35. 'root': [
  36. (r'\s+', Whitespace),
  37. (r'[;#].*', Comment.Single),
  38. (r'(\[.*?\])([ \t]*)$', bygroups(Keyword, Whitespace)),
  39. (r'''(.*?)([ \t]*)([=:])([ \t]*)(["'])''',
  40. bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String),
  41. "quoted_value"),
  42. (r'(.*?)([ \t]*)([=:])([ \t]*)([^;#\n]*)(\\)(\s+)',
  43. bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String,
  44. Text, Whitespace),
  45. "value"),
  46. (r'(.*?)([ \t]*)([=:])([ \t]*)([^ ;#\n]*(?: +[^ ;#\n]+)*)',
  47. bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String)),
  48. # standalone option, supported by some INI parsers
  49. (r'(.+?)$', Name.Attribute),
  50. ],
  51. 'quoted_value': [
  52. (r'''([^"'\n]*)(["'])(\s*)''',
  53. bygroups(String, String, Whitespace), "#pop"),
  54. (r'[;#].*', Comment.Single),
  55. (r'$', String, "#pop"),
  56. ],
  57. 'value': [ # line continuation
  58. (r'\s+', Whitespace),
  59. (r'(\s*)(.*)(\\)([ \t]*)',
  60. bygroups(Whitespace, String, Text, Whitespace)),
  61. (r'.*$', String, "#pop"),
  62. ],
  63. }
  64. def analyse_text(text):
  65. npos = text.find('\n')
  66. if npos < 3:
  67. return False
  68. if text[0] == '[' and text[npos-1] == ']':
  69. return 0.8
  70. return False
  71. class DesktopLexer(RegexLexer):
  72. """
  73. Lexer for .desktop files.
  74. """
  75. name = 'Desktop file'
  76. url = "https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html"
  77. aliases = ['desktop']
  78. filenames = ['*.desktop']
  79. mimetypes = ['application/x-desktop']
  80. version_added = '2.16'
  81. tokens = {
  82. 'root': [
  83. (r'^[ \t]*\n', Whitespace),
  84. (r'^(#.*)(\n)', bygroups(Comment.Single, Whitespace)),
  85. (r'(\[[^\]\n]+\])(\n)', bygroups(Keyword, Whitespace)),
  86. (r'([-A-Za-z0-9]+)(\[[^\] \t=]+\])?([ \t]*)(=)([ \t]*)([^\n]*)([ \t\n]*\n)',
  87. bygroups(Name.Attribute, Name.Namespace, Whitespace, Operator, Whitespace, String, Whitespace)),
  88. ],
  89. }
  90. def analyse_text(text):
  91. if text.startswith("[Desktop Entry]"):
  92. return 1.0
  93. if re.search(r"^\[Desktop Entry\][ \t]*$", text[:500], re.MULTILINE) is not None:
  94. return 0.9
  95. return 0.0
  96. class SystemdLexer(RegexLexer):
  97. """
  98. Lexer for systemd unit files.
  99. """
  100. name = 'Systemd'
  101. url = "https://www.freedesktop.org/software/systemd/man/systemd.syntax.html"
  102. aliases = ['systemd']
  103. filenames = [
  104. '*.service', '*.socket', '*.device', '*.mount', '*.automount',
  105. '*.swap', '*.target', '*.path', '*.timer', '*.slice', '*.scope',
  106. ]
  107. version_added = '2.16'
  108. tokens = {
  109. 'root': [
  110. (r'^[ \t]*\n', Whitespace),
  111. (r'^([;#].*)(\n)', bygroups(Comment.Single, Whitespace)),
  112. (r'(\[[^\]\n]+\])(\n)', bygroups(Keyword, Whitespace)),
  113. (r'([^=]+)([ \t]*)(=)([ \t]*)([^\n]*)(\\)(\n)',
  114. bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String,
  115. Text, Whitespace),
  116. "value"),
  117. (r'([^=]+)([ \t]*)(=)([ \t]*)([^\n]*)(\n)',
  118. bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String, Whitespace)),
  119. ],
  120. 'value': [
  121. # line continuation
  122. (r'^([;#].*)(\n)', bygroups(Comment.Single, Whitespace)),
  123. (r'([ \t]*)([^\n]*)(\\)(\n)',
  124. bygroups(Whitespace, String, Text, Whitespace)),
  125. (r'([ \t]*)([^\n]*)(\n)',
  126. bygroups(Whitespace, String, Whitespace), "#pop"),
  127. ],
  128. }
  129. def analyse_text(text):
  130. if text.startswith("[Unit]"):
  131. return 1.0
  132. if re.search(r"^\[Unit\][ \t]*$", text[:500], re.MULTILINE) is not None:
  133. return 0.9
  134. return 0.0
  135. class RegeditLexer(RegexLexer):
  136. """
  137. Lexer for Windows Registry files produced by regedit.
  138. """
  139. name = 'reg'
  140. url = 'http://en.wikipedia.org/wiki/Windows_Registry#.REG_files'
  141. aliases = ['registry']
  142. filenames = ['*.reg']
  143. mimetypes = ['text/x-windows-registry']
  144. version_added = '1.6'
  145. tokens = {
  146. 'root': [
  147. (r'Windows Registry Editor.*', Text),
  148. (r'\s+', Whitespace),
  149. (r'[;#].*', Comment.Single),
  150. (r'(\[)(-?)(HKEY_[A-Z_]+)(.*?\])$',
  151. bygroups(Keyword, Operator, Name.Builtin, Keyword)),
  152. # String keys, which obey somewhat normal escaping
  153. (r'("(?:\\"|\\\\|[^"])+")([ \t]*)(=)([ \t]*)',
  154. bygroups(Name.Attribute, Whitespace, Operator, Whitespace),
  155. 'value'),
  156. # Bare keys (includes @)
  157. (r'(.*?)([ \t]*)(=)([ \t]*)',
  158. bygroups(Name.Attribute, Whitespace, Operator, Whitespace),
  159. 'value'),
  160. ],
  161. 'value': [
  162. (r'-', Operator, '#pop'), # delete value
  163. (r'(dword|hex(?:\([0-9a-fA-F]\))?)(:)([0-9a-fA-F,]+)',
  164. bygroups(Name.Variable, Punctuation, Number), '#pop'),
  165. # As far as I know, .reg files do not support line continuation.
  166. (r'.+', String, '#pop'),
  167. default('#pop'),
  168. ]
  169. }
  170. def analyse_text(text):
  171. return text.startswith('Windows Registry Editor')
  172. class PropertiesLexer(RegexLexer):
  173. """
  174. Lexer for configuration files in Java's properties format.
  175. Note: trailing whitespace counts as part of the value as per spec
  176. """
  177. name = 'Properties'
  178. aliases = ['properties', 'jproperties']
  179. filenames = ['*.properties']
  180. mimetypes = ['text/x-java-properties']
  181. url = 'https://en.wikipedia.org/wiki/.properties'
  182. version_added = '1.4'
  183. tokens = {
  184. 'root': [
  185. # comments
  186. (r'[!#].*|/{2}.*', Comment.Single),
  187. # ending a comment or whitespace-only line
  188. (r'\n', Whitespace),
  189. # eat whitespace at the beginning of a line
  190. (r'^[^\S\n]+', Whitespace),
  191. # start lexing a key
  192. default('key'),
  193. ],
  194. 'key': [
  195. # non-escaped key characters
  196. (r'[^\\:=\s]+', Name.Attribute),
  197. # escapes
  198. include('escapes'),
  199. # separator is the first non-escaped whitespace or colon or '=' on the line;
  200. # if it's whitespace, = and : are gobbled after it
  201. (r'([^\S\n]*)([:=])([^\S\n]*)',
  202. bygroups(Whitespace, Operator, Whitespace),
  203. ('#pop', 'value')),
  204. (r'[^\S\n]+', Whitespace, ('#pop', 'value')),
  205. # maybe we got no value after all
  206. (r'\n', Whitespace, '#pop'),
  207. ],
  208. 'value': [
  209. # non-escaped value characters
  210. (r'[^\\\n]+', String),
  211. # escapes
  212. include('escapes'),
  213. # end the value on an unescaped newline
  214. (r'\n', Whitespace, '#pop'),
  215. ],
  216. 'escapes': [
  217. # line continuations; these gobble whitespace at the beginning of the next line
  218. (r'(\\\n)([^\S\n]*)', bygroups(String.Escape, Whitespace)),
  219. # other escapes
  220. (r'\\([\s\S])', String.Escape),
  221. ],
  222. }
  223. def _rx_indent(level):
  224. # Kconfig *always* interprets a tab as 8 spaces, so this is the default.
  225. # Edit this if you are in an environment where KconfigLexer gets expanded
  226. # input (tabs expanded to spaces) and the expansion tab width is != 8,
  227. # e.g. in connection with Trac (trac.ini, [mimeviewer], tab_width).
  228. # Value range here is 2 <= {tab_width} <= 8.
  229. tab_width = 8
  230. # Regex matching a given indentation {level}, assuming that indentation is
  231. # a multiple of {tab_width}. In other cases there might be problems.
  232. if tab_width == 2:
  233. space_repeat = '+'
  234. else:
  235. space_repeat = '{1,%d}' % (tab_width - 1)
  236. if level == 1:
  237. level_repeat = ''
  238. else:
  239. level_repeat = f'{{{level}}}'
  240. return rf'(?:\t| {space_repeat}\t| {{{tab_width}}}){level_repeat}.*\n'
  241. class KconfigLexer(RegexLexer):
  242. """
  243. For Linux-style Kconfig files.
  244. """
  245. name = 'Kconfig'
  246. aliases = ['kconfig', 'menuconfig', 'linux-config', 'kernel-config']
  247. version_added = '1.6'
  248. # Adjust this if new kconfig file names appear in your environment
  249. filenames = ['Kconfig*', '*Config.in*', 'external.in*',
  250. 'standard-modules.in']
  251. mimetypes = ['text/x-kconfig']
  252. url = 'https://www.kernel.org/doc/html/latest/kbuild/kconfig-language.html'
  253. # No re.MULTILINE, indentation-aware help text needs line-by-line handling
  254. flags = 0
  255. def call_indent(level):
  256. # If indentation >= {level} is detected, enter state 'indent{level}'
  257. return (_rx_indent(level), String.Doc, f'indent{level}')
  258. def do_indent(level):
  259. # Print paragraphs of indentation level >= {level} as String.Doc,
  260. # ignoring blank lines. Then return to 'root' state.
  261. return [
  262. (_rx_indent(level), String.Doc),
  263. (r'\s*\n', Text),
  264. default('#pop:2')
  265. ]
  266. tokens = {
  267. 'root': [
  268. (r'\s+', Whitespace),
  269. (r'#.*?\n', Comment.Single),
  270. (words((
  271. 'mainmenu', 'config', 'menuconfig', 'choice', 'endchoice',
  272. 'comment', 'menu', 'endmenu', 'visible if', 'if', 'endif',
  273. 'source', 'prompt', 'select', 'depends on', 'default',
  274. 'range', 'option'), suffix=r'\b'),
  275. Keyword),
  276. (r'(---help---|help)[\t ]*\n', Keyword, 'help'),
  277. (r'(bool|tristate|string|hex|int|defconfig_list|modules|env)\b',
  278. Name.Builtin),
  279. (r'[!=&|]', Operator),
  280. (r'[()]', Punctuation),
  281. (r'[0-9]+', Number.Integer),
  282. (r"'(''|[^'])*'", String.Single),
  283. (r'"(""|[^"])*"', String.Double),
  284. (r'\S+', Text),
  285. ],
  286. # Help text is indented, multi-line and ends when a lower indentation
  287. # level is detected.
  288. 'help': [
  289. # Skip blank lines after help token, if any
  290. (r'\s*\n', Text),
  291. # Determine the first help line's indentation level heuristically(!).
  292. # Attention: this is not perfect, but works for 99% of "normal"
  293. # indentation schemes up to a max. indentation level of 7.
  294. call_indent(7),
  295. call_indent(6),
  296. call_indent(5),
  297. call_indent(4),
  298. call_indent(3),
  299. call_indent(2),
  300. call_indent(1),
  301. default('#pop'), # for incomplete help sections without text
  302. ],
  303. # Handle text for indentation levels 7 to 1
  304. 'indent7': do_indent(7),
  305. 'indent6': do_indent(6),
  306. 'indent5': do_indent(5),
  307. 'indent4': do_indent(4),
  308. 'indent3': do_indent(3),
  309. 'indent2': do_indent(2),
  310. 'indent1': do_indent(1),
  311. }
  312. class Cfengine3Lexer(RegexLexer):
  313. """
  314. Lexer for CFEngine3 policy files.
  315. """
  316. name = 'CFEngine3'
  317. url = 'http://cfengine.org'
  318. aliases = ['cfengine3', 'cf3']
  319. filenames = ['*.cf']
  320. mimetypes = []
  321. version_added = '1.5'
  322. tokens = {
  323. 'root': [
  324. (r'#.*?\n', Comment),
  325. (r'(body)(\s+)(\S+)(\s+)(control)',
  326. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  327. (r'(body|bundle)(\s+)(\S+)(\s+)(\w+)(\()',
  328. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Function, Punctuation),
  329. 'arglist'),
  330. (r'(body|bundle)(\s+)(\S+)(\s+)(\w+)',
  331. bygroups(Keyword, Whitespace, Keyword, Whitespace, Name.Function)),
  332. (r'(")([^"]+)(")(\s+)(string|slist|int|real)(\s*)(=>)(\s*)',
  333. bygroups(Punctuation, Name.Variable, Punctuation,
  334. Whitespace, Keyword.Type, Whitespace, Operator, Whitespace)),
  335. (r'(\S+)(\s*)(=>)(\s*)',
  336. bygroups(Keyword.Reserved, Whitespace, Operator, Text)),
  337. (r'"', String, 'string'),
  338. (r'(\w+)(\()', bygroups(Name.Function, Punctuation)),
  339. (r'([\w.!&|()]+)(::)', bygroups(Name.Class, Punctuation)),
  340. (r'(\w+)(:)', bygroups(Keyword.Declaration, Punctuation)),
  341. (r'@[{(][^)}]+[})]', Name.Variable),
  342. (r'[(){},;]', Punctuation),
  343. (r'=>', Operator),
  344. (r'->', Operator),
  345. (r'\d+\.\d+', Number.Float),
  346. (r'\d+', Number.Integer),
  347. (r'\w+', Name.Function),
  348. (r'\s+', Whitespace),
  349. ],
  350. 'string': [
  351. (r'\$[{(]', String.Interpol, 'interpol'),
  352. (r'\\.', String.Escape),
  353. (r'"', String, '#pop'),
  354. (r'\n', String),
  355. (r'.', String),
  356. ],
  357. 'interpol': [
  358. (r'\$[{(]', String.Interpol, '#push'),
  359. (r'[})]', String.Interpol, '#pop'),
  360. (r'[^${()}]+', String.Interpol),
  361. ],
  362. 'arglist': [
  363. (r'\)', Punctuation, '#pop'),
  364. (r',', Punctuation),
  365. (r'\w+', Name.Variable),
  366. (r'\s+', Whitespace),
  367. ],
  368. }
  369. class ApacheConfLexer(RegexLexer):
  370. """
  371. Lexer for configuration files following the Apache config file
  372. format.
  373. """
  374. name = 'ApacheConf'
  375. aliases = ['apacheconf', 'aconf', 'apache']
  376. filenames = ['.htaccess', 'apache.conf', 'apache2.conf']
  377. mimetypes = ['text/x-apacheconf']
  378. url = 'https://httpd.apache.org/docs/current/configuring.html'
  379. version_added = '0.6'
  380. flags = re.MULTILINE | re.IGNORECASE
  381. tokens = {
  382. 'root': [
  383. (r'\s+', Whitespace),
  384. (r'#(.*\\\n)+.*$|(#.*?)$', Comment),
  385. (r'(<[^\s>/][^\s>]*)(?:(\s+)(.*))?(>)',
  386. bygroups(Name.Tag, Whitespace, String, Name.Tag)),
  387. (r'(</[^\s>]+)(>)',
  388. bygroups(Name.Tag, Name.Tag)),
  389. (r'[a-z]\w*', Name.Builtin, 'value'),
  390. (r'\.+', Text),
  391. ],
  392. 'value': [
  393. (r'\\\n', Text),
  394. (r'\n+', Whitespace, '#pop'),
  395. (r'\\', Text),
  396. (r'[^\S\n]+', Whitespace),
  397. (r'\d+\.\d+\.\d+\.\d+(?:/\d+)?', Number),
  398. (r'\d+', Number),
  399. (r'/([*a-z0-9][*\w./-]+)', String.Other),
  400. (r'(on|off|none|any|all|double|email|dns|min|minimal|'
  401. r'os|productonly|full|emerg|alert|crit|error|warn|'
  402. r'notice|info|debug|registry|script|inetd|standalone|'
  403. r'user|group)\b', Keyword),
  404. (r'"([^"\\]*(?:\\([\s\S])[^"\\]*)*)"', String.Double),
  405. (r'[^\s"\\]+', Text)
  406. ],
  407. }
  408. class SquidConfLexer(RegexLexer):
  409. """
  410. Lexer for squid configuration files.
  411. """
  412. name = 'SquidConf'
  413. url = 'http://www.squid-cache.org/'
  414. aliases = ['squidconf', 'squid.conf', 'squid']
  415. filenames = ['squid.conf']
  416. mimetypes = ['text/x-squidconf']
  417. version_added = '0.9'
  418. flags = re.IGNORECASE
  419. keywords = (
  420. "access_log", "acl", "always_direct", "announce_host",
  421. "announce_period", "announce_port", "announce_to", "anonymize_headers",
  422. "append_domain", "as_whois_server", "auth_param_basic",
  423. "authenticate_children", "authenticate_program", "authenticate_ttl",
  424. "broken_posts", "buffered_logs", "cache_access_log", "cache_announce",
  425. "cache_dir", "cache_dns_program", "cache_effective_group",
  426. "cache_effective_user", "cache_host", "cache_host_acl",
  427. "cache_host_domain", "cache_log", "cache_mem", "cache_mem_high",
  428. "cache_mem_low", "cache_mgr", "cachemgr_passwd", "cache_peer",
  429. "cache_peer_access", "cache_replacement_policy", "cache_stoplist",
  430. "cache_stoplist_pattern", "cache_store_log", "cache_swap",
  431. "cache_swap_high", "cache_swap_log", "cache_swap_low", "client_db",
  432. "client_lifetime", "client_netmask", "connect_timeout", "coredump_dir",
  433. "dead_peer_timeout", "debug_options", "delay_access", "delay_class",
  434. "delay_initial_bucket_level", "delay_parameters", "delay_pools",
  435. "deny_info", "dns_children", "dns_defnames", "dns_nameservers",
  436. "dns_testnames", "emulate_httpd_log", "err_html_text",
  437. "fake_user_agent", "firewall_ip", "forwarded_for", "forward_snmpd_port",
  438. "fqdncache_size", "ftpget_options", "ftpget_program", "ftp_list_width",
  439. "ftp_passive", "ftp_user", "half_closed_clients", "header_access",
  440. "header_replace", "hierarchy_stoplist", "high_response_time_warning",
  441. "high_page_fault_warning", "hosts_file", "htcp_port", "http_access",
  442. "http_anonymizer", "httpd_accel", "httpd_accel_host",
  443. "httpd_accel_port", "httpd_accel_uses_host_header",
  444. "httpd_accel_with_proxy", "http_port", "http_reply_access",
  445. "icp_access", "icp_hit_stale", "icp_port", "icp_query_timeout",
  446. "ident_lookup", "ident_lookup_access", "ident_timeout",
  447. "incoming_http_average", "incoming_icp_average", "inside_firewall",
  448. "ipcache_high", "ipcache_low", "ipcache_size", "local_domain",
  449. "local_ip", "logfile_rotate", "log_fqdn", "log_icp_queries",
  450. "log_mime_hdrs", "maximum_object_size", "maximum_single_addr_tries",
  451. "mcast_groups", "mcast_icp_query_timeout", "mcast_miss_addr",
  452. "mcast_miss_encode_key", "mcast_miss_port", "memory_pools",
  453. "memory_pools_limit", "memory_replacement_policy", "mime_table",
  454. "min_http_poll_cnt", "min_icp_poll_cnt", "minimum_direct_hops",
  455. "minimum_object_size", "minimum_retry_timeout", "miss_access",
  456. "negative_dns_ttl", "negative_ttl", "neighbor_timeout",
  457. "neighbor_type_domain", "netdb_high", "netdb_low", "netdb_ping_period",
  458. "netdb_ping_rate", "never_direct", "no_cache", "passthrough_proxy",
  459. "pconn_timeout", "pid_filename", "pinger_program", "positive_dns_ttl",
  460. "prefer_direct", "proxy_auth", "proxy_auth_realm", "query_icmp",
  461. "quick_abort", "quick_abort_max", "quick_abort_min",
  462. "quick_abort_pct", "range_offset_limit", "read_timeout",
  463. "redirect_children", "redirect_program",
  464. "redirect_rewrites_host_header", "reference_age",
  465. "refresh_pattern", "reload_into_ims", "request_body_max_size",
  466. "request_size", "request_timeout", "shutdown_lifetime",
  467. "single_parent_bypass", "siteselect_timeout", "snmp_access",
  468. "snmp_incoming_address", "snmp_port", "source_ping", "ssl_proxy",
  469. "store_avg_object_size", "store_objects_per_bucket",
  470. "strip_query_terms", "swap_level1_dirs", "swap_level2_dirs",
  471. "tcp_incoming_address", "tcp_outgoing_address", "tcp_recv_bufsize",
  472. "test_reachability", "udp_hit_obj", "udp_hit_obj_size",
  473. "udp_incoming_address", "udp_outgoing_address", "unique_hostname",
  474. "unlinkd_program", "uri_whitespace", "useragent_log",
  475. "visible_hostname", "wais_relay", "wais_relay_host", "wais_relay_port",
  476. )
  477. opts = (
  478. "proxy-only", "weight", "ttl", "no-query", "default", "round-robin",
  479. "multicast-responder", "on", "off", "all", "deny", "allow", "via",
  480. "parent", "no-digest", "heap", "lru", "realm", "children", "q1", "q2",
  481. "credentialsttl", "none", "disable", "offline_toggle", "diskd",
  482. )
  483. actions = (
  484. "shutdown", "info", "parameter", "server_list", "client_list",
  485. r'squid.conf',
  486. )
  487. actions_stats = (
  488. "objects", "vm_objects", "utilization", "ipcache", "fqdncache", "dns",
  489. "redirector", "io", "reply_headers", "filedescriptors", "netdb",
  490. )
  491. actions_log = ("status", "enable", "disable", "clear")
  492. acls = (
  493. "url_regex", "urlpath_regex", "referer_regex", "port", "proto",
  494. "req_mime_type", "rep_mime_type", "method", "browser", "user", "src",
  495. "dst", "time", "dstdomain", "ident", "snmp_community",
  496. )
  497. ipv4_group = r'(\d+|0x[0-9a-f]+)'
  498. ipv4 = rf'({ipv4_group}(\.{ipv4_group}){{3}})'
  499. ipv6_group = r'([0-9a-f]{0,4})'
  500. ipv6 = rf'({ipv6_group}(:{ipv6_group}){{1,7}})'
  501. bare_ip = rf'({ipv4}|{ipv6})'
  502. # XXX: /integer is a subnet mark, but what is /IP ?
  503. # There is no test where it is used.
  504. ip = rf'{bare_ip}(/({bare_ip}|\d+))?'
  505. tokens = {
  506. 'root': [
  507. (r'\s+', Whitespace),
  508. (r'#', Comment, 'comment'),
  509. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  510. (words(opts, prefix=r'\b', suffix=r'\b'), Name.Constant),
  511. # Actions
  512. (words(actions, prefix=r'\b', suffix=r'\b'), String),
  513. (words(actions_stats, prefix=r'stats/', suffix=r'\b'), String),
  514. (words(actions_log, prefix=r'log/', suffix=r'='), String),
  515. (words(acls, prefix=r'\b', suffix=r'\b'), Keyword),
  516. (ip, Number.Float),
  517. (r'(?:\b\d+\b(?:-\b\d+|%)?)', Number),
  518. (r'\S+', Text),
  519. ],
  520. 'comment': [
  521. (r'\s*TAG:.*', String.Escape, '#pop'),
  522. (r'.+', Comment, '#pop'),
  523. default('#pop'),
  524. ],
  525. }
  526. class NginxConfLexer(RegexLexer):
  527. """
  528. Lexer for Nginx configuration files.
  529. """
  530. name = 'Nginx configuration file'
  531. url = 'http://nginx.net/'
  532. aliases = ['nginx']
  533. filenames = ['nginx.conf']
  534. mimetypes = ['text/x-nginx-conf']
  535. version_added = '0.11'
  536. tokens = {
  537. 'root': [
  538. (r'(include)(\s+)([^\s;]+)', bygroups(Keyword, Whitespace, Name)),
  539. (r'[^\s;#]+', Keyword, 'stmt'),
  540. include('base'),
  541. ],
  542. 'block': [
  543. (r'\}', Punctuation, '#pop:2'),
  544. (r'[^\s;#]+', Keyword.Namespace, 'stmt'),
  545. include('base'),
  546. ],
  547. 'stmt': [
  548. (r'\{', Punctuation, 'block'),
  549. (r';', Punctuation, '#pop'),
  550. include('base'),
  551. ],
  552. 'base': [
  553. (r'#.*\n', Comment.Single),
  554. (r'on|off', Name.Constant),
  555. (r'\$[^\s;#()]+', Name.Variable),
  556. (r'([a-z0-9.-]+)(:)([0-9]+)',
  557. bygroups(Name, Punctuation, Number.Integer)),
  558. (r'[a-z-]+/[a-z-+]+', String), # mimetype
  559. # (r'[a-zA-Z._-]+', Keyword),
  560. (r'[0-9]+[km]?\b', Number.Integer),
  561. (r'(~)(\s*)([^\s{]+)', bygroups(Punctuation, Whitespace, String.Regex)),
  562. (r'[:=~]', Punctuation),
  563. (r'[^\s;#{}$]+', String), # catch all
  564. (r'/[^\s;#]*', Name), # pathname
  565. (r'\s+', Whitespace),
  566. (r'[$;]', Text), # leftover characters
  567. ],
  568. }
  569. class LighttpdConfLexer(RegexLexer):
  570. """
  571. Lexer for Lighttpd configuration files.
  572. """
  573. name = 'Lighttpd configuration file'
  574. url = 'http://lighttpd.net/'
  575. aliases = ['lighttpd', 'lighty']
  576. filenames = ['lighttpd.conf']
  577. mimetypes = ['text/x-lighttpd-conf']
  578. version_added = '0.11'
  579. tokens = {
  580. 'root': [
  581. (r'#.*\n', Comment.Single),
  582. (r'/\S*', Name), # pathname
  583. (r'[a-zA-Z._-]+', Keyword),
  584. (r'\d+\.\d+\.\d+\.\d+(?:/\d+)?', Number),
  585. (r'[0-9]+', Number),
  586. (r'=>|=~|\+=|==|=|\+', Operator),
  587. (r'\$[A-Z]+', Name.Builtin),
  588. (r'[(){}\[\],]', Punctuation),
  589. (r'"([^"\\]*(?:\\.[^"\\]*)*)"', String.Double),
  590. (r'\s+', Whitespace),
  591. ],
  592. }
  593. class DockerLexer(RegexLexer):
  594. """
  595. Lexer for Docker configuration files.
  596. """
  597. name = 'Docker'
  598. url = 'http://docker.io'
  599. aliases = ['docker', 'dockerfile']
  600. filenames = ['Dockerfile', '*.docker']
  601. mimetypes = ['text/x-dockerfile-config']
  602. version_added = '2.0'
  603. _keywords = (r'(?:MAINTAINER|EXPOSE|WORKDIR|USER|STOPSIGNAL)')
  604. _bash_keywords = (r'(?:RUN|CMD|ENTRYPOINT|ENV|ARG|LABEL|ADD|COPY)')
  605. _lb = r'(?:\s*\\?\s*)' # dockerfile line break regex
  606. flags = re.IGNORECASE | re.MULTILINE
  607. tokens = {
  608. 'root': [
  609. (r'#.*', Comment),
  610. (r'(FROM)([ \t]*)(\S*)([ \t]*)(?:(AS)([ \t]*)(\S*))?',
  611. bygroups(Keyword, Whitespace, String, Whitespace, Keyword, Whitespace, String)),
  612. (rf'(ONBUILD)(\s+)({_lb})', bygroups(Keyword, Whitespace, using(BashLexer))),
  613. (rf'(HEALTHCHECK)(\s+)(({_lb}--\w+=\w+{_lb})*)',
  614. bygroups(Keyword, Whitespace, using(BashLexer))),
  615. (rf'(VOLUME|ENTRYPOINT|CMD|SHELL)(\s+)({_lb})(\[.*?\])',
  616. bygroups(Keyword, Whitespace, using(BashLexer), using(JsonLexer))),
  617. (rf'(LABEL|ENV|ARG)(\s+)(({_lb}\w+=\w+{_lb})*)',
  618. bygroups(Keyword, Whitespace, using(BashLexer))),
  619. (rf'({_keywords}|VOLUME)\b(\s+)(.*)', bygroups(Keyword, Whitespace, String)),
  620. (rf'({_bash_keywords})(\s+)', bygroups(Keyword, Whitespace)),
  621. (r'(.*\\\n)*.+', using(BashLexer)),
  622. ]
  623. }
  624. class TerraformLexer(ExtendedRegexLexer):
  625. """
  626. Lexer for terraformi ``.tf`` files.
  627. """
  628. name = 'Terraform'
  629. url = 'https://www.terraform.io/'
  630. aliases = ['terraform', 'tf', 'hcl']
  631. filenames = ['*.tf', '*.hcl']
  632. mimetypes = ['application/x-tf', 'application/x-terraform']
  633. version_added = '2.1'
  634. classes = ('backend', 'data', 'module', 'output', 'provider',
  635. 'provisioner', 'resource', 'variable')
  636. classes_re = "({})".format(('|').join(classes))
  637. types = ('string', 'number', 'bool', 'list', 'tuple', 'map', 'set', 'object', 'null')
  638. numeric_functions = ('abs', 'ceil', 'floor', 'log', 'max',
  639. 'mix', 'parseint', 'pow', 'signum')
  640. string_functions = ('chomp', 'format', 'formatlist', 'indent',
  641. 'join', 'lower', 'regex', 'regexall', 'replace',
  642. 'split', 'strrev', 'substr', 'title', 'trim',
  643. 'trimprefix', 'trimsuffix', 'trimspace', 'upper'
  644. )
  645. collection_functions = ('alltrue', 'anytrue', 'chunklist', 'coalesce',
  646. 'coalescelist', 'compact', 'concat', 'contains',
  647. 'distinct', 'element', 'flatten', 'index', 'keys',
  648. 'length', 'list', 'lookup', 'map', 'matchkeys',
  649. 'merge', 'range', 'reverse', 'setintersection',
  650. 'setproduct', 'setsubtract', 'setunion', 'slice',
  651. 'sort', 'sum', 'transpose', 'values', 'zipmap'
  652. )
  653. encoding_functions = ('base64decode', 'base64encode', 'base64gzip',
  654. 'csvdecode', 'jsondecode', 'jsonencode', 'textdecodebase64',
  655. 'textencodebase64', 'urlencode', 'yamldecode', 'yamlencode')
  656. filesystem_functions = ('abspath', 'dirname', 'pathexpand', 'basename',
  657. 'file', 'fileexists', 'fileset', 'filebase64', 'templatefile')
  658. date_time_functions = ('formatdate', 'timeadd', 'timestamp')
  659. hash_crypto_functions = ('base64sha256', 'base64sha512', 'bcrypt', 'filebase64sha256',
  660. 'filebase64sha512', 'filemd5', 'filesha1', 'filesha256', 'filesha512',
  661. 'md5', 'rsadecrypt', 'sha1', 'sha256', 'sha512', 'uuid', 'uuidv5')
  662. ip_network_functions = ('cidrhost', 'cidrnetmask', 'cidrsubnet', 'cidrsubnets')
  663. type_conversion_functions = ('can', 'defaults', 'tobool', 'tolist', 'tomap',
  664. 'tonumber', 'toset', 'tostring', 'try')
  665. builtins = numeric_functions + string_functions + collection_functions + encoding_functions +\
  666. filesystem_functions + date_time_functions + hash_crypto_functions + ip_network_functions +\
  667. type_conversion_functions
  668. builtins_re = "({})".format(('|').join(builtins))
  669. def heredoc_callback(self, match, ctx):
  670. # Parse a terraform heredoc
  671. # match: 1 = <<[-]?, 2 = name 3 = rest of line
  672. start = match.start(1)
  673. yield start, Operator, match.group(1) # <<[-]?
  674. yield match.start(2), String.Delimiter, match.group(2) # heredoc name
  675. ctx.pos = match.start(3)
  676. ctx.end = match.end(3)
  677. yield ctx.pos, String.Heredoc, match.group(3)
  678. ctx.pos = match.end()
  679. hdname = match.group(2)
  680. tolerant = True # leading whitespace is always accepted
  681. lines = []
  682. for match in line_re.finditer(ctx.text, ctx.pos):
  683. if tolerant:
  684. check = match.group().strip()
  685. else:
  686. check = match.group().rstrip()
  687. if check == hdname:
  688. for amatch in lines:
  689. yield amatch.start(), String.Heredoc, amatch.group()
  690. yield match.start(), String.Delimiter, match.group()
  691. ctx.pos = match.end()
  692. break
  693. else:
  694. lines.append(match)
  695. else:
  696. # end of heredoc not found -- error!
  697. for amatch in lines:
  698. yield amatch.start(), Error, amatch.group()
  699. ctx.end = len(ctx.text)
  700. tokens = {
  701. 'root': [
  702. include('basic'),
  703. include('whitespace'),
  704. # Strings
  705. (r'(".*")', bygroups(String.Double)),
  706. # Constants
  707. (words(('true', 'false'), prefix=r'\b', suffix=r'\b'), Name.Constant),
  708. # Types
  709. (words(types, prefix=r'\b', suffix=r'\b'), Keyword.Type),
  710. include('identifier'),
  711. include('punctuation'),
  712. (r'[0-9]+', Number),
  713. ],
  714. 'basic': [
  715. (r'\s*/\*', Comment.Multiline, 'comment'),
  716. (r'\s*(#|//).*\n', Comment.Single),
  717. include('whitespace'),
  718. # e.g. terraform {
  719. # e.g. egress {
  720. (r'(\s*)([0-9a-zA-Z-_]+)(\s*)(=?)(\s*)(\{)',
  721. bygroups(Whitespace, Name.Builtin, Whitespace, Operator, Whitespace, Punctuation)),
  722. # Assignment with attributes, e.g. something = ...
  723. (r'(\s*)([0-9a-zA-Z-_]+)(\s*)(=)(\s*)',
  724. bygroups(Whitespace, Name.Attribute, Whitespace, Operator, Whitespace)),
  725. # Assignment with environment variables and similar, e.g. "something" = ...
  726. # or key value assignment, e.g. "SlotName" : ...
  727. (r'(\s*)("\S+")(\s*)([=:])(\s*)',
  728. bygroups(Whitespace, Literal.String.Double, Whitespace, Operator, Whitespace)),
  729. # Functions, e.g. jsonencode(element("value"))
  730. (builtins_re + r'(\()', bygroups(Name.Function, Punctuation)),
  731. # List of attributes, e.g. ignore_changes = [last_modified, filename]
  732. (r'(\[)([a-z_,\s]+)(\])', bygroups(Punctuation, Name.Builtin, Punctuation)),
  733. # e.g. resource "aws_security_group" "allow_tls" {
  734. # e.g. backend "consul" {
  735. (classes_re + r'(\s+)("[0-9a-zA-Z-_]+")?(\s*)("[0-9a-zA-Z-_]+")(\s+)(\{)',
  736. bygroups(Keyword.Reserved, Whitespace, Name.Class, Whitespace, Name.Variable, Whitespace, Punctuation)),
  737. # here-doc style delimited strings
  738. (r'(<<-?)\s*([a-zA-Z_]\w*)(.*?\n)', heredoc_callback),
  739. ],
  740. 'identifier': [
  741. (r'\b(var\.[0-9a-zA-Z-_\.\[\]]+)\b', bygroups(Name.Variable)),
  742. (r'\b([0-9a-zA-Z-_\[\]]+\.[0-9a-zA-Z-_\.\[\]]+)\b',
  743. bygroups(Name.Variable)),
  744. ],
  745. 'punctuation': [
  746. (r'[\[\]()\{\},.?:!=]', Punctuation),
  747. ],
  748. 'comment': [
  749. (r'[^*/]', Comment.Multiline),
  750. (r'/\*', Comment.Multiline, '#push'),
  751. (r'\*/', Comment.Multiline, '#pop'),
  752. (r'[*/]', Comment.Multiline)
  753. ],
  754. 'whitespace': [
  755. (r'\n', Whitespace),
  756. (r'\s+', Whitespace),
  757. (r'(\\)(\n)', bygroups(Text, Whitespace)),
  758. ],
  759. }
  760. class TermcapLexer(RegexLexer):
  761. """
  762. Lexer for termcap database source.
  763. This is very simple and minimal.
  764. """
  765. name = 'Termcap'
  766. aliases = ['termcap']
  767. filenames = ['termcap', 'termcap.src']
  768. mimetypes = []
  769. url = 'https://en.wikipedia.org/wiki/Termcap'
  770. version_added = '2.1'
  771. # NOTE:
  772. # * multiline with trailing backslash
  773. # * separator is ':'
  774. # * to embed colon as data, we must use \072
  775. # * space after separator is not allowed (mayve)
  776. tokens = {
  777. 'root': [
  778. (r'^#.*', Comment),
  779. (r'^[^\s#:|]+', Name.Tag, 'names'),
  780. (r'\s+', Whitespace),
  781. ],
  782. 'names': [
  783. (r'\n', Whitespace, '#pop'),
  784. (r':', Punctuation, 'defs'),
  785. (r'\|', Punctuation),
  786. (r'[^:|]+', Name.Attribute),
  787. ],
  788. 'defs': [
  789. (r'(\\)(\n[ \t]*)', bygroups(Text, Whitespace)),
  790. (r'\n[ \t]*', Whitespace, '#pop:2'),
  791. (r'(#)([0-9]+)', bygroups(Operator, Number)),
  792. (r'=', Operator, 'data'),
  793. (r':', Punctuation),
  794. (r'[^\s:=#]+', Name.Class),
  795. ],
  796. 'data': [
  797. (r'\\072', Literal),
  798. (r':', Punctuation, '#pop'),
  799. (r'[^:\\]+', Literal), # for performance
  800. (r'.', Literal),
  801. ],
  802. }
  803. class TerminfoLexer(RegexLexer):
  804. """
  805. Lexer for terminfo database source.
  806. This is very simple and minimal.
  807. """
  808. name = 'Terminfo'
  809. aliases = ['terminfo']
  810. filenames = ['terminfo', 'terminfo.src']
  811. mimetypes = []
  812. url = 'https://en.wikipedia.org/wiki/Terminfo'
  813. version_added = '2.1'
  814. # NOTE:
  815. # * multiline with leading whitespace
  816. # * separator is ','
  817. # * to embed comma as data, we can use \,
  818. # * space after separator is allowed
  819. tokens = {
  820. 'root': [
  821. (r'^#.*$', Comment),
  822. (r'^[^\s#,|]+', Name.Tag, 'names'),
  823. (r'\s+', Whitespace),
  824. ],
  825. 'names': [
  826. (r'\n', Whitespace, '#pop'),
  827. (r'(,)([ \t]*)', bygroups(Punctuation, Whitespace), 'defs'),
  828. (r'\|', Punctuation),
  829. (r'[^,|]+', Name.Attribute),
  830. ],
  831. 'defs': [
  832. (r'\n[ \t]+', Whitespace),
  833. (r'\n', Whitespace, '#pop:2'),
  834. (r'(#)([0-9]+)', bygroups(Operator, Number)),
  835. (r'=', Operator, 'data'),
  836. (r'(,)([ \t]*)', bygroups(Punctuation, Whitespace)),
  837. (r'[^\s,=#]+', Name.Class),
  838. ],
  839. 'data': [
  840. (r'\\[,\\]', Literal),
  841. (r'(,)([ \t]*)', bygroups(Punctuation, Whitespace), '#pop'),
  842. (r'[^\\,]+', Literal), # for performance
  843. (r'.', Literal),
  844. ],
  845. }
  846. class PkgConfigLexer(RegexLexer):
  847. """
  848. Lexer for pkg-config
  849. (see also `manual page <http://linux.die.net/man/1/pkg-config>`_).
  850. """
  851. name = 'PkgConfig'
  852. url = 'http://www.freedesktop.org/wiki/Software/pkg-config/'
  853. aliases = ['pkgconfig']
  854. filenames = ['*.pc']
  855. mimetypes = []
  856. version_added = '2.1'
  857. tokens = {
  858. 'root': [
  859. (r'#.*$', Comment.Single),
  860. # variable definitions
  861. (r'^(\w+)(=)', bygroups(Name.Attribute, Operator)),
  862. # keyword lines
  863. (r'^([\w.]+)(:)',
  864. bygroups(Name.Tag, Punctuation), 'spvalue'),
  865. # variable references
  866. include('interp'),
  867. # fallback
  868. (r'\s+', Whitespace),
  869. (r'[^${}#=:\n.]+', Text),
  870. (r'.', Text),
  871. ],
  872. 'interp': [
  873. # you can escape literal "$" as "$$"
  874. (r'\$\$', Text),
  875. # variable references
  876. (r'\$\{', String.Interpol, 'curly'),
  877. ],
  878. 'curly': [
  879. (r'\}', String.Interpol, '#pop'),
  880. (r'\w+', Name.Attribute),
  881. ],
  882. 'spvalue': [
  883. include('interp'),
  884. (r'#.*$', Comment.Single, '#pop'),
  885. (r'\n', Whitespace, '#pop'),
  886. # fallback
  887. (r'\s+', Whitespace),
  888. (r'[^${}#\n\s]+', Text),
  889. (r'.', Text),
  890. ],
  891. }
  892. class PacmanConfLexer(RegexLexer):
  893. """
  894. Lexer for pacman.conf.
  895. Actually, IniLexer works almost fine for this format,
  896. but it yield error token. It is because pacman.conf has
  897. a form without assignment like:
  898. UseSyslog
  899. Color
  900. TotalDownload
  901. CheckSpace
  902. VerbosePkgLists
  903. These are flags to switch on.
  904. """
  905. name = 'PacmanConf'
  906. url = 'https://www.archlinux.org/pacman/pacman.conf.5.html'
  907. aliases = ['pacmanconf']
  908. filenames = ['pacman.conf']
  909. mimetypes = []
  910. version_added = '2.1'
  911. tokens = {
  912. 'root': [
  913. # comment
  914. (r'#.*$', Comment.Single),
  915. # section header
  916. (r'^(\s*)(\[.*?\])(\s*)$', bygroups(Whitespace, Keyword, Whitespace)),
  917. # variable definitions
  918. # (Leading space is allowed...)
  919. (r'(\w+)(\s*)(=)',
  920. bygroups(Name.Attribute, Whitespace, Operator)),
  921. # flags to on
  922. (r'^(\s*)(\w+)(\s*)$',
  923. bygroups(Whitespace, Name.Attribute, Whitespace)),
  924. # built-in special values
  925. (words((
  926. '$repo', # repository
  927. '$arch', # architecture
  928. '%o', # outfile
  929. '%u', # url
  930. ), suffix=r'\b'),
  931. Name.Variable),
  932. # fallback
  933. (r'\s+', Whitespace),
  934. (r'.', Text),
  935. ],
  936. }
  937. class AugeasLexer(RegexLexer):
  938. """
  939. Lexer for Augeas.
  940. """
  941. name = 'Augeas'
  942. url = 'http://augeas.net'
  943. aliases = ['augeas']
  944. filenames = ['*.aug']
  945. version_added = '2.4'
  946. tokens = {
  947. 'root': [
  948. (r'(module)(\s*)([^\s=]+)', bygroups(Keyword.Namespace, Whitespace, Name.Namespace)),
  949. (r'(let)(\s*)([^\s=]+)', bygroups(Keyword.Declaration, Whitespace, Name.Variable)),
  950. (r'(del|store|value|counter|seq|key|label|autoload|incl|excl|transform|test|get|put)(\s+)', bygroups(Name.Builtin, Whitespace)),
  951. (r'(\()([^:]+)(\:)(unit|string|regexp|lens|tree|filter)(\))', bygroups(Punctuation, Name.Variable, Punctuation, Keyword.Type, Punctuation)),
  952. (r'\(\*', Comment.Multiline, 'comment'),
  953. (r'[*+\-.;=?|]', Operator),
  954. (r'[()\[\]{}]', Operator),
  955. (r'"', String.Double, 'string'),
  956. (r'\/', String.Regex, 'regex'),
  957. (r'([A-Z]\w*)(\.)(\w+)', bygroups(Name.Namespace, Punctuation, Name.Variable)),
  958. (r'.', Name.Variable),
  959. (r'\s+', Whitespace),
  960. ],
  961. 'string': [
  962. (r'\\.', String.Escape),
  963. (r'[^"]', String.Double),
  964. (r'"', String.Double, '#pop'),
  965. ],
  966. 'regex': [
  967. (r'\\.', String.Escape),
  968. (r'[^/]', String.Regex),
  969. (r'\/', String.Regex, '#pop'),
  970. ],
  971. 'comment': [
  972. (r'[^*)]', Comment.Multiline),
  973. (r'\(\*', Comment.Multiline, '#push'),
  974. (r'\*\)', Comment.Multiline, '#pop'),
  975. (r'[)*]', Comment.Multiline)
  976. ],
  977. }
  978. class TOMLLexer(RegexLexer):
  979. """
  980. Lexer for TOML, a simple language for config files.
  981. """
  982. name = 'TOML'
  983. aliases = ['toml']
  984. filenames = ['*.toml', 'Pipfile', 'poetry.lock']
  985. mimetypes = ['application/toml']
  986. url = 'https://toml.io'
  987. version_added = '2.4'
  988. # Based on the TOML spec: https://toml.io/en/v1.0.0
  989. # The following is adapted from CPython's tomllib:
  990. _time = r"\d\d:\d\d(:\d\d(\.\d+)?)?"
  991. _datetime = rf"""(?x)
  992. \d\d\d\d-\d\d-\d\d # date, e.g., 1988-10-27
  993. (
  994. [Tt ] {_time} # optional time
  995. (
  996. [Zz]|[+-]\d\d:\d\d # optional time offset
  997. )?
  998. )?
  999. """
  1000. tokens = {
  1001. 'root': [
  1002. # Note that we make an effort in order to distinguish
  1003. # moments at which we're parsing a key and moments at
  1004. # which we're parsing a value. In the TOML code
  1005. #
  1006. # 1234 = 1234
  1007. #
  1008. # the first "1234" should be Name, the second Integer.
  1009. # Whitespace
  1010. (r'\s+', Whitespace),
  1011. # Comment
  1012. (r'#.*', Comment.Single),
  1013. # Assignment keys
  1014. include('key'),
  1015. # After "=", find a value
  1016. (r'(=)(\s*)', bygroups(Operator, Whitespace), 'value'),
  1017. # Table header
  1018. (r'\[\[?', Keyword, 'table-key'),
  1019. ],
  1020. 'key': [
  1021. # Start of bare key (only ASCII is allowed here).
  1022. (r'[A-Za-z0-9_-]+', Name),
  1023. # Quoted key
  1024. (r'"', String.Double, 'basic-string'),
  1025. (r"'", String.Single, 'literal-string'),
  1026. # Dots act as separators in keys
  1027. (r'\.', Punctuation),
  1028. ],
  1029. 'table-key': [
  1030. # This is like 'key', but highlights the name components
  1031. # and separating dots as Keyword because it looks better
  1032. # when the whole table header is Keyword. We do highlight
  1033. # strings as strings though.
  1034. # Start of bare key (only ASCII is allowed here).
  1035. (r'[A-Za-z0-9_-]+', Keyword),
  1036. (r'"', String.Double, 'basic-string'),
  1037. (r"'", String.Single, 'literal-string'),
  1038. (r'\.', Keyword),
  1039. (r'\]\]?', Keyword, '#pop'),
  1040. # Inline whitespace allowed
  1041. (r'[ \t]+', Whitespace),
  1042. ],
  1043. 'value': [
  1044. # Datetime, baretime
  1045. (_datetime, Literal.Date, '#pop'),
  1046. (_time, Literal.Date, '#pop'),
  1047. # Recognize as float if there is a fractional part
  1048. # and/or an exponent.
  1049. (r'[+-]?\d[0-9_]*[eE][+-]?\d[0-9_]*', Number.Float, '#pop'),
  1050. (r'[+-]?\d[0-9_]*\.\d[0-9_]*([eE][+-]?\d[0-9_]*)?',
  1051. Number.Float, '#pop'),
  1052. # Infinities and NaN
  1053. (r'[+-]?(inf|nan)', Number.Float, '#pop'),
  1054. # Integers
  1055. (r'-?0b[01_]+', Number.Bin, '#pop'),
  1056. (r'-?0o[0-7_]+', Number.Oct, '#pop'),
  1057. (r'-?0x[0-9a-fA-F_]+', Number.Hex, '#pop'),
  1058. (r'[+-]?[0-9_]+', Number.Integer, '#pop'),
  1059. # Strings
  1060. (r'"""', String.Double, ('#pop', 'multiline-basic-string')),
  1061. (r'"', String.Double, ('#pop', 'basic-string')),
  1062. (r"'''", String.Single, ('#pop', 'multiline-literal-string')),
  1063. (r"'", String.Single, ('#pop', 'literal-string')),
  1064. # Booleans
  1065. (r'true|false', Keyword.Constant, '#pop'),
  1066. # Start of array
  1067. (r'\[', Punctuation, ('#pop', 'array')),
  1068. # Start of inline table
  1069. (r'\{', Punctuation, ('#pop', 'inline-table')),
  1070. ],
  1071. 'array': [
  1072. # Whitespace, including newlines, is ignored inside arrays,
  1073. # and comments are allowed.
  1074. (r'\s+', Whitespace),
  1075. (r'#.*', Comment.Single),
  1076. # Delimiters
  1077. (r',', Punctuation),
  1078. # End of array
  1079. (r'\]', Punctuation, '#pop'),
  1080. # Parse a value and come back
  1081. default('value'),
  1082. ],
  1083. 'inline-table': [
  1084. # Whitespace (since TOML 1.1.0, same as in array)
  1085. (r'\s+', Whitespace),
  1086. (r'#.*', Comment.Single),
  1087. # Keys
  1088. include('key'),
  1089. # Values
  1090. (r'(=)(\s*)', bygroups(Punctuation, Whitespace), 'value'),
  1091. # Delimiters
  1092. (r',', Punctuation),
  1093. # End of inline table
  1094. (r'\}', Punctuation, '#pop'),
  1095. ],
  1096. 'basic-string': [
  1097. (r'"', String.Double, '#pop'),
  1098. include('escapes'),
  1099. (r'[^"\\]+', String.Double),
  1100. ],
  1101. 'literal-string': [
  1102. (r".*?'", String.Single, '#pop'),
  1103. ],
  1104. 'multiline-basic-string': [
  1105. (r'"""', String.Double, '#pop'),
  1106. (r'(\\)(\n)', bygroups(String.Escape, Whitespace)),
  1107. include('escapes'),
  1108. (r'[^"\\]+', String.Double),
  1109. (r'"', String.Double),
  1110. ],
  1111. 'multiline-literal-string': [
  1112. (r"'''", String.Single, '#pop'),
  1113. (r"[^']+", String.Single),
  1114. (r"'", String.Single),
  1115. ],
  1116. 'escapes': [
  1117. (r'\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}', String.Escape),
  1118. (r'\\.', String.Escape),
  1119. ],
  1120. }
  1121. class NestedTextLexer(RegexLexer):
  1122. """
  1123. Lexer for *NextedText*, a human-friendly data format.
  1124. .. versionchanged:: 2.16
  1125. Added support for *NextedText* v3.0.
  1126. """
  1127. name = 'NestedText'
  1128. url = 'https://nestedtext.org'
  1129. aliases = ['nestedtext', 'nt']
  1130. filenames = ['*.nt']
  1131. version_added = '2.9'
  1132. tokens = {
  1133. 'root': [
  1134. # Comment: # ...
  1135. (r'^([ ]*)(#.*)$', bygroups(Whitespace, Comment)),
  1136. # Inline dictionary: {...}
  1137. (r'^([ ]*)(\{)', bygroups(Whitespace, Punctuation), 'inline_dict'),
  1138. # Inline list: [...]
  1139. (r'^([ ]*)(\[)', bygroups(Whitespace, Punctuation), 'inline_list'),
  1140. # empty multiline string item: >
  1141. (r'^([ ]*)(>)$', bygroups(Whitespace, Punctuation)),
  1142. # multiline string item: > ...
  1143. (r'^([ ]*)(>)( )(.*?)([ \t]*)$', bygroups(Whitespace, Punctuation, Whitespace, Text, Whitespace)),
  1144. # empty list item: -
  1145. (r'^([ ]*)(-)$', bygroups(Whitespace, Punctuation)),
  1146. # list item: - ...
  1147. (r'^([ ]*)(-)( )(.*?)([ \t]*)$', bygroups(Whitespace, Punctuation, Whitespace, Text, Whitespace)),
  1148. # empty multiline key item: :
  1149. (r'^([ ]*)(:)$', bygroups(Whitespace, Punctuation)),
  1150. # multiline key item: : ...
  1151. (r'^([ ]*)(:)( )([^\n]*?)([ \t]*)$', bygroups(Whitespace, Punctuation, Whitespace, Name.Tag, Whitespace)),
  1152. # empty dict key item: ...:
  1153. (r'^([ ]*)([^\{\[\s].*?)(:)$', bygroups(Whitespace, Name.Tag, Punctuation)),
  1154. # dict key item: ...: ...
  1155. (r'^([ ]*)([^\{\[\s].*?)(:)( )(.*?)([ \t]*)$', bygroups(Whitespace, Name.Tag, Punctuation, Whitespace, Text, Whitespace)),
  1156. ],
  1157. 'inline_list': [
  1158. include('whitespace'),
  1159. (r'[^\{\}\[\],\s]+', Text),
  1160. include('inline_value'),
  1161. (r',', Punctuation),
  1162. (r'\]', Punctuation, '#pop'),
  1163. (r'\n', Error, '#pop'),
  1164. ],
  1165. 'inline_dict': [
  1166. include('whitespace'),
  1167. (r'[^\{\}\[\],:\s]+', Name.Tag),
  1168. (r':', Punctuation, 'inline_dict_value'),
  1169. (r'\}', Punctuation, '#pop'),
  1170. (r'\n', Error, '#pop'),
  1171. ],
  1172. 'inline_dict_value': [
  1173. include('whitespace'),
  1174. (r'[^\{\}\[\],:\s]+', Text),
  1175. include('inline_value'),
  1176. (r',', Punctuation, '#pop'),
  1177. (r'\}', Punctuation, '#pop:2'),
  1178. ],
  1179. 'inline_value': [
  1180. include('whitespace'),
  1181. (r'\{', Punctuation, 'inline_dict'),
  1182. (r'\[', Punctuation, 'inline_list'),
  1183. ],
  1184. 'whitespace': [
  1185. (r'[ \t]+', Whitespace),
  1186. ],
  1187. }
  1188. class SingularityLexer(RegexLexer):
  1189. """
  1190. Lexer for Singularity definition files.
  1191. """
  1192. name = 'Singularity'
  1193. url = 'https://www.sylabs.io/guides/3.0/user-guide/definition_files.html'
  1194. aliases = ['singularity']
  1195. filenames = ['*.def', 'Singularity']
  1196. version_added = '2.6'
  1197. flags = re.IGNORECASE | re.MULTILINE | re.DOTALL
  1198. _headers = r'^(\s*)(bootstrap|from|osversion|mirrorurl|include|registry|namespace|includecmd)(:)'
  1199. _section = r'^(%(?:pre|post|setup|environment|help|labels|test|runscript|files|startscript))(\s*)'
  1200. _appsect = r'^(%app(?:install|help|run|labels|env|test|files))(\s*)'
  1201. tokens = {
  1202. 'root': [
  1203. (_section, bygroups(Generic.Heading, Whitespace), 'script'),
  1204. (_appsect, bygroups(Generic.Heading, Whitespace), 'script'),
  1205. (_headers, bygroups(Whitespace, Keyword, Text)),
  1206. (r'\s*#.*?\n', Comment),
  1207. (r'\b(([0-9]+\.?[0-9]*)|(\.[0-9]+))\b', Number),
  1208. (r'[ \t]+', Whitespace),
  1209. (r'(?!^\s*%).', Text),
  1210. ],
  1211. 'script': [
  1212. (r'(.+?(?=^\s*%))|(.*)', using(BashLexer), '#pop'),
  1213. ],
  1214. }
  1215. def analyse_text(text):
  1216. """This is a quite simple script file, but there are a few keywords
  1217. which seem unique to this language."""
  1218. result = 0
  1219. if re.search(r'\b(?:osversion|includecmd|mirrorurl)\b', text, re.IGNORECASE):
  1220. result += 0.5
  1221. if re.search(SingularityLexer._section[1:], text):
  1222. result += 0.49
  1223. return result
  1224. class UnixConfigLexer(RegexLexer):
  1225. """
  1226. Lexer for Unix/Linux config files using colon-separated values, e.g.
  1227. * ``/etc/group``
  1228. * ``/etc/passwd``
  1229. * ``/etc/shadow``
  1230. """
  1231. name = 'Unix/Linux config files'
  1232. aliases = ['unixconfig', 'linuxconfig']
  1233. filenames = []
  1234. url = 'https://en.wikipedia.org/wiki/Configuration_file#Unix_and_Unix-like_operating_systems'
  1235. version_added = '2.12'
  1236. tokens = {
  1237. 'root': [
  1238. (r'^#.*', Comment),
  1239. (r'\n', Whitespace),
  1240. (r':', Punctuation),
  1241. (r'[0-9]+', Number),
  1242. (r'((?!\n)[a-zA-Z0-9\_\-\s\(\),]){2,}', Text),
  1243. (r'[^:\n]+', String),
  1244. ],
  1245. }
  1246. class CaddyfileLexer(RegexLexer):
  1247. """
  1248. Lexer for Caddyfile, the configuration file format of the Caddy web server.
  1249. """
  1250. name = 'Caddyfile'
  1251. url = 'https://caddyserver.com/docs/caddyfile'
  1252. aliases = ['caddyfile', 'caddy']
  1253. filenames = ['Caddyfile']
  1254. version_added = '2.21'
  1255. # The standard HTTP directives shipped with Caddy.
  1256. directives = (
  1257. 'abort', 'acme_server', 'basic_auth', 'bind', 'encode', 'error',
  1258. 'file_server', 'forward_auth', 'handle', 'handle_errors',
  1259. 'handle_path', 'header', 'import', 'invoke', 'log', 'map', 'metrics',
  1260. 'php_fastcgi', 'push', 'redir', 'request_body', 'request_header',
  1261. 'respond', 'reverse_proxy', 'rewrite', 'root', 'route', 'templates',
  1262. 'tls', 'tracing', 'try_files', 'uri', 'vars',
  1263. )
  1264. tokens = {
  1265. 'root': [
  1266. # Directives are only keywords as the first token on a line, so
  1267. # anchor them to the start of the (possibly indented) line. This
  1268. # consumes the preceding newline(s)/indentation itself, otherwise
  1269. # the generic whitespace rule below would swallow it first.
  1270. (r'((?:\A|\s*\n)[ \t]*)(' + '|'.join(directives) + r')\b',
  1271. bygroups(Whitespace, Keyword)),
  1272. (r'\s+', Whitespace),
  1273. (r'#.*', Comment.Single),
  1274. # Heredoc: <<LABEL <args> ... LABEL. The remainder of the opening
  1275. # line (e.g. a status code) is not part of the body, so re-lex it;
  1276. # the closing label must stand alone on its line.
  1277. (r'(<<)([a-zA-Z_]\w*)(.*\n)((?:.*\n)*?)([ \t]*)(\2)(?=[ \t]*$)',
  1278. bygroups(Operator, String.Delimiter, using(this),
  1279. String.Heredoc, Whitespace, String.Delimiter)),
  1280. # Placeholders: {path}, {http.request.uri}, {$ENV}, {args[0]}, ...
  1281. (r'\{[^{}\s]+\}', Name.Variable),
  1282. # Blocks and snippet parentheses
  1283. (r'[(){}]', Punctuation),
  1284. # Named matcher, e.g. @api
  1285. (r'@[\w.-]+', Name.Decorator),
  1286. (r'"', String.Double, 'string'),
  1287. (r'`', String.Backtick, 'backtick'),
  1288. # Numbers, durations and sizes (443, 200, 30s, 1.5m, 10MB); a
  1289. # trailing dot or more digits/letters means it is an address or
  1290. # hostname, which must stay Text.
  1291. (r'\d+(?:\.\d+)?[a-zA-Z]*(?=[\s#{}()"`]|$)', Number),
  1292. (r'[^\s#{}()"`]+', Text),
  1293. ],
  1294. 'string': [
  1295. (r'\\.', String.Escape),
  1296. (r'\{[^{}\s]+\}', Name.Variable),
  1297. (r'[^"\\{]+', String.Double),
  1298. (r'"', String.Double, '#pop'),
  1299. (r'.', String.Double),
  1300. ],
  1301. 'backtick': [
  1302. (r'[^`]+', String.Backtick),
  1303. (r'`', String.Backtick, '#pop'),
  1304. ],
  1305. }