eric6/ThirdParty/Pygments/pygments/lexers/int_fiction.py

changeset 6942
2602857055c5
parent 6651
e8f3b5568b21
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.int_fiction
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for interactive fiction languages.
7
8 :copyright: Copyright 2006-2017 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, include, bygroups, using, \
15 this, default, words
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
17 Number, Punctuation, Error, Generic
18
19 __all__ = ['Inform6Lexer', 'Inform6TemplateLexer', 'Inform7Lexer',
20 'Tads3Lexer']
21
22
23 class Inform6Lexer(RegexLexer):
24 """
25 For `Inform 6 <http://inform-fiction.org/>`_ source code.
26
27 .. versionadded:: 2.0
28 """
29
30 name = 'Inform 6'
31 aliases = ['inform6', 'i6']
32 filenames = ['*.inf']
33
34 flags = re.MULTILINE | re.DOTALL | re.UNICODE
35
36 _name = r'[a-zA-Z_]\w*'
37
38 # Inform 7 maps these four character classes to their ASCII
39 # equivalents. To support Inform 6 inclusions within Inform 7,
40 # Inform6Lexer maps them too.
41 _dash = u'\\-\u2010-\u2014'
42 _dquote = u'"\u201c\u201d'
43 _squote = u"'\u2018\u2019"
44 _newline = u'\\n\u0085\u2028\u2029'
45
46 tokens = {
47 'root': [
48 (r'\A(!%%[^%s]*[%s])+' % (_newline, _newline), Comment.Preproc,
49 'directive'),
50 default('directive')
51 ],
52 '_whitespace': [
53 (r'\s+', Text),
54 (r'![^%s]*' % _newline, Comment.Single)
55 ],
56 'default': [
57 include('_whitespace'),
58 (r'\[', Punctuation, 'many-values'), # Array initialization
59 (r':|(?=;)', Punctuation, '#pop'),
60 (r'<', Punctuation), # Second angle bracket in an action statement
61 default(('expression', '_expression'))
62 ],
63
64 # Expressions
65 '_expression': [
66 include('_whitespace'),
67 (r'(?=sp\b)', Text, '#pop'),
68 (r'(?=[%s%s$0-9#a-zA-Z_])' % (_dquote, _squote), Text,
69 ('#pop', 'value')),
70 (r'\+\+|[%s]{1,2}(?!>)|~~?' % _dash, Operator),
71 (r'(?=[()\[%s,?@{:;])' % _dash, Text, '#pop')
72 ],
73 'expression': [
74 include('_whitespace'),
75 (r'\(', Punctuation, ('expression', '_expression')),
76 (r'\)', Punctuation, '#pop'),
77 (r'\[', Punctuation, ('#pop', 'statements', 'locals')),
78 (r'>(?=(\s+|(![^%s]*))*[>;])' % _newline, Punctuation),
79 (r'\+\+|[%s]{2}(?!>)' % _dash, Operator),
80 (r',', Punctuation, '_expression'),
81 (r'&&?|\|\|?|[=~><]?=|[%s]{1,2}>?|\.\.?[&#]?|::|[<>+*/%%]' % _dash,
82 Operator, '_expression'),
83 (r'(has|hasnt|in|notin|ofclass|or|provides)\b', Operator.Word,
84 '_expression'),
85 (r'sp\b', Name),
86 (r'\?~?', Name.Label, 'label?'),
87 (r'[@{]', Error),
88 default('#pop')
89 ],
90 '_assembly-expression': [
91 (r'\(', Punctuation, ('#push', '_expression')),
92 (r'[\[\]]', Punctuation),
93 (r'[%s]>' % _dash, Punctuation, '_expression'),
94 (r'sp\b', Keyword.Pseudo),
95 (r';', Punctuation, '#pop:3'),
96 include('expression')
97 ],
98 '_for-expression': [
99 (r'\)', Punctuation, '#pop:2'),
100 (r':', Punctuation, '#pop'),
101 include('expression')
102 ],
103 '_keyword-expression': [
104 (r'(from|near|to)\b', Keyword, '_expression'),
105 include('expression')
106 ],
107 '_list-expression': [
108 (r',', Punctuation, '#pop'),
109 include('expression')
110 ],
111 '_object-expression': [
112 (r'has\b', Keyword.Declaration, '#pop'),
113 include('_list-expression')
114 ],
115
116 # Values
117 'value': [
118 include('_whitespace'),
119 # Strings
120 (r'[%s][^@][%s]' % (_squote, _squote), String.Char, '#pop'),
121 (r'([%s])(@\{[0-9a-fA-F]{1,4}\})([%s])' % (_squote, _squote),
122 bygroups(String.Char, String.Escape, String.Char), '#pop'),
123 (r'([%s])(@.{2})([%s])' % (_squote, _squote),
124 bygroups(String.Char, String.Escape, String.Char), '#pop'),
125 (r'[%s]' % _squote, String.Single, ('#pop', 'dictionary-word')),
126 (r'[%s]' % _dquote, String.Double, ('#pop', 'string')),
127 # Numbers
128 (r'\$[+%s][0-9]*\.?[0-9]*([eE][+%s]?[0-9]+)?' % (_dash, _dash),
129 Number.Float, '#pop'),
130 (r'\$[0-9a-fA-F]+', Number.Hex, '#pop'),
131 (r'\$\$[01]+', Number.Bin, '#pop'),
132 (r'[0-9]+', Number.Integer, '#pop'),
133 # Values prefixed by hashes
134 (r'(##|#a\$)(%s)' % _name, bygroups(Operator, Name), '#pop'),
135 (r'(#g\$)(%s)' % _name,
136 bygroups(Operator, Name.Variable.Global), '#pop'),
137 (r'#[nw]\$', Operator, ('#pop', 'obsolete-dictionary-word')),
138 (r'(#r\$)(%s)' % _name, bygroups(Operator, Name.Function), '#pop'),
139 (r'#', Name.Builtin, ('#pop', 'system-constant')),
140 # System functions
141 (words((
142 'child', 'children', 'elder', 'eldest', 'glk', 'indirect', 'metaclass',
143 'parent', 'random', 'sibling', 'younger', 'youngest'), suffix=r'\b'),
144 Name.Builtin, '#pop'),
145 # Metaclasses
146 (r'(?i)(Class|Object|Routine|String)\b', Name.Builtin, '#pop'),
147 # Veneer routines
148 (words((
149 'Box__Routine', 'CA__Pr', 'CDefArt', 'CInDefArt', 'Cl__Ms',
150 'Copy__Primitive', 'CP__Tab', 'DA__Pr', 'DB__Pr', 'DefArt', 'Dynam__String',
151 'EnglishNumber', 'Glk__Wrap', 'IA__Pr', 'IB__Pr', 'InDefArt', 'Main__',
152 'Meta__class', 'OB__Move', 'OB__Remove', 'OC__Cl', 'OP__Pr', 'Print__Addr',
153 'Print__PName', 'PrintShortName', 'RA__Pr', 'RA__Sc', 'RL__Pr', 'R_Process',
154 'RT__ChG', 'RT__ChGt', 'RT__ChLDB', 'RT__ChLDW', 'RT__ChPR', 'RT__ChPrintA',
155 'RT__ChPrintC', 'RT__ChPrintO', 'RT__ChPrintS', 'RT__ChPS', 'RT__ChR',
156 'RT__ChSTB', 'RT__ChSTW', 'RT__ChT', 'RT__Err', 'RT__TrPS', 'RV__Pr',
157 'Symb__Tab', 'Unsigned__Compare', 'WV__Pr', 'Z__Region'),
158 prefix='(?i)', suffix=r'\b'),
159 Name.Builtin, '#pop'),
160 # Other built-in symbols
161 (words((
162 'call', 'copy', 'create', 'DEBUG', 'destroy', 'DICT_CHAR_SIZE',
163 'DICT_ENTRY_BYTES', 'DICT_IS_UNICODE', 'DICT_WORD_SIZE', 'false',
164 'FLOAT_INFINITY', 'FLOAT_NAN', 'FLOAT_NINFINITY', 'GOBJFIELD_CHAIN',
165 'GOBJFIELD_CHILD', 'GOBJFIELD_NAME', 'GOBJFIELD_PARENT',
166 'GOBJFIELD_PROPTAB', 'GOBJFIELD_SIBLING', 'GOBJ_EXT_START',
167 'GOBJ_TOTAL_LENGTH', 'Grammar__Version', 'INDIV_PROP_START', 'INFIX',
168 'infix__watching', 'MODULE_MODE', 'name', 'nothing', 'NUM_ATTR_BYTES', 'print',
169 'print_to_array', 'recreate', 'remaining', 'self', 'sender', 'STRICT_MODE',
170 'sw__var', 'sys__glob0', 'sys__glob1', 'sys__glob2', 'sys_statusline_flag',
171 'TARGET_GLULX', 'TARGET_ZCODE', 'temp__global2', 'temp__global3',
172 'temp__global4', 'temp_global', 'true', 'USE_MODULES', 'WORDSIZE'),
173 prefix='(?i)', suffix=r'\b'),
174 Name.Builtin, '#pop'),
175 # Other values
176 (_name, Name, '#pop')
177 ],
178 # Strings
179 'dictionary-word': [
180 (r'[~^]+', String.Escape),
181 (r'[^~^\\@({%s]+' % _squote, String.Single),
182 (r'[({]', String.Single),
183 (r'@\{[0-9a-fA-F]{,4}\}', String.Escape),
184 (r'@.{2}', String.Escape),
185 (r'[%s]' % _squote, String.Single, '#pop')
186 ],
187 'string': [
188 (r'[~^]+', String.Escape),
189 (r'[^~^\\@({%s]+' % _dquote, String.Double),
190 (r'[({]', String.Double),
191 (r'\\', String.Escape),
192 (r'@(\\\s*[%s]\s*)*@((\\\s*[%s]\s*)*[0-9])*' %
193 (_newline, _newline), String.Escape),
194 (r'@(\\\s*[%s]\s*)*\{((\\\s*[%s]\s*)*[0-9a-fA-F]){,4}'
195 r'(\\\s*[%s]\s*)*\}' % (_newline, _newline, _newline),
196 String.Escape),
197 (r'@(\\\s*[%s]\s*)*.(\\\s*[%s]\s*)*.' % (_newline, _newline),
198 String.Escape),
199 (r'[%s]' % _dquote, String.Double, '#pop')
200 ],
201 'plain-string': [
202 (r'[^~^\\({\[\]%s]+' % _dquote, String.Double),
203 (r'[~^({\[\]]', String.Double),
204 (r'\\', String.Escape),
205 (r'[%s]' % _dquote, String.Double, '#pop')
206 ],
207 # Names
208 '_constant': [
209 include('_whitespace'),
210 (_name, Name.Constant, '#pop'),
211 include('value')
212 ],
213 '_global': [
214 include('_whitespace'),
215 (_name, Name.Variable.Global, '#pop'),
216 include('value')
217 ],
218 'label?': [
219 include('_whitespace'),
220 (_name, Name.Label, '#pop'),
221 default('#pop')
222 ],
223 'variable?': [
224 include('_whitespace'),
225 (_name, Name.Variable, '#pop'),
226 default('#pop')
227 ],
228 # Values after hashes
229 'obsolete-dictionary-word': [
230 (r'\S\w*', String.Other, '#pop')
231 ],
232 'system-constant': [
233 include('_whitespace'),
234 (_name, Name.Builtin, '#pop')
235 ],
236
237 # Directives
238 'directive': [
239 include('_whitespace'),
240 (r'#', Punctuation),
241 (r';', Punctuation, '#pop'),
242 (r'\[', Punctuation,
243 ('default', 'statements', 'locals', 'routine-name?')),
244 (words((
245 'abbreviate', 'endif', 'dictionary', 'ifdef', 'iffalse', 'ifndef', 'ifnot',
246 'iftrue', 'ifv3', 'ifv5', 'release', 'serial', 'switches', 'system_file',
247 'version'), prefix='(?i)', suffix=r'\b'),
248 Keyword, 'default'),
249 (r'(?i)(array|global)\b', Keyword,
250 ('default', 'directive-keyword?', '_global')),
251 (r'(?i)attribute\b', Keyword, ('default', 'alias?', '_constant')),
252 (r'(?i)class\b', Keyword,
253 ('object-body', 'duplicates', 'class-name')),
254 (r'(?i)(constant|default)\b', Keyword,
255 ('default', 'expression', '_constant')),
256 (r'(?i)(end\b)(.*)', bygroups(Keyword, Text)),
257 (r'(?i)(extend|verb)\b', Keyword, 'grammar'),
258 (r'(?i)fake_action\b', Keyword, ('default', '_constant')),
259 (r'(?i)import\b', Keyword, 'manifest'),
260 (r'(?i)(include|link)\b', Keyword,
261 ('default', 'before-plain-string')),
262 (r'(?i)(lowstring|undef)\b', Keyword, ('default', '_constant')),
263 (r'(?i)message\b', Keyword, ('default', 'diagnostic')),
264 (r'(?i)(nearby|object)\b', Keyword,
265 ('object-body', '_object-head')),
266 (r'(?i)property\b', Keyword,
267 ('default', 'alias?', '_constant', 'property-keyword*')),
268 (r'(?i)replace\b', Keyword,
269 ('default', 'routine-name?', 'routine-name?')),
270 (r'(?i)statusline\b', Keyword, ('default', 'directive-keyword?')),
271 (r'(?i)stub\b', Keyword, ('default', 'routine-name?')),
272 (r'(?i)trace\b', Keyword,
273 ('default', 'trace-keyword?', 'trace-keyword?')),
274 (r'(?i)zcharacter\b', Keyword,
275 ('default', 'directive-keyword?', 'directive-keyword?')),
276 (_name, Name.Class, ('object-body', '_object-head'))
277 ],
278 # [, Replace, Stub
279 'routine-name?': [
280 include('_whitespace'),
281 (_name, Name.Function, '#pop'),
282 default('#pop')
283 ],
284 'locals': [
285 include('_whitespace'),
286 (r';', Punctuation, '#pop'),
287 (r'\*', Punctuation),
288 (r'"', String.Double, 'plain-string'),
289 (_name, Name.Variable)
290 ],
291 # Array
292 'many-values': [
293 include('_whitespace'),
294 (r';', Punctuation),
295 (r'\]', Punctuation, '#pop'),
296 (r':', Error),
297 default(('expression', '_expression'))
298 ],
299 # Attribute, Property
300 'alias?': [
301 include('_whitespace'),
302 (r'alias\b', Keyword, ('#pop', '_constant')),
303 default('#pop')
304 ],
305 # Class, Object, Nearby
306 'class-name': [
307 include('_whitespace'),
308 (r'(?=[,;]|(class|has|private|with)\b)', Text, '#pop'),
309 (_name, Name.Class, '#pop')
310 ],
311 'duplicates': [
312 include('_whitespace'),
313 (r'\(', Punctuation, ('#pop', 'expression', '_expression')),
314 default('#pop')
315 ],
316 '_object-head': [
317 (r'[%s]>' % _dash, Punctuation),
318 (r'(class|has|private|with)\b', Keyword.Declaration, '#pop'),
319 include('_global')
320 ],
321 'object-body': [
322 include('_whitespace'),
323 (r';', Punctuation, '#pop:2'),
324 (r',', Punctuation),
325 (r'class\b', Keyword.Declaration, 'class-segment'),
326 (r'(has|private|with)\b', Keyword.Declaration),
327 (r':', Error),
328 default(('_object-expression', '_expression'))
329 ],
330 'class-segment': [
331 include('_whitespace'),
332 (r'(?=[,;]|(class|has|private|with)\b)', Text, '#pop'),
333 (_name, Name.Class),
334 default('value')
335 ],
336 # Extend, Verb
337 'grammar': [
338 include('_whitespace'),
339 (r'=', Punctuation, ('#pop', 'default')),
340 (r'\*', Punctuation, ('#pop', 'grammar-line')),
341 default('_directive-keyword')
342 ],
343 'grammar-line': [
344 include('_whitespace'),
345 (r';', Punctuation, '#pop'),
346 (r'[/*]', Punctuation),
347 (r'[%s]>' % _dash, Punctuation, 'value'),
348 (r'(noun|scope)\b', Keyword, '=routine'),
349 default('_directive-keyword')
350 ],
351 '=routine': [
352 include('_whitespace'),
353 (r'=', Punctuation, 'routine-name?'),
354 default('#pop')
355 ],
356 # Import
357 'manifest': [
358 include('_whitespace'),
359 (r';', Punctuation, '#pop'),
360 (r',', Punctuation),
361 (r'(?i)global\b', Keyword, '_global'),
362 default('_global')
363 ],
364 # Include, Link, Message
365 'diagnostic': [
366 include('_whitespace'),
367 (r'[%s]' % _dquote, String.Double, ('#pop', 'message-string')),
368 default(('#pop', 'before-plain-string', 'directive-keyword?'))
369 ],
370 'before-plain-string': [
371 include('_whitespace'),
372 (r'[%s]' % _dquote, String.Double, ('#pop', 'plain-string'))
373 ],
374 'message-string': [
375 (r'[~^]+', String.Escape),
376 include('plain-string')
377 ],
378
379 # Keywords used in directives
380 '_directive-keyword!': [
381 include('_whitespace'),
382 (words((
383 'additive', 'alias', 'buffer', 'class', 'creature', 'data', 'error', 'fatalerror',
384 'first', 'has', 'held', 'initial', 'initstr', 'last', 'long', 'meta', 'multi',
385 'multiexcept', 'multiheld', 'multiinside', 'noun', 'number', 'only', 'private',
386 'replace', 'reverse', 'scope', 'score', 'special', 'string', 'table', 'terminating',
387 'time', 'topic', 'warning', 'with'), suffix=r'\b'),
388 Keyword, '#pop'),
389 (r'[%s]{1,2}>|[+=]' % _dash, Punctuation, '#pop')
390 ],
391 '_directive-keyword': [
392 include('_directive-keyword!'),
393 include('value')
394 ],
395 'directive-keyword?': [
396 include('_directive-keyword!'),
397 default('#pop')
398 ],
399 'property-keyword*': [
400 include('_whitespace'),
401 (r'(additive|long)\b', Keyword),
402 default('#pop')
403 ],
404 'trace-keyword?': [
405 include('_whitespace'),
406 (words((
407 'assembly', 'dictionary', 'expressions', 'lines', 'linker',
408 'objects', 'off', 'on', 'symbols', 'tokens', 'verbs'), suffix=r'\b'),
409 Keyword, '#pop'),
410 default('#pop')
411 ],
412
413 # Statements
414 'statements': [
415 include('_whitespace'),
416 (r'\]', Punctuation, '#pop'),
417 (r'[;{}]', Punctuation),
418 (words((
419 'box', 'break', 'continue', 'default', 'give', 'inversion',
420 'new_line', 'quit', 'read', 'remove', 'return', 'rfalse', 'rtrue',
421 'spaces', 'string', 'until'), suffix=r'\b'),
422 Keyword, 'default'),
423 (r'(do|else)\b', Keyword),
424 (r'(font|style)\b', Keyword,
425 ('default', 'miscellaneous-keyword?')),
426 (r'for\b', Keyword, ('for', '(?')),
427 (r'(if|switch|while)', Keyword,
428 ('expression', '_expression', '(?')),
429 (r'(jump|save|restore)\b', Keyword, ('default', 'label?')),
430 (r'objectloop\b', Keyword,
431 ('_keyword-expression', 'variable?', '(?')),
432 (r'print(_ret)?\b|(?=[%s])' % _dquote, Keyword, 'print-list'),
433 (r'\.', Name.Label, 'label?'),
434 (r'@', Keyword, 'opcode'),
435 (r'#(?![agrnw]\$|#)', Punctuation, 'directive'),
436 (r'<', Punctuation, 'default'),
437 (r'move\b', Keyword,
438 ('default', '_keyword-expression', '_expression')),
439 default(('default', '_keyword-expression', '_expression'))
440 ],
441 'miscellaneous-keyword?': [
442 include('_whitespace'),
443 (r'(bold|fixed|from|near|off|on|reverse|roman|to|underline)\b',
444 Keyword, '#pop'),
445 (r'(a|A|an|address|char|name|number|object|property|string|the|'
446 r'The)\b(?=(\s+|(![^%s]*))*\))' % _newline, Keyword.Pseudo,
447 '#pop'),
448 (r'%s(?=(\s+|(![^%s]*))*\))' % (_name, _newline), Name.Function,
449 '#pop'),
450 default('#pop')
451 ],
452 '(?': [
453 include('_whitespace'),
454 (r'\(', Punctuation, '#pop'),
455 default('#pop')
456 ],
457 'for': [
458 include('_whitespace'),
459 (r';', Punctuation, ('_for-expression', '_expression')),
460 default(('_for-expression', '_expression'))
461 ],
462 'print-list': [
463 include('_whitespace'),
464 (r';', Punctuation, '#pop'),
465 (r':', Error),
466 default(('_list-expression', '_expression', '_list-expression', 'form'))
467 ],
468 'form': [
469 include('_whitespace'),
470 (r'\(', Punctuation, ('#pop', 'miscellaneous-keyword?')),
471 default('#pop')
472 ],
473
474 # Assembly
475 'opcode': [
476 include('_whitespace'),
477 (r'[%s]' % _dquote, String.Double, ('operands', 'plain-string')),
478 (_name, Keyword, 'operands')
479 ],
480 'operands': [
481 (r':', Error),
482 default(('_assembly-expression', '_expression'))
483 ]
484 }
485
486 def get_tokens_unprocessed(self, text):
487 # 'in' is either a keyword or an operator.
488 # If the token two tokens after 'in' is ')', 'in' is a keyword:
489 # objectloop(a in b)
490 # Otherwise, it is an operator:
491 # objectloop(a in b && true)
492 objectloop_queue = []
493 objectloop_token_count = -1
494 previous_token = None
495 for index, token, value in RegexLexer.get_tokens_unprocessed(self,
496 text):
497 if previous_token is Name.Variable and value == 'in':
498 objectloop_queue = [[index, token, value]]
499 objectloop_token_count = 2
500 elif objectloop_token_count > 0:
501 if token not in Comment and token not in Text:
502 objectloop_token_count -= 1
503 objectloop_queue.append((index, token, value))
504 else:
505 if objectloop_token_count == 0:
506 if objectloop_queue[-1][2] == ')':
507 objectloop_queue[0][1] = Keyword
508 while objectloop_queue:
509 yield objectloop_queue.pop(0)
510 objectloop_token_count = -1
511 yield index, token, value
512 if token not in Comment and token not in Text:
513 previous_token = token
514 while objectloop_queue:
515 yield objectloop_queue.pop(0)
516
517
518 class Inform7Lexer(RegexLexer):
519 """
520 For `Inform 7 <http://inform7.com/>`_ source code.
521
522 .. versionadded:: 2.0
523 """
524
525 name = 'Inform 7'
526 aliases = ['inform7', 'i7']
527 filenames = ['*.ni', '*.i7x']
528
529 flags = re.MULTILINE | re.DOTALL | re.UNICODE
530
531 _dash = Inform6Lexer._dash
532 _dquote = Inform6Lexer._dquote
533 _newline = Inform6Lexer._newline
534 _start = r'\A|(?<=[%s])' % _newline
535
536 # There are three variants of Inform 7, differing in how to
537 # interpret at signs and braces in I6T. In top-level inclusions, at
538 # signs in the first column are inweb syntax. In phrase definitions
539 # and use options, tokens in braces are treated as I7. Use options
540 # also interpret "{N}".
541 tokens = {}
542 token_variants = ['+i6t-not-inline', '+i6t-inline', '+i6t-use-option']
543
544 for level in token_variants:
545 tokens[level] = {
546 '+i6-root': list(Inform6Lexer.tokens['root']),
547 '+i6t-root': [ # For Inform6TemplateLexer
548 (r'[^%s]*' % Inform6Lexer._newline, Comment.Preproc,
549 ('directive', '+p'))
550 ],
551 'root': [
552 (r'(\|?\s)+', Text),
553 (r'\[', Comment.Multiline, '+comment'),
554 (r'[%s]' % _dquote, Generic.Heading,
555 ('+main', '+titling', '+titling-string')),
556 default(('+main', '+heading?'))
557 ],
558 '+titling-string': [
559 (r'[^%s]+' % _dquote, Generic.Heading),
560 (r'[%s]' % _dquote, Generic.Heading, '#pop')
561 ],
562 '+titling': [
563 (r'\[', Comment.Multiline, '+comment'),
564 (r'[^%s.;:|%s]+' % (_dquote, _newline), Generic.Heading),
565 (r'[%s]' % _dquote, Generic.Heading, '+titling-string'),
566 (r'[%s]{2}|(?<=[\s%s])\|[\s%s]' % (_newline, _dquote, _dquote),
567 Text, ('#pop', '+heading?')),
568 (r'[.;:]|(?<=[\s%s])\|' % _dquote, Text, '#pop'),
569 (r'[|%s]' % _newline, Generic.Heading)
570 ],
571 '+main': [
572 (r'(?i)[^%s:a\[(|%s]+' % (_dquote, _newline), Text),
573 (r'[%s]' % _dquote, String.Double, '+text'),
574 (r':', Text, '+phrase-definition'),
575 (r'(?i)\bas\b', Text, '+use-option'),
576 (r'\[', Comment.Multiline, '+comment'),
577 (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
578 bygroups(Punctuation,
579 using(this, state=('+i6-root', 'directive'),
580 i6t='+i6t-not-inline'), Punctuation)),
581 (r'(%s|(?<=[\s;:.%s]))\|\s|[%s]{2,}' %
582 (_start, _dquote, _newline), Text, '+heading?'),
583 (r'(?i)[a(|%s]' % _newline, Text)
584 ],
585 '+phrase-definition': [
586 (r'\s+', Text),
587 (r'\[', Comment.Multiline, '+comment'),
588 (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
589 bygroups(Punctuation,
590 using(this, state=('+i6-root', 'directive',
591 'default', 'statements'),
592 i6t='+i6t-inline'), Punctuation), '#pop'),
593 default('#pop')
594 ],
595 '+use-option': [
596 (r'\s+', Text),
597 (r'\[', Comment.Multiline, '+comment'),
598 (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
599 bygroups(Punctuation,
600 using(this, state=('+i6-root', 'directive'),
601 i6t='+i6t-use-option'), Punctuation), '#pop'),
602 default('#pop')
603 ],
604 '+comment': [
605 (r'[^\[\]]+', Comment.Multiline),
606 (r'\[', Comment.Multiline, '#push'),
607 (r'\]', Comment.Multiline, '#pop')
608 ],
609 '+text': [
610 (r'[^\[%s]+' % _dquote, String.Double),
611 (r'\[.*?\]', String.Interpol),
612 (r'[%s]' % _dquote, String.Double, '#pop')
613 ],
614 '+heading?': [
615 (r'(\|?\s)+', Text),
616 (r'\[', Comment.Multiline, '+comment'),
617 (r'[%s]{4}\s+' % _dash, Text, '+documentation-heading'),
618 (r'[%s]{1,3}' % _dash, Text),
619 (r'(?i)(volume|book|part|chapter|section)\b[^%s]*' % _newline,
620 Generic.Heading, '#pop'),
621 default('#pop')
622 ],
623 '+documentation-heading': [
624 (r'\s+', Text),
625 (r'\[', Comment.Multiline, '+comment'),
626 (r'(?i)documentation\s+', Text, '+documentation-heading2'),
627 default('#pop')
628 ],
629 '+documentation-heading2': [
630 (r'\s+', Text),
631 (r'\[', Comment.Multiline, '+comment'),
632 (r'[%s]{4}\s' % _dash, Text, '+documentation'),
633 default('#pop:2')
634 ],
635 '+documentation': [
636 (r'(?i)(%s)\s*(chapter|example)\s*:[^%s]*' %
637 (_start, _newline), Generic.Heading),
638 (r'(?i)(%s)\s*section\s*:[^%s]*' % (_start, _newline),
639 Generic.Subheading),
640 (r'((%s)\t.*?[%s])+' % (_start, _newline),
641 using(this, state='+main')),
642 (r'[^%s\[]+|[%s\[]' % (_newline, _newline), Text),
643 (r'\[', Comment.Multiline, '+comment'),
644 ],
645 '+i6t-not-inline': [
646 (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
647 Comment.Preproc),
648 (r'(%s)@([%s]+|Purpose:)[^%s]*' % (_start, _dash, _newline),
649 Comment.Preproc),
650 (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
651 Generic.Heading, '+p')
652 ],
653 '+i6t-use-option': [
654 include('+i6t-not-inline'),
655 (r'(\{)(N)(\})', bygroups(Punctuation, Text, Punctuation))
656 ],
657 '+i6t-inline': [
658 (r'(\{)(\S[^}]*)?(\})',
659 bygroups(Punctuation, using(this, state='+main'),
660 Punctuation))
661 ],
662 '+i6t': [
663 (r'(\{[%s])(![^}]*)(\}?)' % _dash,
664 bygroups(Punctuation, Comment.Single, Punctuation)),
665 (r'(\{[%s])(lines)(:)([^}]*)(\}?)' % _dash,
666 bygroups(Punctuation, Keyword, Punctuation, Text,
667 Punctuation), '+lines'),
668 (r'(\{[%s])([^:}]*)(:?)([^}]*)(\}?)' % _dash,
669 bygroups(Punctuation, Keyword, Punctuation, Text,
670 Punctuation)),
671 (r'(\(\+)(.*?)(\+\)|\Z)',
672 bygroups(Punctuation, using(this, state='+main'),
673 Punctuation))
674 ],
675 '+p': [
676 (r'[^@]+', Comment.Preproc),
677 (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
678 Comment.Preproc, '#pop'),
679 (r'(%s)@([%s]|Purpose:)' % (_start, _dash), Comment.Preproc),
680 (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
681 Generic.Heading),
682 (r'@', Comment.Preproc)
683 ],
684 '+lines': [
685 (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
686 Comment.Preproc),
687 (r'(%s)@([%s]|Purpose:)[^%s]*' % (_start, _dash, _newline),
688 Comment.Preproc),
689 (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
690 Generic.Heading, '+p'),
691 (r'(%s)@\w*[ %s]' % (_start, _newline), Keyword),
692 (r'![^%s]*' % _newline, Comment.Single),
693 (r'(\{)([%s]endlines)(\})' % _dash,
694 bygroups(Punctuation, Keyword, Punctuation), '#pop'),
695 (r'[^@!{]+?([%s]|\Z)|.' % _newline, Text)
696 ]
697 }
698 # Inform 7 can include snippets of Inform 6 template language,
699 # so all of Inform6Lexer's states are copied here, with
700 # modifications to account for template syntax. Inform7Lexer's
701 # own states begin with '+' to avoid name conflicts. Some of
702 # Inform6Lexer's states begin with '_': these are not modified.
703 # They deal with template syntax either by including modified
704 # states, or by matching r'' then pushing to modified states.
705 for token in Inform6Lexer.tokens:
706 if token == 'root':
707 continue
708 tokens[level][token] = list(Inform6Lexer.tokens[token])
709 if not token.startswith('_'):
710 tokens[level][token][:0] = [include('+i6t'), include(level)]
711
712 def __init__(self, **options):
713 level = options.get('i6t', '+i6t-not-inline')
714 if level not in self._all_tokens:
715 self._tokens = self.__class__.process_tokendef(level)
716 else:
717 self._tokens = self._all_tokens[level]
718 RegexLexer.__init__(self, **options)
719
720
721 class Inform6TemplateLexer(Inform7Lexer):
722 """
723 For `Inform 6 template
724 <http://inform7.com/sources/src/i6template/Woven/index.html>`_ code.
725
726 .. versionadded:: 2.0
727 """
728
729 name = 'Inform 6 template'
730 aliases = ['i6t']
731 filenames = ['*.i6t']
732
733 def get_tokens_unprocessed(self, text, stack=('+i6t-root',)):
734 return Inform7Lexer.get_tokens_unprocessed(self, text, stack)
735
736
737 class Tads3Lexer(RegexLexer):
738 """
739 For `TADS 3 <http://www.tads.org/>`_ source code.
740 """
741
742 name = 'TADS 3'
743 aliases = ['tads3']
744 filenames = ['*.t']
745
746 flags = re.DOTALL | re.MULTILINE
747
748 _comment_single = r'(?://(?:[^\\\n]|\\+[\w\W])*$)'
749 _comment_multiline = r'(?:/\*(?:[^*]|\*(?!/))*\*/)'
750 _escape = (r'(?:\\(?:[\n\\<>"\'^v bnrt]|u[\da-fA-F]{,4}|x[\da-fA-F]{,2}|'
751 r'[0-3]?[0-7]{1,2}))')
752 _name = r'(?:[_a-zA-Z]\w*)'
753 _no_quote = r'(?=\s|\\?>)'
754 _operator = (r'(?:&&|\|\||\+\+|--|\?\?|::|[.,@\[\]~]|'
755 r'(?:[=+\-*/%!&|^]|<<?|>>?>?)=?)')
756 _ws = r'(?:\\|\s|%s|%s)' % (_comment_single, _comment_multiline)
757 _ws_pp = r'(?:\\\n|[^\S\n]|%s|%s)' % (_comment_single, _comment_multiline)
758
759 def _make_string_state(triple, double, verbatim=None, _escape=_escape):
760 if verbatim:
761 verbatim = ''.join(['(?:%s|%s)' % (re.escape(c.lower()),
762 re.escape(c.upper()))
763 for c in verbatim])
764 char = r'"' if double else r"'"
765 token = String.Double if double else String.Single
766 escaped_quotes = r'+|%s(?!%s{2})' % (char, char) if triple else r''
767 prefix = '%s%s' % ('t' if triple else '', 'd' if double else 's')
768 tag_state_name = '%sqt' % prefix
769 state = []
770 if triple:
771 state += [
772 (r'%s{3,}' % char, token, '#pop'),
773 (r'\\%s+' % char, String.Escape),
774 (char, token)
775 ]
776 else:
777 state.append((char, token, '#pop'))
778 state += [
779 include('s/verbatim'),
780 (r'[^\\<&{}%s]+' % char, token)
781 ]
782 if verbatim:
783 # This regex can't use `(?i)` because escape sequences are
784 # case-sensitive. `<\XMP>` works; `<\xmp>` doesn't.
785 state.append((r'\\?<(/|\\\\|(?!%s)\\)%s(?=[\s=>])' %
786 (_escape, verbatim),
787 Name.Tag, ('#pop', '%sqs' % prefix, tag_state_name)))
788 else:
789 state += [
790 (r'\\?<!([^><\\%s]|<(?!<)|\\%s%s|%s|\\.)*>?' %
791 (char, char, escaped_quotes, _escape), Comment.Multiline),
792 (r'(?i)\\?<listing(?=[\s=>]|\\>)', Name.Tag,
793 ('#pop', '%sqs/listing' % prefix, tag_state_name)),
794 (r'(?i)\\?<xmp(?=[\s=>]|\\>)', Name.Tag,
795 ('#pop', '%sqs/xmp' % prefix, tag_state_name)),
796 (r'\\?<([^\s=><\\%s]|<(?!<)|\\%s%s|%s|\\.)*' %
797 (char, char, escaped_quotes, _escape), Name.Tag,
798 tag_state_name),
799 include('s/entity')
800 ]
801 state += [
802 include('s/escape'),
803 (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
804 (char, char, escaped_quotes, _escape), String.Interpol),
805 (r'[\\&{}<]', token)
806 ]
807 return state
808
809 def _make_tag_state(triple, double, _escape=_escape):
810 char = r'"' if double else r"'"
811 quantifier = r'{3,}' if triple else r''
812 state_name = '%s%sqt' % ('t' if triple else '', 'd' if double else 's')
813 token = String.Double if double else String.Single
814 escaped_quotes = r'+|%s(?!%s{2})' % (char, char) if triple else r''
815 return [
816 (r'%s%s' % (char, quantifier), token, '#pop:2'),
817 (r'(\s|\\\n)+', Text),
818 (r'(=)(\\?")', bygroups(Punctuation, String.Double),
819 'dqs/%s' % state_name),
820 (r"(=)(\\?')", bygroups(Punctuation, String.Single),
821 'sqs/%s' % state_name),
822 (r'=', Punctuation, 'uqs/%s' % state_name),
823 (r'\\?>', Name.Tag, '#pop'),
824 (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
825 (char, char, escaped_quotes, _escape), String.Interpol),
826 (r'([^\s=><\\%s]|<(?!<)|\\%s%s|%s|\\.)+' %
827 (char, char, escaped_quotes, _escape), Name.Attribute),
828 include('s/escape'),
829 include('s/verbatim'),
830 include('s/entity'),
831 (r'[\\{}&]', Name.Attribute)
832 ]
833
834 def _make_attribute_value_state(terminator, host_triple, host_double,
835 _escape=_escape):
836 token = (String.Double if terminator == r'"' else
837 String.Single if terminator == r"'" else String.Other)
838 host_char = r'"' if host_double else r"'"
839 host_quantifier = r'{3,}' if host_triple else r''
840 host_token = String.Double if host_double else String.Single
841 escaped_quotes = (r'+|%s(?!%s{2})' % (host_char, host_char)
842 if host_triple else r'')
843 return [
844 (r'%s%s' % (host_char, host_quantifier), host_token, '#pop:3'),
845 (r'%s%s' % (r'' if token is String.Other else r'\\?', terminator),
846 token, '#pop'),
847 include('s/verbatim'),
848 include('s/entity'),
849 (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
850 (host_char, host_char, escaped_quotes, _escape), String.Interpol),
851 (r'([^\s"\'<%s{}\\&])+' % (r'>' if token is String.Other else r''),
852 token),
853 include('s/escape'),
854 (r'["\'\s&{<}\\]', token)
855 ]
856
857 tokens = {
858 'root': [
859 (u'\ufeff', Text),
860 (r'\{', Punctuation, 'object-body'),
861 (r';+', Punctuation),
862 (r'(?=(argcount|break|case|catch|continue|default|definingobj|'
863 r'delegated|do|else|for|foreach|finally|goto|if|inherited|'
864 r'invokee|local|nil|new|operator|replaced|return|self|switch|'
865 r'targetobj|targetprop|throw|true|try|while)\b)', Text, 'block'),
866 (r'(%s)(%s*)(\()' % (_name, _ws),
867 bygroups(Name.Function, using(this, state='whitespace'),
868 Punctuation),
869 ('block?/root', 'more/parameters', 'main/parameters')),
870 include('whitespace'),
871 (r'\++', Punctuation),
872 (r'[^\s!"%-(*->@-_a-z{-~]+', Error), # Averts an infinite loop
873 (r'(?!\Z)', Text, 'main/root')
874 ],
875 'main/root': [
876 include('main/basic'),
877 default(('#pop', 'object-body/no-braces', 'classes', 'class'))
878 ],
879 'object-body/no-braces': [
880 (r';', Punctuation, '#pop'),
881 (r'\{', Punctuation, ('#pop', 'object-body')),
882 include('object-body')
883 ],
884 'object-body': [
885 (r';', Punctuation),
886 (r'\{', Punctuation, '#push'),
887 (r'\}', Punctuation, '#pop'),
888 (r':', Punctuation, ('classes', 'class')),
889 (r'(%s?)(%s*)(\()' % (_name, _ws),
890 bygroups(Name.Function, using(this, state='whitespace'),
891 Punctuation),
892 ('block?', 'more/parameters', 'main/parameters')),
893 (r'(%s)(%s*)(\{)' % (_name, _ws),
894 bygroups(Name.Function, using(this, state='whitespace'),
895 Punctuation), 'block'),
896 (r'(%s)(%s*)(:)' % (_name, _ws),
897 bygroups(Name.Variable, using(this, state='whitespace'),
898 Punctuation),
899 ('object-body/no-braces', 'classes', 'class')),
900 include('whitespace'),
901 (r'->|%s' % _operator, Punctuation, 'main'),
902 default('main/object-body')
903 ],
904 'main/object-body': [
905 include('main/basic'),
906 (r'(%s)(%s*)(=?)' % (_name, _ws),
907 bygroups(Name.Variable, using(this, state='whitespace'),
908 Punctuation), ('#pop', 'more', 'main')),
909 default('#pop:2')
910 ],
911 'block?/root': [
912 (r'\{', Punctuation, ('#pop', 'block')),
913 include('whitespace'),
914 (r'(?=[\[\'"<(:])', Text, # It might be a VerbRule macro.
915 ('#pop', 'object-body/no-braces', 'grammar', 'grammar-rules')),
916 # It might be a macro like DefineAction.
917 default(('#pop', 'object-body/no-braces'))
918 ],
919 'block?': [
920 (r'\{', Punctuation, ('#pop', 'block')),
921 include('whitespace'),
922 default('#pop')
923 ],
924 'block/basic': [
925 (r'[;:]+', Punctuation),
926 (r'\{', Punctuation, '#push'),
927 (r'\}', Punctuation, '#pop'),
928 (r'default\b', Keyword.Reserved),
929 (r'(%s)(%s*)(:)' % (_name, _ws),
930 bygroups(Name.Label, using(this, state='whitespace'),
931 Punctuation)),
932 include('whitespace')
933 ],
934 'block': [
935 include('block/basic'),
936 (r'(?!\Z)', Text, ('more', 'main'))
937 ],
938 'block/embed': [
939 (r'>>', String.Interpol, '#pop'),
940 include('block/basic'),
941 (r'(?!\Z)', Text, ('more/embed', 'main'))
942 ],
943 'main/basic': [
944 include('whitespace'),
945 (r'\(', Punctuation, ('#pop', 'more', 'main')),
946 (r'\[', Punctuation, ('#pop', 'more/list', 'main')),
947 (r'\{', Punctuation, ('#pop', 'more/inner', 'main/inner',
948 'more/parameters', 'main/parameters')),
949 (r'\*|\.{3}', Punctuation, '#pop'),
950 (r'(?i)0x[\da-f]+', Number.Hex, '#pop'),
951 (r'(\d+\.(?!\.)\d*|\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+',
952 Number.Float, '#pop'),
953 (r'0[0-7]+', Number.Oct, '#pop'),
954 (r'\d+', Number.Integer, '#pop'),
955 (r'"""', String.Double, ('#pop', 'tdqs')),
956 (r"'''", String.Single, ('#pop', 'tsqs')),
957 (r'"', String.Double, ('#pop', 'dqs')),
958 (r"'", String.Single, ('#pop', 'sqs')),
959 (r'R"""', String.Regex, ('#pop', 'tdqr')),
960 (r"R'''", String.Regex, ('#pop', 'tsqr')),
961 (r'R"', String.Regex, ('#pop', 'dqr')),
962 (r"R'", String.Regex, ('#pop', 'sqr')),
963 # Two-token keywords
964 (r'(extern)(%s+)(object\b)' % _ws,
965 bygroups(Keyword.Reserved, using(this, state='whitespace'),
966 Keyword.Reserved)),
967 (r'(function|method)(%s*)(\()' % _ws,
968 bygroups(Keyword.Reserved, using(this, state='whitespace'),
969 Punctuation),
970 ('#pop', 'block?', 'more/parameters', 'main/parameters')),
971 (r'(modify)(%s+)(grammar\b)' % _ws,
972 bygroups(Keyword.Reserved, using(this, state='whitespace'),
973 Keyword.Reserved),
974 ('#pop', 'object-body/no-braces', ':', 'grammar')),
975 (r'(new)(%s+(?=(?:function|method)\b))' % _ws,
976 bygroups(Keyword.Reserved, using(this, state='whitespace'))),
977 (r'(object)(%s+)(template\b)' % _ws,
978 bygroups(Keyword.Reserved, using(this, state='whitespace'),
979 Keyword.Reserved), ('#pop', 'template')),
980 (r'(string)(%s+)(template\b)' % _ws,
981 bygroups(Keyword, using(this, state='whitespace'),
982 Keyword.Reserved), ('#pop', 'function-name')),
983 # Keywords
984 (r'(argcount|definingobj|invokee|replaced|targetobj|targetprop)\b',
985 Name.Builtin, '#pop'),
986 (r'(break|continue|goto)\b', Keyword.Reserved, ('#pop', 'label')),
987 (r'(case|extern|if|intrinsic|return|static|while)\b',
988 Keyword.Reserved),
989 (r'catch\b', Keyword.Reserved, ('#pop', 'catch')),
990 (r'class\b', Keyword.Reserved,
991 ('#pop', 'object-body/no-braces', 'class')),
992 (r'(default|do|else|finally|try)\b', Keyword.Reserved, '#pop'),
993 (r'(dictionary|property)\b', Keyword.Reserved,
994 ('#pop', 'constants')),
995 (r'enum\b', Keyword.Reserved, ('#pop', 'enum')),
996 (r'export\b', Keyword.Reserved, ('#pop', 'main')),
997 (r'(for|foreach)\b', Keyword.Reserved,
998 ('#pop', 'more/inner', 'main/inner')),
999 (r'(function|method)\b', Keyword.Reserved,
1000 ('#pop', 'block?', 'function-name')),
1001 (r'grammar\b', Keyword.Reserved,
1002 ('#pop', 'object-body/no-braces', 'grammar')),
1003 (r'inherited\b', Keyword.Reserved, ('#pop', 'inherited')),
1004 (r'local\b', Keyword.Reserved,
1005 ('#pop', 'more/local', 'main/local')),
1006 (r'(modify|replace|switch|throw|transient)\b', Keyword.Reserved,
1007 '#pop'),
1008 (r'new\b', Keyword.Reserved, ('#pop', 'class')),
1009 (r'(nil|true)\b', Keyword.Constant, '#pop'),
1010 (r'object\b', Keyword.Reserved, ('#pop', 'object-body/no-braces')),
1011 (r'operator\b', Keyword.Reserved, ('#pop', 'operator')),
1012 (r'propertyset\b', Keyword.Reserved,
1013 ('#pop', 'propertyset', 'main')),
1014 (r'self\b', Name.Builtin.Pseudo, '#pop'),
1015 (r'template\b', Keyword.Reserved, ('#pop', 'template')),
1016 # Operators
1017 (r'(__objref|defined)(%s*)(\()' % _ws,
1018 bygroups(Operator.Word, using(this, state='whitespace'),
1019 Operator), ('#pop', 'more/__objref', 'main')),
1020 (r'delegated\b', Operator.Word),
1021 # Compiler-defined macros and built-in properties
1022 (r'(__DATE__|__DEBUG|__LINE__|__FILE__|'
1023 r'__TADS_MACRO_FORMAT_VERSION|__TADS_SYS_\w*|__TADS_SYSTEM_NAME|'
1024 r'__TADS_VERSION_MAJOR|__TADS_VERSION_MINOR|__TADS3|__TIME__|'
1025 r'construct|finalize|grammarInfo|grammarTag|lexicalParent|'
1026 r'miscVocab|sourceTextGroup|sourceTextGroupName|'
1027 r'sourceTextGroupOrder|sourceTextOrder)\b', Name.Builtin, '#pop')
1028 ],
1029 'main': [
1030 include('main/basic'),
1031 (_name, Name, '#pop'),
1032 default('#pop')
1033 ],
1034 'more/basic': [
1035 (r'\(', Punctuation, ('more/list', 'main')),
1036 (r'\[', Punctuation, ('more', 'main')),
1037 (r'\.{3}', Punctuation),
1038 (r'->|\.\.', Punctuation, 'main'),
1039 (r'(?=;)|[:)\]]', Punctuation, '#pop'),
1040 include('whitespace'),
1041 (_operator, Operator, 'main'),
1042 (r'\?', Operator, ('main', 'more/conditional', 'main')),
1043 (r'(is|not)(%s+)(in\b)' % _ws,
1044 bygroups(Operator.Word, using(this, state='whitespace'),
1045 Operator.Word)),
1046 (r'[^\s!"%-_a-z{-~]+', Error) # Averts an infinite loop
1047 ],
1048 'more': [
1049 include('more/basic'),
1050 default('#pop')
1051 ],
1052 # Then expression (conditional operator)
1053 'more/conditional': [
1054 (r':(?!:)', Operator, '#pop'),
1055 include('more')
1056 ],
1057 # Embedded expressions
1058 'more/embed': [
1059 (r'>>', String.Interpol, '#pop:2'),
1060 include('more')
1061 ],
1062 # For/foreach loop initializer or short-form anonymous function
1063 'main/inner': [
1064 (r'\(', Punctuation, ('#pop', 'more/inner', 'main/inner')),
1065 (r'local\b', Keyword.Reserved, ('#pop', 'main/local')),
1066 include('main')
1067 ],
1068 'more/inner': [
1069 (r'\}', Punctuation, '#pop'),
1070 (r',', Punctuation, 'main/inner'),
1071 (r'(in|step)\b', Keyword, 'main/inner'),
1072 include('more')
1073 ],
1074 # Local
1075 'main/local': [
1076 (_name, Name.Variable, '#pop'),
1077 include('whitespace')
1078 ],
1079 'more/local': [
1080 (r',', Punctuation, 'main/local'),
1081 include('more')
1082 ],
1083 # List
1084 'more/list': [
1085 (r'[,:]', Punctuation, 'main'),
1086 include('more')
1087 ],
1088 # Parameter list
1089 'main/parameters': [
1090 (r'(%s)(%s*)(?=:)' % (_name, _ws),
1091 bygroups(Name.Variable, using(this, state='whitespace')), '#pop'),
1092 (r'(%s)(%s+)(%s)' % (_name, _ws, _name),
1093 bygroups(Name.Class, using(this, state='whitespace'),
1094 Name.Variable), '#pop'),
1095 (r'\[+', Punctuation),
1096 include('main/basic'),
1097 (_name, Name.Variable, '#pop'),
1098 default('#pop')
1099 ],
1100 'more/parameters': [
1101 (r'(:)(%s*(?=[?=,:)]))' % _ws,
1102 bygroups(Punctuation, using(this, state='whitespace'))),
1103 (r'[?\]]+', Punctuation),
1104 (r'[:)]', Punctuation, ('#pop', 'multimethod?')),
1105 (r',', Punctuation, 'main/parameters'),
1106 (r'=', Punctuation, ('more/parameter', 'main')),
1107 include('more')
1108 ],
1109 'more/parameter': [
1110 (r'(?=[,)])', Text, '#pop'),
1111 include('more')
1112 ],
1113 'multimethod?': [
1114 (r'multimethod\b', Keyword, '#pop'),
1115 include('whitespace'),
1116 default('#pop')
1117 ],
1118
1119 # Statements and expressions
1120 'more/__objref': [
1121 (r',', Punctuation, 'mode'),
1122 (r'\)', Operator, '#pop'),
1123 include('more')
1124 ],
1125 'mode': [
1126 (r'(error|warn)\b', Keyword, '#pop'),
1127 include('whitespace')
1128 ],
1129 'catch': [
1130 (r'\(+', Punctuation),
1131 (_name, Name.Exception, ('#pop', 'variables')),
1132 include('whitespace')
1133 ],
1134 'enum': [
1135 include('whitespace'),
1136 (r'token\b', Keyword, ('#pop', 'constants')),
1137 default(('#pop', 'constants'))
1138 ],
1139 'grammar': [
1140 (r'\)+', Punctuation),
1141 (r'\(', Punctuation, 'grammar-tag'),
1142 (r':', Punctuation, 'grammar-rules'),
1143 (_name, Name.Class),
1144 include('whitespace')
1145 ],
1146 'grammar-tag': [
1147 include('whitespace'),
1148 (r'"""([^\\"<]|""?(?!")|\\"+|\\.|<(?!<))+("{3,}|<<)|'
1149 r'R"""([^\\"]|""?(?!")|\\"+|\\.)+"{3,}|'
1150 r"'''([^\\'<]|''?(?!')|\\'+|\\.|<(?!<))+('{3,}|<<)|"
1151 r"R'''([^\\']|''?(?!')|\\'+|\\.)+'{3,}|"
1152 r'"([^\\"<]|\\.|<(?!<))+("|<<)|R"([^\\"]|\\.)+"|'
1153 r"'([^\\'<]|\\.|<(?!<))+('|<<)|R'([^\\']|\\.)+'|"
1154 r"([^)\s\\/]|/(?![/*]))+|\)", String.Other, '#pop')
1155 ],
1156 'grammar-rules': [
1157 include('string'),
1158 include('whitespace'),
1159 (r'(\[)(%s*)(badness)' % _ws,
1160 bygroups(Punctuation, using(this, state='whitespace'), Keyword),
1161 'main'),
1162 (r'->|%s|[()]' % _operator, Punctuation),
1163 (_name, Name.Constant),
1164 default('#pop:2')
1165 ],
1166 ':': [
1167 (r':', Punctuation, '#pop')
1168 ],
1169 'function-name': [
1170 (r'(<<([^>]|>>>|>(?!>))*>>)+', String.Interpol),
1171 (r'(?=%s?%s*[({])' % (_name, _ws), Text, '#pop'),
1172 (_name, Name.Function, '#pop'),
1173 include('whitespace')
1174 ],
1175 'inherited': [
1176 (r'<', Punctuation, ('#pop', 'classes', 'class')),
1177 include('whitespace'),
1178 (_name, Name.Class, '#pop'),
1179 default('#pop')
1180 ],
1181 'operator': [
1182 (r'negate\b', Operator.Word, '#pop'),
1183 include('whitespace'),
1184 (_operator, Operator),
1185 default('#pop')
1186 ],
1187 'propertyset': [
1188 (r'\(', Punctuation, ('more/parameters', 'main/parameters')),
1189 (r'\{', Punctuation, ('#pop', 'object-body')),
1190 include('whitespace')
1191 ],
1192 'template': [
1193 (r'(?=;)', Text, '#pop'),
1194 include('string'),
1195 (r'inherited\b', Keyword.Reserved),
1196 include('whitespace'),
1197 (r'->|\?|%s' % _operator, Punctuation),
1198 (_name, Name.Variable)
1199 ],
1200
1201 # Identifiers
1202 'class': [
1203 (r'\*|\.{3}', Punctuation, '#pop'),
1204 (r'object\b', Keyword.Reserved, '#pop'),
1205 (r'transient\b', Keyword.Reserved),
1206 (_name, Name.Class, '#pop'),
1207 include('whitespace'),
1208 default('#pop')
1209 ],
1210 'classes': [
1211 (r'[:,]', Punctuation, 'class'),
1212 include('whitespace'),
1213 (r'>', Punctuation, '#pop'),
1214 default('#pop')
1215 ],
1216 'constants': [
1217 (r',+', Punctuation),
1218 (r';', Punctuation, '#pop'),
1219 (r'property\b', Keyword.Reserved),
1220 (_name, Name.Constant),
1221 include('whitespace')
1222 ],
1223 'label': [
1224 (_name, Name.Label, '#pop'),
1225 include('whitespace'),
1226 default('#pop')
1227 ],
1228 'variables': [
1229 (r',+', Punctuation),
1230 (r'\)', Punctuation, '#pop'),
1231 include('whitespace'),
1232 (_name, Name.Variable)
1233 ],
1234
1235 # Whitespace and comments
1236 'whitespace': [
1237 (r'^%s*#(%s|[^\n]|(?<=\\)\n)*\n?' % (_ws_pp, _comment_multiline),
1238 Comment.Preproc),
1239 (_comment_single, Comment.Single),
1240 (_comment_multiline, Comment.Multiline),
1241 (r'\\+\n+%s*#?|\n+|([^\S\n]|\\)+' % _ws_pp, Text)
1242 ],
1243
1244 # Strings
1245 'string': [
1246 (r'"""', String.Double, 'tdqs'),
1247 (r"'''", String.Single, 'tsqs'),
1248 (r'"', String.Double, 'dqs'),
1249 (r"'", String.Single, 'sqs')
1250 ],
1251 's/escape': [
1252 (r'\{\{|\}\}|%s' % _escape, String.Escape)
1253 ],
1254 's/verbatim': [
1255 (r'<<\s*(as\s+decreasingly\s+likely\s+outcomes|cycling|else|end|'
1256 r'first\s+time|one\s+of|only|or|otherwise|'
1257 r'(sticky|(then\s+)?(purely\s+)?at)\s+random|stopping|'
1258 r'(then\s+)?(half\s+)?shuffled|\|\|)\s*>>', String.Interpol),
1259 (r'<<(%%(_(%s|\\?.)|[\-+ ,#]|\[\d*\]?)*\d*\.?\d*(%s|\\?.)|'
1260 r'\s*((else|otherwise)\s+)?(if|unless)\b)?' % (_escape, _escape),
1261 String.Interpol, ('block/embed', 'more/embed', 'main'))
1262 ],
1263 's/entity': [
1264 (r'(?i)&(#(x[\da-f]+|\d+)|[a-z][\da-z]*);?', Name.Entity)
1265 ],
1266 'tdqs': _make_string_state(True, True),
1267 'tsqs': _make_string_state(True, False),
1268 'dqs': _make_string_state(False, True),
1269 'sqs': _make_string_state(False, False),
1270 'tdqs/listing': _make_string_state(True, True, 'listing'),
1271 'tsqs/listing': _make_string_state(True, False, 'listing'),
1272 'dqs/listing': _make_string_state(False, True, 'listing'),
1273 'sqs/listing': _make_string_state(False, False, 'listing'),
1274 'tdqs/xmp': _make_string_state(True, True, 'xmp'),
1275 'tsqs/xmp': _make_string_state(True, False, 'xmp'),
1276 'dqs/xmp': _make_string_state(False, True, 'xmp'),
1277 'sqs/xmp': _make_string_state(False, False, 'xmp'),
1278
1279 # Tags
1280 'tdqt': _make_tag_state(True, True),
1281 'tsqt': _make_tag_state(True, False),
1282 'dqt': _make_tag_state(False, True),
1283 'sqt': _make_tag_state(False, False),
1284 'dqs/tdqt': _make_attribute_value_state(r'"', True, True),
1285 'dqs/tsqt': _make_attribute_value_state(r'"', True, False),
1286 'dqs/dqt': _make_attribute_value_state(r'"', False, True),
1287 'dqs/sqt': _make_attribute_value_state(r'"', False, False),
1288 'sqs/tdqt': _make_attribute_value_state(r"'", True, True),
1289 'sqs/tsqt': _make_attribute_value_state(r"'", True, False),
1290 'sqs/dqt': _make_attribute_value_state(r"'", False, True),
1291 'sqs/sqt': _make_attribute_value_state(r"'", False, False),
1292 'uqs/tdqt': _make_attribute_value_state(_no_quote, True, True),
1293 'uqs/tsqt': _make_attribute_value_state(_no_quote, True, False),
1294 'uqs/dqt': _make_attribute_value_state(_no_quote, False, True),
1295 'uqs/sqt': _make_attribute_value_state(_no_quote, False, False),
1296
1297 # Regular expressions
1298 'tdqr': [
1299 (r'[^\\"]+', String.Regex),
1300 (r'\\"*', String.Regex),
1301 (r'"{3,}', String.Regex, '#pop'),
1302 (r'"', String.Regex)
1303 ],
1304 'tsqr': [
1305 (r"[^\\']+", String.Regex),
1306 (r"\\'*", String.Regex),
1307 (r"'{3,}", String.Regex, '#pop'),
1308 (r"'", String.Regex)
1309 ],
1310 'dqr': [
1311 (r'[^\\"]+', String.Regex),
1312 (r'\\"?', String.Regex),
1313 (r'"', String.Regex, '#pop')
1314 ],
1315 'sqr': [
1316 (r"[^\\']+", String.Regex),
1317 (r"\\'?", String.Regex),
1318 (r"'", String.Regex, '#pop')
1319 ]
1320 }
1321
1322 def get_tokens_unprocessed(self, text, **kwargs):
1323 pp = r'^%s*#%s*' % (self._ws_pp, self._ws_pp)
1324 if_false_level = 0
1325 for index, token, value in (
1326 RegexLexer.get_tokens_unprocessed(self, text, **kwargs)):
1327 if if_false_level == 0: # Not in a false #if
1328 if (token is Comment.Preproc and
1329 re.match(r'%sif%s+(0|nil)%s*$\n?' %
1330 (pp, self._ws_pp, self._ws_pp), value)):
1331 if_false_level = 1
1332 else: # In a false #if
1333 if token is Comment.Preproc:
1334 if (if_false_level == 1 and
1335 re.match(r'%sel(if|se)\b' % pp, value)):
1336 if_false_level = 0
1337 elif re.match(r'%sif' % pp, value):
1338 if_false_level += 1
1339 elif re.match(r'%sendif\b' % pp, value):
1340 if_false_level -= 1
1341 else:
1342 token = Comment
1343 yield index, token, value

eric ide

mercurial