ThirdParty/Pygments/pygments/lexers/dsls.py

changeset 4172
4f20dba37ab6
child 4697
c2e9bf425554
equal deleted inserted replaced
4170:8bc578136279 4172:4f20dba37ab6
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.dsls
4 ~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for various domain-specific languages.
7
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 from pygments.lexer import RegexLexer, bygroups, words, include, default
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
16 Number, Punctuation, Literal
17
18 __all__ = ['ProtoBufLexer', 'BroLexer', 'PuppetLexer', 'RslLexer',
19 'MscgenLexer', 'VGLLexer', 'AlloyLexer', 'PanLexer']
20
21
22 class ProtoBufLexer(RegexLexer):
23 """
24 Lexer for `Protocol Buffer <http://code.google.com/p/protobuf/>`_
25 definition files.
26
27 .. versionadded:: 1.4
28 """
29
30 name = 'Protocol Buffer'
31 aliases = ['protobuf', 'proto']
32 filenames = ['*.proto']
33
34 tokens = {
35 'root': [
36 (r'[ \t]+', Text),
37 (r'[,;{}\[\]()]', Punctuation),
38 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
39 (r'/(\\\n)?\*(.|\n)*?\*(\\\n)?/', Comment.Multiline),
40 (words((
41 'import', 'option', 'optional', 'required', 'repeated', 'default',
42 'packed', 'ctype', 'extensions', 'to', 'max', 'rpc', 'returns',
43 'oneof'), prefix=r'\b', suffix=r'\b'),
44 Keyword),
45 (words((
46 'int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64',
47 'fixed32', 'fixed64', 'sfixed32', 'sfixed64',
48 'float', 'double', 'bool', 'string', 'bytes'), suffix=r'\b'),
49 Keyword.Type),
50 (r'(true|false)\b', Keyword.Constant),
51 (r'(package)(\s+)', bygroups(Keyword.Namespace, Text), 'package'),
52 (r'(message|extend)(\s+)',
53 bygroups(Keyword.Declaration, Text), 'message'),
54 (r'(enum|group|service)(\s+)',
55 bygroups(Keyword.Declaration, Text), 'type'),
56 (r'\".*?\"', String),
57 (r'\'.*?\'', String),
58 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
59 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
60 (r'(\-?(inf|nan))\b', Number.Float),
61 (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
62 (r'0[0-7]+[LlUu]*', Number.Oct),
63 (r'\d+[LlUu]*', Number.Integer),
64 (r'[+-=]', Operator),
65 (r'([a-zA-Z_][\w.]*)([ \t]*)(=)',
66 bygroups(Name.Attribute, Text, Operator)),
67 ('[a-zA-Z_][\w.]*', Name),
68 ],
69 'package': [
70 (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'),
71 default('#pop'),
72 ],
73 'message': [
74 (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
75 default('#pop'),
76 ],
77 'type': [
78 (r'[a-zA-Z_]\w*', Name, '#pop'),
79 default('#pop'),
80 ],
81 }
82
83
84 class BroLexer(RegexLexer):
85 """
86 For `Bro <http://bro-ids.org/>`_ scripts.
87
88 .. versionadded:: 1.5
89 """
90 name = 'Bro'
91 aliases = ['bro']
92 filenames = ['*.bro']
93
94 _hex = r'[0-9a-fA-F_]'
95 _float = r'((\d*\.?\d+)|(\d+\.?\d*))([eE][-+]?\d+)?'
96 _h = r'[A-Za-z0-9][-A-Za-z0-9]*'
97
98 tokens = {
99 'root': [
100 # Whitespace
101 (r'^@.*?\n', Comment.Preproc),
102 (r'#.*?\n', Comment.Single),
103 (r'\n', Text),
104 (r'\s+', Text),
105 (r'\\\n', Text),
106 # Keywords
107 (r'(add|alarm|break|case|const|continue|delete|do|else|enum|event'
108 r'|export|for|function|if|global|hook|local|module|next'
109 r'|of|print|redef|return|schedule|switch|type|when|while)\b', Keyword),
110 (r'(addr|any|bool|count|counter|double|file|int|interval|net'
111 r'|pattern|port|record|set|string|subnet|table|time|timer'
112 r'|vector)\b', Keyword.Type),
113 (r'(T|F)\b', Keyword.Constant),
114 (r'(&)((?:add|delete|expire)_func|attr|(?:create|read|write)_expire'
115 r'|default|disable_print_hook|raw_output|encrypt|group|log'
116 r'|mergeable|optional|persistent|priority|redef'
117 r'|rotate_(?:interval|size)|synchronized)\b',
118 bygroups(Punctuation, Keyword)),
119 (r'\s+module\b', Keyword.Namespace),
120 # Addresses, ports and networks
121 (r'\d+/(tcp|udp|icmp|unknown)\b', Number),
122 (r'(\d+\.){3}\d+', Number),
123 (r'(' + _hex + r'){7}' + _hex, Number),
124 (r'0x' + _hex + r'(' + _hex + r'|:)*::(' + _hex + r'|:)*', Number),
125 (r'((\d+|:)(' + _hex + r'|:)*)?::(' + _hex + r'|:)*', Number),
126 (r'(\d+\.\d+\.|(\d+\.){2}\d+)', Number),
127 # Hostnames
128 (_h + r'(\.' + _h + r')+', String),
129 # Numeric
130 (_float + r'\s+(day|hr|min|sec|msec|usec)s?\b', Literal.Date),
131 (r'0[xX]' + _hex, Number.Hex),
132 (_float, Number.Float),
133 (r'\d+', Number.Integer),
134 (r'/', String.Regex, 'regex'),
135 (r'"', String, 'string'),
136 # Operators
137 (r'[!%*/+:<=>?~|-]', Operator),
138 (r'([-+=&|]{2}|[+=!><-]=)', Operator),
139 (r'(in|match)\b', Operator.Word),
140 (r'[{}()\[\]$.,;]', Punctuation),
141 # Identfier
142 (r'([_a-zA-Z]\w*)(::)', bygroups(Name, Name.Namespace)),
143 (r'[a-zA-Z_]\w*', Name)
144 ],
145 'string': [
146 (r'"', String, '#pop'),
147 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
148 (r'[^\\"\n]+', String),
149 (r'\\\n', String),
150 (r'\\', String)
151 ],
152 'regex': [
153 (r'/', String.Regex, '#pop'),
154 (r'\\[\\nt/]', String.Regex), # String.Escape is too intense here.
155 (r'[^\\/\n]+', String.Regex),
156 (r'\\\n', String.Regex),
157 (r'\\', String.Regex)
158 ]
159 }
160
161
162 class PuppetLexer(RegexLexer):
163 """
164 For `Puppet <http://puppetlabs.com/>`__ configuration DSL.
165
166 .. versionadded:: 1.6
167 """
168 name = 'Puppet'
169 aliases = ['puppet']
170 filenames = ['*.pp']
171
172 tokens = {
173 'root': [
174 include('comments'),
175 include('keywords'),
176 include('names'),
177 include('numbers'),
178 include('operators'),
179 include('strings'),
180
181 (r'[]{}:(),;[]', Punctuation),
182 (r'[^\S\n]+', Text),
183 ],
184
185 'comments': [
186 (r'\s*#.*$', Comment),
187 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
188 ],
189
190 'operators': [
191 (r'(=>|\?|<|>|=|\+|-|/|\*|~|!|\|)', Operator),
192 (r'(in|and|or|not)\b', Operator.Word),
193 ],
194
195 'names': [
196 ('[a-zA-Z_]\w*', Name.Attribute),
197 (r'(\$\S+)(\[)(\S+)(\])', bygroups(Name.Variable, Punctuation,
198 String, Punctuation)),
199 (r'\$\S+', Name.Variable),
200 ],
201
202 'numbers': [
203 # Copypasta from the Python lexer
204 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
205 (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
206 (r'0[0-7]+j?', Number.Oct),
207 (r'0[xX][a-fA-F0-9]+', Number.Hex),
208 (r'\d+L', Number.Integer.Long),
209 (r'\d+j?', Number.Integer)
210 ],
211
212 'keywords': [
213 # Left out 'group' and 'require'
214 # Since they're often used as attributes
215 (words((
216 'absent', 'alert', 'alias', 'audit', 'augeas', 'before', 'case',
217 'check', 'class', 'computer', 'configured', 'contained',
218 'create_resources', 'crit', 'cron', 'debug', 'default',
219 'define', 'defined', 'directory', 'else', 'elsif', 'emerg',
220 'err', 'exec', 'extlookup', 'fail', 'false', 'file',
221 'filebucket', 'fqdn_rand', 'generate', 'host', 'if', 'import',
222 'include', 'info', 'inherits', 'inline_template', 'installed',
223 'interface', 'k5login', 'latest', 'link', 'loglevel',
224 'macauthorization', 'mailalias', 'maillist', 'mcx', 'md5',
225 'mount', 'mounted', 'nagios_command', 'nagios_contact',
226 'nagios_contactgroup', 'nagios_host', 'nagios_hostdependency',
227 'nagios_hostescalation', 'nagios_hostextinfo', 'nagios_hostgroup',
228 'nagios_service', 'nagios_servicedependency', 'nagios_serviceescalation',
229 'nagios_serviceextinfo', 'nagios_servicegroup', 'nagios_timeperiod',
230 'node', 'noop', 'notice', 'notify', 'package', 'present', 'purged',
231 'realize', 'regsubst', 'resources', 'role', 'router', 'running',
232 'schedule', 'scheduled_task', 'search', 'selboolean', 'selmodule',
233 'service', 'sha1', 'shellquote', 'split', 'sprintf',
234 'ssh_authorized_key', 'sshkey', 'stage', 'stopped', 'subscribe',
235 'tag', 'tagged', 'template', 'tidy', 'true', 'undef', 'unmounted',
236 'user', 'versioncmp', 'vlan', 'warning', 'yumrepo', 'zfs', 'zone',
237 'zpool'), prefix='(?i)', suffix=r'\b'),
238 Keyword),
239 ],
240
241 'strings': [
242 (r'"([^"])*"', String),
243 (r"'(\\'|[^'])*'", String),
244 ],
245
246 }
247
248
249 class RslLexer(RegexLexer):
250 """
251 `RSL <http://en.wikipedia.org/wiki/RAISE>`_ is the formal specification
252 language used in RAISE (Rigorous Approach to Industrial Software Engineering)
253 method.
254
255 .. versionadded:: 2.0
256 """
257 name = 'RSL'
258 aliases = ['rsl']
259 filenames = ['*.rsl']
260 mimetypes = ['text/rsl']
261
262 flags = re.MULTILINE | re.DOTALL
263
264 tokens = {
265 'root': [
266 (words((
267 'Bool', 'Char', 'Int', 'Nat', 'Real', 'Text', 'Unit', 'abs',
268 'all', 'always', 'any', 'as', 'axiom', 'card', 'case', 'channel',
269 'chaos', 'class', 'devt_relation', 'dom', 'elems', 'else', 'elif',
270 'end', 'exists', 'extend', 'false', 'for', 'hd', 'hide', 'if',
271 'in', 'is', 'inds', 'initialise', 'int', 'inter', 'isin', 'len',
272 'let', 'local', 'ltl_assertion', 'object', 'of', 'out', 'post',
273 'pre', 'read', 'real', 'rng', 'scheme', 'skip', 'stop', 'swap',
274 'then', 'theory', 'test_case', 'tl', 'transition_system', 'true',
275 'type', 'union', 'until', 'use', 'value', 'variable', 'while',
276 'with', 'write', '~isin', '-inflist', '-infset', '-list',
277 '-set'), prefix=r'\b', suffix=r'\b'),
278 Keyword),
279 (r'(variable|value)\b', Keyword.Declaration),
280 (r'--.*?\n', Comment),
281 (r'<:.*?:>', Comment),
282 (r'\{!.*?!\}', Comment),
283 (r'/\*.*?\*/', Comment),
284 (r'^[ \t]*([\w]+)[ \t]*:[^:]', Name.Function),
285 (r'(^[ \t]*)([\w]+)([ \t]*\([\w\s,]*\)[ \t]*)(is|as)',
286 bygroups(Text, Name.Function, Text, Keyword)),
287 (r'\b[A-Z]\w*\b', Keyword.Type),
288 (r'(true|false)\b', Keyword.Constant),
289 (r'".*"', String),
290 (r'\'.\'', String.Char),
291 (r'(><|->|-m->|/\\|<=|<<=|<\.|\|\||\|\^\||-~->|-~m->|\\/|>=|>>|'
292 r'\.>|\+\+|-\\|<->|=>|:-|~=|\*\*|<<|>>=|\+>|!!|\|=\||#)',
293 Operator),
294 (r'[0-9]+\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
295 (r'0x[0-9a-f]+', Number.Hex),
296 (r'[0-9]+', Number.Integer),
297 (r'.', Text),
298 ],
299 }
300
301 def analyse_text(text):
302 """
303 Check for the most common text in the beginning of a RSL file.
304 """
305 if re.search(r'scheme\s*.*?=\s*class\s*type', text, re.I) is not None:
306 return 1.0
307
308
309 class MscgenLexer(RegexLexer):
310 """
311 For `Mscgen <http://www.mcternan.me.uk/mscgen/>`_ files.
312
313 .. versionadded:: 1.6
314 """
315 name = 'Mscgen'
316 aliases = ['mscgen', 'msc']
317 filenames = ['*.msc']
318
319 _var = r'(\w+|"(?:\\"|[^"])*")'
320
321 tokens = {
322 'root': [
323 (r'msc\b', Keyword.Type),
324 # Options
325 (r'(hscale|HSCALE|width|WIDTH|wordwraparcs|WORDWRAPARCS'
326 r'|arcgradient|ARCGRADIENT)\b', Name.Property),
327 # Operators
328 (r'(abox|ABOX|rbox|RBOX|box|BOX|note|NOTE)\b', Operator.Word),
329 (r'(\.|-|\|){3}', Keyword),
330 (r'(?:-|=|\.|:){2}'
331 r'|<<=>>|<->|<=>|<<>>|<:>'
332 r'|->|=>>|>>|=>|:>|-x|-X'
333 r'|<-|<<=|<<|<=|<:|x-|X-|=', Operator),
334 # Names
335 (r'\*', Name.Builtin),
336 (_var, Name.Variable),
337 # Other
338 (r'\[', Punctuation, 'attrs'),
339 (r'\{|\}|,|;', Punctuation),
340 include('comments')
341 ],
342 'attrs': [
343 (r'\]', Punctuation, '#pop'),
344 (_var + r'(\s*)(=)(\s*)' + _var,
345 bygroups(Name.Attribute, Text.Whitespace, Operator, Text.Whitespace,
346 String)),
347 (r',', Punctuation),
348 include('comments')
349 ],
350 'comments': [
351 (r'(?://|#).*?\n', Comment.Single),
352 (r'/\*(?:.|\n)*?\*/', Comment.Multiline),
353 (r'[ \t\r\n]+', Text.Whitespace)
354 ]
355 }
356
357
358 class VGLLexer(RegexLexer):
359 """
360 For `SampleManager VGL <http://www.thermoscientific.com/samplemanager>`_
361 source code.
362
363 .. versionadded:: 1.6
364 """
365 name = 'VGL'
366 aliases = ['vgl']
367 filenames = ['*.rpf']
368
369 flags = re.MULTILINE | re.DOTALL | re.IGNORECASE
370
371 tokens = {
372 'root': [
373 (r'\{[^}]*\}', Comment.Multiline),
374 (r'declare', Keyword.Constant),
375 (r'(if|then|else|endif|while|do|endwhile|and|or|prompt|object'
376 r'|create|on|line|with|global|routine|value|endroutine|constant'
377 r'|global|set|join|library|compile_option|file|exists|create|copy'
378 r'|delete|enable|windows|name|notprotected)(?! *[=<>.,()])',
379 Keyword),
380 (r'(true|false|null|empty|error|locked)', Keyword.Constant),
381 (r'[~^*#!%&\[\]()<>|+=:;,./?-]', Operator),
382 (r'"[^"]*"', String),
383 (r'(\.)([a-z_$][\w$]*)', bygroups(Operator, Name.Attribute)),
384 (r'[0-9][0-9]*(\.[0-9]+(e[+\-]?[0-9]+)?)?', Number),
385 (r'[a-z_$][\w$]*', Name),
386 (r'[\r\n]+', Text),
387 (r'\s+', Text)
388 ]
389 }
390
391
392 class AlloyLexer(RegexLexer):
393 """
394 For `Alloy <http://alloy.mit.edu>`_ source code.
395
396 .. versionadded:: 2.0
397 """
398
399 name = 'Alloy'
400 aliases = ['alloy']
401 filenames = ['*.als']
402 mimetypes = ['text/x-alloy']
403
404 flags = re.MULTILINE | re.DOTALL
405
406 iden_rex = r'[a-zA-Z_][\w\']*'
407 text_tuple = (r'[^\S\n]+', Text)
408
409 tokens = {
410 'sig': [
411 (r'(extends)\b', Keyword, '#pop'),
412 (iden_rex, Name),
413 text_tuple,
414 (r',', Punctuation),
415 (r'\{', Operator, '#pop'),
416 ],
417 'module': [
418 text_tuple,
419 (iden_rex, Name, '#pop'),
420 ],
421 'fun': [
422 text_tuple,
423 (r'\{', Operator, '#pop'),
424 (iden_rex, Name, '#pop'),
425 ],
426 'root': [
427 (r'--.*?$', Comment.Single),
428 (r'//.*?$', Comment.Single),
429 (r'/\*.*?\*/', Comment.Multiline),
430 text_tuple,
431 (r'(module|open)(\s+)', bygroups(Keyword.Namespace, Text),
432 'module'),
433 (r'(sig|enum)(\s+)', bygroups(Keyword.Declaration, Text), 'sig'),
434 (r'(iden|univ|none)\b', Keyword.Constant),
435 (r'(int|Int)\b', Keyword.Type),
436 (r'(this|abstract|extends|set|seq|one|lone|let)\b', Keyword),
437 (r'(all|some|no|sum|disj|when|else)\b', Keyword),
438 (r'(run|check|for|but|exactly|expect|as)\b', Keyword),
439 (r'(and|or|implies|iff|in)\b', Operator.Word),
440 (r'(fun|pred|fact|assert)(\s+)', bygroups(Keyword, Text), 'fun'),
441 (r'!|#|&&|\+\+|<<|>>|>=|<=>|<=|\.|->', Operator),
442 (r'[-+/*%=<>&!^|~{}\[\]().]', Operator),
443 (iden_rex, Name),
444 (r'[:,]', Punctuation),
445 (r'[0-9]+', Number.Integer),
446 (r'"(\\\\|\\"|[^"])*"', String),
447 (r'\n', Text),
448 ]
449 }
450
451
452 class PanLexer(RegexLexer):
453 """
454 Lexer for `pan <http://github.com/quattor/pan/>`_ source files.
455
456 Based on tcsh lexer.
457
458 .. versionadded:: 2.0
459 """
460
461 name = 'Pan'
462 aliases = ['pan']
463 filenames = ['*.pan']
464
465 tokens = {
466 'root': [
467 include('basic'),
468 (r'\(', Keyword, 'paren'),
469 (r'\{', Keyword, 'curly'),
470 include('data'),
471 ],
472 'basic': [
473 (words((
474 'if', 'for', 'with', 'else', 'type', 'bind', 'while', 'valid', 'final', 'prefix',
475 'unique', 'object', 'foreach', 'include', 'template', 'function', 'variable',
476 'structure', 'extensible', 'declaration'), prefix=r'\b', suffix=r'\s*\b'),
477 Keyword),
478 (words((
479 'file_contents', 'format', 'index', 'length', 'match', 'matches', 'replace',
480 'splice', 'split', 'substr', 'to_lowercase', 'to_uppercase', 'debug', 'error',
481 'traceback', 'deprecated', 'base64_decode', 'base64_encode', 'digest', 'escape',
482 'unescape', 'append', 'create', 'first', 'nlist', 'key', 'list', 'merge', 'next',
483 'prepend', 'is_boolean', 'is_defined', 'is_double', 'is_list', 'is_long',
484 'is_nlist', 'is_null', 'is_number', 'is_property', 'is_resource', 'is_string',
485 'to_boolean', 'to_double', 'to_long', 'to_string', 'clone', 'delete', 'exists',
486 'path_exists', 'if_exists', 'return', 'value'), prefix=r'\b', suffix=r'\s*\b'),
487 Name.Builtin),
488 (r'#.*', Comment),
489 (r'\\[\w\W]', String.Escape),
490 (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
491 (r'[\[\]{}()=]+', Operator),
492 (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
493 (r';', Punctuation),
494 ],
495 'data': [
496 (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
497 (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
498 (r'\s+', Text),
499 (r'[^=\s\[\]{}()$"\'`\\;#]+', Text),
500 (r'\d+(?= |\Z)', Number),
501 ],
502 'curly': [
503 (r'\}', Keyword, '#pop'),
504 (r':-', Keyword),
505 (r'\w+', Name.Variable),
506 (r'[^}:"\'`$]+', Punctuation),
507 (r':', Punctuation),
508 include('root'),
509 ],
510 'paren': [
511 (r'\)', Keyword, '#pop'),
512 include('root'),
513 ],
514 }

eric ide

mercurial