1"""
2 pygments.lexers.haxe
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for Haxe and related stuff.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13from pygments.lexer import ExtendedRegexLexer, RegexLexer, include, bygroups, \
14 default
15from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
16 Number, Punctuation, Generic, Whitespace
17
18__all__ = ['HaxeLexer', 'HxmlLexer']
19
20
21class HaxeLexer(ExtendedRegexLexer):
22 """
23 For Haxe source code.
24 """
25
26 name = 'Haxe'
27 url = 'http://haxe.org/'
28 aliases = ['haxe', 'hxsl', 'hx']
29 filenames = ['*.hx', '*.hxsl']
30 mimetypes = ['text/haxe', 'text/x-haxe', 'text/x-hx']
31 version_added = '1.3'
32
33 # keywords extracted from lexer.mll in the haxe compiler source
34 keyword = (r'(?:function|class|static|var|if|else|while|do|for|'
35 r'break|return|continue|extends|implements|import|'
36 r'switch|case|default|public|private|try|untyped|'
37 r'catch|new|this|throw|extern|enum|in|interface|'
38 r'cast|override|dynamic|typedef|package|'
39 r'inline|using|null|true|false|abstract)\b')
40
41 # idtype in lexer.mll
42 typeid = r'_*[A-Z]\w*'
43
44 # combined ident and dollar and idtype
45 ident = r'(?:_*[a-z]\w*|_+[0-9]\w*|' + typeid + r'|_+|\$\w+)'
46
47 binop = (r'(?:%=|&=|\|=|\^=|\+=|\-=|\*=|/=|<<=|>\s*>\s*=|>\s*>\s*>\s*=|==|'
48 r'!=|<=|>\s*=|&&|\|\||<<|>>>|>\s*>|\.\.\.|<|>|%|&|\||\^|\+|\*|'
49 r'/|\-|=>|=)')
50
51 # ident except keywords
52 ident_no_keyword = r'(?!' + keyword + ')' + ident
53
54 flags = re.DOTALL | re.MULTILINE
55
56 preproc_stack = []
57
58 def preproc_callback(self, match, ctx):
59 proc = match.group(2)
60
61 if proc == 'if':
62 # store the current stack
63 self.preproc_stack.append(ctx.stack[:])
64 elif proc in ['else', 'elseif']:
65 # restore the stack back to right before #if
66 if self.preproc_stack:
67 ctx.stack = self.preproc_stack[-1][:]
68 elif proc == 'end':
69 # remove the saved stack of previous #if
70 if self.preproc_stack:
71 self.preproc_stack.pop()
72
73 # #if and #elseif should follow by an expr
74 if proc in ['if', 'elseif']:
75 ctx.stack.append('preproc-expr')
76
77 # #error can be optionally follow by the error msg
78 if proc in ['error']:
79 ctx.stack.append('preproc-error')
80
81 yield match.start(), Comment.Preproc, '#' + proc
82 ctx.pos = match.end()
83
84 tokens = {
85 'root': [
86 include('spaces'),
87 include('meta'),
88 (r'(?:package)\b', Keyword.Namespace, ('semicolon', 'package')),
89 (r'(?:import)\b', Keyword.Namespace, ('semicolon', 'import')),
90 (r'(?:using)\b', Keyword.Namespace, ('semicolon', 'using')),
91 (r'(?:extern|private)\b', Keyword.Declaration),
92 (r'(?:abstract)\b', Keyword.Declaration, 'abstract'),
93 (r'(?:class|interface)\b', Keyword.Declaration, 'class'),
94 (r'(?:enum)\b', Keyword.Declaration, 'enum'),
95 (r'(?:typedef)\b', Keyword.Declaration, 'typedef'),
96
97 # top-level expression
98 # although it is not supported in haxe, but it is common to write
99 # expression in web pages the positive lookahead here is to prevent
100 # an infinite loop at the EOF
101 (r'(?=.)', Text, 'expr-statement'),
102 ],
103
104 # space/tab/comment/preproc
105 'spaces': [
106 (r'\s+', Whitespace),
107 (r'//[^\n\r]*', Comment.Single),
108 (r'/\*.*?\*/', Comment.Multiline),
109 (r'(#)(if|elseif|else|end|error)\b', preproc_callback),
110 ],
111
112 'string-single-interpol': [
113 (r'\$\{', String.Interpol, ('string-interpol-close', 'expr')),
114 (r'\$\$', String.Escape),
115 (r'\$(?=' + ident + ')', String.Interpol, 'ident'),
116 include('string-single'),
117 ],
118
119 'string-single': [
120 (r"'", String.Single, '#pop'),
121 (r'\\.', String.Escape),
122 (r'.', String.Single),
123 ],
124
125 'string-double': [
126 (r'"', String.Double, '#pop'),
127 (r'\\.', String.Escape),
128 (r'.', String.Double),
129 ],
130
131 'string-interpol-close': [
132 (r'\$'+ident, String.Interpol),
133 (r'\}', String.Interpol, '#pop'),
134 ],
135
136 'package': [
137 include('spaces'),
138 (ident, Name.Namespace),
139 (r'\.', Punctuation, 'import-ident'),
140 default('#pop'),
141 ],
142
143 'import': [
144 include('spaces'),
145 (ident, Name.Namespace),
146 (r'\*', Keyword), # wildcard import
147 (r'\.', Punctuation, 'import-ident'),
148 (r'in', Keyword.Namespace, 'ident'),
149 default('#pop'),
150 ],
151
152 'import-ident': [
153 include('spaces'),
154 (r'\*', Keyword, '#pop'), # wildcard import
155 (ident, Name.Namespace, '#pop'),
156 ],
157
158 'using': [
159 include('spaces'),
160 (ident, Name.Namespace),
161 (r'\.', Punctuation, 'import-ident'),
162 default('#pop'),
163 ],
164
165 'preproc-error': [
166 (r'\s+', Whitespace),
167 (r"'", String.Single, ('#pop', 'string-single')),
168 (r'"', String.Double, ('#pop', 'string-double')),
169 default('#pop'),
170 ],
171
172 'preproc-expr': [
173 (r'\s+', Whitespace),
174 (r'\!', Comment.Preproc),
175 (r'\(', Comment.Preproc, ('#pop', 'preproc-parenthesis')),
176
177 (ident, Comment.Preproc, '#pop'),
178
179 # Float
180 (r'\.[0-9]+', Number.Float),
181 (r'[0-9]+[eE][+\-]?[0-9]+', Number.Float),
182 (r'[0-9]+\.[0-9]*[eE][+\-]?[0-9]+', Number.Float),
183 (r'[0-9]+\.[0-9]+', Number.Float),
184 (r'[0-9]+\.(?!' + ident + r'|\.\.)', Number.Float),
185
186 # Int
187 (r'0x[0-9a-fA-F]+', Number.Hex),
188 (r'[0-9]+', Number.Integer),
189
190 # String
191 (r"'", String.Single, ('#pop', 'string-single')),
192 (r'"', String.Double, ('#pop', 'string-double')),
193 ],
194
195 'preproc-parenthesis': [
196 (r'\s+', Whitespace),
197 (r'\)', Comment.Preproc, '#pop'),
198 default('preproc-expr-in-parenthesis'),
199 ],
200
201 'preproc-expr-chain': [
202 (r'\s+', Whitespace),
203 (binop, Comment.Preproc, ('#pop', 'preproc-expr-in-parenthesis')),
204 default('#pop'),
205 ],
206
207 # same as 'preproc-expr' but able to chain 'preproc-expr-chain'
208 'preproc-expr-in-parenthesis': [
209 (r'\s+', Whitespace),
210 (r'\!', Comment.Preproc),
211 (r'\(', Comment.Preproc,
212 ('#pop', 'preproc-expr-chain', 'preproc-parenthesis')),
213
214 (ident, Comment.Preproc, ('#pop', 'preproc-expr-chain')),
215
216 # Float
217 (r'\.[0-9]+', Number.Float, ('#pop', 'preproc-expr-chain')),
218 (r'[0-9]+[eE][+\-]?[0-9]+', Number.Float, ('#pop', 'preproc-expr-chain')),
219 (r'[0-9]+\.[0-9]*[eE][+\-]?[0-9]+', Number.Float, ('#pop', 'preproc-expr-chain')),
220 (r'[0-9]+\.[0-9]+', Number.Float, ('#pop', 'preproc-expr-chain')),
221 (r'[0-9]+\.(?!' + ident + r'|\.\.)', Number.Float, ('#pop', 'preproc-expr-chain')),
222
223 # Int
224 (r'0x[0-9a-fA-F]+', Number.Hex, ('#pop', 'preproc-expr-chain')),
225 (r'[0-9]+', Number.Integer, ('#pop', 'preproc-expr-chain')),
226
227 # String
228 (r"'", String.Single,
229 ('#pop', 'preproc-expr-chain', 'string-single')),
230 (r'"', String.Double,
231 ('#pop', 'preproc-expr-chain', 'string-double')),
232 ],
233
234 'abstract': [
235 include('spaces'),
236 default(('#pop', 'abstract-body', 'abstract-relation',
237 'abstract-opaque', 'type-param-constraint', 'type-name')),
238 ],
239
240 'abstract-body': [
241 include('spaces'),
242 (r'\{', Punctuation, ('#pop', 'class-body')),
243 ],
244
245 'abstract-opaque': [
246 include('spaces'),
247 (r'\(', Punctuation, ('#pop', 'parenthesis-close', 'type')),
248 default('#pop'),
249 ],
250
251 'abstract-relation': [
252 include('spaces'),
253 (r'(?:to|from)', Keyword.Declaration, 'type'),
254 (r',', Punctuation),
255 default('#pop'),
256 ],
257
258 'meta': [
259 include('spaces'),
260 (r'@', Name.Decorator, ('meta-body', 'meta-ident', 'meta-colon')),
261 ],
262
263 # optional colon
264 'meta-colon': [
265 include('spaces'),
266 (r':', Name.Decorator, '#pop'),
267 default('#pop'),
268 ],
269
270 # same as 'ident' but set token as Name.Decorator instead of Name
271 'meta-ident': [
272 include('spaces'),
273 (ident, Name.Decorator, '#pop'),
274 ],
275
276 'meta-body': [
277 include('spaces'),
278 (r'\(', Name.Decorator, ('#pop', 'meta-call')),
279 default('#pop'),
280 ],
281
282 'meta-call': [
283 include('spaces'),
284 (r'\)', Name.Decorator, '#pop'),
285 default(('#pop', 'meta-call-sep', 'expr')),
286 ],
287
288 'meta-call-sep': [
289 include('spaces'),
290 (r'\)', Name.Decorator, '#pop'),
291 (r',', Punctuation, ('#pop', 'meta-call')),
292 ],
293
294 'typedef': [
295 include('spaces'),
296 default(('#pop', 'typedef-body', 'type-param-constraint',
297 'type-name')),
298 ],
299
300 'typedef-body': [
301 include('spaces'),
302 (r'=', Operator, ('#pop', 'optional-semicolon', 'type')),
303 ],
304
305 'enum': [
306 include('spaces'),
307 default(('#pop', 'enum-body', 'bracket-open',
308 'type-param-constraint', 'type-name')),
309 ],
310
311 'enum-body': [
312 include('spaces'),
313 include('meta'),
314 (r'\}', Punctuation, '#pop'),
315 (ident_no_keyword, Name, ('enum-member', 'type-param-constraint')),
316 ],
317
318 'enum-member': [
319 include('spaces'),
320 (r'\(', Punctuation,
321 ('#pop', 'semicolon', 'flag', 'function-param')),
322 default(('#pop', 'semicolon', 'flag')),
323 ],
324
325 'class': [
326 include('spaces'),
327 default(('#pop', 'class-body', 'bracket-open', 'extends',
328 'type-param-constraint', 'type-name')),
329 ],
330
331 'extends': [
332 include('spaces'),
333 (r'(?:extends|implements)\b', Keyword.Declaration, 'type'),
334 (r',', Punctuation), # the comma is made optional here, since haxe2
335 # requires the comma but haxe3 does not allow it
336 default('#pop'),
337 ],
338
339 'bracket-open': [
340 include('spaces'),
341 (r'\{', Punctuation, '#pop'),
342 ],
343
344 'bracket-close': [
345 include('spaces'),
346 (r'\}', Punctuation, '#pop'),
347 ],
348
349 'class-body': [
350 include('spaces'),
351 include('meta'),
352 (r'\}', Punctuation, '#pop'),
353 (r'(?:static|public|private|override|dynamic|inline|macro)\b',
354 Keyword.Declaration),
355 default('class-member'),
356 ],
357
358 'class-member': [
359 include('spaces'),
360 (r'(var)\b', Keyword.Declaration,
361 ('#pop', 'optional-semicolon', 'var')),
362 (r'(function)\b', Keyword.Declaration,
363 ('#pop', 'optional-semicolon', 'class-method')),
364 ],
365
366 # local function, anonymous or not
367 'function-local': [
368 include('spaces'),
369 (ident_no_keyword, Name.Function,
370 ('#pop', 'optional-expr', 'flag', 'function-param',
371 'parenthesis-open', 'type-param-constraint')),
372 default(('#pop', 'optional-expr', 'flag', 'function-param',
373 'parenthesis-open', 'type-param-constraint')),
374 ],
375
376 'optional-expr': [
377 include('spaces'),
378 include('expr'),
379 default('#pop'),
380 ],
381
382 'class-method': [
383 include('spaces'),
384 (ident, Name.Function, ('#pop', 'optional-expr', 'flag',
385 'function-param', 'parenthesis-open',
386 'type-param-constraint')),
387 ],
388
389 # function arguments
390 'function-param': [
391 include('spaces'),
392 (r'\)', Punctuation, '#pop'),
393 (r'\?', Punctuation),
394 (ident_no_keyword, Name,
395 ('#pop', 'function-param-sep', 'assign', 'flag')),
396 ],
397
398 'function-param-sep': [
399 include('spaces'),
400 (r'\)', Punctuation, '#pop'),
401 (r',', Punctuation, ('#pop', 'function-param')),
402 ],
403
404 'prop-get-set': [
405 include('spaces'),
406 (r'\(', Punctuation, ('#pop', 'parenthesis-close',
407 'prop-get-set-opt', 'comma', 'prop-get-set-opt')),
408 default('#pop'),
409 ],
410
411 'prop-get-set-opt': [
412 include('spaces'),
413 (r'(?:default|null|never|dynamic|get|set)\b', Keyword, '#pop'),
414 (ident_no_keyword, Text, '#pop'), # custom getter/setter
415 ],
416
417 'expr-statement': [
418 include('spaces'),
419 # makes semicolon optional here, just to avoid checking the last
420 # one is bracket or not.
421 default(('#pop', 'optional-semicolon', 'expr')),
422 ],
423
424 'expr': [
425 include('spaces'),
426 (r'@', Name.Decorator, ('#pop', 'optional-expr', 'meta-body',
427 'meta-ident', 'meta-colon')),
428 (r'(?:\+\+|\-\-|~(?!/)|!|\-)', Operator),
429 (r'\(', Punctuation, ('#pop', 'expr-chain', 'parenthesis')),
430 (r'(?:static|public|private|override|dynamic|inline)\b',
431 Keyword.Declaration),
432 (r'(?:function)\b', Keyword.Declaration, ('#pop', 'expr-chain',
433 'function-local')),
434 (r'\{', Punctuation, ('#pop', 'expr-chain', 'bracket')),
435 (r'(?:true|false|null)\b', Keyword.Constant, ('#pop', 'expr-chain')),
436 (r'(?:this)\b', Keyword, ('#pop', 'expr-chain')),
437 (r'(?:cast)\b', Keyword, ('#pop', 'expr-chain', 'cast')),
438 (r'(?:try)\b', Keyword, ('#pop', 'catch', 'expr')),
439 (r'(?:var)\b', Keyword.Declaration, ('#pop', 'var')),
440 (r'(?:new)\b', Keyword, ('#pop', 'expr-chain', 'new')),
441 (r'(?:switch)\b', Keyword, ('#pop', 'switch')),
442 (r'(?:if)\b', Keyword, ('#pop', 'if')),
443 (r'(?:do)\b', Keyword, ('#pop', 'do')),
444 (r'(?:while)\b', Keyword, ('#pop', 'while')),
445 (r'(?:for)\b', Keyword, ('#pop', 'for')),
446 (r'(?:untyped|throw)\b', Keyword),
447 (r'(?:return)\b', Keyword, ('#pop', 'optional-expr')),
448 (r'(?:macro)\b', Keyword, ('#pop', 'macro')),
449 (r'(?:continue|break)\b', Keyword, '#pop'),
450 (r'(?:\$\s*[a-z]\b|\$(?!'+ident+'))', Name, ('#pop', 'dollar')),
451 (ident_no_keyword, Name, ('#pop', 'expr-chain')),
452
453 # Float
454 (r'\.[0-9]+', Number.Float, ('#pop', 'expr-chain')),
455 (r'[0-9]+[eE][+\-]?[0-9]+', Number.Float, ('#pop', 'expr-chain')),
456 (r'[0-9]+\.[0-9]*[eE][+\-]?[0-9]+', Number.Float, ('#pop', 'expr-chain')),
457 (r'[0-9]+\.[0-9]+', Number.Float, ('#pop', 'expr-chain')),
458 (r'[0-9]+\.(?!' + ident + r'|\.\.)', Number.Float, ('#pop', 'expr-chain')),
459
460 # Int
461 (r'0x[0-9a-fA-F]+', Number.Hex, ('#pop', 'expr-chain')),
462 (r'[0-9]+', Number.Integer, ('#pop', 'expr-chain')),
463
464 # String
465 (r"'", String.Single, ('#pop', 'expr-chain', 'string-single-interpol')),
466 (r'"', String.Double, ('#pop', 'expr-chain', 'string-double')),
467
468 # EReg
469 (r'~/(\\\\|\\[^\\]|[^/\\\n])*/[gimsu]*', String.Regex, ('#pop', 'expr-chain')),
470
471 # Array
472 (r'\[', Punctuation, ('#pop', 'expr-chain', 'array-decl')),
473 ],
474
475 'expr-chain': [
476 include('spaces'),
477 (r'(?:\+\+|\-\-)', Operator),
478 (binop, Operator, ('#pop', 'expr')),
479 (r'(?:in)\b', Keyword, ('#pop', 'expr')),
480 (r'\?', Operator, ('#pop', 'expr', 'ternary', 'expr')),
481 (r'(\.)(' + ident_no_keyword + ')', bygroups(Punctuation, Name)),
482 (r'\[', Punctuation, 'array-access'),
483 (r'\(', Punctuation, 'call'),
484 default('#pop'),
485 ],
486
487 # macro reification
488 'macro': [
489 include('spaces'),
490 include('meta'),
491 (r':', Punctuation, ('#pop', 'type')),
492
493 (r'(?:extern|private)\b', Keyword.Declaration),
494 (r'(?:abstract)\b', Keyword.Declaration, ('#pop', 'optional-semicolon', 'abstract')),
495 (r'(?:class|interface)\b', Keyword.Declaration, ('#pop', 'optional-semicolon', 'macro-class')),
496 (r'(?:enum)\b', Keyword.Declaration, ('#pop', 'optional-semicolon', 'enum')),
497 (r'(?:typedef)\b', Keyword.Declaration, ('#pop', 'optional-semicolon', 'typedef')),
498
499 default(('#pop', 'expr')),
500 ],
501
502 'macro-class': [
503 (r'\{', Punctuation, ('#pop', 'class-body')),
504 include('class')
505 ],
506
507 # cast can be written as "cast expr" or "cast(expr, type)"
508 'cast': [
509 include('spaces'),
510 (r'\(', Punctuation, ('#pop', 'parenthesis-close',
511 'cast-type', 'expr')),
512 default(('#pop', 'expr')),
513 ],
514
515 # optionally give a type as the 2nd argument of cast()
516 'cast-type': [
517 include('spaces'),
518 (r',', Punctuation, ('#pop', 'type')),
519 default('#pop'),
520 ],
521
522 'catch': [
523 include('spaces'),
524 (r'(?:catch)\b', Keyword, ('expr', 'function-param',
525 'parenthesis-open')),
526 default('#pop'),
527 ],
528
529 # do-while loop
530 'do': [
531 include('spaces'),
532 default(('#pop', 'do-while', 'expr')),
533 ],
534
535 # the while after do
536 'do-while': [
537 include('spaces'),
538 (r'(?:while)\b', Keyword, ('#pop', 'parenthesis',
539 'parenthesis-open')),
540 ],
541
542 'while': [
543 include('spaces'),
544 (r'\(', Punctuation, ('#pop', 'expr', 'parenthesis')),
545 ],
546
547 'for': [
548 include('spaces'),
549 (r'\(', Punctuation, ('#pop', 'expr', 'parenthesis')),
550 ],
551
552 'if': [
553 include('spaces'),
554 (r'\(', Punctuation, ('#pop', 'else', 'optional-semicolon', 'expr',
555 'parenthesis')),
556 ],
557
558 'else': [
559 include('spaces'),
560 (r'(?:else)\b', Keyword, ('#pop', 'expr')),
561 default('#pop'),
562 ],
563
564 'switch': [
565 include('spaces'),
566 default(('#pop', 'switch-body', 'bracket-open', 'expr')),
567 ],
568
569 'switch-body': [
570 include('spaces'),
571 (r'(?:case|default)\b', Keyword, ('case-block', 'case')),
572 (r'\}', Punctuation, '#pop'),
573 ],
574
575 'case': [
576 include('spaces'),
577 (r':', Punctuation, '#pop'),
578 default(('#pop', 'case-sep', 'case-guard', 'expr')),
579 ],
580
581 'case-sep': [
582 include('spaces'),
583 (r':', Punctuation, '#pop'),
584 (r',', Punctuation, ('#pop', 'case')),
585 ],
586
587 'case-guard': [
588 include('spaces'),
589 (r'(?:if)\b', Keyword, ('#pop', 'parenthesis', 'parenthesis-open')),
590 default('#pop'),
591 ],
592
593 # optional multiple expr under a case
594 'case-block': [
595 include('spaces'),
596 (r'(?!(?:case|default)\b|\})', Keyword, 'expr-statement'),
597 default('#pop'),
598 ],
599
600 'new': [
601 include('spaces'),
602 default(('#pop', 'call', 'parenthesis-open', 'type')),
603 ],
604
605 'array-decl': [
606 include('spaces'),
607 (r'\]', Punctuation, '#pop'),
608 default(('#pop', 'array-decl-sep', 'expr')),
609 ],
610
611 'array-decl-sep': [
612 include('spaces'),
613 (r'\]', Punctuation, '#pop'),
614 (r',', Punctuation, ('#pop', 'array-decl')),
615 ],
616
617 'array-access': [
618 include('spaces'),
619 default(('#pop', 'array-access-close', 'expr')),
620 ],
621
622 'array-access-close': [
623 include('spaces'),
624 (r'\]', Punctuation, '#pop'),
625 ],
626
627 'comma': [
628 include('spaces'),
629 (r',', Punctuation, '#pop'),
630 ],
631
632 'colon': [
633 include('spaces'),
634 (r':', Punctuation, '#pop'),
635 ],
636
637 'semicolon': [
638 include('spaces'),
639 (r';', Punctuation, '#pop'),
640 ],
641
642 'optional-semicolon': [
643 include('spaces'),
644 (r';', Punctuation, '#pop'),
645 default('#pop'),
646 ],
647
648 # identity that CAN be a Haxe keyword
649 'ident': [
650 include('spaces'),
651 (ident, Name, '#pop'),
652 ],
653
654 'dollar': [
655 include('spaces'),
656 (r'\{', Punctuation, ('#pop', 'expr-chain', 'bracket-close', 'expr')),
657 default(('#pop', 'expr-chain')),
658 ],
659
660 'type-name': [
661 include('spaces'),
662 (typeid, Name, '#pop'),
663 ],
664
665 'type-full-name': [
666 include('spaces'),
667 (r'\.', Punctuation, 'ident'),
668 default('#pop'),
669 ],
670
671 'type': [
672 include('spaces'),
673 (r'\?', Punctuation),
674 (ident, Name, ('#pop', 'type-check', 'type-full-name')),
675 (r'\{', Punctuation, ('#pop', 'type-check', 'type-struct')),
676 (r'\(', Punctuation, ('#pop', 'type-check', 'type-parenthesis')),
677 ],
678
679 'type-parenthesis': [
680 include('spaces'),
681 default(('#pop', 'parenthesis-close', 'type')),
682 ],
683
684 'type-check': [
685 include('spaces'),
686 (r'->', Punctuation, ('#pop', 'type')),
687 (r'<(?!=)', Punctuation, 'type-param'),
688 default('#pop'),
689 ],
690
691 'type-struct': [
692 include('spaces'),
693 (r'\}', Punctuation, '#pop'),
694 (r'\?', Punctuation),
695 (r'>', Punctuation, ('comma', 'type')),
696 (ident_no_keyword, Name, ('#pop', 'type-struct-sep', 'type', 'colon')),
697 include('class-body'),
698 ],
699
700 'type-struct-sep': [
701 include('spaces'),
702 (r'\}', Punctuation, '#pop'),
703 (r',', Punctuation, ('#pop', 'type-struct')),
704 ],
705
706 # type-param can be a normal type or a constant literal...
707 'type-param-type': [
708 # Float
709 (r'\.[0-9]+', Number.Float, '#pop'),
710 (r'[0-9]+[eE][+\-]?[0-9]+', Number.Float, '#pop'),
711 (r'[0-9]+\.[0-9]*[eE][+\-]?[0-9]+', Number.Float, '#pop'),
712 (r'[0-9]+\.[0-9]+', Number.Float, '#pop'),
713 (r'[0-9]+\.(?!' + ident + r'|\.\.)', Number.Float, '#pop'),
714
715 # Int
716 (r'0x[0-9a-fA-F]+', Number.Hex, '#pop'),
717 (r'[0-9]+', Number.Integer, '#pop'),
718
719 # String
720 (r"'", String.Single, ('#pop', 'string-single')),
721 (r'"', String.Double, ('#pop', 'string-double')),
722
723 # EReg
724 (r'~/(\\\\|\\[^\\]|[^/\\\n])*/[gim]*', String.Regex, '#pop'),
725
726 # Array
727 (r'\[', Operator, ('#pop', 'array-decl')),
728
729 include('type'),
730 ],
731
732 # type-param part of a type
733 # ie. the <A,B> path in Map<A,B>
734 'type-param': [
735 include('spaces'),
736 default(('#pop', 'type-param-sep', 'type-param-type')),
737 ],
738
739 'type-param-sep': [
740 include('spaces'),
741 (r'>', Punctuation, '#pop'),
742 (r',', Punctuation, ('#pop', 'type-param')),
743 ],
744
745 # optional type-param that may include constraint
746 # ie. <T:Constraint, T2:(ConstraintA,ConstraintB)>
747 'type-param-constraint': [
748 include('spaces'),
749 (r'<(?!=)', Punctuation, ('#pop', 'type-param-constraint-sep',
750 'type-param-constraint-flag', 'type-name')),
751 default('#pop'),
752 ],
753
754 'type-param-constraint-sep': [
755 include('spaces'),
756 (r'>', Punctuation, '#pop'),
757 (r',', Punctuation, ('#pop', 'type-param-constraint-sep',
758 'type-param-constraint-flag', 'type-name')),
759 ],
760
761 # the optional constraint inside type-param
762 'type-param-constraint-flag': [
763 include('spaces'),
764 (r':', Punctuation, ('#pop', 'type-param-constraint-flag-type')),
765 default('#pop'),
766 ],
767
768 'type-param-constraint-flag-type': [
769 include('spaces'),
770 (r'\(', Punctuation, ('#pop', 'type-param-constraint-flag-type-sep',
771 'type')),
772 default(('#pop', 'type')),
773 ],
774
775 'type-param-constraint-flag-type-sep': [
776 include('spaces'),
777 (r'\)', Punctuation, '#pop'),
778 (r',', Punctuation, 'type'),
779 ],
780
781 # a parenthesis expr that contain exactly one expr
782 'parenthesis': [
783 include('spaces'),
784 default(('#pop', 'parenthesis-close', 'flag', 'expr')),
785 ],
786
787 'parenthesis-open': [
788 include('spaces'),
789 (r'\(', Punctuation, '#pop'),
790 ],
791
792 'parenthesis-close': [
793 include('spaces'),
794 (r'\)', Punctuation, '#pop'),
795 ],
796
797 'var': [
798 include('spaces'),
799 (ident_no_keyword, Text, ('#pop', 'var-sep', 'assign', 'flag', 'prop-get-set')),
800 ],
801
802 # optional more var decl.
803 'var-sep': [
804 include('spaces'),
805 (r',', Punctuation, ('#pop', 'var')),
806 default('#pop'),
807 ],
808
809 # optional assignment
810 'assign': [
811 include('spaces'),
812 (r'=', Operator, ('#pop', 'expr')),
813 default('#pop'),
814 ],
815
816 # optional type flag
817 'flag': [
818 include('spaces'),
819 (r':', Punctuation, ('#pop', 'type')),
820 default('#pop'),
821 ],
822
823 # colon as part of a ternary operator (?:)
824 'ternary': [
825 include('spaces'),
826 (r':', Operator, '#pop'),
827 ],
828
829 # function call
830 'call': [
831 include('spaces'),
832 (r'\)', Punctuation, '#pop'),
833 default(('#pop', 'call-sep', 'expr')),
834 ],
835
836 # after a call param
837 'call-sep': [
838 include('spaces'),
839 (r'\)', Punctuation, '#pop'),
840 (r',', Punctuation, ('#pop', 'call')),
841 ],
842
843 # bracket can be block or object
844 'bracket': [
845 include('spaces'),
846 (r'(?!(?:\$\s*[a-z]\b|\$(?!'+ident+')))' + ident_no_keyword, Name,
847 ('#pop', 'bracket-check')),
848 (r"'", String.Single, ('#pop', 'bracket-check', 'string-single')),
849 (r'"', String.Double, ('#pop', 'bracket-check', 'string-double')),
850 default(('#pop', 'block')),
851 ],
852
853 'bracket-check': [
854 include('spaces'),
855 (r':', Punctuation, ('#pop', 'object-sep', 'expr')), # is object
856 default(('#pop', 'block', 'optional-semicolon', 'expr-chain')), # is block
857 ],
858
859 # code block
860 'block': [
861 include('spaces'),
862 (r'\}', Punctuation, '#pop'),
863 default('expr-statement'),
864 ],
865
866 # object in key-value pairs
867 'object': [
868 include('spaces'),
869 (r'\}', Punctuation, '#pop'),
870 default(('#pop', 'object-sep', 'expr', 'colon', 'ident-or-string'))
871 ],
872
873 # a key of an object
874 'ident-or-string': [
875 include('spaces'),
876 (ident_no_keyword, Name, '#pop'),
877 (r"'", String.Single, ('#pop', 'string-single')),
878 (r'"', String.Double, ('#pop', 'string-double')),
879 ],
880
881 # after a key-value pair in object
882 'object-sep': [
883 include('spaces'),
884 (r'\}', Punctuation, '#pop'),
885 (r',', Punctuation, ('#pop', 'object')),
886 ],
887
888
889
890 }
891
892 def analyse_text(text):
893 if re.match(r'\w+\s*:\s*\w', text):
894 return 0.3
895
896
897class HxmlLexer(RegexLexer):
898 """
899 Lexer for haXe build files.
900 """
901 name = 'Hxml'
902 url = 'https://haxe.org/manual/compiler-usage-hxml.html'
903 aliases = ['haxeml', 'hxml']
904 filenames = ['*.hxml']
905 version_added = '1.6'
906
907 tokens = {
908 'root': [
909 # Separator
910 (r'(--)(next)', bygroups(Punctuation, Generic.Heading)),
911 # Compiler switches with one dash
912 (r'(-)(prompt|debug|v)', bygroups(Punctuation, Keyword.Keyword)),
913 # Compilerswitches with two dashes
914 (r'(--)(neko-source|flash-strict|flash-use-stage|no-opt|no-traces|'
915 r'no-inline|times|no-output)', bygroups(Punctuation, Keyword)),
916 # Targets and other options that take an argument
917 (r'(-)(cpp|js|neko|x|as3|swf9?|swf-lib|php|xml|main|lib|D|resource|'
918 r'cp|cmd)( +)(.+)',
919 bygroups(Punctuation, Keyword, Whitespace, String)),
920 # Options that take only numerical arguments
921 (r'(-)(swf-version)( +)(\d+)',
922 bygroups(Punctuation, Keyword, Whitespace, Number.Integer)),
923 # An Option that defines the size, the fps and the background
924 # color of an flash movie
925 (r'(-)(swf-header)( +)(\d+)(:)(\d+)(:)(\d+)(:)([A-Fa-f0-9]{6})',
926 bygroups(Punctuation, Keyword, Whitespace, Number.Integer,
927 Punctuation, Number.Integer, Punctuation, Number.Integer,
928 Punctuation, Number.Hex)),
929 # options with two dashes that takes arguments
930 (r'(--)(js-namespace|php-front|php-lib|remap|gen-hx-classes)( +)'
931 r'(.+)', bygroups(Punctuation, Keyword, Whitespace, String)),
932 # Single line comment, multiline ones are not allowed.
933 (r'#.*', Comment.Single)
934 ]
935 }