dsls.py 36 KB

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