configs.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.configs
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for configuration file formats.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, default, words, bygroups, include, using
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation, Whitespace, Literal
  13. from pygments.lexers.shell import BashLexer
  14. from pygments.lexers.data import JsonLexer
  15. __all__ = ['IniLexer', 'RegeditLexer', 'PropertiesLexer', 'KconfigLexer',
  16. 'Cfengine3Lexer', 'ApacheConfLexer', 'SquidConfLexer',
  17. 'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer',
  18. 'TerraformLexer', 'TermcapLexer', 'TerminfoLexer',
  19. 'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer']
  20. class IniLexer(RegexLexer):
  21. """
  22. Lexer for configuration files in INI style.
  23. """
  24. name = 'INI'
  25. aliases = ['ini', 'cfg', 'dosini']
  26. filenames = ['*.ini', '*.cfg', '*.inf']
  27. mimetypes = ['text/x-ini', 'text/inf']
  28. tokens = {
  29. 'root': [
  30. (r'\s+', Text),
  31. (r'[;#].*', Comment.Single),
  32. (r'\[.*?\]$', Keyword),
  33. (r'(.*?)([ \t]*)(=)([ \t]*)(.*(?:\n[ \t].+)*)',
  34. bygroups(Name.Attribute, Text, Operator, Text, String)),
  35. # standalone option, supported by some INI parsers
  36. (r'(.+?)$', Name.Attribute),
  37. ],
  38. }
  39. def analyse_text(text):
  40. npos = text.find('\n')
  41. if npos < 3:
  42. return False
  43. return text[0] == '[' and text[npos-1] == ']'
  44. class RegeditLexer(RegexLexer):
  45. """
  46. Lexer for `Windows Registry
  47. <http://en.wikipedia.org/wiki/Windows_Registry#.REG_files>`_ files produced
  48. by regedit.
  49. .. versionadded:: 1.6
  50. """
  51. name = 'reg'
  52. aliases = ['registry']
  53. filenames = ['*.reg']
  54. mimetypes = ['text/x-windows-registry']
  55. tokens = {
  56. 'root': [
  57. (r'Windows Registry Editor.*', Text),
  58. (r'\s+', Text),
  59. (r'[;#].*', Comment.Single),
  60. (r'(\[)(-?)(HKEY_[A-Z_]+)(.*?\])$',
  61. bygroups(Keyword, Operator, Name.Builtin, Keyword)),
  62. # String keys, which obey somewhat normal escaping
  63. (r'("(?:\\"|\\\\|[^"])+")([ \t]*)(=)([ \t]*)',
  64. bygroups(Name.Attribute, Text, Operator, Text),
  65. 'value'),
  66. # Bare keys (includes @)
  67. (r'(.*?)([ \t]*)(=)([ \t]*)',
  68. bygroups(Name.Attribute, Text, Operator, Text),
  69. 'value'),
  70. ],
  71. 'value': [
  72. (r'-', Operator, '#pop'), # delete value
  73. (r'(dword|hex(?:\([0-9a-fA-F]\))?)(:)([0-9a-fA-F,]+)',
  74. bygroups(Name.Variable, Punctuation, Number), '#pop'),
  75. # As far as I know, .reg files do not support line continuation.
  76. (r'.+', String, '#pop'),
  77. default('#pop'),
  78. ]
  79. }
  80. def analyse_text(text):
  81. return text.startswith('Windows Registry Editor')
  82. class PropertiesLexer(RegexLexer):
  83. """
  84. Lexer for configuration files in Java's properties format.
  85. Note: trailing whitespace counts as part of the value as per spec
  86. .. versionadded:: 1.4
  87. """
  88. name = 'Properties'
  89. aliases = ['properties', 'jproperties']
  90. filenames = ['*.properties']
  91. mimetypes = ['text/x-java-properties']
  92. tokens = {
  93. 'root': [
  94. (r'^(\w+)([ \t])(\w+\s*)$', bygroups(Name.Attribute, Text, String)),
  95. (r'^\w+(\\[ \t]\w*)*$', Name.Attribute),
  96. (r'(^ *)([#!].*)', bygroups(Text, Comment)),
  97. # More controversial comments
  98. (r'(^ *)((?:;|//).*)', bygroups(Text, Comment)),
  99. (r'(.*?)([ \t]*)([=:])([ \t]*)(.*(?:(?<=\\)\n.*)*)',
  100. bygroups(Name.Attribute, Text, Operator, Text, String)),
  101. (r'\s', Text),
  102. ],
  103. }
  104. def _rx_indent(level):
  105. # Kconfig *always* interprets a tab as 8 spaces, so this is the default.
  106. # Edit this if you are in an environment where KconfigLexer gets expanded
  107. # input (tabs expanded to spaces) and the expansion tab width is != 8,
  108. # e.g. in connection with Trac (trac.ini, [mimeviewer], tab_width).
  109. # Value range here is 2 <= {tab_width} <= 8.
  110. tab_width = 8
  111. # Regex matching a given indentation {level}, assuming that indentation is
  112. # a multiple of {tab_width}. In other cases there might be problems.
  113. if tab_width == 2:
  114. space_repeat = '+'
  115. else:
  116. space_repeat = '{1,%d}' % (tab_width - 1)
  117. if level == 1:
  118. level_repeat = ''
  119. else:
  120. level_repeat = '{%s}' % level
  121. return r'(?:\t| %s\t| {%s})%s.*\n' % (space_repeat, tab_width, level_repeat)
  122. class KconfigLexer(RegexLexer):
  123. """
  124. For Linux-style Kconfig files.
  125. .. versionadded:: 1.6
  126. """
  127. name = 'Kconfig'
  128. aliases = ['kconfig', 'menuconfig', 'linux-config', 'kernel-config']
  129. # Adjust this if new kconfig file names appear in your environment
  130. filenames = ['Kconfig', '*Config.in*', 'external.in*',
  131. 'standard-modules.in']
  132. mimetypes = ['text/x-kconfig']
  133. # No re.MULTILINE, indentation-aware help text needs line-by-line handling
  134. flags = 0
  135. def call_indent(level):
  136. # If indentation >= {level} is detected, enter state 'indent{level}'
  137. return (_rx_indent(level), String.Doc, 'indent%s' % level)
  138. def do_indent(level):
  139. # Print paragraphs of indentation level >= {level} as String.Doc,
  140. # ignoring blank lines. Then return to 'root' state.
  141. return [
  142. (_rx_indent(level), String.Doc),
  143. (r'\s*\n', Text),
  144. default('#pop:2')
  145. ]
  146. tokens = {
  147. 'root': [
  148. (r'\s+', Text),
  149. (r'#.*?\n', Comment.Single),
  150. (words((
  151. 'mainmenu', 'config', 'menuconfig', 'choice', 'endchoice',
  152. 'comment', 'menu', 'endmenu', 'visible if', 'if', 'endif',
  153. 'source', 'prompt', 'select', 'depends on', 'default',
  154. 'range', 'option'), suffix=r'\b'),
  155. Keyword),
  156. (r'(---help---|help)[\t ]*\n', Keyword, 'help'),
  157. (r'(bool|tristate|string|hex|int|defconfig_list|modules|env)\b',
  158. Name.Builtin),
  159. (r'[!=&|]', Operator),
  160. (r'[()]', Punctuation),
  161. (r'[0-9]+', Number.Integer),
  162. (r"'(''|[^'])*'", String.Single),
  163. (r'"(""|[^"])*"', String.Double),
  164. (r'\S+', Text),
  165. ],
  166. # Help text is indented, multi-line and ends when a lower indentation
  167. # level is detected.
  168. 'help': [
  169. # Skip blank lines after help token, if any
  170. (r'\s*\n', Text),
  171. # Determine the first help line's indentation level heuristically(!).
  172. # Attention: this is not perfect, but works for 99% of "normal"
  173. # indentation schemes up to a max. indentation level of 7.
  174. call_indent(7),
  175. call_indent(6),
  176. call_indent(5),
  177. call_indent(4),
  178. call_indent(3),
  179. call_indent(2),
  180. call_indent(1),
  181. default('#pop'), # for incomplete help sections without text
  182. ],
  183. # Handle text for indentation levels 7 to 1
  184. 'indent7': do_indent(7),
  185. 'indent6': do_indent(6),
  186. 'indent5': do_indent(5),
  187. 'indent4': do_indent(4),
  188. 'indent3': do_indent(3),
  189. 'indent2': do_indent(2),
  190. 'indent1': do_indent(1),
  191. }
  192. class Cfengine3Lexer(RegexLexer):
  193. """
  194. Lexer for `CFEngine3 <http://cfengine.org>`_ policy files.
  195. .. versionadded:: 1.5
  196. """
  197. name = 'CFEngine3'
  198. aliases = ['cfengine3', 'cf3']
  199. filenames = ['*.cf']
  200. mimetypes = []
  201. tokens = {
  202. 'root': [
  203. (r'#.*?\n', Comment),
  204. (r'(body)(\s+)(\S+)(\s+)(control)',
  205. bygroups(Keyword, Text, Keyword, Text, Keyword)),
  206. (r'(body|bundle)(\s+)(\S+)(\s+)(\w+)(\()',
  207. bygroups(Keyword, Text, Keyword, Text, Name.Function, Punctuation),
  208. 'arglist'),
  209. (r'(body|bundle)(\s+)(\S+)(\s+)(\w+)',
  210. bygroups(Keyword, Text, Keyword, Text, Name.Function)),
  211. (r'(")([^"]+)(")(\s+)(string|slist|int|real)(\s*)(=>)(\s*)',
  212. bygroups(Punctuation, Name.Variable, Punctuation,
  213. Text, Keyword.Type, Text, Operator, Text)),
  214. (r'(\S+)(\s*)(=>)(\s*)',
  215. bygroups(Keyword.Reserved, Text, Operator, Text)),
  216. (r'"', String, 'string'),
  217. (r'(\w+)(\()', bygroups(Name.Function, Punctuation)),
  218. (r'([\w.!&|()]+)(::)', bygroups(Name.Class, Punctuation)),
  219. (r'(\w+)(:)', bygroups(Keyword.Declaration, Punctuation)),
  220. (r'@[{(][^)}]+[})]', Name.Variable),
  221. (r'[(){},;]', Punctuation),
  222. (r'=>', Operator),
  223. (r'->', Operator),
  224. (r'\d+\.\d+', Number.Float),
  225. (r'\d+', Number.Integer),
  226. (r'\w+', Name.Function),
  227. (r'\s+', Text),
  228. ],
  229. 'string': [
  230. (r'\$[{(]', String.Interpol, 'interpol'),
  231. (r'\\.', String.Escape),
  232. (r'"', String, '#pop'),
  233. (r'\n', String),
  234. (r'.', String),
  235. ],
  236. 'interpol': [
  237. (r'\$[{(]', String.Interpol, '#push'),
  238. (r'[})]', String.Interpol, '#pop'),
  239. (r'[^${()}]+', String.Interpol),
  240. ],
  241. 'arglist': [
  242. (r'\)', Punctuation, '#pop'),
  243. (r',', Punctuation),
  244. (r'\w+', Name.Variable),
  245. (r'\s+', Text),
  246. ],
  247. }
  248. class ApacheConfLexer(RegexLexer):
  249. """
  250. Lexer for configuration files following the Apache config file
  251. format.
  252. .. versionadded:: 0.6
  253. """
  254. name = 'ApacheConf'
  255. aliases = ['apacheconf', 'aconf', 'apache']
  256. filenames = ['.htaccess', 'apache.conf', 'apache2.conf']
  257. mimetypes = ['text/x-apacheconf']
  258. flags = re.MULTILINE | re.IGNORECASE
  259. tokens = {
  260. 'root': [
  261. (r'\s+', Text),
  262. (r'#(.*\\\n)+.*$|(#.*?)$', Comment),
  263. (r'(<[^\s>]+)(?:(\s+)(.*))?(>)',
  264. bygroups(Name.Tag, Text, String, Name.Tag)),
  265. (r'([a-z]\w*)(\s+)',
  266. bygroups(Name.Builtin, Text), 'value'),
  267. (r'\.+', Text),
  268. ],
  269. 'value': [
  270. (r'\\\n', Text),
  271. (r'$', Text, '#pop'),
  272. (r'\\', Text),
  273. (r'[^\S\n]+', Text),
  274. (r'\d+\.\d+\.\d+\.\d+(?:/\d+)?', Number),
  275. (r'\d+', Number),
  276. (r'/([a-z0-9][\w./-]+)', String.Other),
  277. (r'(on|off|none|any|all|double|email|dns|min|minimal|'
  278. r'os|productonly|full|emerg|alert|crit|error|warn|'
  279. r'notice|info|debug|registry|script|inetd|standalone|'
  280. r'user|group)\b', Keyword),
  281. (r'"([^"\\]*(?:\\(.|[\n])[^"\\]*)*)"', String.Double),
  282. (r'[^\s"\\]+', Text)
  283. ],
  284. }
  285. class SquidConfLexer(RegexLexer):
  286. """
  287. Lexer for `squid <http://www.squid-cache.org/>`_ configuration files.
  288. .. versionadded:: 0.9
  289. """
  290. name = 'SquidConf'
  291. aliases = ['squidconf', 'squid.conf', 'squid']
  292. filenames = ['squid.conf']
  293. mimetypes = ['text/x-squidconf']
  294. flags = re.IGNORECASE
  295. keywords = (
  296. "access_log", "acl", "always_direct", "announce_host",
  297. "announce_period", "announce_port", "announce_to", "anonymize_headers",
  298. "append_domain", "as_whois_server", "auth_param_basic",
  299. "authenticate_children", "authenticate_program", "authenticate_ttl",
  300. "broken_posts", "buffered_logs", "cache_access_log", "cache_announce",
  301. "cache_dir", "cache_dns_program", "cache_effective_group",
  302. "cache_effective_user", "cache_host", "cache_host_acl",
  303. "cache_host_domain", "cache_log", "cache_mem", "cache_mem_high",
  304. "cache_mem_low", "cache_mgr", "cachemgr_passwd", "cache_peer",
  305. "cache_peer_access", "cahce_replacement_policy", "cache_stoplist",
  306. "cache_stoplist_pattern", "cache_store_log", "cache_swap",
  307. "cache_swap_high", "cache_swap_log", "cache_swap_low", "client_db",
  308. "client_lifetime", "client_netmask", "connect_timeout", "coredump_dir",
  309. "dead_peer_timeout", "debug_options", "delay_access", "delay_class",
  310. "delay_initial_bucket_level", "delay_parameters", "delay_pools",
  311. "deny_info", "dns_children", "dns_defnames", "dns_nameservers",
  312. "dns_testnames", "emulate_httpd_log", "err_html_text",
  313. "fake_user_agent", "firewall_ip", "forwarded_for", "forward_snmpd_port",
  314. "fqdncache_size", "ftpget_options", "ftpget_program", "ftp_list_width",
  315. "ftp_passive", "ftp_user", "half_closed_clients", "header_access",
  316. "header_replace", "hierarchy_stoplist", "high_response_time_warning",
  317. "high_page_fault_warning", "hosts_file", "htcp_port", "http_access",
  318. "http_anonymizer", "httpd_accel", "httpd_accel_host",
  319. "httpd_accel_port", "httpd_accel_uses_host_header",
  320. "httpd_accel_with_proxy", "http_port", "http_reply_access",
  321. "icp_access", "icp_hit_stale", "icp_port", "icp_query_timeout",
  322. "ident_lookup", "ident_lookup_access", "ident_timeout",
  323. "incoming_http_average", "incoming_icp_average", "inside_firewall",
  324. "ipcache_high", "ipcache_low", "ipcache_size", "local_domain",
  325. "local_ip", "logfile_rotate", "log_fqdn", "log_icp_queries",
  326. "log_mime_hdrs", "maximum_object_size", "maximum_single_addr_tries",
  327. "mcast_groups", "mcast_icp_query_timeout", "mcast_miss_addr",
  328. "mcast_miss_encode_key", "mcast_miss_port", "memory_pools",
  329. "memory_pools_limit", "memory_replacement_policy", "mime_table",
  330. "min_http_poll_cnt", "min_icp_poll_cnt", "minimum_direct_hops",
  331. "minimum_object_size", "minimum_retry_timeout", "miss_access",
  332. "negative_dns_ttl", "negative_ttl", "neighbor_timeout",
  333. "neighbor_type_domain", "netdb_high", "netdb_low", "netdb_ping_period",
  334. "netdb_ping_rate", "never_direct", "no_cache", "passthrough_proxy",
  335. "pconn_timeout", "pid_filename", "pinger_program", "positive_dns_ttl",
  336. "prefer_direct", "proxy_auth", "proxy_auth_realm", "query_icmp",
  337. "quick_abort", "quick_abort_max", "quick_abort_min",
  338. "quick_abort_pct", "range_offset_limit", "read_timeout",
  339. "redirect_children", "redirect_program",
  340. "redirect_rewrites_host_header", "reference_age",
  341. "refresh_pattern", "reload_into_ims", "request_body_max_size",
  342. "request_size", "request_timeout", "shutdown_lifetime",
  343. "single_parent_bypass", "siteselect_timeout", "snmp_access",
  344. "snmp_incoming_address", "snmp_port", "source_ping", "ssl_proxy",
  345. "store_avg_object_size", "store_objects_per_bucket",
  346. "strip_query_terms", "swap_level1_dirs", "swap_level2_dirs",
  347. "tcp_incoming_address", "tcp_outgoing_address", "tcp_recv_bufsize",
  348. "test_reachability", "udp_hit_obj", "udp_hit_obj_size",
  349. "udp_incoming_address", "udp_outgoing_address", "unique_hostname",
  350. "unlinkd_program", "uri_whitespace", "useragent_log",
  351. "visible_hostname", "wais_relay", "wais_relay_host", "wais_relay_port",
  352. )
  353. opts = (
  354. "proxy-only", "weight", "ttl", "no-query", "default", "round-robin",
  355. "multicast-responder", "on", "off", "all", "deny", "allow", "via",
  356. "parent", "no-digest", "heap", "lru", "realm", "children", "q1", "q2",
  357. "credentialsttl", "none", "disable", "offline_toggle", "diskd",
  358. )
  359. actions = (
  360. "shutdown", "info", "parameter", "server_list", "client_list",
  361. r'squid.conf',
  362. )
  363. actions_stats = (
  364. "objects", "vm_objects", "utilization", "ipcache", "fqdncache", "dns",
  365. "redirector", "io", "reply_headers", "filedescriptors", "netdb",
  366. )
  367. actions_log = ("status", "enable", "disable", "clear")
  368. acls = (
  369. "url_regex", "urlpath_regex", "referer_regex", "port", "proto",
  370. "req_mime_type", "rep_mime_type", "method", "browser", "user", "src",
  371. "dst", "time", "dstdomain", "ident", "snmp_community",
  372. )
  373. ip_re = (
  374. r'(?:(?:(?:[3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}|0x0*[0-9a-f]{1,2}|'
  375. r'0+[1-3]?[0-7]{0,2})(?:\.(?:[3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}|'
  376. r'0x0*[0-9a-f]{1,2}|0+[1-3]?[0-7]{0,2})){3})|(?!.*::.*::)(?:(?!:)|'
  377. r':(?=:))(?:[0-9a-f]{0,4}(?:(?<=::)|(?<!::):)){6}(?:[0-9a-f]{0,4}'
  378. r'(?:(?<=::)|(?<!::):)[0-9a-f]{0,4}(?:(?<=::)|(?<!:)|(?<=:)(?<!::):)|'
  379. r'(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-4]|2[0-4]\d|1\d\d|'
  380. r'[1-9]?\d)){3}))'
  381. )
  382. tokens = {
  383. 'root': [
  384. (r'\s+', Whitespace),
  385. (r'#', Comment, 'comment'),
  386. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  387. (words(opts, prefix=r'\b', suffix=r'\b'), Name.Constant),
  388. # Actions
  389. (words(actions, prefix=r'\b', suffix=r'\b'), String),
  390. (words(actions_stats, prefix=r'stats/', suffix=r'\b'), String),
  391. (words(actions_log, prefix=r'log/', suffix=r'='), String),
  392. (words(acls, prefix=r'\b', suffix=r'\b'), Keyword),
  393. (ip_re + r'(?:/(?:' + ip_re + r'|\b\d+\b))?', Number.Float),
  394. (r'(?:\b\d+\b(?:-\b\d+|%)?)', Number),
  395. (r'\S+', Text),
  396. ],
  397. 'comment': [
  398. (r'\s*TAG:.*', String.Escape, '#pop'),
  399. (r'.+', Comment, '#pop'),
  400. default('#pop'),
  401. ],
  402. }
  403. class NginxConfLexer(RegexLexer):
  404. """
  405. Lexer for `Nginx <http://nginx.net/>`_ configuration files.
  406. .. versionadded:: 0.11
  407. """
  408. name = 'Nginx configuration file'
  409. aliases = ['nginx']
  410. filenames = ['nginx.conf']
  411. mimetypes = ['text/x-nginx-conf']
  412. tokens = {
  413. 'root': [
  414. (r'(include)(\s+)([^\s;]+)', bygroups(Keyword, Text, Name)),
  415. (r'[^\s;#]+', Keyword, 'stmt'),
  416. include('base'),
  417. ],
  418. 'block': [
  419. (r'\}', Punctuation, '#pop:2'),
  420. (r'[^\s;#]+', Keyword.Namespace, 'stmt'),
  421. include('base'),
  422. ],
  423. 'stmt': [
  424. (r'\{', Punctuation, 'block'),
  425. (r';', Punctuation, '#pop'),
  426. include('base'),
  427. ],
  428. 'base': [
  429. (r'#.*\n', Comment.Single),
  430. (r'on|off', Name.Constant),
  431. (r'\$[^\s;#()]+', Name.Variable),
  432. (r'([a-z0-9.-]+)(:)([0-9]+)',
  433. bygroups(Name, Punctuation, Number.Integer)),
  434. (r'[a-z-]+/[a-z-+]+', String), # mimetype
  435. # (r'[a-zA-Z._-]+', Keyword),
  436. (r'[0-9]+[km]?\b', Number.Integer),
  437. (r'(~)(\s*)([^\s{]+)', bygroups(Punctuation, Text, String.Regex)),
  438. (r'[:=~]', Punctuation),
  439. (r'[^\s;#{}$]+', String), # catch all
  440. (r'/[^\s;#]*', Name), # pathname
  441. (r'\s+', Text),
  442. (r'[$;]', Text), # leftover characters
  443. ],
  444. }
  445. class LighttpdConfLexer(RegexLexer):
  446. """
  447. Lexer for `Lighttpd <http://lighttpd.net/>`_ configuration files.
  448. .. versionadded:: 0.11
  449. """
  450. name = 'Lighttpd configuration file'
  451. aliases = ['lighty', 'lighttpd']
  452. filenames = []
  453. mimetypes = ['text/x-lighttpd-conf']
  454. tokens = {
  455. 'root': [
  456. (r'#.*\n', Comment.Single),
  457. (r'/\S*', Name), # pathname
  458. (r'[a-zA-Z._-]+', Keyword),
  459. (r'\d+\.\d+\.\d+\.\d+(?:/\d+)?', Number),
  460. (r'[0-9]+', Number),
  461. (r'=>|=~|\+=|==|=|\+', Operator),
  462. (r'\$[A-Z]+', Name.Builtin),
  463. (r'[(){}\[\],]', Punctuation),
  464. (r'"([^"\\]*(?:\\.[^"\\]*)*)"', String.Double),
  465. (r'\s+', Text),
  466. ],
  467. }
  468. class DockerLexer(RegexLexer):
  469. """
  470. Lexer for `Docker <http://docker.io>`_ configuration files.
  471. .. versionadded:: 2.0
  472. """
  473. name = 'Docker'
  474. aliases = ['docker', 'dockerfile']
  475. filenames = ['Dockerfile', '*.docker']
  476. mimetypes = ['text/x-dockerfile-config']
  477. _keywords = (r'(?:MAINTAINER|EXPOSE|WORKDIR|USER|STOPSIGNAL)')
  478. _bash_keywords = (r'(?:RUN|CMD|ENTRYPOINT|ENV|ARG|LABEL|ADD|COPY)')
  479. _lb = r'(?:\s*\\?\s*)' # dockerfile line break regex
  480. flags = re.IGNORECASE | re.MULTILINE
  481. tokens = {
  482. 'root': [
  483. (r'#.*', Comment),
  484. (r'(FROM)([ \t]*)(\S*)([ \t]*)(?:(AS)([ \t]*)(\S*))?',
  485. bygroups(Keyword, Text, String, Text, Keyword, Text, String)),
  486. (r'(ONBUILD)(%s)' % (_lb,), bygroups(Keyword, using(BashLexer))),
  487. (r'(HEALTHCHECK)((%s--\w+=\w+%s)*)' % (_lb, _lb),
  488. bygroups(Keyword, using(BashLexer))),
  489. (r'(VOLUME|ENTRYPOINT|CMD|SHELL)(%s)(\[.*?\])' % (_lb,),
  490. bygroups(Keyword, using(BashLexer), using(JsonLexer))),
  491. (r'(LABEL|ENV|ARG)((%s\w+=\w+%s)*)' % (_lb, _lb),
  492. bygroups(Keyword, using(BashLexer))),
  493. (r'(%s|VOLUME)\b(.*)' % (_keywords), bygroups(Keyword, String)),
  494. (r'(%s)' % (_bash_keywords,), Keyword),
  495. (r'(.*\\\n)*.+', using(BashLexer)),
  496. ]
  497. }
  498. class TerraformLexer(RegexLexer):
  499. """
  500. Lexer for `terraformi .tf files <https://www.terraform.io/>`_.
  501. .. versionadded:: 2.1
  502. """
  503. name = 'Terraform'
  504. aliases = ['terraform', 'tf']
  505. filenames = ['*.tf']
  506. mimetypes = ['application/x-tf', 'application/x-terraform']
  507. embedded_keywords = ('ingress', 'egress', 'listener', 'default',
  508. 'connection', 'alias', 'terraform', 'tags', 'vars',
  509. 'config', 'lifecycle', 'timeouts')
  510. tokens = {
  511. 'root': [
  512. include('string'),
  513. include('punctuation'),
  514. include('curly'),
  515. include('basic'),
  516. include('whitespace'),
  517. (r'[0-9]+', Number),
  518. ],
  519. 'basic': [
  520. (words(('true', 'false'), prefix=r'\b', suffix=r'\b'), Keyword.Type),
  521. (r'\s*/\*', Comment.Multiline, 'comment'),
  522. (r'\s*#.*\n', Comment.Single),
  523. (r'(.*?)(\s*)(=)', bygroups(Name.Attribute, Text, Operator)),
  524. (words(('variable', 'resource', 'provider', 'provisioner', 'module',
  525. 'backend', 'data', 'output'), prefix=r'\b', suffix=r'\b'),
  526. Keyword.Reserved, 'function'),
  527. (words(embedded_keywords, prefix=r'\b', suffix=r'\b'),
  528. Keyword.Declaration),
  529. (r'\$\{', String.Interpol, 'var_builtin'),
  530. ],
  531. 'function': [
  532. (r'(\s+)(".*")(\s+)', bygroups(Text, String, Text)),
  533. include('punctuation'),
  534. include('curly'),
  535. ],
  536. 'var_builtin': [
  537. (r'\$\{', String.Interpol, '#push'),
  538. (words(('concat', 'file', 'join', 'lookup', 'element'),
  539. prefix=r'\b', suffix=r'\b'), Name.Builtin),
  540. include('string'),
  541. include('punctuation'),
  542. (r'\s+', Text),
  543. (r'\}', String.Interpol, '#pop'),
  544. ],
  545. 'string': [
  546. (r'(".*")', bygroups(String.Double)),
  547. ],
  548. 'punctuation': [
  549. (r'[\[\](),.]', Punctuation),
  550. ],
  551. # Keep this seperate from punctuation - we sometimes want to use different
  552. # Tokens for { }
  553. 'curly': [
  554. (r'\{', Text.Punctuation),
  555. (r'\}', Text.Punctuation),
  556. ],
  557. 'comment': [
  558. (r'[^*/]', Comment.Multiline),
  559. (r'/\*', Comment.Multiline, '#push'),
  560. (r'\*/', Comment.Multiline, '#pop'),
  561. (r'[*/]', Comment.Multiline)
  562. ],
  563. 'whitespace': [
  564. (r'\n', Text),
  565. (r'\s+', Text),
  566. (r'\\\n', Text),
  567. ],
  568. }
  569. class TermcapLexer(RegexLexer):
  570. """
  571. Lexer for termcap database source.
  572. This is very simple and minimal.
  573. .. versionadded:: 2.1
  574. """
  575. name = 'Termcap'
  576. aliases = ['termcap']
  577. filenames = ['termcap', 'termcap.src']
  578. mimetypes = []
  579. # NOTE:
  580. # * multiline with trailing backslash
  581. # * separator is ':'
  582. # * to embed colon as data, we must use \072
  583. # * space after separator is not allowed (mayve)
  584. tokens = {
  585. 'root': [
  586. (r'^#.*$', Comment),
  587. (r'^[^\s#:|]+', Name.Tag, 'names'),
  588. ],
  589. 'names': [
  590. (r'\n', Text, '#pop'),
  591. (r':', Punctuation, 'defs'),
  592. (r'\|', Punctuation),
  593. (r'[^:|]+', Name.Attribute),
  594. ],
  595. 'defs': [
  596. (r'\\\n[ \t]*', Text),
  597. (r'\n[ \t]*', Text, '#pop:2'),
  598. (r'(#)([0-9]+)', bygroups(Operator, Number)),
  599. (r'=', Operator, 'data'),
  600. (r':', Punctuation),
  601. (r'[^\s:=#]+', Name.Class),
  602. ],
  603. 'data': [
  604. (r'\\072', Literal),
  605. (r':', Punctuation, '#pop'),
  606. (r'[^:\\]+', Literal), # for performance
  607. (r'.', Literal),
  608. ],
  609. }
  610. class TerminfoLexer(RegexLexer):
  611. """
  612. Lexer for terminfo database source.
  613. This is very simple and minimal.
  614. .. versionadded:: 2.1
  615. """
  616. name = 'Terminfo'
  617. aliases = ['terminfo']
  618. filenames = ['terminfo', 'terminfo.src']
  619. mimetypes = []
  620. # NOTE:
  621. # * multiline with leading whitespace
  622. # * separator is ','
  623. # * to embed comma as data, we can use \,
  624. # * space after separator is allowed
  625. tokens = {
  626. 'root': [
  627. (r'^#.*$', Comment),
  628. (r'^[^\s#,|]+', Name.Tag, 'names'),
  629. ],
  630. 'names': [
  631. (r'\n', Text, '#pop'),
  632. (r'(,)([ \t]*)', bygroups(Punctuation, Text), 'defs'),
  633. (r'\|', Punctuation),
  634. (r'[^,|]+', Name.Attribute),
  635. ],
  636. 'defs': [
  637. (r'\n[ \t]+', Text),
  638. (r'\n', Text, '#pop:2'),
  639. (r'(#)([0-9]+)', bygroups(Operator, Number)),
  640. (r'=', Operator, 'data'),
  641. (r'(,)([ \t]*)', bygroups(Punctuation, Text)),
  642. (r'[^\s,=#]+', Name.Class),
  643. ],
  644. 'data': [
  645. (r'\\[,\\]', Literal),
  646. (r'(,)([ \t]*)', bygroups(Punctuation, Text), '#pop'),
  647. (r'[^\\,]+', Literal), # for performance
  648. (r'.', Literal),
  649. ],
  650. }
  651. class PkgConfigLexer(RegexLexer):
  652. """
  653. Lexer for `pkg-config
  654. <http://www.freedesktop.org/wiki/Software/pkg-config/>`_
  655. (see also `manual page <http://linux.die.net/man/1/pkg-config>`_).
  656. .. versionadded:: 2.1
  657. """
  658. name = 'PkgConfig'
  659. aliases = ['pkgconfig']
  660. filenames = ['*.pc']
  661. mimetypes = []
  662. tokens = {
  663. 'root': [
  664. (r'#.*$', Comment.Single),
  665. # variable definitions
  666. (r'^(\w+)(=)', bygroups(Name.Attribute, Operator)),
  667. # keyword lines
  668. (r'^([\w.]+)(:)',
  669. bygroups(Name.Tag, Punctuation), 'spvalue'),
  670. # variable references
  671. include('interp'),
  672. # fallback
  673. (r'[^${}#=:\n.]+', Text),
  674. (r'.', Text),
  675. ],
  676. 'interp': [
  677. # you can escape literal "$" as "$$"
  678. (r'\$\$', Text),
  679. # variable references
  680. (r'\$\{', String.Interpol, 'curly'),
  681. ],
  682. 'curly': [
  683. (r'\}', String.Interpol, '#pop'),
  684. (r'\w+', Name.Attribute),
  685. ],
  686. 'spvalue': [
  687. include('interp'),
  688. (r'#.*$', Comment.Single, '#pop'),
  689. (r'\n', Text, '#pop'),
  690. # fallback
  691. (r'[^${}#\n]+', Text),
  692. (r'.', Text),
  693. ],
  694. }
  695. class PacmanConfLexer(RegexLexer):
  696. """
  697. Lexer for `pacman.conf
  698. <https://www.archlinux.org/pacman/pacman.conf.5.html>`_.
  699. Actually, IniLexer works almost fine for this format,
  700. but it yield error token. It is because pacman.conf has
  701. a form without assignment like:
  702. UseSyslog
  703. Color
  704. TotalDownload
  705. CheckSpace
  706. VerbosePkgLists
  707. These are flags to switch on.
  708. .. versionadded:: 2.1
  709. """
  710. name = 'PacmanConf'
  711. aliases = ['pacmanconf']
  712. filenames = ['pacman.conf']
  713. mimetypes = []
  714. tokens = {
  715. 'root': [
  716. # comment
  717. (r'#.*$', Comment.Single),
  718. # section header
  719. (r'^\s*\[.*?\]\s*$', Keyword),
  720. # variable definitions
  721. # (Leading space is allowed...)
  722. (r'(\w+)(\s*)(=)',
  723. bygroups(Name.Attribute, Text, Operator)),
  724. # flags to on
  725. (r'^(\s*)(\w+)(\s*)$',
  726. bygroups(Text, Name.Attribute, Text)),
  727. # built-in special values
  728. (words((
  729. '$repo', # repository
  730. '$arch', # architecture
  731. '%o', # outfile
  732. '%u', # url
  733. ), suffix=r'\b'),
  734. Name.Variable),
  735. # fallback
  736. (r'.', Text),
  737. ],
  738. }
  739. class AugeasLexer(RegexLexer):
  740. """
  741. Lexer for `Augeas <http://augeas.net>`_.
  742. .. versionadded:: 2.4
  743. """
  744. name = 'Augeas'
  745. aliases = ['augeas']
  746. filenames = ['*.aug']
  747. tokens = {
  748. 'root': [
  749. (r'(module)(\s*)([^\s=]+)', bygroups(Keyword.Namespace, Text, Name.Namespace)),
  750. (r'(let)(\s*)([^\s=]+)', bygroups(Keyword.Declaration, Text, Name.Variable)),
  751. (r'(del|store|value|counter|seq|key|label|autoload|incl|excl|transform|test|get|put)(\s+)', bygroups(Name.Builtin, Text)),
  752. (r'(\()([^:]+)(\:)(unit|string|regexp|lens|tree|filter)(\))', bygroups(Punctuation, Name.Variable, Punctuation, Keyword.Type, Punctuation)),
  753. (r'\(\*', Comment.Multiline, 'comment'),
  754. (r'[*+\-.;=?|]', Operator),
  755. (r'[()\[\]{}]', Operator),
  756. (r'"', String.Double, 'string'),
  757. (r'\/', String.Regex, 'regex'),
  758. (r'([A-Z]\w*)(\.)(\w+)', bygroups(Name.Namespace, Punctuation, Name.Variable)),
  759. (r'.', Name.Variable),
  760. (r'\s', Text),
  761. ],
  762. 'string': [
  763. (r'\\.', String.Escape),
  764. (r'[^"]', String.Double),
  765. (r'"', String.Double, '#pop'),
  766. ],
  767. 'regex': [
  768. (r'\\.', String.Escape),
  769. (r'[^/]', String.Regex),
  770. (r'\/', String.Regex, '#pop'),
  771. ],
  772. 'comment': [
  773. (r'[^*)]', Comment.Multiline),
  774. (r'\(\*', Comment.Multiline, '#push'),
  775. (r'\*\)', Comment.Multiline, '#pop'),
  776. (r'[)*]', Comment.Multiline)
  777. ],
  778. }
  779. class TOMLLexer(RegexLexer):
  780. """
  781. Lexer for `TOML <https://github.com/toml-lang/toml>`_, a simple language
  782. for config files.
  783. .. versionadded:: 2.4
  784. """
  785. name = 'TOML'
  786. aliases = ['toml']
  787. filenames = ['*.toml']
  788. tokens = {
  789. 'root': [
  790. # Basics, comments, strings
  791. (r'\s+', Text),
  792. (r'#.*?$', Comment.Single),
  793. # Basic string
  794. (r'"(\\\\|\\"|[^"])*"', String),
  795. # Literal string
  796. (r'\'\'\'(.*)\'\'\'', String),
  797. (r'\'[^\']*\'', String),
  798. (r'(true|false)$', Keyword.Constant),
  799. (r'[a-zA-Z_][\w\-]*', Name),
  800. (r'\[.*?\]$', Keyword),
  801. # Datetime
  802. # TODO this needs to be expanded, as TOML is rather flexible:
  803. # https://github.com/toml-lang/toml#offset-date-time
  804. (r'\d{4}-\d{2}-\d{2}(?:T| )\d{2}:\d{2}:\d{2}(?:Z|[-+]\d{2}:\d{2})', Number.Integer),
  805. # Numbers
  806. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
  807. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  808. # Handle +-inf, +-infinity, +-nan
  809. (r'[+-]?(?:(inf(?:inity)?)|nan)', Number.Float),
  810. (r'[+-]?\d+', Number.Integer),
  811. # Punctuation
  812. (r'[]{}:(),;[]', Punctuation),
  813. (r'\.', Punctuation),
  814. # Operators
  815. (r'=', Operator)
  816. ]
  817. }