dsls.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.dsls
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Lexers for various domain-specific languages.
  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 ExtendedRegexLexer, RegexLexer, bygroups, words, \
  11. include, default, this, using, combined
  12. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  13. Number, Punctuation, Literal, Whitespace
  14. __all__ = ['ProtoBufLexer', 'ZeekLexer', 'PuppetLexer', 'RslLexer',
  15. 'MscgenLexer', 'VGLLexer', 'AlloyLexer', 'PanLexer',
  16. 'CrmshLexer', 'ThriftLexer', 'FlatlineLexer', 'SnowballLexer']
  17. class ProtoBufLexer(RegexLexer):
  18. """
  19. Lexer for `Protocol Buffer <http://code.google.com/p/protobuf/>`_
  20. definition files.
  21. .. versionadded:: 1.4
  22. """
  23. name = 'Protocol Buffer'
  24. aliases = ['protobuf', 'proto']
  25. filenames = ['*.proto']
  26. tokens = {
  27. 'root': [
  28. (r'[ \t]+', Text),
  29. (r'[,;{}\[\]()<>]', Punctuation),
  30. (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
  31. (r'/(\\\n)?\*(.|\n)*?\*(\\\n)?/', Comment.Multiline),
  32. (words((
  33. 'import', 'option', 'optional', 'required', 'repeated',
  34. 'reserved', 'default', 'packed', 'ctype', 'extensions', 'to',
  35. 'max', 'rpc', 'returns', 'oneof'), prefix=r'\b', suffix=r'\b'),
  36. Keyword),
  37. (words((
  38. 'int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64',
  39. 'fixed32', 'fixed64', 'sfixed32', 'sfixed64',
  40. 'float', 'double', 'bool', 'string', 'bytes'), suffix=r'\b'),
  41. Keyword.Type),
  42. (r'(true|false)\b', Keyword.Constant),
  43. (r'(package)(\s+)', bygroups(Keyword.Namespace, Text), 'package'),
  44. (r'(message|extend)(\s+)',
  45. bygroups(Keyword.Declaration, Text), 'message'),
  46. (r'(enum|group|service)(\s+)',
  47. bygroups(Keyword.Declaration, Text), 'type'),
  48. (r'\".*?\"', String),
  49. (r'\'.*?\'', String),
  50. (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
  51. (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
  52. (r'(\-?(inf|nan))\b', Number.Float),
  53. (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
  54. (r'0[0-7]+[LlUu]*', Number.Oct),
  55. (r'\d+[LlUu]*', Number.Integer),
  56. (r'[+-=]', Operator),
  57. (r'([a-zA-Z_][\w.]*)([ \t]*)(=)',
  58. bygroups(Name.Attribute, Text, Operator)),
  59. (r'[a-zA-Z_][\w.]*', Name),
  60. ],
  61. 'package': [
  62. (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'),
  63. default('#pop'),
  64. ],
  65. 'message': [
  66. (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
  67. default('#pop'),
  68. ],
  69. 'type': [
  70. (r'[a-zA-Z_]\w*', Name, '#pop'),
  71. default('#pop'),
  72. ],
  73. }
  74. class ThriftLexer(RegexLexer):
  75. """
  76. For `Thrift <https://thrift.apache.org/>`__ interface definitions.
  77. .. versionadded:: 2.1
  78. """
  79. name = 'Thrift'
  80. aliases = ['thrift']
  81. filenames = ['*.thrift']
  82. mimetypes = ['application/x-thrift']
  83. tokens = {
  84. 'root': [
  85. include('whitespace'),
  86. include('comments'),
  87. (r'"', String.Double, combined('stringescape', 'dqs')),
  88. (r'\'', String.Single, combined('stringescape', 'sqs')),
  89. (r'(namespace)(\s+)',
  90. bygroups(Keyword.Namespace, Text.Whitespace), 'namespace'),
  91. (r'(enum|union|struct|service|exception)(\s+)',
  92. bygroups(Keyword.Declaration, Text.Whitespace), 'class'),
  93. (r'((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)' # return arguments
  94. r'((?:[^\W\d]|\$)[\w$]*)' # method name
  95. r'(\s*)(\()', # signature start
  96. bygroups(using(this), Name.Function, Text, Operator)),
  97. include('keywords'),
  98. include('numbers'),
  99. (r'[&=]', Operator),
  100. (r'[:;,{}()<>\[\]]', Punctuation),
  101. (r'[a-zA-Z_](\.\w|\w)*', Name),
  102. ],
  103. 'whitespace': [
  104. (r'\n', Text.Whitespace),
  105. (r'\s+', Text.Whitespace),
  106. ],
  107. 'comments': [
  108. (r'#.*$', Comment),
  109. (r'//.*?\n', Comment),
  110. (r'/\*[\w\W]*?\*/', Comment.Multiline),
  111. ],
  112. 'stringescape': [
  113. (r'\\([\\nrt"\'])', String.Escape),
  114. ],
  115. 'dqs': [
  116. (r'"', String.Double, '#pop'),
  117. (r'[^\\"\n]+', String.Double),
  118. ],
  119. 'sqs': [
  120. (r"'", String.Single, '#pop'),
  121. (r'[^\\\'\n]+', String.Single),
  122. ],
  123. 'namespace': [
  124. (r'[a-z*](\.\w|\w)*', Name.Namespace, '#pop'),
  125. default('#pop'),
  126. ],
  127. 'class': [
  128. (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
  129. default('#pop'),
  130. ],
  131. 'keywords': [
  132. (r'(async|oneway|extends|throws|required|optional)\b', Keyword),
  133. (r'(true|false)\b', Keyword.Constant),
  134. (r'(const|typedef)\b', Keyword.Declaration),
  135. (words((
  136. 'cpp_namespace', 'cpp_include', 'cpp_type', 'java_package',
  137. 'cocoa_prefix', 'csharp_namespace', 'delphi_namespace',
  138. 'php_namespace', 'py_module', 'perl_package',
  139. 'ruby_namespace', 'smalltalk_category', 'smalltalk_prefix',
  140. 'xsd_all', 'xsd_optional', 'xsd_nillable', 'xsd_namespace',
  141. 'xsd_attrs', 'include'), suffix=r'\b'),
  142. Keyword.Namespace),
  143. (words((
  144. 'void', 'bool', 'byte', 'i16', 'i32', 'i64', 'double',
  145. 'string', 'binary', 'map', 'list', 'set', 'slist',
  146. 'senum'), suffix=r'\b'),
  147. Keyword.Type),
  148. (words((
  149. 'BEGIN', 'END', '__CLASS__', '__DIR__', '__FILE__',
  150. '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__',
  151. 'abstract', 'alias', 'and', 'args', 'as', 'assert', 'begin',
  152. 'break', 'case', 'catch', 'class', 'clone', 'continue',
  153. 'declare', 'def', 'default', 'del', 'delete', 'do', 'dynamic',
  154. 'elif', 'else', 'elseif', 'elsif', 'end', 'enddeclare',
  155. 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile',
  156. 'ensure', 'except', 'exec', 'finally', 'float', 'for',
  157. 'foreach', 'function', 'global', 'goto', 'if', 'implements',
  158. 'import', 'in', 'inline', 'instanceof', 'interface', 'is',
  159. 'lambda', 'module', 'native', 'new', 'next', 'nil', 'not',
  160. 'or', 'pass', 'public', 'print', 'private', 'protected',
  161. 'raise', 'redo', 'rescue', 'retry', 'register', 'return',
  162. 'self', 'sizeof', 'static', 'super', 'switch', 'synchronized',
  163. 'then', 'this', 'throw', 'transient', 'try', 'undef',
  164. 'unless', 'unsigned', 'until', 'use', 'var', 'virtual',
  165. 'volatile', 'when', 'while', 'with', 'xor', 'yield'),
  166. prefix=r'\b', suffix=r'\b'),
  167. Keyword.Reserved),
  168. ],
  169. 'numbers': [
  170. (r'[+-]?(\d+\.\d+([eE][+-]?\d+)?|\.?\d+[eE][+-]?\d+)', Number.Float),
  171. (r'[+-]?0x[0-9A-Fa-f]+', Number.Hex),
  172. (r'[+-]?[0-9]+', Number.Integer),
  173. ],
  174. }
  175. class ZeekLexer(RegexLexer):
  176. """
  177. For `Zeek <https://www.zeek.org/>`_ scripts.
  178. .. versionadded:: 2.5
  179. """
  180. name = 'Zeek'
  181. aliases = ['zeek', 'bro']
  182. filenames = ['*.zeek', '*.bro']
  183. _hex = r'[0-9a-fA-F]'
  184. _float = r'((\d*\.?\d+)|(\d+\.?\d*))([eE][-+]?\d+)?'
  185. _h = r'[A-Za-z0-9][-A-Za-z0-9]*'
  186. tokens = {
  187. 'root': [
  188. include('whitespace'),
  189. include('comments'),
  190. include('directives'),
  191. include('attributes'),
  192. include('types'),
  193. include('keywords'),
  194. include('literals'),
  195. include('operators'),
  196. include('punctuation'),
  197. (r'((?:[A-Za-z_]\w*)(?:::(?:[A-Za-z_]\w*))*)(?=\s*\()',
  198. Name.Function),
  199. include('identifiers'),
  200. ],
  201. 'whitespace': [
  202. (r'\n', Text),
  203. (r'\s+', Text),
  204. (r'\\\n', Text),
  205. ],
  206. 'comments': [
  207. (r'#.*$', Comment),
  208. ],
  209. 'directives': [
  210. (r'@(load-plugin|load-sigs|load|unload)\b.*$', Comment.Preproc),
  211. (r'@(DEBUG|DIR|FILENAME|deprecated|if|ifdef|ifndef|else|endif)\b', Comment.Preproc),
  212. (r'(@prefixes)\s*(\+?=).*$', Comment.Preproc),
  213. ],
  214. 'attributes': [
  215. (words(('redef', 'priority', 'log', 'optional', 'default', 'add_func',
  216. 'delete_func', 'expire_func', 'read_expire', 'write_expire',
  217. 'create_expire', 'synchronized', 'persistent', 'rotate_interval',
  218. 'rotate_size', 'encrypt', 'raw_output', 'mergeable', 'error_handler',
  219. 'type_column', 'deprecated'),
  220. prefix=r'&', suffix=r'\b'),
  221. Keyword.Pseudo),
  222. ],
  223. 'types': [
  224. (words(('any',
  225. 'enum', 'record', 'set', 'table', 'vector',
  226. 'function', 'hook', 'event',
  227. 'addr', 'bool', 'count', 'double', 'file', 'int', 'interval',
  228. 'pattern', 'port', 'string', 'subnet', 'time'),
  229. suffix=r'\b'),
  230. Keyword.Type),
  231. (r'(opaque)(\s+)(of)(\s+)((?:[A-Za-z_]\w*)(?:::(?:[A-Za-z_]\w*))*)\b',
  232. bygroups(Keyword.Type, Text, Operator.Word, Text, Keyword.Type)),
  233. (r'(type)(\s+)((?:[A-Za-z_]\w*)(?:::(?:[A-Za-z_]\w*))*)(\s*)(:)(\s*)\b(record|enum)\b',
  234. bygroups(Keyword, Text, Name.Class, Text, Operator, Text, Keyword.Type)),
  235. (r'(type)(\s+)((?:[A-Za-z_]\w*)(?:::(?:[A-Za-z_]\w*))*)(\s*)(:)',
  236. bygroups(Keyword, Text, Name, Text, Operator)),
  237. (r'(redef)(\s+)(record|enum)(\s+)((?:[A-Za-z_]\w*)(?:::(?:[A-Za-z_]\w*))*)\b',
  238. bygroups(Keyword, Text, Keyword.Type, Text, Name.Class)),
  239. ],
  240. 'keywords': [
  241. (words(('redef', 'export', 'if', 'else', 'for', 'while',
  242. 'return', 'break', 'next', 'continue', 'fallthrough',
  243. 'switch', 'default', 'case',
  244. 'add', 'delete',
  245. 'when', 'timeout', 'schedule'),
  246. suffix=r'\b'),
  247. Keyword),
  248. (r'(print)\b', Keyword),
  249. (r'(global|local|const|option)\b', Keyword.Declaration),
  250. (r'(module)(\s+)(([A-Za-z_]\w*)(?:::([A-Za-z_]\w*))*)\b',
  251. bygroups(Keyword.Namespace, Text, Name.Namespace)),
  252. ],
  253. 'literals': [
  254. (r'"', String, 'string'),
  255. # Not the greatest match for patterns, but generally helps
  256. # disambiguate between start of a pattern and just a division
  257. # operator.
  258. (r'/(?=.*/)', String.Regex, 'regex'),
  259. (r'(T|F)\b', Keyword.Constant),
  260. # Port
  261. (r'\d{1,5}/(udp|tcp|icmp|unknown)\b', Number),
  262. # IPv4 Address
  263. (r'(\d{1,3}.){3}(\d{1,3})\b', Number),
  264. # IPv6 Address
  265. (r'\[([0-9a-fA-F]{0,4}:){2,7}([0-9a-fA-F]{0,4})?((\d{1,3}.){3}(\d{1,3}))?\]', Number),
  266. # Numeric
  267. (r'0[xX]' + _hex + r'+\b', Number.Hex),
  268. (_float + r'\s*(day|hr|min|sec|msec|usec)s?\b', Number.Float),
  269. (_float + r'\b', Number.Float),
  270. (r'(\d+)\b', Number.Integer),
  271. # Hostnames
  272. (_h + r'(\.' + _h + r')+', String),
  273. ],
  274. 'operators': [
  275. (r'[!%*/+<=>~|&^-]', Operator),
  276. (r'([-+=&|]{2}|[+=!><-]=)', Operator),
  277. (r'(in|as|is|of)\b', Operator.Word),
  278. (r'\??\$', Operator),
  279. ],
  280. 'punctuation': [
  281. (r'[{}()\[\],;.]', Punctuation),
  282. # The "ternary if", which uses '?' and ':', could instead be
  283. # treated as an Operator, but colons are more frequently used to
  284. # separate field/identifier names from their types, so the (often)
  285. # less-prominent Punctuation is used even with '?' for consistency.
  286. (r'[?:]', Punctuation),
  287. ],
  288. 'identifiers': [
  289. (r'([a-zA-Z_]\w*)(::)', bygroups(Name, Punctuation)),
  290. (r'[a-zA-Z_]\w*', Name)
  291. ],
  292. 'string': [
  293. (r'\\.', String.Escape),
  294. (r'%-?[0-9]*(\.[0-9]+)?[DTdxsefg]', String.Escape),
  295. (r'"', String, '#pop'),
  296. (r'.', String),
  297. ],
  298. 'regex': [
  299. (r'\\.', String.Escape),
  300. (r'/', String.Regex, '#pop'),
  301. (r'.', String.Regex),
  302. ],
  303. }
  304. BroLexer = ZeekLexer
  305. class PuppetLexer(RegexLexer):
  306. """
  307. For `Puppet <http://puppetlabs.com/>`__ configuration DSL.
  308. .. versionadded:: 1.6
  309. """
  310. name = 'Puppet'
  311. aliases = ['puppet']
  312. filenames = ['*.pp']
  313. tokens = {
  314. 'root': [
  315. include('comments'),
  316. include('keywords'),
  317. include('names'),
  318. include('numbers'),
  319. include('operators'),
  320. include('strings'),
  321. (r'[]{}:(),;[]', Punctuation),
  322. (r'[^\S\n]+', Text),
  323. ],
  324. 'comments': [
  325. (r'\s*#.*$', Comment),
  326. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  327. ],
  328. 'operators': [
  329. (r'(=>|\?|<|>|=|\+|-|/|\*|~|!|\|)', Operator),
  330. (r'(in|and|or|not)\b', Operator.Word),
  331. ],
  332. 'names': [
  333. (r'[a-zA-Z_]\w*', Name.Attribute),
  334. (r'(\$\S+)(\[)(\S+)(\])', bygroups(Name.Variable, Punctuation,
  335. String, Punctuation)),
  336. (r'\$\S+', Name.Variable),
  337. ],
  338. 'numbers': [
  339. # Copypasta from the Python lexer
  340. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
  341. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  342. (r'0[0-7]+j?', Number.Oct),
  343. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  344. (r'\d+L', Number.Integer.Long),
  345. (r'\d+j?', Number.Integer)
  346. ],
  347. 'keywords': [
  348. # Left out 'group' and 'require'
  349. # Since they're often used as attributes
  350. (words((
  351. 'absent', 'alert', 'alias', 'audit', 'augeas', 'before', 'case',
  352. 'check', 'class', 'computer', 'configured', 'contained',
  353. 'create_resources', 'crit', 'cron', 'debug', 'default',
  354. 'define', 'defined', 'directory', 'else', 'elsif', 'emerg',
  355. 'err', 'exec', 'extlookup', 'fail', 'false', 'file',
  356. 'filebucket', 'fqdn_rand', 'generate', 'host', 'if', 'import',
  357. 'include', 'info', 'inherits', 'inline_template', 'installed',
  358. 'interface', 'k5login', 'latest', 'link', 'loglevel',
  359. 'macauthorization', 'mailalias', 'maillist', 'mcx', 'md5',
  360. 'mount', 'mounted', 'nagios_command', 'nagios_contact',
  361. 'nagios_contactgroup', 'nagios_host', 'nagios_hostdependency',
  362. 'nagios_hostescalation', 'nagios_hostextinfo', 'nagios_hostgroup',
  363. 'nagios_service', 'nagios_servicedependency', 'nagios_serviceescalation',
  364. 'nagios_serviceextinfo', 'nagios_servicegroup', 'nagios_timeperiod',
  365. 'node', 'noop', 'notice', 'notify', 'package', 'present', 'purged',
  366. 'realize', 'regsubst', 'resources', 'role', 'router', 'running',
  367. 'schedule', 'scheduled_task', 'search', 'selboolean', 'selmodule',
  368. 'service', 'sha1', 'shellquote', 'split', 'sprintf',
  369. 'ssh_authorized_key', 'sshkey', 'stage', 'stopped', 'subscribe',
  370. 'tag', 'tagged', 'template', 'tidy', 'true', 'undef', 'unmounted',
  371. 'user', 'versioncmp', 'vlan', 'warning', 'yumrepo', 'zfs', 'zone',
  372. 'zpool'), prefix='(?i)', suffix=r'\b'),
  373. Keyword),
  374. ],
  375. 'strings': [
  376. (r'"([^"])*"', String),
  377. (r"'(\\'|[^'])*'", String),
  378. ],
  379. }
  380. class RslLexer(RegexLexer):
  381. """
  382. `RSL <http://en.wikipedia.org/wiki/RAISE>`_ is the formal specification
  383. language used in RAISE (Rigorous Approach to Industrial Software Engineering)
  384. method.
  385. .. versionadded:: 2.0
  386. """
  387. name = 'RSL'
  388. aliases = ['rsl']
  389. filenames = ['*.rsl']
  390. mimetypes = ['text/rsl']
  391. flags = re.MULTILINE | re.DOTALL
  392. tokens = {
  393. 'root': [
  394. (words((
  395. 'Bool', 'Char', 'Int', 'Nat', 'Real', 'Text', 'Unit', 'abs',
  396. 'all', 'always', 'any', 'as', 'axiom', 'card', 'case', 'channel',
  397. 'chaos', 'class', 'devt_relation', 'dom', 'elems', 'else', 'elif',
  398. 'end', 'exists', 'extend', 'false', 'for', 'hd', 'hide', 'if',
  399. 'in', 'is', 'inds', 'initialise', 'int', 'inter', 'isin', 'len',
  400. 'let', 'local', 'ltl_assertion', 'object', 'of', 'out', 'post',
  401. 'pre', 'read', 'real', 'rng', 'scheme', 'skip', 'stop', 'swap',
  402. 'then', 'theory', 'test_case', 'tl', 'transition_system', 'true',
  403. 'type', 'union', 'until', 'use', 'value', 'variable', 'while',
  404. 'with', 'write', '~isin', '-inflist', '-infset', '-list',
  405. '-set'), prefix=r'\b', suffix=r'\b'),
  406. Keyword),
  407. (r'(variable|value)\b', Keyword.Declaration),
  408. (r'--.*?\n', Comment),
  409. (r'<:.*?:>', Comment),
  410. (r'\{!.*?!\}', Comment),
  411. (r'/\*.*?\*/', Comment),
  412. (r'^[ \t]*([\w]+)[ \t]*:[^:]', Name.Function),
  413. (r'(^[ \t]*)([\w]+)([ \t]*\([\w\s,]*\)[ \t]*)(is|as)',
  414. bygroups(Text, Name.Function, Text, Keyword)),
  415. (r'\b[A-Z]\w*\b', Keyword.Type),
  416. (r'(true|false)\b', Keyword.Constant),
  417. (r'".*"', String),
  418. (r'\'.\'', String.Char),
  419. (r'(><|->|-m->|/\\|<=|<<=|<\.|\|\||\|\^\||-~->|-~m->|\\/|>=|>>|'
  420. r'\.>|\+\+|-\\|<->|=>|:-|~=|\*\*|<<|>>=|\+>|!!|\|=\||#)',
  421. Operator),
  422. (r'[0-9]+\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  423. (r'0x[0-9a-f]+', Number.Hex),
  424. (r'[0-9]+', Number.Integer),
  425. (r'.', Text),
  426. ],
  427. }
  428. def analyse_text(text):
  429. """
  430. Check for the most common text in the beginning of a RSL file.
  431. """
  432. if re.search(r'scheme\s*.*?=\s*class\s*type', text, re.I) is not None:
  433. return 1.0
  434. class MscgenLexer(RegexLexer):
  435. """
  436. For `Mscgen <http://www.mcternan.me.uk/mscgen/>`_ files.
  437. .. versionadded:: 1.6
  438. """
  439. name = 'Mscgen'
  440. aliases = ['mscgen', 'msc']
  441. filenames = ['*.msc']
  442. _var = r'(\w+|"(?:\\"|[^"])*")'
  443. tokens = {
  444. 'root': [
  445. (r'msc\b', Keyword.Type),
  446. # Options
  447. (r'(hscale|HSCALE|width|WIDTH|wordwraparcs|WORDWRAPARCS'
  448. r'|arcgradient|ARCGRADIENT)\b', Name.Property),
  449. # Operators
  450. (r'(abox|ABOX|rbox|RBOX|box|BOX|note|NOTE)\b', Operator.Word),
  451. (r'(\.|-|\|){3}', Keyword),
  452. (r'(?:-|=|\.|:){2}'
  453. r'|<<=>>|<->|<=>|<<>>|<:>'
  454. r'|->|=>>|>>|=>|:>|-x|-X'
  455. r'|<-|<<=|<<|<=|<:|x-|X-|=', Operator),
  456. # Names
  457. (r'\*', Name.Builtin),
  458. (_var, Name.Variable),
  459. # Other
  460. (r'\[', Punctuation, 'attrs'),
  461. (r'\{|\}|,|;', Punctuation),
  462. include('comments')
  463. ],
  464. 'attrs': [
  465. (r'\]', Punctuation, '#pop'),
  466. (_var + r'(\s*)(=)(\s*)' + _var,
  467. bygroups(Name.Attribute, Text.Whitespace, Operator, Text.Whitespace,
  468. String)),
  469. (r',', Punctuation),
  470. include('comments')
  471. ],
  472. 'comments': [
  473. (r'(?://|#).*?\n', Comment.Single),
  474. (r'/\*(?:.|\n)*?\*/', Comment.Multiline),
  475. (r'[ \t\r\n]+', Text.Whitespace)
  476. ]
  477. }
  478. class VGLLexer(RegexLexer):
  479. """
  480. For `SampleManager VGL <http://www.thermoscientific.com/samplemanager>`_
  481. source code.
  482. .. versionadded:: 1.6
  483. """
  484. name = 'VGL'
  485. aliases = ['vgl']
  486. filenames = ['*.rpf']
  487. flags = re.MULTILINE | re.DOTALL | re.IGNORECASE
  488. tokens = {
  489. 'root': [
  490. (r'\{[^}]*\}', Comment.Multiline),
  491. (r'declare', Keyword.Constant),
  492. (r'(if|then|else|endif|while|do|endwhile|and|or|prompt|object'
  493. r'|create|on|line|with|global|routine|value|endroutine|constant'
  494. r'|global|set|join|library|compile_option|file|exists|create|copy'
  495. r'|delete|enable|windows|name|notprotected)(?! *[=<>.,()])',
  496. Keyword),
  497. (r'(true|false|null|empty|error|locked)', Keyword.Constant),
  498. (r'[~^*#!%&\[\]()<>|+=:;,./?-]', Operator),
  499. (r'"[^"]*"', String),
  500. (r'(\.)([a-z_$][\w$]*)', bygroups(Operator, Name.Attribute)),
  501. (r'[0-9][0-9]*(\.[0-9]+(e[+\-]?[0-9]+)?)?', Number),
  502. (r'[a-z_$][\w$]*', Name),
  503. (r'[\r\n]+', Text),
  504. (r'\s+', Text)
  505. ]
  506. }
  507. class AlloyLexer(RegexLexer):
  508. """
  509. For `Alloy <http://alloy.mit.edu>`_ source code.
  510. .. versionadded:: 2.0
  511. """
  512. name = 'Alloy'
  513. aliases = ['alloy']
  514. filenames = ['*.als']
  515. mimetypes = ['text/x-alloy']
  516. flags = re.MULTILINE | re.DOTALL
  517. iden_rex = r'[a-zA-Z_][\w\']*'
  518. text_tuple = (r'[^\S\n]+', Text)
  519. tokens = {
  520. 'sig': [
  521. (r'(extends)\b', Keyword, '#pop'),
  522. (iden_rex, Name),
  523. text_tuple,
  524. (r',', Punctuation),
  525. (r'\{', Operator, '#pop'),
  526. ],
  527. 'module': [
  528. text_tuple,
  529. (iden_rex, Name, '#pop'),
  530. ],
  531. 'fun': [
  532. text_tuple,
  533. (r'\{', Operator, '#pop'),
  534. (iden_rex, Name, '#pop'),
  535. ],
  536. 'root': [
  537. (r'--.*?$', Comment.Single),
  538. (r'//.*?$', Comment.Single),
  539. (r'/\*.*?\*/', Comment.Multiline),
  540. text_tuple,
  541. (r'(module|open)(\s+)', bygroups(Keyword.Namespace, Text),
  542. 'module'),
  543. (r'(sig|enum)(\s+)', bygroups(Keyword.Declaration, Text), 'sig'),
  544. (r'(iden|univ|none)\b', Keyword.Constant),
  545. (r'(int|Int)\b', Keyword.Type),
  546. (r'(this|abstract|extends|set|seq|one|lone|let)\b', Keyword),
  547. (r'(all|some|no|sum|disj|when|else)\b', Keyword),
  548. (r'(run|check|for|but|exactly|expect|as)\b', Keyword),
  549. (r'(and|or|implies|iff|in)\b', Operator.Word),
  550. (r'(fun|pred|fact|assert)(\s+)', bygroups(Keyword, Text), 'fun'),
  551. (r'!|#|&&|\+\+|<<|>>|>=|<=>|<=|\.|->', Operator),
  552. (r'[-+/*%=<>&!^|~{}\[\]().]', Operator),
  553. (iden_rex, Name),
  554. (r'[:,]', Punctuation),
  555. (r'[0-9]+', Number.Integer),
  556. (r'"(\\\\|\\"|[^"])*"', String),
  557. (r'\n', Text),
  558. ]
  559. }
  560. class PanLexer(RegexLexer):
  561. """
  562. Lexer for `pan <http://github.com/quattor/pan/>`_ source files.
  563. Based on tcsh lexer.
  564. .. versionadded:: 2.0
  565. """
  566. name = 'Pan'
  567. aliases = ['pan']
  568. filenames = ['*.pan']
  569. tokens = {
  570. 'root': [
  571. include('basic'),
  572. (r'\(', Keyword, 'paren'),
  573. (r'\{', Keyword, 'curly'),
  574. include('data'),
  575. ],
  576. 'basic': [
  577. (words((
  578. 'if', 'for', 'with', 'else', 'type', 'bind', 'while', 'valid', 'final',
  579. 'prefix', 'unique', 'object', 'foreach', 'include', 'template',
  580. 'function', 'variable', 'structure', 'extensible', 'declaration'),
  581. prefix=r'\b', suffix=r'\s*\b'),
  582. Keyword),
  583. (words((
  584. 'file_contents', 'format', 'index', 'length', 'match', 'matches',
  585. 'replace', 'splice', 'split', 'substr', 'to_lowercase', 'to_uppercase',
  586. 'debug', 'error', 'traceback', 'deprecated', 'base64_decode',
  587. 'base64_encode', 'digest', 'escape', 'unescape', 'append', 'create',
  588. 'first', 'nlist', 'key', 'list', 'merge', 'next', 'prepend', 'is_boolean',
  589. 'is_defined', 'is_double', 'is_list', 'is_long', 'is_nlist', 'is_null',
  590. 'is_number', 'is_property', 'is_resource', 'is_string', 'to_boolean',
  591. 'to_double', 'to_long', 'to_string', 'clone', 'delete', 'exists',
  592. 'path_exists', 'if_exists', 'return', 'value'),
  593. prefix=r'\b', suffix=r'\s*\b'),
  594. Name.Builtin),
  595. (r'#.*', Comment),
  596. (r'\\[\w\W]', String.Escape),
  597. (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
  598. (r'[\[\]{}()=]+', Operator),
  599. (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
  600. (r';', Punctuation),
  601. ],
  602. 'data': [
  603. (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
  604. (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
  605. (r'\s+', Text),
  606. (r'[^=\s\[\]{}()$"\'`\\;#]+', Text),
  607. (r'\d+(?= |\Z)', Number),
  608. ],
  609. 'curly': [
  610. (r'\}', Keyword, '#pop'),
  611. (r':-', Keyword),
  612. (r'\w+', Name.Variable),
  613. (r'[^}:"\'`$]+', Punctuation),
  614. (r':', Punctuation),
  615. include('root'),
  616. ],
  617. 'paren': [
  618. (r'\)', Keyword, '#pop'),
  619. include('root'),
  620. ],
  621. }
  622. class CrmshLexer(RegexLexer):
  623. """
  624. Lexer for `crmsh <http://crmsh.github.io/>`_ configuration files
  625. for Pacemaker clusters.
  626. .. versionadded:: 2.1
  627. """
  628. name = 'Crmsh'
  629. aliases = ['crmsh', 'pcmk']
  630. filenames = ['*.crmsh', '*.pcmk']
  631. mimetypes = []
  632. elem = words((
  633. 'node', 'primitive', 'group', 'clone', 'ms', 'location',
  634. 'colocation', 'order', 'fencing_topology', 'rsc_ticket',
  635. 'rsc_template', 'property', 'rsc_defaults',
  636. 'op_defaults', 'acl_target', 'acl_group', 'user', 'role',
  637. 'tag'), suffix=r'(?![\w#$-])')
  638. sub = words((
  639. 'params', 'meta', 'operations', 'op', 'rule',
  640. 'attributes', 'utilization'), suffix=r'(?![\w#$-])')
  641. acl = words(('read', 'write', 'deny'), suffix=r'(?![\w#$-])')
  642. bin_rel = words(('and', 'or'), suffix=r'(?![\w#$-])')
  643. un_ops = words(('defined', 'not_defined'), suffix=r'(?![\w#$-])')
  644. date_exp = words(('in_range', 'date', 'spec', 'in'), suffix=r'(?![\w#$-])')
  645. acl_mod = (r'(?:tag|ref|reference|attribute|type|xpath)')
  646. bin_ops = (r'(?:lt|gt|lte|gte|eq|ne)')
  647. val_qual = (r'(?:string|version|number)')
  648. rsc_role_action = (r'(?:Master|Started|Slave|Stopped|'
  649. r'start|promote|demote|stop)')
  650. tokens = {
  651. 'root': [
  652. (r'^#.*\n?', Comment),
  653. # attr=value (nvpair)
  654. (r'([\w#$-]+)(=)("(?:""|[^"])*"|\S+)',
  655. bygroups(Name.Attribute, Punctuation, String)),
  656. # need this construct, otherwise numeric node ids
  657. # are matched as scores
  658. # elem id:
  659. (r'(node)(\s+)([\w#$-]+)(:)',
  660. bygroups(Keyword, Whitespace, Name, Punctuation)),
  661. # scores
  662. (r'([+-]?([0-9]+|inf)):', Number),
  663. # keywords (elements and other)
  664. (elem, Keyword),
  665. (sub, Keyword),
  666. (acl, Keyword),
  667. # binary operators
  668. (r'(?:%s:)?(%s)(?![\w#$-])' % (val_qual, bin_ops), Operator.Word),
  669. # other operators
  670. (bin_rel, Operator.Word),
  671. (un_ops, Operator.Word),
  672. (date_exp, Operator.Word),
  673. # builtin attributes (e.g. #uname)
  674. (r'#[a-z]+(?![\w#$-])', Name.Builtin),
  675. # acl_mod:blah
  676. (r'(%s)(:)("(?:""|[^"])*"|\S+)' % acl_mod,
  677. bygroups(Keyword, Punctuation, Name)),
  678. # rsc_id[:(role|action)]
  679. # NB: this matches all other identifiers
  680. (r'([\w#$-]+)(?:(:)(%s))?(?![\w#$-])' % rsc_role_action,
  681. bygroups(Name, Punctuation, Operator.Word)),
  682. # punctuation
  683. (r'(\\(?=\n)|[\[\](){}/:@])', Punctuation),
  684. (r'\s+|\n', Whitespace),
  685. ],
  686. }
  687. class FlatlineLexer(RegexLexer):
  688. """
  689. Lexer for `Flatline <https://github.com/bigmlcom/flatline>`_ expressions.
  690. .. versionadded:: 2.2
  691. """
  692. name = 'Flatline'
  693. aliases = ['flatline']
  694. filenames = []
  695. mimetypes = ['text/x-flatline']
  696. special_forms = ('let',)
  697. builtins = (
  698. "!=", "*", "+", "-", "<", "<=", "=", ">", ">=", "abs", "acos", "all",
  699. "all-but", "all-with-defaults", "all-with-numeric-default", "and",
  700. "asin", "atan", "avg", "avg-window", "bin-center", "bin-count", "call",
  701. "category-count", "ceil", "cond", "cond-window", "cons", "cos", "cosh",
  702. "count", "diff-window", "div", "ensure-value", "ensure-weighted-value",
  703. "epoch", "epoch-day", "epoch-fields", "epoch-hour", "epoch-millisecond",
  704. "epoch-minute", "epoch-month", "epoch-second", "epoch-weekday",
  705. "epoch-year", "exp", "f", "field", "field-prop", "fields", "filter",
  706. "first", "floor", "head", "if", "in", "integer", "language", "length",
  707. "levenshtein", "linear-regression", "list", "ln", "log", "log10", "map",
  708. "matches", "matches?", "max", "maximum", "md5", "mean", "median", "min",
  709. "minimum", "missing", "missing-count", "missing?", "missing_count",
  710. "mod", "mode", "normalize", "not", "nth", "occurrences", "or",
  711. "percentile", "percentile-label", "population", "population-fraction",
  712. "pow", "preferred", "preferred?", "quantile-label", "rand", "rand-int",
  713. "random-value", "re-quote", "real", "replace", "replace-first", "rest",
  714. "round", "row-number", "segment-label", "sha1", "sha256", "sin", "sinh",
  715. "sqrt", "square", "standard-deviation", "standard_deviation", "str",
  716. "subs", "sum", "sum-squares", "sum-window", "sum_squares", "summary",
  717. "summary-no", "summary-str", "tail", "tan", "tanh", "to-degrees",
  718. "to-radians", "variance", "vectorize", "weighted-random-value", "window",
  719. "winnow", "within-percentiles?", "z-score",
  720. )
  721. valid_name = r'(?!#)[\w!$%*+<=>?/.#-]+'
  722. tokens = {
  723. 'root': [
  724. # whitespaces - usually not relevant
  725. (r'[,\s]+', Text),
  726. # numbers
  727. (r'-?\d+\.\d+', Number.Float),
  728. (r'-?\d+', Number.Integer),
  729. (r'0x-?[a-f\d]+', Number.Hex),
  730. # strings, symbols and characters
  731. (r'"(\\\\|\\"|[^"])*"', String),
  732. (r"\\(.|[a-z]+)", String.Char),
  733. # expression template placeholder
  734. (r'_', String.Symbol),
  735. # highlight the special forms
  736. (words(special_forms, suffix=' '), Keyword),
  737. # highlight the builtins
  738. (words(builtins, suffix=' '), Name.Builtin),
  739. # the remaining functions
  740. (r'(?<=\()' + valid_name, Name.Function),
  741. # find the remaining variables
  742. (valid_name, Name.Variable),
  743. # parentheses
  744. (r'(\(|\))', Punctuation),
  745. ],
  746. }
  747. class SnowballLexer(ExtendedRegexLexer):
  748. """
  749. Lexer for `Snowball <http://snowballstem.org/>`_ source code.
  750. .. versionadded:: 2.2
  751. """
  752. name = 'Snowball'
  753. aliases = ['snowball']
  754. filenames = ['*.sbl']
  755. _ws = r'\n\r\t '
  756. def __init__(self, **options):
  757. self._reset_stringescapes()
  758. ExtendedRegexLexer.__init__(self, **options)
  759. def _reset_stringescapes(self):
  760. self._start = "'"
  761. self._end = "'"
  762. def _string(do_string_first):
  763. def callback(lexer, match, ctx):
  764. s = match.start()
  765. text = match.group()
  766. string = re.compile(r'([^%s]*)(.)' % re.escape(lexer._start)).match
  767. escape = re.compile(r'([^%s]*)(.)' % re.escape(lexer._end)).match
  768. pos = 0
  769. do_string = do_string_first
  770. while pos < len(text):
  771. if do_string:
  772. match = string(text, pos)
  773. yield s + match.start(1), String.Single, match.group(1)
  774. if match.group(2) == "'":
  775. yield s + match.start(2), String.Single, match.group(2)
  776. ctx.stack.pop()
  777. break
  778. yield s + match.start(2), String.Escape, match.group(2)
  779. pos = match.end()
  780. match = escape(text, pos)
  781. yield s + match.start(), String.Escape, match.group()
  782. if match.group(2) != lexer._end:
  783. ctx.stack[-1] = 'escape'
  784. break
  785. pos = match.end()
  786. do_string = True
  787. ctx.pos = s + match.end()
  788. return callback
  789. def _stringescapes(lexer, match, ctx):
  790. lexer._start = match.group(3)
  791. lexer._end = match.group(5)
  792. return bygroups(Keyword.Reserved, Text, String.Escape, Text,
  793. String.Escape)(lexer, match, ctx)
  794. tokens = {
  795. 'root': [
  796. (words(('len', 'lenof'), suffix=r'\b'), Operator.Word),
  797. include('root1'),
  798. ],
  799. 'root1': [
  800. (r'[%s]+' % _ws, Text),
  801. (r'\d+', Number.Integer),
  802. (r"'", String.Single, 'string'),
  803. (r'[()]', Punctuation),
  804. (r'/\*[\w\W]*?\*/', Comment.Multiline),
  805. (r'//.*', Comment.Single),
  806. (r'[!*+\-/<=>]=|[-=]>|<[+-]|[$*+\-/<=>?\[\]]', Operator),
  807. (words(('as', 'get', 'hex', 'among', 'define', 'decimal',
  808. 'backwardmode'), suffix=r'\b'),
  809. Keyword.Reserved),
  810. (words(('strings', 'booleans', 'integers', 'routines', 'externals',
  811. 'groupings'), suffix=r'\b'),
  812. Keyword.Reserved, 'declaration'),
  813. (words(('do', 'or', 'and', 'for', 'hop', 'non', 'not', 'set', 'try',
  814. 'fail', 'goto', 'loop', 'next', 'test', 'true',
  815. 'false', 'unset', 'atmark', 'attach', 'delete', 'gopast',
  816. 'insert', 'repeat', 'sizeof', 'tomark', 'atleast',
  817. 'atlimit', 'reverse', 'setmark', 'tolimit', 'setlimit',
  818. 'backwards', 'substring'), suffix=r'\b'),
  819. Operator.Word),
  820. (words(('size', 'limit', 'cursor', 'maxint', 'minint'),
  821. suffix=r'\b'),
  822. Name.Builtin),
  823. (r'(stringdef\b)([%s]*)([^%s]+)' % (_ws, _ws),
  824. bygroups(Keyword.Reserved, Text, String.Escape)),
  825. (r'(stringescapes\b)([%s]*)(.)([%s]*)(.)' % (_ws, _ws),
  826. _stringescapes),
  827. (r'[A-Za-z]\w*', Name),
  828. ],
  829. 'declaration': [
  830. (r'\)', Punctuation, '#pop'),
  831. (words(('len', 'lenof'), suffix=r'\b'), Name,
  832. ('root1', 'declaration')),
  833. include('root1'),
  834. ],
  835. 'string': [
  836. (r"[^']*'", _string(True)),
  837. ],
  838. 'escape': [
  839. (r"[^']*'", _string(False)),
  840. ],
  841. }
  842. def get_tokens_unprocessed(self, text=None, context=None):
  843. self._reset_stringescapes()
  844. return ExtendedRegexLexer.get_tokens_unprocessed(self, text, context)