configs.py 28 KB

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