configs.py 49 KB

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