Pygments

generic syntax highlighter


root/pygments/lexers/compiled.py

Revision 1057:6a83c3ff66b8, 109.8 kB (checked in by Georg Brandl <georg@…>, 11 days ago)

merge with tim

Line 
1# -*- coding: utf-8 -*-
2"""
3    pygments.lexers.compiled
4    ~~~~~~~~~~~~~~~~~~~~~~~~
5
6    Lexers for compiled languages.
7
8    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9    :license: BSD, see LICENSE for details.
10"""
11
12import re
13
14from pygments.scanner import Scanner
15from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
16                           this, combined
17from pygments.util import get_bool_opt, get_list_opt
18from pygments.token import \
19     Text, Comment, Operator, Keyword, Name, String, Number, Punctuation, \
20     Error
21
22# backwards compatibility
23from pygments.lexers.functional import OcamlLexer
24
25__all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer',
26           'ScalaLexer', 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer',
27           'FortranLexer', 'GLShaderLexer', 'PrologLexer', 'CythonLexer',
28           'ValaLexer', 'OocLexer', 'GoLexer', 'FelixLexer', 'AdaLexer',
29           'Modula2Lexer', 'BlitzMaxLexer']
30
31
32class CLexer(RegexLexer):
33    """
34    For C source code with preprocessor directives.
35    """
36    name = 'C'
37    aliases = ['c']
38    filenames = ['*.c', '*.h']
39    mimetypes = ['text/x-chdr', 'text/x-csrc']
40
41    #: optional Comment or Whitespace
42    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
43
44    tokens = {
45        'whitespace': [
46            (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
47            (r'^\s*#', Comment.Preproc, 'macro'),
48            (r'^(\s*)([a-zA-Z_][a-zA-Z0-9_]*:(?!:))', bygroups(Text, Name.Label)),
49            (r'\n', Text),
50            (r'\s+', Text),
51            (r'\\\n', Text), # line continuation
52            (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single),
53            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
54        ],
55        'statements': [
56            (r'L?"', String, 'string'),
57            (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
58            (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
59            (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
60            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
61            (r'0[0-7]+[Ll]?', Number.Oct),
62            (r'\d+[Ll]?', Number.Integer),
63            (r'\*/', Error),
64            (r'[~!%^&*+=|?:<>/-]', Operator),
65            (r'[()\[\],.]', Punctuation),
66            (r'\b(case)(.+?)(:)', bygroups(Keyword, using(this), Text)),
67            (r'(auto|break|case|const|continue|default|do|else|enum|extern|'
68             r'for|goto|if|register|restricted|return|sizeof|static|struct|'
69             r'switch|typedef|union|volatile|virtual|while)\b', Keyword),
70            (r'(int|long|float|short|double|char|unsigned|signed|void)\b',
71             Keyword.Type),
72            (r'(_{0,2}inline|naked|restrict|thread|typename)\b', Keyword.Reserved),
73            (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
74             r'declspec|finally|int64|try|leave)\b', Keyword.Reserved),
75            (r'(true|false|NULL)\b', Name.Builtin),
76            ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
77        ],
78        'root': [
79            include('whitespace'),
80            # functions
81            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
82             r'([a-zA-Z_][a-zA-Z0-9_]*)'             # method name
83             r'(\s*\([^;]*?\))'                      # signature
84             r'(' + _ws + r')({)',
85             bygroups(using(this), Name.Function, using(this), using(this),
86                      Punctuation),
87             'function'),
88            # function declarations
89            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
90             r'([a-zA-Z_][a-zA-Z0-9_]*)'             # method name
91             r'(\s*\([^;]*?\))'                      # signature
92             r'(' + _ws + r')(;)',
93             bygroups(using(this), Name.Function, using(this), using(this),
94                      Punctuation)),
95            ('', Text, 'statement'),
96        ],
97        'statement' : [
98            include('whitespace'),
99            include('statements'),
100            ('[{}]', Punctuation),
101            (';', Punctuation, '#pop'),
102        ],
103        'function': [
104            include('whitespace'),
105            include('statements'),
106            (';', Punctuation),
107            ('{', Punctuation, '#push'),
108            ('}', Punctuation, '#pop'),
109        ],
110        'string': [
111            (r'"', String, '#pop'),
112            (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
113            (r'[^\\"\n]+', String), # all other characters
114            (r'\\\n', String), # line continuation
115            (r'\\', String), # stray backslash
116        ],
117        'macro': [
118            (r'[^/\n]+', Comment.Preproc),
119            (r'/[*](.|\n)*?[*]/', Comment.Multiline),
120            (r'//.*?\n', Comment.Single, '#pop'),
121            (r'/', Comment.Preproc),
122            (r'(?<=\\)\n', Comment.Preproc),
123            (r'\n', Comment.Preproc, '#pop'),
124        ],
125        'if0': [
126            (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
127            (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
128            (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
129            (r'.*?\n', Comment),
130        ]
131    }
132
133    stdlib_types = ['size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t',
134            'sig_atomic_t', 'fpos_t', 'clock_t', 'time_t', 'va_list',
135            'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 'mbstate_t',
136            'wctrans_t', 'wint_t', 'wctype_t']
137    c99_types = ['_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t',
138            'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t',
139            'int_least16_t', 'int_least32_t', 'int_least64_t',
140            'uint_least8_t', 'uint_least16_t', 'uint_least32_t',
141            'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t',
142            'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t',
143            'uint_fast64_t', 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t']
144
145    def __init__(self, **options):
146        self.stdlibhighlighting = get_bool_opt(options,
147                'stdlibhighlighting', True)
148        self.c99highlighting = get_bool_opt(options,
149                'c99highlighting', True)
150        RegexLexer.__init__(self, **options)
151
152    def get_tokens_unprocessed(self, text):
153        for index, token, value in \
154            RegexLexer.get_tokens_unprocessed(self, text):
155            if token is Name:
156                if self.stdlibhighlighting and value in self.stdlib_types:
157                    token = Keyword.Type
158                elif self.c99highlighting and value in self.c99_types:
159                    token = Keyword.Type
160            yield index, token, value
161
162class CppLexer(RegexLexer):
163    """
164    For C++ source code with preprocessor directives.
165    """
166    name = 'C++'
167    aliases = ['cpp', 'c++']
168    filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', '*.cc', '*.hh', '*.cxx', '*.hxx']
169    mimetypes = ['text/x-c++hdr', 'text/x-c++src']
170
171    tokens = {
172        'root': [
173            (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
174            (r'^\s*#', Comment.Preproc, 'macro'),
175            (r'\n', Text),
176            (r'\s+', Text),
177            (r'\\\n', Text), # line continuation
178            (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
179            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
180            (r'[{}]', Punctuation),
181            (r'L?"', String, 'string'),
182            (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
183            (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
184            (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
185            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
186            (r'0[0-7]+[Ll]?', Number.Oct),
187            (r'\d+[Ll]?', Number.Integer),
188            (r'\*/', Error),
189            (r'[~!%^&*+=|?:<>/-]', Operator),
190            (r'[()\[\],.;]', Punctuation),
191            (r'(asm|auto|break|case|catch|const|const_cast|continue|'
192             r'default|delete|do|dynamic_cast|else|enum|explicit|export|'
193             r'extern|for|friend|goto|if|mutable|namespace|new|operator|'
194             r'private|protected|public|register|reinterpret_cast|return|'
195             r'restrict|sizeof|static|static_cast|struct|switch|template|'
196             r'this|throw|throws|try|typedef|typeid|typename|union|using|'
197             r'volatile|virtual|while)\b', Keyword),
198            (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
199            (r'(bool|int|long|float|short|double|char|unsigned|signed|'
200             r'void|wchar_t)\b', Keyword.Type),
201            (r'(_{0,2}inline|naked|thread)\b', Keyword.Reserved),
202            (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
203             r'declspec|finally|int64|try|leave|wchar_t|w64|virtual_inheritance|'
204             r'uuidof|unaligned|super|single_inheritance|raise|noop|'
205             r'multiple_inheritance|m128i|m128d|m128|m64|interface|'
206             r'identifier|forceinline|event|assume)\b', Keyword.Reserved),
207            # Offload C++ extensions, http://offload.codeplay.com/
208            (r'(__offload|__blockingoffload|__outer)\b', Keyword.Psuedo),
209            (r'(true|false)\b', Keyword.Constant),
210            (r'NULL\b', Name.Builtin),
211            ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label),
212            ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
213        ],
214        'classname': [
215            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop'),
216            # template specification
217            (r'\s*(?=>)', Text, '#pop'),
218        ],
219        'string': [
220            (r'"', String, '#pop'),
221            (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
222            (r'[^\\"\n]+', String), # all other characters
223            (r'\\\n', String), # line continuation
224            (r'\\', String), # stray backslash
225        ],
226        'macro': [
227            (r'[^/\n]+', Comment.Preproc),
228            (r'/[*](.|\n)*?[*]/', Comment.Multiline),
229            (r'//.*?\n', Comment.Single, '#pop'),
230            (r'/', Comment.Preproc),
231            (r'(?<=\\)\n', Comment.Preproc),
232            (r'\n', Comment.Preproc, '#pop'),
233        ],
234        'if0': [
235            (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
236            (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
237            (r'.*?\n', Comment),
238        ]
239    }
240
241
242class DLexer(RegexLexer):
243    """
244    For D source.
245
246    *New in Pygments 1.2.*
247    """
248    name = 'D'
249    filenames = ['*.d', '*.di']
250    aliases = ['d']
251    mimetypes = ['text/x-dsrc']
252
253    tokens = {
254        'root': [
255            (r'\n', Text),
256            (r'\s+', Text),
257            #(r'\\\n', Text), # line continuations
258            # Comments
259            (r'//(.*?)\n', Comment.Single),
260            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
261            (r'/\+', Comment.Multiline, 'nested_comment'),
262            # Keywords
263            (r'(abstract|alias|align|asm|assert|auto|body|break|case|cast'
264             r'|catch|class|const|continue|debug|default|delegate|delete'
265             r'|deprecated|do|else|enum|export|extern|finally|final'
266             r'|foreach_reverse|foreach|for|function|goto|if|import|inout'
267             r'|interface|invariant|in|is|lazy|mixin|module|new|nothrow|out'
268             r'|override|package|pragma|private|protected|public|pure|ref|return'
269             r'|scope|static|struct|super|switch|synchronized|template|this'
270             r'|throw|try|typedef|typeid|typeof|union|unittest|version|volatile'
271             r'|while|with|__traits)\b', Keyword
272            ),
273            (r'(bool|byte|cdouble|cent|cfloat|char|creal|dchar|double|float'
274             r'|idouble|ifloat|int|ireal|long|real|short|ubyte|ucent|uint|ulong'
275             r'|ushort|void|wchar)\b', Keyword.Type
276            ),
277            (r'(false|true|null)\b', Keyword.Constant),
278            (r'macro\b', Keyword.Reserved),
279            (r'(string|wstring|dstring)\b', Name.Builtin),
280            # FloatLiteral
281            # -- HexFloat
282            (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
283             r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float),
284            # -- DecimalFloat
285            (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
286             r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float),
287            (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float),
288            # IntegerLiteral
289            # -- Binary
290            (r'0[Bb][01_]+', Number),
291            # -- Octal
292            (r'0[0-7_]+', Number.Oct),
293            # -- Hexadecimal
294            (r'0[xX][0-9a-fA-F_]+', Number.Hex),
295            # -- Decimal
296            (r'(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?', Number.Integer),
297            # CharacterLiteral
298            (r"""'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
299             r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'""",
300             String.Char
301            ),
302            # StringLiteral
303            # -- WysiwygString
304            (r'r"[^"]*"[cwd]?', String),
305            # -- AlternateWysiwygString
306            (r'`[^`]*`[cwd]?', String),
307            # -- DoubleQuotedString
308            (r'"(\\\\|\\"|[^"])*"[cwd]?', String),
309            # -- EscapeSequence
310            (r"\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}"
311             r"|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)",
312             String
313            ),
314            # -- HexString
315            (r'x"[0-9a-fA-F_\s]*"[cwd]?', String),
316            # -- DelimitedString
317            (r'q"\[', String, 'delimited_bracket'),
318            (r'q"\(', String, 'delimited_parenthesis'),
319            (r'q"<', String, 'delimited_angle'),
320            (r'q"{', String, 'delimited_curly'),
321            (r'q"([a-zA-Z_]\w*)\n.*?\n\1"', String),
322            (r'q"(.).*?\1"', String),
323            # -- TokenString
324            (r'q{', String, 'token_string'),
325            # Tokens
326            (r'(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>='
327             r'|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)'
328             r'|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]', Punctuation
329            ),
330            # Identifier
331            (r'[a-zA-Z_]\w*', Name),
332        ],
333        'nested_comment': [
334            (r'[^+/]+', Comment.Multiline),
335            (r'/\+', Comment.Multiline, '#push'),
336            (r'\+/', Comment.Multiline, '#pop'),
337            (r'[+/]', Comment.Multiline),
338        ],
339        'token_string': [
340            (r'{', Punctuation, 'token_string_nest'),
341            (r'}', String, '#pop'),
342            include('root'),
343        ],
344        'token_string_nest': [
345            (r'{', Punctuation, '#push'),
346            (r'}', Punctuation, '#pop'),
347            include('root'),
348        ],
349        'delimited_bracket': [
350            (r'[^\[\]]+', String),
351            (r'\[', String, 'delimited_inside_bracket'),
352            (r'\]"', String, '#pop'),
353        ],
354        'delimited_inside_bracket': [
355            (r'[^\[\]]+', String),
356            (r'\[', String, '#push'),
357            (r'\]', String, '#pop'),
358        ],
359        'delimited_parenthesis': [
360            (r'[^\(\)]+', String),
361            (r'\(', String, 'delimited_inside_parenthesis'),
362            (r'\)"', String, '#pop'),
363        ],
364        'delimited_inside_parenthesis': [
365            (r'[^\(\)]+', String),
366            (r'\(', String, '#push'),
367            (r'\)', String, '#pop'),
368        ],
369        'delimited_angle': [
370            (r'[^<>]+', String),
371            (r'<', String, 'delimited_inside_angle'),
372            (r'>"', String, '#pop'),
373        ],
374        'delimited_inside_angle': [
375            (r'[^<>]+', String),
376            (r'<', String, '#push'),
377            (r'>', String, '#pop'),
378        ],
379        'delimited_curly': [
380            (r'[^{}]+', String),
381            (r'{', String, 'delimited_inside_curly'),
382            (r'}"', String, '#pop'),
383        ],
384        'delimited_inside_curly': [
385            (r'[^{}]+', String),
386            (r'{', String, '#push'),
387            (r'}', String, '#pop'),
388        ],
389    }
390
391
392class DelphiLexer(Lexer):
393    """
394    For `Delphi <http://www.borland.com/delphi/>`_ (Borland Object Pascal),
395    Turbo Pascal and Free Pascal source code.
396
397    Additional options accepted:
398
399    `turbopascal`
400        Highlight Turbo Pascal specific keywords (default: ``True``).
401    `delphi`
402        Highlight Borland Delphi specific keywords (default: ``True``).
403    `freepascal`
404        Highlight Free Pascal specific keywords (default: ``True``).
405    `units`
406        A list of units that should be considered builtin, supported are
407        ``System``, ``SysUtils``, ``Classes`` and ``Math``.
408        Default is to consider all of them builtin.
409    """
410    name = 'Delphi'
411    aliases = ['delphi', 'pas', 'pascal', 'objectpascal']
412    filenames = ['*.pas']
413    mimetypes = ['text/x-pascal']
414
415    TURBO_PASCAL_KEYWORDS = [
416        'absolute', 'and', 'array', 'asm', 'begin', 'break', 'case',
417        'const', 'constructor', 'continue', 'destructor', 'div', 'do',
418        'downto', 'else', 'end', 'file', 'for', 'function', 'goto',
419        'if', 'implementation', 'in', 'inherited', 'inline', 'interface',
420        'label', 'mod', 'nil', 'not', 'object', 'of', 'on', 'operator',
421        'or', 'packed', 'procedure', 'program', 'record', 'reintroduce',
422        'repeat', 'self', 'set', 'shl', 'shr', 'string', 'then', 'to',
423        'type', 'unit', 'until', 'uses', 'var', 'while', 'with', 'xor'
424    ]
425
426    DELPHI_KEYWORDS = [
427        'as', 'class', 'except', 'exports', 'finalization', 'finally',
428        'initialization', 'is', 'library', 'on', 'property', 'raise',
429        'threadvar', 'try'
430    ]
431
432    FREE_PASCAL_KEYWORDS = [
433        'dispose', 'exit', 'false', 'new', 'true'
434    ]
435
436    BLOCK_KEYWORDS = set([
437        'begin', 'class', 'const', 'constructor', 'destructor', 'end',
438        'finalization', 'function', 'implementation', 'initialization',
439        'label', 'library', 'operator', 'procedure', 'program', 'property',
440        'record', 'threadvar', 'type', 'unit', 'uses', 'var'
441    ])
442
443    FUNCTION_MODIFIERS = set([
444        'alias', 'cdecl', 'export', 'inline', 'interrupt', 'nostackframe',
445        'pascal', 'register', 'safecall', 'softfloat', 'stdcall',
446        'varargs', 'name', 'dynamic', 'near', 'virtual', 'external',
447        'override', 'assembler'
448    ])
449
450    # XXX: those aren't global. but currently we know no way for defining
451    #      them just for the type context.
452    DIRECTIVES = set([
453        'absolute', 'abstract', 'assembler', 'cppdecl', 'default', 'far',
454        'far16', 'forward', 'index', 'oldfpccall', 'private', 'protected',
455        'published', 'public'
456    ])
457
458    BUILTIN_TYPES = set([
459        'ansichar', 'ansistring', 'bool', 'boolean', 'byte', 'bytebool',
460        'cardinal', 'char', 'comp', 'currency', 'double', 'dword',
461        'extended', 'int64', 'integer', 'iunknown', 'longbool', 'longint',
462        'longword', 'pansichar', 'pansistring', 'pbool', 'pboolean',
463        'pbyte', 'pbytearray', 'pcardinal', 'pchar', 'pcomp', 'pcurrency',
464        'pdate', 'pdatetime', 'pdouble', 'pdword', 'pextended', 'phandle',
465        'pint64', 'pinteger', 'plongint', 'plongword', 'pointer',
466        'ppointer', 'pshortint', 'pshortstring', 'psingle', 'psmallint',
467        'pstring', 'pvariant', 'pwidechar', 'pwidestring', 'pword',
468        'pwordarray', 'pwordbool', 'real', 'real48', 'shortint',
469        'shortstring', 'single', 'smallint', 'string', 'tclass', 'tdate',
470        'tdatetime', 'textfile', 'thandle', 'tobject', 'ttime', 'variant',
471        'widechar', 'widestring', 'word', 'wordbool'
472    ])
473
474    BUILTIN_UNITS = {
475        'System': [
476            'abs', 'acquireexceptionobject', 'addr', 'ansitoutf8',
477            'append', 'arctan', 'assert', 'assigned', 'assignfile',
478            'beginthread', 'blockread', 'blockwrite', 'break', 'chdir',
479            'chr', 'close', 'closefile', 'comptocurrency', 'comptodouble',
480            'concat', 'continue', 'copy', 'cos', 'dec', 'delete',
481            'dispose', 'doubletocomp', 'endthread', 'enummodules',
482            'enumresourcemodules', 'eof', 'eoln', 'erase', 'exceptaddr',
483            'exceptobject', 'exclude', 'exit', 'exp', 'filepos', 'filesize',
484            'fillchar', 'finalize', 'findclasshinstance', 'findhinstance',
485            'findresourcehinstance', 'flush', 'frac', 'freemem',
486            'get8087cw', 'getdir', 'getlasterror', 'getmem',
487            'getmemorymanager', 'getmodulefilename', 'getvariantmanager',
488            'halt', 'hi', 'high', 'inc', 'include', 'initialize', 'insert',
489            'int', 'ioresult', 'ismemorymanagerset', 'isvariantmanagerset',
490            'length', 'ln', 'lo', 'low', 'mkdir', 'move', 'new', 'odd',
491            'olestrtostring', 'olestrtostrvar', 'ord', 'paramcount',
492            'paramstr', 'pi', 'pos', 'pred', 'ptr', 'pucs4chars', 'random',
493            'randomize', 'read', 'readln', 'reallocmem',
494            'releaseexceptionobject', 'rename', 'reset', 'rewrite', 'rmdir',
495            'round', 'runerror', 'seek', 'seekeof', 'seekeoln',
496            'set8087cw', 'setlength', 'setlinebreakstyle',
497            'setmemorymanager', 'setstring', 'settextbuf',
498            'setvariantmanager', 'sin', 'sizeof', 'slice', 'sqr', 'sqrt',
499            'str', 'stringofchar', 'stringtoolestr', 'stringtowidechar',
500            'succ', 'swap', 'trunc', 'truncate', 'typeinfo',
501            'ucs4stringtowidestring', 'unicodetoutf8', 'uniquestring',
502            'upcase', 'utf8decode', 'utf8encode', 'utf8toansi',
503            'utf8tounicode', 'val', 'vararrayredim', 'varclear',
504            'widecharlentostring', 'widecharlentostrvar',
505            'widechartostring', 'widechartostrvar',
506            'widestringtoucs4string', 'write', 'writeln'
507        ],
508        'SysUtils': [
509            'abort', 'addexitproc', 'addterminateproc', 'adjustlinebreaks',
510            'allocmem', 'ansicomparefilename', 'ansicomparestr',
511            'ansicomparetext', 'ansidequotedstr', 'ansiextractquotedstr',
512            'ansilastchar', 'ansilowercase', 'ansilowercasefilename',
513            'ansipos', 'ansiquotedstr', 'ansisamestr', 'ansisametext',
514            'ansistrcomp', 'ansistricomp', 'ansistrlastchar', 'ansistrlcomp',
515            'ansistrlicomp', 'ansistrlower', 'ansistrpos', 'ansistrrscan',
516            'ansistrscan', 'ansistrupper', 'ansiuppercase',
517            'ansiuppercasefilename', 'appendstr', 'assignstr', 'beep',
518            'booltostr', 'bytetocharindex', 'bytetocharlen', 'bytetype',
519            'callterminateprocs', 'changefileext', 'charlength',
520            'chartobyteindex', 'chartobytelen', 'comparemem', 'comparestr',
521            'comparetext', 'createdir', 'createguid', 'currentyear',
522            'currtostr', 'currtostrf', 'date', 'datetimetofiledate',
523            'datetimetostr', 'datetimetostring', 'datetimetosystemtime',
524            'datetimetotimestamp', 'datetostr', 'dayofweek', 'decodedate',
525            'decodedatefully', 'decodetime', 'deletefile', 'directoryexists',
526            'diskfree', 'disksize', 'disposestr', 'encodedate', 'encodetime',
527            'exceptionerrormessage', 'excludetrailingbackslash',
528            'excludetrailingpathdelimiter', 'expandfilename',
529            'expandfilenamecase', 'expanduncfilename', 'extractfiledir',
530            'extractfiledrive', 'extractfileext', 'extractfilename',
531            'extractfilepath', 'extractrelativepath', 'extractshortpathname',
532            'fileage', 'fileclose', 'filecreate', 'filedatetodatetime',
533            'fileexists', 'filegetattr', 'filegetdate', 'fileisreadonly',
534            'fileopen', 'fileread', 'filesearch', 'fileseek', 'filesetattr',
535            'filesetdate', 'filesetreadonly', 'filewrite', 'finalizepackage',
536            'findclose', 'findcmdlineswitch', 'findfirst', 'findnext',
537            'floattocurr', 'floattodatetime', 'floattodecimal', 'floattostr',
538            'floattostrf', 'floattotext', 'floattotextfmt', 'fmtloadstr',
539            'fmtstr', 'forcedirectories', 'format', 'formatbuf', 'formatcurr',
540            'formatdatetime', 'formatfloat', 'freeandnil', 'getcurrentdir',
541            'getenvironmentvariable', 'getfileversion', 'getformatsettings',
542            'getlocaleformatsettings', 'getmodulename', 'getpackagedescription',
543            'getpackageinfo', 'gettime', 'guidtostring', 'incamonth',
544            'includetrailingbackslash', 'includetrailingpathdelimiter',
545            'incmonth', 'initializepackage', 'interlockeddecrement',
546            'interlockedexchange', 'interlockedexchangeadd',
547            'interlockedincrement', 'inttohex', 'inttostr', 'isdelimiter',
548            'isequalguid', 'isleapyear', 'ispathdelimiter', 'isvalidident',
549            'languages', 'lastdelimiter', 'loadpackage', 'loadstr',
550            'lowercase', 'msecstotimestamp', 'newstr', 'nextcharindex', 'now',
551            'outofmemoryerror', 'quotedstr', 'raiselastoserror',
552            'raiselastwin32error', 'removedir', 'renamefile', 'replacedate',
553            'replacetime', 'safeloadlibrary', 'samefilename', 'sametext',
554            'setcurrentdir', 'showexception', 'sleep', 'stralloc', 'strbufsize',
555            'strbytetype', 'strcat', 'strcharlength', 'strcomp', 'strcopy',
556            'strdispose', 'strecopy', 'strend', 'strfmt', 'stricomp',
557            'stringreplace', 'stringtoguid', 'strlcat', 'strlcomp', 'strlcopy',
558            'strlen', 'strlfmt', 'strlicomp', 'strlower', 'strmove', 'strnew',
559            'strnextchar', 'strpas', 'strpcopy', 'strplcopy', 'strpos',
560            'strrscan', 'strscan', 'strtobool', 'strtobooldef', 'strtocurr',
561            'strtocurrdef', 'strtodate', 'strtodatedef', 'strtodatetime',
562            'strtodatetimedef', 'strtofloat', 'strtofloatdef', 'strtoint',
563            'strtoint64', 'strtoint64def', 'strtointdef', 'strtotime',
564            'strtotimedef', 'strupper', 'supports', 'syserrormessage',
565            'systemtimetodatetime', 'texttofloat', 'time', 'timestamptodatetime',
566            'timestamptomsecs', 'timetostr', 'trim', 'trimleft', 'trimright',
567            'tryencodedate', 'tryencodetime', 'tryfloattocurr', 'tryfloattodatetime',
568            'trystrtobool', 'trystrtocurr', 'trystrtodate', 'trystrtodatetime',
569            'trystrtofloat', 'trystrtoint', 'trystrtoint64', 'trystrtotime',
570            'unloadpackage', 'uppercase', 'widecomparestr', 'widecomparetext',
571            'widefmtstr', 'wideformat', 'wideformatbuf', 'widelowercase',
572            'widesamestr', 'widesametext', 'wideuppercase', 'win32check',
573            'wraptext'
574        ],
575        'Classes': [
576            'activateclassgroup', 'allocatehwnd', 'bintohex', 'checksynchronize',
577            'collectionsequal', 'countgenerations', 'deallocatehwnd', 'equalrect',
578            'extractstrings', 'findclass', 'findglobalcomponent', 'getclass',
579            'groupdescendantswith', 'hextobin', 'identtoint',
580            'initinheritedcomponent', 'inttoident', 'invalidpoint',
581            'isuniqueglobalcomponentname', 'linestart', 'objectbinarytotext',
582            'objectresourcetotext', 'objecttexttobinary', 'objecttexttoresource',
583            'pointsequal', 'readcomponentres', 'readcomponentresex',
584            'readcomponentresfile', 'rect', 'registerclass', 'registerclassalias',
585            'registerclasses', 'registercomponents', 'registerintegerconsts',
586            'registernoicon', 'registernonactivex', 'smallpoint', 'startclassgroup',
587            'teststreamformat', 'unregisterclass', 'unregisterclasses',
588            'unregisterintegerconsts', 'unregistermoduleclasses',
589            'writecomponentresfile'
590        ],
591        'Math': [
592            'arccos', 'arccosh', 'arccot', 'arccoth', 'arccsc', 'arccsch', 'arcsec',
593            'arcsech', 'arcsin', 'arcsinh', 'arctan2', 'arctanh', 'ceil',
594            'comparevalue', 'cosecant', 'cosh', 'cot', 'cotan', 'coth', 'csc',
595            'csch', 'cycletodeg', 'cycletograd', 'cycletorad', 'degtocycle',
596            'degtograd', 'degtorad', 'divmod', 'doubledecliningbalance',
597            'ensurerange', 'floor', 'frexp', 'futurevalue', 'getexceptionmask',
598            'getprecisionmode', 'getroundmode', 'gradtocycle', 'gradtodeg',
599            'gradtorad', 'hypot', 'inrange', 'interestpayment', 'interestrate',
600            'internalrateofreturn', 'intpower', 'isinfinite', 'isnan', 'iszero',
601            'ldexp', 'lnxp1', 'log10', 'log2', 'logn', 'max', 'maxintvalue',
602            'maxvalue', 'mean', 'meanandstddev', 'min', 'minintvalue', 'minvalue',
603            'momentskewkurtosis', 'netpresentvalue', 'norm', 'numberofperiods',
604            'payment', 'periodpayment', 'poly', 'popnstddev', 'popnvariance',
605            'power', 'presentvalue', 'radtocycle', 'radtodeg', 'radtograd',
606            'randg', 'randomrange', 'roundto', 'samevalue', 'sec', 'secant',
607            'sech', 'setexceptionmask', 'setprecisionmode', 'setroundmode',
608            'sign', 'simpleroundto', 'sincos', 'sinh', 'slndepreciation', 'stddev',
609            'sum', 'sumint', 'sumofsquares', 'sumsandsquares', 'syddepreciation',
610            'tan', 'tanh', 'totalvariance', 'variance'
611        ]
612    }
613
614    ASM_REGISTERS = set([
615        'ah', 'al', 'ax', 'bh', 'bl', 'bp', 'bx', 'ch', 'cl', 'cr0',
616        'cr1', 'cr2', 'cr3', 'cr4', 'cs', 'cx', 'dh', 'di', 'dl', 'dr0',
617        'dr1', 'dr2', 'dr3', 'dr4', 'dr5', 'dr6', 'dr7', 'ds', 'dx',
618        'eax', 'ebp', 'ebx', 'ecx', 'edi', 'edx', 'es', 'esi', 'esp',
619        'fs', 'gs', 'mm0', 'mm1', 'mm2', 'mm3', 'mm4', 'mm5', 'mm6',
620        'mm7', 'si', 'sp', 'ss', 'st0', 'st1', 'st2', 'st3', 'st4', 'st5',
621        'st6', 'st7', 'xmm0', 'xmm1', 'xmm2', 'xmm3', 'xmm4', 'xmm5',
622        'xmm6', 'xmm7'
623    ])
624
625    ASM_INSTRUCTIONS = set([
626        'aaa', 'aad', 'aam', 'aas', 'adc', 'add', 'and', 'arpl', 'bound',
627        'bsf', 'bsr', 'bswap', 'bt', 'btc', 'btr', 'bts', 'call', 'cbw',
628        'cdq', 'clc', 'cld', 'cli', 'clts', 'cmc', 'cmova', 'cmovae',
629        'cmovb', 'cmovbe', 'cmovc', 'cmovcxz', 'cmove', 'cmovg',
630        'cmovge', 'cmovl', 'cmovle', 'cmovna', 'cmovnae', 'cmovnb',
631        'cmovnbe', 'cmovnc', 'cmovne', 'cmovng', 'cmovnge', 'cmovnl',
632        'cmovnle', 'cmovno', 'cmovnp', 'cmovns', 'cmovnz', 'cmovo',
633        'cmovp', 'cmovpe', 'cmovpo', 'cmovs', 'cmovz', 'cmp', 'cmpsb',
634        'cmpsd', 'cmpsw', 'cmpxchg', 'cmpxchg486', 'cmpxchg8b', 'cpuid',
635        'cwd', 'cwde', 'daa', 'das', 'dec', 'div', 'emms', 'enter', 'hlt',
636        'ibts', 'icebp', 'idiv', 'imul', 'in', 'inc', 'insb', 'insd',
637        'insw', 'int', 'int01', 'int03', 'int1', 'int3', 'into', 'invd',
638        'invlpg', 'iret', 'iretd', 'iretw', 'ja', 'jae', 'jb', 'jbe',
639        'jc', 'jcxz', 'jcxz', 'je', 'jecxz', 'jg', 'jge', 'jl', 'jle',
640        'jmp', 'jna', 'jnae', 'jnb', 'jnbe', 'jnc', 'jne', 'jng', 'jnge',
641        'jnl', 'jnle', 'jno', 'jnp', 'jns', 'jnz', 'jo', 'jp', 'jpe',
642        'jpo', 'js', 'jz', 'lahf', 'lar', 'lcall', 'lds', 'lea', 'leave',
643        'les', 'lfs', 'lgdt', 'lgs', 'lidt', 'ljmp', 'lldt', 'lmsw',
644        'loadall', 'loadall286', 'lock', 'lodsb', 'lodsd', 'lodsw',
645        'loop', 'loope', 'loopne', 'loopnz', 'loopz', 'lsl', 'lss', 'ltr',
646        'mov', 'movd', 'movq', 'movsb', 'movsd', 'movsw', 'movsx',
647        'movzx', 'mul', 'neg', 'nop', 'not', 'or', 'out', 'outsb', 'outsd',
648        'outsw', 'pop', 'popa', 'popad', 'popaw', 'popf', 'popfd', 'popfw',
649        'push', 'pusha', 'pushad', 'pushaw', 'pushf', 'pushfd', 'pushfw',
650        'rcl', 'rcr', 'rdmsr', 'rdpmc', 'rdshr', 'rdtsc', 'rep', 'repe',
651        'repne', 'repnz', 'repz', 'ret', 'retf', 'retn', 'rol', 'ror',
652        'rsdc', 'rsldt', 'rsm', 'sahf', 'sal', 'salc', 'sar', 'sbb',
653        'scasb', 'scasd', 'scasw', 'seta', 'setae', 'setb', 'setbe',
654        'setc', 'setcxz', 'sete', 'setg', 'setge', 'setl', 'setle',
655        'setna', 'setnae', 'setnb', 'setnbe', 'setnc', 'setne', 'setng',
656        'setnge', 'setnl', 'setnle', 'setno', 'setnp', 'setns', 'setnz',
657        'seto', 'setp', 'setpe', 'setpo', 'sets', 'setz', 'sgdt', 'shl',
658        'shld', 'shr', 'shrd', 'sidt', 'sldt', 'smi', 'smint', 'smintold',
659        'smsw', 'stc', 'std', 'sti', 'stosb', 'stosd', 'stosw', 'str',
660        'sub', 'svdc', 'svldt', 'svts', 'syscall', 'sysenter', 'sysexit',
661        'sysret', 'test', 'ud1', 'ud2', 'umov', 'verr', 'verw', 'wait',
662        'wbinvd', 'wrmsr', 'wrshr', 'xadd', 'xbts', 'xchg', 'xlat',
663        'xlatb', 'xor'
664    ])
665
666    def __init__(self, **options):
667        Lexer.__init__(self, **options)
668        self.keywords = set()
669        if get_bool_opt(options, 'turbopascal', True):
670            self.keywords.update(self.TURBO_PASCAL_KEYWORDS)
671        if get_bool_opt(options, 'delphi', True):
672            self.keywords.update(self.DELPHI_KEYWORDS)
673        if get_bool_opt(options, 'freepascal', True):
674            self.keywords.update(self.FREE_PASCAL_KEYWORDS)
675        self.builtins = set()
676        for unit in get_list_opt(options, 'units', self.BUILTIN_UNITS.keys()):
677            self.builtins.update(self.BUILTIN_UNITS[unit])
678
679    def get_tokens_unprocessed(self, text):
680        scanner = Scanner(text, re.DOTALL | re.MULTILINE | re.IGNORECASE)
681        stack = ['initial']
682        in_function_block = False
683        in_property_block = False
684        was_dot = False
685        next_token_is_function = False
686        next_token_is_property = False
687        collect_labels = False
688        block_labels = set()
689        brace_balance = [0, 0]
690
691        while not scanner.eos:
692            token = Error
693
694            if stack[-1] == 'initial':
695                if scanner.scan(r'\s+'):
696                    token = Text
697                elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'):
698                    if scanner.match.startswith('$'):
699                        token = Comment.Preproc
700                    else:
701                        token = Comment.Multiline
702                elif scanner.scan(r'//.*?$'):
703                    token = Comment.Single
704                elif scanner.scan(r'[-+*\/=<>:;,.@\^]'):
705                    token = Operator
706                    # stop label highlighting on next ";"
707                    if collect_labels and scanner.match == ';':
708                        collect_labels = False
709                elif scanner.scan(r'[\(\)\[\]]+'):
710                    token = Punctuation
711                    # abort function naming ``foo = Function(...)``
712                    next_token_is_function = False
713                    # if we are in a function block we count the open
714                    # braces because ootherwise it's impossible to
715                    # determine the end of the modifier context
716                    if in_function_block or in_property_block:
717                        if scanner.match == '(':
718                            brace_balance[0] += 1
719                        elif scanner.match == ')':
720                            brace_balance[0] -= 1
721                        elif scanner.match == '[':
722                            brace_balance[1] += 1
723                        elif scanner.match == ']':
724                            brace_balance[1] -= 1
725                elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'):
726                    lowercase_name = scanner.match.lower()
727                    if lowercase_name == 'result':
728                        token = Name.Builtin.Pseudo
729                    elif lowercase_name in self.keywords:
730                        token = Keyword
731                        # if we are in a special block and a
732                        # block ending keyword occours (and the parenthesis
733                        # is balanced) we end the current block context
734                        if (in_function_block or in_property_block) and \
735                           lowercase_name in self.BLOCK_KEYWORDS and \
736                           brace_balance[0] <= 0 and \
737                           brace_balance[1] <= 0:
738                            in_function_block = False
739                            in_property_block = False
740                            brace_balance = [0, 0]
741                            block_labels = set()
742                        if lowercase_name in ('label', 'goto'):
743                            collect_labels = True
744                        elif lowercase_name == 'asm':
745                            stack.append('asm')
746                        elif lowercase_name == 'property':
747                            in_property_block = True
748                            next_token_is_property = True
749                        elif lowercase_name in ('procedure', 'operator',
750                                                'function', 'constructor',
751                                                'destructor'):
752                            in_function_block = True
753                            next_token_is_function = True
754                    # we are in a function block and the current name
755                    # is in the set of registered modifiers. highlight
756                    # it as pseudo keyword
757                    elif in_function_block and \
758                         lowercase_name in self.FUNCTION_MODIFIERS:
759                        token = Keyword.Pseudo
760                    # if we are in a property highlight some more
761                    # modifiers
762                    elif in_property_block and \
763                         lowercase_name in ('read', 'write'):
764                        token = Keyword.Pseudo
765                        next_token_is_function = True
766                    # if the last iteration set next_token_is_function
767                    # to true we now want this name highlighted as
768                    # function. so do that and reset the state
769                    elif next_token_is_function:
770                        # Look if the next token is a dot. If yes it's
771                        # not a function, but a class name and the
772                        # part after the dot a function name
773                        if scanner.test(r'\s*\.\s*'):
774                            token = Name.Class
775                        # it's not a dot, our job is done
776                        else:
777                            token = Name.Function
778                            next_token_is_function = False
779                    # same for properties
780                    elif next_token_is_property:
781                        token = Name.Property
782                        next_token_is_property = False
783                    # Highlight this token as label and add it
784                    # to the list of known labels
785                    elif collect_labels:
786                        token = Name.Label
787                        block_labels.add(scanner.match.lower())
788                    # name is in list of known labels
789                    elif lowercase_name in block_labels:
790                        token = Name.Label
791                    elif lowercase_name in self.BUILTIN_TYPES:
792                        token = Keyword.Type
793                    elif lowercase_name in self.DIRECTIVES:
794                        token = Keyword.Pseudo
795                    # builtins are just builtins if the token
796                    # before isn't a dot
797                    elif not was_dot and lowercase_name in self.builtins:
798                        token = Name.Builtin
799                    else:
800                        token = Name
801                elif scanner.scan(r"'"):
802                    token = String
803                    stack.append('string')
804                elif scanner.scan(r'\#(\d+|\$[0-9A-Fa-f]+)'):
805                    token = String.Char
806                elif scanner.scan(r'\$[0-9A-Fa-f]+'):
807                    token = Number.Hex
808                elif scanner.scan(r'\d+(?![eE]|\.[^.])'):
809                    token = Number.Integer
810                elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'):
811                    token = Number.Float
812                else:
813                    # if the stack depth is deeper than once, pop
814                    if len(stack) > 1:
815                        stack.pop()
816                    scanner.get_char()
817
818            elif stack[-1] == 'string':
819                if scanner.scan(r"''"):
820                    token = String.Escape
821                elif scanner.scan(r"'"):
822                    token = String
823                    stack.pop()
824                elif scanner.scan(r"[^']*"):
825                    token = String
826                else:
827                    scanner.get_char()
828                    stack.pop()
829
830            elif stack[-1] == 'asm':
831                if scanner.scan(r'\s+'):
832                    token = Text
833                elif scanner.scan(r'end'):
834                    token = Keyword
835                    stack.pop()
836                elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'):
837                    if scanner.match.startswith('$'):
838                        token = Comment.Preproc
839                    else:
840                        token = Comment.Multiline
841                elif scanner.scan(r'//.*?$'):
842                    token = Comment.Single
843                elif scanner.scan(r"'"):
844                    token = String
845                    stack.append('string')
846                elif scanner.scan(r'@@[A-Za-z_][A-Za-z_0-9]*'):
847                    token = Name.Label
848                elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'):
849                    lowercase_name = scanner.match.lower()
850                    if lowercase_name in self.ASM_INSTRUCTIONS:
851                        token = Keyword
852                    elif lowercase_name in self.ASM_REGISTERS:
853                        token = Name.Builtin
854                    else:
855                        token = Name
856                elif scanner.scan(r'[-+*\/=<>:;,.@\^]+'):
857                    token = Operator
858                elif scanner.scan(r'[\(\)\[\]]+'):
859                    token = Punctuation
860                elif scanner.scan(r'\$[0-9A-Fa-f]+'):
861                    token = Number.Hex
862                elif scanner.scan(r'\d+(?![eE]|\.[^.])'):
863                    token = Number.Integer
864                elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'):
865                    token = Number.Float
866                else:
867                    scanner.get_char()
868                    stack.pop()
869
870            # save the dot!!!11
871            if scanner.match.strip():
872                was_dot = scanner.match == '.'
873            yield scanner.start_pos, token, scanner.match or ''
874
875
876class JavaLexer(RegexLexer):
877    """
878    For `Java <http://www.sun.com/java/>`_ source code.
879    """
880
881    name = 'Java'
882    aliases = ['java']
883    filenames = ['*.java']
884    mimetypes = ['text/x-java']
885
886    flags = re.MULTILINE | re.DOTALL
887
888    #: optional Comment or Whitespace
889    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
890
891    tokens = {
892        'root': [
893            # method names
894            (r'^(\s*(?:[a-zA-Z_][a-zA-Z0-9_\.\[\]]*\s+)+?)' # return arguments
895             r'([a-zA-Z_][a-zA-Z0-9_]*)'                    # method name
896             r'(\s*)(\()',                                  # signature start
897             bygroups(using(this), Name.Function, Text, Operator)),
898            (r'[^\S\n]+', Text),
899            (r'//.*?\n', Comment.Single),
900            (r'/\*.*?\*/', Comment.Multiline),
901            (r'@[a-zA-Z_][a-zA-Z0-9_\.]*', Name.Decorator),
902            (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
903             r'if|goto|instanceof|new|return|switch|this|throw|try|while)\b',
904             Keyword),
905            (r'(abstract|const|enum|extends|final|implements|native|private|'
906             r'protected|public|static|strictfp|super|synchronized|throws|'
907             r'transient|volatile)\b', Keyword.Declaration),
908            (r'(boolean|byte|char|double|float|int|long|short|void)\b',
909             Keyword.Type),
910            (r'(package)(\s+)', bygroups(Keyword.Namespace, Text)),
911            (r'(true|false|null)\b', Keyword.Constant),
912            (r'(class|interface)(\s+)', bygroups(Keyword.Declaration, Text), 'class'),
913            (r'(import)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
914            (r'"(\\\\|\\"|[^"])*"', String),
915            (r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char),
916            (r'(\.)([a-zA-Z_][a-zA-Z0-9_]*)', bygroups(Operator, Name.Attribute)),
917            (r'[a-zA-Z_][a-zA-Z0-9_]*:', Name.Label),
918            (r'[a-zA-Z_\$][a-zA-Z0-9_]*', Name),
919            (r'[~\^\*!%&\[\]\(\)\{\}<>\|+=:;,./?-]', Operator),
920            (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
921            (r'0x[0-9a-f]+', Number.Hex),
922            (r'[0-9]+L?', Number.Integer),
923            (r'\n', Text)
924        ],
925        'class': [
926            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
927        ],
928        'import': [
929            (r'[a-zA-Z0-9_.]+\*?', Name.Namespace, '#pop')
930        ],
931    }
932
933
934class ScalaLexer(RegexLexer):
935    """
936    For `Scala <http://www.scala-lang.org>`_ source code.
937    """
938
939    name = 'Scala'
940    aliases = ['scala']
941    filenames = ['*.scala']
942    mimetypes = ['text/x-scala']
943
944    flags = re.MULTILINE | re.DOTALL
945
946    #: optional Comment or Whitespace
947    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
948
949    # don't use raw unicode strings!
950    op = u'[-~\\^\\*!%&\\\\<>\\|+=:/?@\u00a6-\u00a7\u00a9\u00ac\u00ae\u00b0-\u00b1\u00b6\u00d7\u00f7\u03f6\u0482\u0606-\u0608\u060e-\u060f\u06e9\u06fd-\u06fe\u07f6\u09fa\u0b70\u0bf3-\u0bf8\u0bfa\u0c7f\u0cf1-\u0cf2\u0d79\u0f01-\u0f03\u0f13-\u0f17\u0f1a-\u0f1f\u0f34\u0f36\u0f38\u0fbe-\u0fc5\u0fc7-\u0fcf\u109e-\u109f\u1360\u1390-\u1399\u1940\u19e0-\u19ff\u1b61-\u1b6a\u1b74-\u1b7c\u2044\u2052\u207a-\u207c\u208a-\u208c\u2100-\u2101\u2103-\u2106\u2108-\u2109\u2114\u2116-\u2118\u211e-\u2123\u2125\u2127\u2129\u212e\u213a-\u213b\u2140-\u2144\u214a-\u214d\u214f\u2190-\u2328\u232b-\u244a\u249c-\u24e9\u2500-\u2767\u2794-\u27c4\u27c7-\u27e5\u27f0-\u2982\u2999-\u29d7\u29dc-\u29fb\u29fe-\u2b54\u2ce5-\u2cea\u2e80-\u2ffb\u3004\u3012-\u3013\u3020\u3036-\u3037\u303e-\u303f\u3190-\u3191\u3196-\u319f\u31c0-\u31e3\u3200-\u321e\u322a-\u3250\u3260-\u327f\u328a-\u32b0\u32c0-\u33ff\u4dc0-\u4dff\ua490-\ua4c6\ua828-\ua82b\ufb29\ufdfd\ufe62\ufe64-\ufe66\uff0b\uff1c-\uff1e\uff5c\uff5e\uffe2\uffe4\uffe8-\uffee\ufffc-\ufffd]+'
951
952    letter = u'[a-zA-Z\\$_\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02af\u0370-\u0373\u0376-\u0377\u037b-\u037d\u0386\u0388-\u03f5\u03f7-\u0481\u048a-\u0556\u0561-\u0587\u05d0-\u05f2\u0621-\u063f\u0641-\u064a\u066e-\u066f\u0671-\u06d3\u06d5\u06ee-\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u0904-\u0939\u093d\u0950\u0958-\u0961\u0972-\u097f\u0985-\u09b9\u09bd\u09ce\u09dc-\u09e1\u09f0-\u09f1\u0a05-\u0a39\u0a59-\u0a5e\u0a72-\u0a74\u0a85-\u0ab9\u0abd\u0ad0-\u0ae1\u0b05-\u0b39\u0b3d\u0b5c-\u0b61\u0b71\u0b83-\u0bb9\u0bd0\u0c05-\u0c3d\u0c58-\u0c61\u0c85-\u0cb9\u0cbd\u0cde-\u0ce1\u0d05-\u0d3d\u0d60-\u0d61\u0d7a-\u0d7f\u0d85-\u0dc6\u0e01-\u0e30\u0e32-\u0e33\u0e40-\u0e45\u0e81-\u0eb0\u0eb2-\u0eb3\u0ebd-\u0ec4\u0edc-\u0f00\u0f40-\u0f6c\u0f88-\u0f8b\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065-\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10fa\u1100-\u135a\u1380-\u138f\u13a0-\u166c\u166f-\u1676\u1681-\u169a\u16a0-\u16ea\u16ee-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u1770\u1780-\u17b3\u17dc\u1820-\u1842\u1844-\u18a8\u18aa-\u191c\u1950-\u19a9\u19c1-\u19c7\u1a00-\u1a16\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae-\u1baf\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c77\u1d00-\u1d2b\u1d62-\u1d77\u1d79-\u1d9a\u1e00-\u1fbc\u1fbe\u1fc2-\u1fcc\u1fd0-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ffc\u2071\u207f\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c7c\u2c80-\u2ce4\u2d00-\u2d65\u2d80-\u2dde\u3006-\u3007\u3021-\u3029\u3038-\u303a\u303c\u3041-\u3096\u309f\u30a1-\u30fa\u30ff-\u318e\u31a0-\u31b7\u31f0-\u31ff\u3400-\u4db5\u4e00-\ua014\ua016-\ua48c\ua500-\ua60b\ua610-\ua61f\ua62a-\ua66e\ua680-\ua697\ua722-\ua76f\ua771-\ua787\ua78b-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua90a-\ua925\ua930-\ua946\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uac00-\ud7a3\uf900-\ufb1d\ufb1f-\ufb28\ufb2a-\ufd3d\ufd50-\ufdfb\ufe70-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uff6f\uff71-\uff9d\uffa0-\uffdc]'
953
954    upper = u'[A-Z\\$_\u00c0-\u00d6\u00d8-\u00de\u0100\u0102\u0104\u0106\u0108\u010a\u010c\u010e\u0110\u0112\u0114\u0116\u0118\u011a\u011c\u011e\u0120\u0122\u0124\u0126\u0128\u012a\u012c\u012e\u0130\u0132\u0134\u0136\u0139\u013b\u013d\u013f\u0141\u0143\u0145\u0147\u014a\u014c\u014e\u0150\u0152\u0154\u0156\u0158\u015a\u015c\u015e\u0160\u0162\u0164\u0166\u0168\u016a\u016c\u016e\u0170\u0172\u0174\u0176\u0178-\u0179\u017b\u017d\u0181-\u0182\u0184\u0186-\u0187\u0189-\u018b\u018e-\u0191\u0193-\u0194\u0196-\u0198\u019c-\u019d\u019f-\u01a0\u01a2\u01a4\u01a6-\u01a7\u01a9\u01ac\u01ae-\u01af\u01b1-\u01b3\u01b5\u01b7-\u01b8\u01bc\u01c4\u01c7\u01ca\u01cd\u01cf\u01d1\u01d3\u01d5\u01d7\u01d9\u01db\u01de\u01e0\u01e2\u01e4\u01e6\u01e8\u01ea\u01ec\u01ee\u01f1\u01f4\u01f6-\u01f8\u01fa\u01fc\u01fe\u0200\u0202\u0204\u0206\u0208\u020a\u020c\u020e\u0210\u0212\u0214\u0216\u0218\u021a\u021c\u021e\u0220\u0222\u0224\u0226\u0228\u022a\u022c\u022e\u0230\u0232\u023a-\u023b\u023d-\u023e\u0241\u0243-\u0246\u0248\u024a\u024c\u024e\u0370\u0372\u0376\u0386\u0388-\u038f\u0391-\u03ab\u03cf\u03d2-\u03d4\u03d8\u03da\u03dc\u03de\u03e0\u03e2\u03e4\u03e6\u03e8\u03ea\u03ec\u03ee\u03f4\u03f7\u03f9-\u03fa\u03fd-\u042f\u0460\u0462\u0464\u0466\u0468\u046a\u046c\u046e\u0470\u0472\u0474\u0476\u0478\u047a\u047c\u047e\u0480\u048a\u048c\u048e\u0490\u0492\u0494\u0496\u0498\u049a\u049c\u049e\u04a0\u04a2\u04a4\u04a6\u04a8\u04aa\u04ac\u04ae\u04b0\u04b2\u04b4\u04b6\u04b8\u04ba\u04bc\u04be\u04c0-\u04c1\u04c3\u04c5\u04c7\u04c9\u04cb\u04cd\u04d0\u04d2\u04d4\u04d6\u04d8\u04da\u04dc\u04de\u04e0\u04e2\u04e4\u04e6\u04e8\u04ea\u04ec\u04ee\u04f0\u04f2\u04f4\u04f6\u04f8\u04fa\u04fc\u04fe\u0500\u0502\u0504\u0506\u0508\u050a\u050c\u050e\u0510\u0512\u0514\u0516\u0518\u051a\u051c\u051e\u0520\u0522\u0531-\u0556\u10a0-\u10c5\u1e00\u1e02\u1e04\u1e06\u1e08\u1e0a\u1e0c\u1e0e\u1e10\u1e12\u1e14\u1e16\u1e18\u1e1a\u1e1c\u1e1e\u1e20\u1e22\u1e24\u1e26\u1e28\u1e2a\u1e2c\u1e2e\u1e30\u1e32\u1e34\u1e36\u1e38\u1e3a\u1e3c\u1e3e\u1e40\u1e42\u1e44\u1e46\u1e48\u1e4a\u1e4c\u1e4e\u1e50\u1e52\u1e54\u1e56\u1e58\u1e5a\u1e5c\u1e5e\u1e60\u1e62\u1e64\u1e66\u1e68\u1e6a\u1e6c\u1e6e\u1e70\u1e72\u1e74\u1e76\u1e78\u1e7a\u1e7c\u1e7e\u1e80\u1e82\u1e84\u1e86\u1e88\u1e8a\u1e8c\u1e8e\u1e90\u1e92\u1e94\u1e9e\u1ea0\u1ea2\u1ea4\u1ea6\u1ea8\u1eaa\u1eac\u1eae\u1eb0\u1eb2\u1eb4\u1eb6\u1eb8\u1eba\u1ebc\u1ebe\u1ec0\u1ec2\u1ec4\u1ec6\u1ec8\u1eca\u1ecc\u1ece\u1ed0\u1ed2\u1ed4\u1ed6\u1ed8\u1eda\u1edc\u1ede\u1ee0\u1ee2\u1ee4\u1ee6\u1ee8\u1eea\u1eec\u1eee\u1ef0\u1ef2\u1ef4\u1ef6\u1ef8\u1efa\u1efc\u1efe\u1f08-\u1f0f\u1f18-\u1f1d\u1f28-\u1f2f\u1f38-\u1f3f\u1f48-\u1f4d\u1f59-\u1f5f\u1f68-\u1f6f\u1fb8-\u1fbb\u1fc8-\u1fcb\u1fd8-\u1fdb\u1fe8-\u1fec\u1ff8-\u1ffb\u2102\u2107\u210b-\u210d\u2110-\u2112\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u2130-\u2133\u213e-\u213f\u2145\u2183\u2c00-\u2c2e\u2c60\u2c62-\u2c64\u2c67\u2c69\u2c6b\u2c6d-\u2c6f\u2c72\u2c75\u2c80\u2c82\u2c84\u2c86\u2c88\u2c8a\u2c8c\u2c8e\u2c90\u2c92\u2c94\u2c96\u2c98\u2c9a\u2c9c\u2c9e\u2ca0\u2ca2\u2ca4\u2ca6\u2ca8\u2caa\u2cac\u2cae\u2cb0\u2cb2\u2cb4\u2cb6\u2cb8\u2cba\u2cbc\u2cbe\u2cc0\u2cc2\u2cc4\u2cc6\u2cc8\u2cca\u2ccc\u2cce\u2cd0\u2cd2\u2cd4\u2cd6\u2cd8\u2cda\u2cdc\u2cde\u2ce0\u2ce2\ua640\ua642\ua644\ua646\ua648\ua64a\ua64c\ua64e\ua650\ua652\ua654\ua656\ua658\ua65a\ua65c\ua65e\ua662\ua664\ua666\ua668\ua66a\ua66c\ua680\ua682\ua684\ua686\ua688\ua68a\ua68c\ua68e\ua690\ua692\ua694\ua696\ua722\ua724\ua726\ua728\ua72a\ua72c\ua72e\ua732\ua734\ua736\ua738\ua73a\ua73c\ua73e\ua740\ua742\ua744\ua746\ua748\ua74a\ua74c\ua74e\ua750\ua752\ua754\ua756\ua758\ua75a\ua75c\ua75e\ua760\ua762\ua764\ua766\ua768\ua76a\ua76c\ua76e\ua779\ua77b\ua77d-\ua77e\ua780\ua782\ua784\ua786\ua78b\uff21-\uff3a]'
955
956    idrest = ur'%s(?:%s|[0-9])*(?:(?<=_)%s)?' % (letter, letter, op)
957
958    tokens = {
959        'root': [
960            # method names
961            (r'(class|trait|object)(\s+)', bygroups(Keyword, Text), 'class'),
962            (ur"'%s" % idrest, Text.Symbol),
963            (r'[^\S\n]+', Text),
964            (r'//.*?\n', Comment.Single),
965            (r'/\*', Comment.Multiline, 'comment'),
966            (ur'@%s' % idrest, Name.Decorator),
967            (ur'(abstract|ca(?:se|tch)|d(?:ef|o)|e(?:lse|xtends)|'
968             ur'f(?:inal(?:ly)?|or(?:Some)?)|i(?:f|mplicit)|'
969             ur'lazy|match|new|override|pr(?:ivate|otected)'
970             ur'|re(?:quires|turn)|s(?:ealed|uper)|'
971             ur't(?:h(?:is|row)|ry)|va[lr]|w(?:hile|ith)|yield)\b|'
972             u'(<[%:-]|=>|>:|[#=@_\u21D2\u2190])(\b|(?=\\s)|$)', Keyword),
973            (ur':(?!%s)' % op, Keyword, 'type'),
974            (ur'%s%s\b' % (upper, idrest), Name.Class),
975            (r'(true|false|null)\b', Keyword.Constant),
976            (r'(import|package)(\s+)', bygroups(Keyword, Text), 'import'),
977            (r'(type)(\s+)', bygroups(Keyword, Text), 'type'),
978            (r'"""(?:.|\n)*?"""', String),
979            (r'"(\\\\|\\"|[^"])*"', String),
980            (ur"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char),
981#            (ur'(\.)(%s|%s|`[^`]+`)' % (idrest, op), bygroups(Operator,
982#             Name.Attribute)),
983            (idrest, Name),
984            (r'`[^`]+`', Name),
985            (r'\[', Operator, 'typeparam'),
986            (r'[\(\)\{\};,.]', Operator),
987            (op, Operator),
988            (ur'([0-9][0-9]*\.[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?[fFdD]?',
989             Number.Float),
990            (r'0x[0-9a-f]+', Number.Hex),
991            (r'[0-9]+L?', Number.Integer),
992            (r'\n', Text)
993        ],
994        'class': [
995            (ur'(%s|%s|`[^`]+`)(\s*)(\[)' % (idrest, op),
996             bygroups(Name.Class, Text, Operator), 'typeparam'),
997            (r'[\s\n]+', Text),
998            (r'{', Operator, '#pop'),
999            (r'\(', Operator, '#pop'),
1000            (ur'%s|%s|`[^`]+`' % (idrest, op), Name.Class, '#pop'),
1001        ],
1002        'type': [
1003            (r'\s+', Text),
1004            (u'<[%:]|>:|[#_\u21D2]|forSome|type', Keyword),
1005            (r'([,\);}]|=>|=)([\s\n]*)', bygroups(Operator, Text), '#pop'),
1006            (r'[\(\{]', Operator, '#push'),
1007            (ur'((?:%s|%s|`[^`]+`)(?:\.(?:%s|%s|`[^`]+`))*)(\s*)(\[)' %
1008             (idrest, op, idrest, op),
1009             bygroups(Keyword.Type, Text, Operator), ('#pop', 'typeparam')),
1010            (ur'((?:%s|%s|`[^`]+`)(?:\.(?:%s|%s|`[^`]+`))*)(\s*)$' %
1011             (idrest, op, idrest, op),
1012             bygroups(Keyword.Type, Text), '#pop'),
1013            (ur'\.|%s|%s|`[^`]+`' % (idrest, op), Keyword.Type)
1014        ],
1015        'typeparam': [
1016            (r'[\s\n,]+', Text),
1017            (u'<[%:]|=>|>:|[#_\u21D2]|forSome|type', Keyword),
1018            (r'([\]\)\}])', Operator, '#pop'),
1019            (r'[\(\[\{]', Operator, '#push'),
1020            (ur'\.|%s|%s|`[^`]+`' % (idrest, op), Keyword.Type)
1021        ],
1022        'comment': [
1023            (r'[^/\*]+', Comment.Multiline),
1024            (r'/\*', Comment.Multiline, '#push'),
1025            (r'\*/', Comment.Multiline, '#pop'),
1026            (r'[*/]', Comment.Multiline)
1027        ],
1028        'import': [
1029            (ur'(%s|\.)+' % idrest, Name.Namespace, '#pop')
1030        ],
1031    }
1032
1033
1034class DylanLexer(RegexLexer):
1035    """
1036    For the `Dylan <http://www.opendylan.org/>`_ language.
1037
1038    *New in Pygments 0.7.*
1039    """
1040
1041    name = 'Dylan'
1042    aliases = ['dylan']
1043    filenames = ['*.dylan']
1044    mimetypes = ['text/x-dylan']
1045
1046    flags = re.DOTALL
1047
1048    tokens = {
1049        'root': [
1050            (r'\b(subclass|abstract|block|c(on(crete|stant)|lass)|domain'
1051             r'|ex(c(eption|lude)|port)|f(unction(|al))|generic|handler'
1052             r'|i(n(herited|line|stance|terface)|mport)|library|m(acro|ethod)'
1053             r'|open|primary|sealed|si(deways|ngleton)|slot'
1054             r'|v(ariable|irtual))\b', Name.Builtin),
1055            (r'<\w+>', Keyword.Type),
1056            (r'#?"(?:\\.|[^"])+?"', String.Double),
1057            (r'//.*?\n', Comment.Single),
1058            (r'/\*[\w\W]*?\*/', Comment.Multiline),
1059            (r'\'.*?\'', String.Single),
1060            (r'=>|\b(a(bove|fterwards)|b(e(gin|low)|y)|c(ase|leanup|reate)'
1061             r'|define|else(|if)|end|f(inally|or|rom)|i[fn]|l(et|ocal)|otherwise'
1062             r'|rename|s(elect|ignal)|t(hen|o)|u(n(less|til)|se)|wh(en|ile))\b',
1063             Keyword),
1064            (r'([ \t])([!\$%&\*\/:<=>\?~_^a-zA-Z0-9.+\-]*:)',
1065             bygroups(Text, Name.Variable)),
1066            (r'([ \t]*)(\S+[^:])([ \t]*)(\()([ \t]*)',
1067             bygroups(Text, Name.Function, Text, Punctuation, Text)),
1068            (r'-?[0-9.]+', Number),
1069            (r'[(),;]', Punctuation),
1070            (r'\$[a-zA-Z0-9-]+', Name.Constant),
1071            (r'[!$%&*/:<>=?~^.+\[\]{}-]+', Operator),
1072            (r'\s+', Text),
1073            (r'#[a-zA-Z0-9-]+', Keyword),
1074            (r'[a-zA-Z0-9-]+', Name.Variable),
1075        ],
1076    }
1077
1078
1079class ObjectiveCLexer(RegexLexer):
1080    """
1081    For Objective-C source code with preprocessor directives.
1082    """
1083
1084    name = 'Objective-C'
1085    aliases = ['objective-c', 'objectivec', 'obj-c', 'objc']
1086    #XXX: objc has .h files too :-/
1087    filenames = ['*.m']
1088    mimetypes = ['text/x-objective-c']
1089
1090    #: optional Comment or Whitespace
1091    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
1092
1093    tokens = {
1094        'whitespace': [
1095            (r'^(\s*)(#if\s+0)', bygroups(Text, Comment.Preproc), 'if0'),
1096            (r'^(\s*)(#)', bygroups(Text, Comment.Preproc), 'macro'),
1097            (r'\n', Text),
1098            (r'\s+', Text),
1099            (r'\\\n', Text), # line continuation
1100            (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single),
1101            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
1102        ],
1103        'statements': [
1104            (r'(L|@)?"', String, 'string'),
1105            (r"(L|@)?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
1106             String.Char),
1107            (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
1108            (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
1109            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
1110            (r'0[0-7]+[Ll]?', Number.Oct),
1111            (r'\d+[Ll]?', Number.Integer),
1112            (r'[~!%^&*+=|?:<>/-]', Operator),
1113            (r'[()\[\],.]', Punctuation),
1114            (r'(auto|break|case|const|continue|default|do|else|enum|extern|'
1115             r'for|goto|if|register|restricted|return|sizeof|static|struct|'
1116             r'switch|typedef|union|volatile|virtual|while|in|@selector|'
1117             r'@private|@protected|@public|@encode|'
1118             r'@synchronized|@try|@throw|@catch|@finally|@end|@property|'
1119             r'@synthesize|@dynamic)\b', Keyword),
1120            (r'(int|long|float|short|double|char|unsigned|signed|void|'
1121             r'id|BOOL|IBOutlet|IBAction|SEL)\b', Keyword.Type),
1122            (r'(_{0,2}inline|naked|restrict|thread|typename)\b',
1123             Keyword.Reserved),
1124            (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
1125             r'declspec|finally|int64|try|leave)\b', Keyword.Reserved),
1126            (r'(TRUE|FALSE|nil|NULL)\b', Name.Builtin),
1127            ('[a-zA-Z$_][a-zA-Z0-9$_]*:(?!:)', Name.Label),
1128            ('[a-zA-Z$_][a-zA-Z0-9$_]*', Name),
1129        ],
1130        'root': [
1131            include('whitespace'),
1132            # functions
1133            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
1134             r'([a-zA-Z$_][a-zA-Z0-9$_]*)'           # method name
1135             r'(\s*\([^;]*?\))'                      # signature
1136             r'(' + _ws + r')({)',
1137             bygroups(using(this), Name.Function,
1138                      using(this), Text, Punctuation),
1139             'function'),
1140            # function declarations
1141            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
1142             r'([a-zA-Z$_][a-zA-Z0-9$_]*)'           # method name
1143             r'(\s*\([^;]*?\))'                      # signature
1144             r'(' + _ws + r')(;)',
1145             bygroups(using(this), Name.Function,
1146                      using(this), Text, Punctuation)),
1147            (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text),
1148             'classname'),
1149            (r'(@class|@protocol)(\s+)', bygroups(Keyword, Text),
1150             'forward_classname'),
1151            (r'(\s*)(@end)(\s*)', bygroups(Text, Keyword, Text)),
1152            ('', Text, 'statement'),
1153        ],
1154        'classname' : [
1155            # interface definition that inherits
1156            ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*:\s*)([a-zA-Z$_][a-zA-Z0-9$_]*)?',
1157             bygroups(Name.Class, Text, Name.Class), '#pop'),
1158            # interface definition for a category
1159            ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*)(\([a-zA-Z$_][a-zA-Z0-9$_]*\))',
1160             bygroups(Name.Class, Text, Name.Label), '#pop'),
1161            # simple interface / implementation
1162            ('([a-zA-Z$_][a-zA-Z0-9$_]*)', Name.Class, '#pop')
1163        ],
1164        'forward_classname' : [
1165          ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*,\s*)',
1166           bygroups(Name.Class, Text), 'forward_classname'),
1167          ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*;?)',
1168           bygroups(Name.Class, Text), '#pop')
1169        ],
1170        'statement' : [
1171            include('whitespace'),
1172            include('statements'),
1173            ('[{}]', Punctuation),
1174            (';', Punctuation, '#pop'),
1175        ],
1176        'function': [
1177            include('whitespace'),
1178            include('statements'),
1179            (';', Punctuation),
1180            ('{', Punctuation, '#push'),
1181            ('}', Punctuation, '#pop'),
1182        ],
1183        'string': [
1184            (r'"', String, '#pop'),
1185            (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
1186            (r'[^\\"\n]+', String), # all other characters
1187            (r'\\\n', String), # line continuation
1188            (r'\\', String), # stray backslash
1189        ],
1190        'macro': [
1191            (r'[^/\n]+', Comment.Preproc),
1192            (r'/[*](.|\n)*?[*]/', Comment.Multiline),
1193            (r'//.*?\n', Comment.Single, '#pop'),
1194            (r'/', Comment.Preproc),
1195            (r'(?<=\\)\n', Comment.Preproc),
1196            (r'\n', Comment.Preproc, '#pop'),
1197        ],
1198        'if0': [
1199            (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
1200            (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
1201            (r'.*?\n', Comment),
1202        ]
1203    }
1204
1205    def analyse_text(text):
1206        if '@"' in text: # strings
1207            return True
1208        if re.match(r'\[[a-zA-Z0-9.]:', text): # message
1209            return True
1210        return False
1211
1212class FortranLexer(RegexLexer):
1213    '''
1214    Lexer for FORTRAN 90 code.
1215
1216    *New in Pygments 0.10.*
1217    '''
1218    name = 'Fortran'
1219    aliases = ['fortran']
1220    filenames = ['*.f', '*.f90']
1221    mimetypes = ['text/x-fortran']
1222    flags = re.IGNORECASE
1223
1224    # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and DOUBLE PRECISION
1225    # Operators: **, *, +, -, /, <, >, <=, >=, ==, /=
1226    # Logical (?): NOT, AND, OR, EQV, NEQV
1227
1228    # Builtins:
1229    # http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Table-of-Intrinsic-Functions.html
1230
1231    tokens = {
1232        'root': [
1233            (r'!.*\n', Comment),
1234            include('strings'),
1235            include('core'),
1236            (r'[a-z][a-z0-9_]*', Name.Variable),
1237            include('nums'),
1238            (r'[\s]+', Text),
1239        ],
1240        'core': [
1241            # Statements
1242            (r'\b(ACCEPT|ALLOCATABLE|ALLOCATE|ARRAY|ASSIGN|BACKSPACE|BLOCK DATA|'
1243             r'BYTE|CALL|CASE|CLOSE|COMMON|CONTAINS|CONTINUE|CYCLE|DATA|'
1244             r'DEALLOCATE|DECODE|DIMENSION|DO|ENCODE|END FILE|ENDIF|END|ENTRY|'
1245             r'EQUIVALENCE|EXIT|EXTERNAL|EXTRINSIC|FORALL|FORMAT|FUNCTION|GOTO|'
1246             r'IF|IMPLICIT|INCLUDE|INQUIRE|INTENT|INTERFACE|INTRINSIC|MODULE|'
1247             r'NAMELIST|NULLIFY|NONE|OPEN|OPTIONAL|OPTIONS|PARAMETER|PAUSE|'
1248             r'POINTER|PRINT|PRIVATE|PROGRAM|PUBLIC|PURE|READ|RECURSIVE|RETURN|'
1249             r'REWIND|SAVE|SELECT|SEQUENCE|STOP|SUBROUTINE|TARGET|TYPE|USE|'
1250             r'VOLATILE|WHERE|WRITE|WHILE|THEN|ELSE|ENDIF)\s*\b',
1251             Keyword),
1252
1253            # Data Types
1254            (r'\b(CHARACTER|COMPLEX|DOUBLE PRECISION|DOUBLE COMPLEX|INTEGER|'
1255             r'LOGICAL|REAL)\s*\b',
1256             Keyword.Type),
1257
1258            # Operators
1259            (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator),
1260
1261            (r'(::)', Keyword.Declaration),
1262
1263            (r'[(),:&%;]', Punctuation),
1264
1265            # Intrinsics
1266            (r'\b(Abort|Abs|Access|AChar|ACos|AdjustL|AdjustR|AImag|AInt|Alarm|'
1267             r'All|Allocated|ALog|AMax|AMin|AMod|And|ANInt|Any|'
1268             r'ASin|Associated|ATan|BesJ|BesJN|BesY|BesYN|'
1269             r'Bit_Size|BTest|CAbs|CCos|Ceiling|CExp|Char|ChDir|ChMod|CLog|'
1270             r'Cmplx|Complex|Conjg|Cos|CosH|Count|CPU_Time|CShift|CSin|CSqRt|'
1271             r'CTime|DAbs|DACos|DASin|DATan|Date_and_Time|DbesJ|'
1272             r'DbesJ|DbesJN|DbesY|DbesY|DbesYN|Dble|DCos|DCosH|DDiM|DErF|DErFC|'
1273             r'DExp|Digits|DiM|DInt|DLog|DLog|DMax|DMin|DMod|DNInt|Dot_Product|'
1274             r'DProd|DSign|DSinH|DSin|DSqRt|DTanH|DTan|DTime|EOShift|Epsilon|'
1275             r'ErF|ErFC|ETime|Exit|Exp|Exponent|FDate|FGet|FGetC|Float|'
1276             r'Floor|Flush|FNum|FPutC|FPut|Fraction|FSeek|FStat|FTell|'
1277             r'GError|GetArg|GetCWD|GetEnv|GetGId|GetLog|GetPId|GetUId|'
1278             r'GMTime|HostNm|Huge|IAbs|IAChar|IAnd|IArgC|IBClr|IBits|'
1279             r'IBSet|IChar|IDate|IDiM|IDInt|IDNInt|IEOr|IErrNo|IFix|Imag|'
1280             r'ImagPart|Index|Int|IOr|IRand|IsaTty|IShft|IShftC|ISign|'
1281             r'ITime|Kill|Kind|LBound|Len|Len_Trim|LGe|LGt|Link|LLe|LLt|LnBlnk|'
1282             r'Loc|Log|Log|Logical|Long|LShift|LStat|LTime|MatMul|Max|'
1283             r'MaxExponent|MaxLoc|MaxVal|MClock|Merge|Min|MinExponent|MinLoc|'
1284             r'MinVal|Mod|Modulo|MvBits|Nearest|NInt|Not|Or|Pack|PError|'
1285             r'Precision|Present|Product|Radix|Rand|Random_Number|Random_Seed|'
1286             r'Range|Real|RealPart|Rename|Repeat|Reshape|RRSpacing|RShift|Scale|'
1287             r'Scan|Second|Selected_Int_Kind|Selected_Real_Kind|Set_Exponent|'
1288             r'Shape|Short|Sign|Signal|SinH|Sin|Sleep|Sngl|Spacing|Spread|SqRt|'
1289             r'SRand|Stat|Sum|SymLnk|System|System_Clock|Tan|TanH|Time|'
1290             r'Tiny|Transfer|Transpose|Trim|TtyNam|UBound|UMask|Unlink|Unpack|'
1291             r'Verify|XOr|ZAbs|ZCos|ZExp|ZLog|ZSin|ZSqRt)\s*\b',
1292             Name.Builtin),
1293
1294            # Booleans
1295            (r'\.(true|false)\.', Name.Builtin),
1296            # Comparing Operators
1297            (r'\.(eq|ne|lt|le|gt|ge|not|and|or|eqv|neqv)\.', Operator.Word),
1298        ],
1299
1300        'strings': [
1301            (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
1302            (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
1303        ],
1304
1305        'nums': [
1306            (r'\d+(?![.Ee])', Number.Integer),
1307            (r'[+-]?\d*\.\d+([eE][-+]?\d+)?', Number.Float),
1308            (r'[+-]?\d+\.\d*([eE][-+]?\d+)?', Number.Float),
1309        ],
1310    }
1311
1312
1313class GLShaderLexer(RegexLexer):
1314    """
1315    GLSL (OpenGL Shader) lexer.
1316
1317    *New in Pygments 1.1.*
1318    """
1319    name = 'GLSL'
1320    aliases = ['glsl']
1321    filenames = ['*.vert', '*.frag', '*.geo']
1322    mimetypes = ['text/x-glslsrc']
1323
1324    tokens = {
1325        'root': [
1326            (r'^#.*', Comment.Preproc),
1327            (r'//.*', Comment.Single),
1328            (r'/\*[\w\W]*\*/', Comment.Multiline),
1329            (r'\+|-|~|!=?|\*|/|%|<<|>>|<=?|>=?|==?|&&?|\^|\|\|?',
1330             Operator),
1331            (r'[?:]', Operator), # quick hack for ternary
1332            (r'\bdefined\b', Operator),
1333            (r'[;{}(),\[\]]', Punctuation),
1334            #FIXME when e is present, no decimal point needed
1335            (r'[+-]?\d*\.\d+([eE][-+]?\d+)?', Number.Float),
1336            (r'[+-]?\d+\.\d*([eE][-+]?\d+)?', Number.Float),
1337            (r'0[xX][0-9a-fA-F]*', Number.Hex),
1338            (r'0[0-7]*', Number.Octal),
1339            (r'[1-9][0-9]*', Number.Integer),
1340            (r'\b(attribute|const|uniform|varying|centroid|break|continue|'
1341             r'do|for|while|if|else|in|out|inout|float|int|void|bool|true|'
1342             r'false|invariant|discard|return|mat[234]|mat[234]x[234]|'
1343             r'vec[234]|[ib]vec[234]|sampler[123]D|samplerCube|'
1344             r'sampler[12]DShadow|struct)\b', Keyword),
1345            (r'\b(asm|class|union|enum|typedef|template|this|packed|goto|'
1346             r'switch|default|inline|noinline|volatile|public|static|extern|'
1347             r'external|interface|long|short|double|half|fixed|unsigned|'
1348             r'lowp|mediump|highp|precision|input|output|hvec[234]|'
1349             r'[df]vec[234]|sampler[23]DRect|sampler2DRectShadow|sizeof|'
1350             r'cast|namespace|using)\b', Keyword), #future use
1351            (r'[a-zA-Z_][a-zA-Z_0-9]*', Name.Variable),
1352            (r'\.', Punctuation),
1353            (r'\s+', Text),
1354        ],
1355    }
1356
1357class PrologLexer(RegexLexer):
1358    """
1359    Lexer for Prolog files.
1360    """
1361    name = 'Prolog'
1362    aliases = ['prolog']
1363    filenames = ['*.prolog', '*.pro', '*.pl']
1364    mimetypes = ['text/x-prolog']
1365
1366    flags = re.UNICODE
1367
1368    tokens = {
1369        'root': [
1370            (r'^#.*', Comment.Single),
1371            (r'/\*', Comment.Multiline, 'nested-comment'),
1372            (r'%.*', Comment.Single),
1373            (r'[0-9]+', Number),
1374            (r'[\[\](){}|.,;!]', Punctuation),
1375            (r':-|-->', Punctuation),
1376            (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
1377             r'\\[0-7]+\\|\\[\w\W]|[^"])*"', String.Double),
1378            (r"'(?:''|[^'])*'", String.Atom), # quoted atom
1379            # Needs to not be followed by an atom.
1380            #(r'=(?=\s|[a-zA-Z\[])', Operator),
1381            (r'(is|<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])',
1382             Operator),
1383            (r'(mod|div|not)\b', Operator),
1384            (r'_', Keyword), # The don't-care variable
1385            (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)),
1386            (u'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
1387             u'[a-zA-Z0-9_$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
1388             u'(\\s*)(:-|-->)',
1389             bygroups(Name.Function, Text, Operator)), # function defn
1390            (u'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
1391             u'[a-zA-Z0-9_$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
1392             u'(\\s*)(\\()',
1393             bygroups(Name.Function, Text, Punctuation)),
1394            (u'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
1395             u'[a-zA-Z0-9_$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*',
1396             String.Atom), # atom, characters
1397            # This one includes !
1398            (u'[#&*+\\-./:<=>?@\\\\^~\u00a1-\u00bf\u2010-\u303f]+',
1399             String.Atom), # atom, graphics
1400            (r'[A-Z_][A-Za-z0-9_]*', Name.Variable),
1401            (u'\\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text),
1402        ],
1403        'nested-comment': [
1404            (r'\*/', Comment.Multiline, '#pop'),
1405            (r'/\*', Comment.Multiline, '#push'),
1406            (r'[^*/]+', Comment.Multiline),
1407            (r'[*/]', Comment.Multiline),
1408        ],
1409    }
1410
1411    def analyse_text(text):
1412        return ':-' in text
1413
1414
1415class CythonLexer(RegexLexer):
1416    """
1417    For Pyrex and `Cython <http://cython.org>`_ source code.
1418
1419    *New in Pygments 1.1.*
1420    """
1421
1422    name = 'Cython'
1423    aliases = ['cython', 'pyx']
1424    filenames = ['*.pyx', '*.pxd', '*.pxi']
1425    mimetypes = ['text/x-cython', 'application/x-cython']
1426
1427    tokens = {
1428        'root': [
1429            (r'\n', Text),
1430            (r'^(\s*)("""(?:.|\n)*?""")', bygroups(Text, String.Doc)),
1431            (r"^(\s*)('''(?:.|\n)*?''')", bygroups(Text, String.Doc)),
1432            (r'[^\S\n]+', Text),
1433            (r'#.*$', Comment),
1434            (r'[]{}:(),;[]', Punctuation),
1435            (r'\\\n', Text),
1436            (r'\\', Text),
1437            (r'(in|is|and|or|not)\b', Operator.Word),
1438            (r'(<)([a-zA-Z0-9.?]+)(>)',
1439             bygroups(Punctuation, Keyword.Type, Punctuation)),
1440            (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator),
1441            (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)',
1442             bygroups(Keyword, Number.Integer, Operator, Name, Operator,
1443                      Name, Punctuation)),
1444            include('keywords'),
1445            (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'),
1446            (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'),
1447            (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'),
1448            (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'),
1449            (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'),
1450            include('builtins'),
1451            include('backtick'),
1452            ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'),
1453            ("(?:[rR]|[uU][rR]|[rR][uU])'''", String, 'tsqs'),
1454            ('(?:[rR]|[uU][rR]|[rR][uU])"', String, 'dqs'),
1455            ("(?:[rR]|[uU][rR]|[rR][uU])'", String, 'sqs'),
1456            ('[uU]?"""', String, combined('stringescape', 'tdqs')),
1457            ("[uU]?'''", String, combined('stringescape', 'tsqs')),
1458            ('[uU]?"', String, combined('stringescape', 'dqs')),
1459            ("[uU]?'", String, combined('stringescape', 'sqs')),
1460            include('name'),
1461            include('numbers'),
1462        ],
1463        'keywords': [
1464            (r'(assert|break|by|continue|ctypedef|del|elif|else|except\??|exec|'
1465             r'finally|for|gil|global|if|include|lambda|nogil|pass|print|raise|'
1466             r'return|try|while|yield|as|with)\b', Keyword),
1467            (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc),
1468        ],
1469        'builtins': [
1470            (r'(?<!\.)(__import__|abs|all|any|apply|basestring|bin|bool|buffer|'
1471             r'bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|'
1472             r'complex|delattr|dict|dir|divmod|enumerate|eval|execfile|exit|'
1473             r'file|filter|float|frozenset|getattr|globals|hasattr|hash|hex|id|'
1474             r'input|int|intern|isinstance|issubclass|iter|len|list|locals|'
1475             r'long|map|max|min|next|object|oct|open|ord|pow|property|range|'
1476             r'raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|'
1477             r'sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|'
1478             r'vars|xrange|zip)\b', Name.Builtin),
1479            (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'
1480             r')\b', Name.Builtin.Pseudo),
1481            (r'(?<!\.)(ArithmeticError|AssertionError|AttributeError|'
1482             r'BaseException|DeprecationWarning|EOFError|EnvironmentError|'
1483             r'Exception|FloatingPointError|FutureWarning|GeneratorExit|IOError|'
1484             r'ImportError|ImportWarning|IndentationError|IndexError|KeyError|'
1485             r'KeyboardInterrupt|LookupError|MemoryError|NameError|'
1486             r'NotImplemented|NotImplementedError|OSError|OverflowError|'
1487             r'OverflowWarning|PendingDeprecationWarning|ReferenceError|'
1488             r'RuntimeError|RuntimeWarning|StandardError|StopIteration|'
1489             r'SyntaxError|SyntaxWarning|SystemError|SystemExit|TabError|'
1490             r'TypeError|UnboundLocalError|UnicodeDecodeError|'
1491             r'UnicodeEncodeError|UnicodeError|UnicodeTranslateError|'
1492             r'UnicodeWarning|UserWarning|ValueError|Warning|ZeroDivisionError'
1493             r')\b', Name.Exception),
1494        ],
1495        'numbers': [
1496            (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
1497            (r'0\d+', Number.Oct),
1498            (r'0[xX][a-fA-F0-9]+', Number.Hex),
1499            (r'\d+L', Number.Integer.Long),
1500            (r'\d+', Number.Integer)
1501        ],
1502        'backtick': [
1503            ('`.*?`', String.Backtick),
1504        ],
1505        'name': [
1506            (r'@[a-zA-Z0-9_]+', Name.Decorator),
1507            ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
1508        ],
1509        'funcname': [
1510            ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop')
1511        ],
1512        'cdef': [
1513            (r'(public|readonly|extern|api|inline)\b', Keyword.Reserved),
1514            (r'(struct|enum|union|class)\b', Keyword),
1515            (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(?=[(:#=]|$)',
1516             bygroups(Name.Function, Text), '#pop'),
1517            (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(,)',
1518             bygroups(Name.Function, Text, Punctuation)),
1519            (r'from\b', Keyword, '#pop'),
1520            (r'as\b', Keyword),
1521            (r':', Punctuation, '#pop'),
1522            (r'(?=["\'])', Text, '#pop'),
1523            (r'[a-zA-Z_][a-zA-Z0-9_]*', Keyword.Type),
1524            (r'.', Text),
1525        ],
1526        'classname': [
1527            ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
1528        ],
1529        'import': [
1530            (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
1531            (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name.Namespace),
1532            (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),
1533            (r'', Text, '#pop') # all else: go back
1534        ],
1535        'fromimport': [
1536            (r'(\s+)(c?import)\b', bygroups(Text, Keyword), '#pop'),
1537            (r'[a-zA-Z_.][a-zA-Z0-9_.]*', Name.Namespace),
1538            # ``cdef foo from "header"``, or ``for foo from 0 < i < 10``
1539            (r'', Text, '#pop'),
1540        ],
1541        'stringescape': [
1542            (r'\\([\\abfnrtv"\']|\n|N{.*?}|u[a-fA-F0-9]{4}|'
1543             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
1544        ],
1545        'strings': [
1546            (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
1547             '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
1548            (r'[^\\\'"%\n]+', String),
1549            # quotes, percents and backslashes must be parsed one at a time
1550            (r'[\'"\\]', String),
1551            # unhandled string formatting sign
1552            (r'%', String)
1553            # newlines are an error (use "nl" state)
1554        ],
1555        'nl': [
1556            (r'\n', String)
1557        ],
1558        'dqs': [
1559            (r'"', String, '#pop'),
1560            (r'\\\\|\\"|\\\n', String.Escape), # included here again for raw strings
1561            include('strings')
1562        ],
1563        'sqs': [
1564            (r"'", String, '#pop'),
1565            (r"\\\\|\\'|\\\n", String.Escape), # included here again for raw strings
1566            include('strings')
1567        ],
1568        'tdqs': [
1569            (r'"""', String, '#pop'),
1570            include('strings'),
1571            include('nl')
1572        ],
1573        'tsqs': [
1574            (r"'''", String, '#pop'),
1575            include('strings'),
1576            include('nl')
1577        ],
1578    }
1579
1580
1581class ValaLexer(RegexLexer):
1582    """
1583    For Vala source code with preprocessor directives.
1584
1585    *New in Pygments 1.1.*
1586    """
1587    name = 'Vala'
1588    aliases = ['vala', 'vapi']
1589    filenames = ['*.vala', '*.vapi']
1590    mimetypes = ['text/x-vala']
1591
1592    tokens = {
1593        'whitespace': [
1594            (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
1595            (r'\n', Text),
1596            (r'\s+', Text),
1597            (r'\\\n', Text), # line continuation
1598            (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single),
1599            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
1600        ],
1601        'statements': [
1602            (r'L?"', String, 'string'),
1603            (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
1604             String.Char),
1605            (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
1606            (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
1607            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
1608            (r'0[0-7]+[Ll]?', Number.Oct),
1609            (r'\d+[Ll]?', Number.Integer),
1610            (r'[~!%^&*+=|?:<>/-]', Operator),
1611            (r'(\[)(Compact|Immutable|(?:Boolean|Simple)Type)(\])',
1612             bygroups(Punctuation, Name.Decorator, Punctuation)),
1613            # TODO: "correctly" parse complex code attributes
1614            (r'(\[)(CCode|(?:Integer|Floating)Type)',
1615             bygroups(Punctuation, Name.Decorator)),
1616            (r'[()\[\],.]', Punctuation),
1617            (r'(as|base|break|case|catch|construct|continue|default|delete|do|'
1618             r'else|enum|finally|for|foreach|get|if|in|is|lock|new|out|params|'
1619             r'return|set|sizeof|switch|this|throw|try|typeof|while|yield)\b',
1620             Keyword),
1621            (r'(abstract|const|delegate|dynamic|ensures|extern|inline|internal|'
1622             r'override|owned|private|protected|public|ref|requires|signal|'
1623             r'static|throws|unowned|var|virtual|volatile|weak|yields)\b',
1624             Keyword.Declaration),
1625            (r'(namespace|using)(\s+)', bygroups(Keyword.Namespace, Text),
1626             'namespace'),
1627            (r'(class|errordomain|interface|struct)(\s+)',
1628             bygroups(Keyword.Declaration, Text), 'class'),
1629            (r'(\.)([a-zA-Z_][a-zA-Z0-9_]*)',
1630             bygroups(Operator, Name.Attribute)),
1631            # void is an actual keyword, others are in glib-2.0.vapi
1632            (r'(void|bool|char|double|float|int|int8|int16|int32|int64|long|'
1633             r'short|size_t|ssize_t|string|time_t|uchar|uint|uint8|uint16|'
1634             r'uint32|uint64|ulong|unichar|ushort)\b', Keyword.Type),
1635            (r'(true|false|null)\b', Name.Builtin),
1636            ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
1637        ],
1638        'root': [
1639            include('whitespace'),
1640            ('', Text, 'statement'),
1641        ],
1642        'statement' : [
1643            include('whitespace'),
1644            include('statements'),
1645            ('[{}]', Punctuation),
1646            (';', Punctuation, '#pop'),
1647        ],
1648        'string': [
1649            (r'"', String, '#pop'),
1650            (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
1651            (r'[^\\"\n]+', String), # all other characters
1652            (r'\\\n', String), # line continuation
1653            (r'\\', String), # stray backslash
1654        ],
1655        'if0': [
1656            (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
1657            (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
1658            (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
1659            (r'.*?\n', Comment),
1660        ],
1661        'class': [
1662            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
1663        ],
1664        'namespace': [
1665            (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name.Namespace, '#pop')
1666        ],
1667    }
1668
1669
1670class OocLexer(RegexLexer):
1671    """
1672    For `Ooc <http://ooc-lang.org/>`_ source code
1673
1674    *New in Pygments 1.2.*
1675    """
1676    name = 'Ooc'
1677    aliases = ['ooc']
1678    filenames = ['*.ooc']
1679    mimetypes = ['text/x-ooc']
1680
1681    tokens = {
1682        'root': [
1683            (r'\b(class|interface|implement|abstract|extends|from|'
1684             r'this|super|new|const|final|static|import|use|extern|'
1685             r'inline|proto|break|continue|fallthrough|operator|if|else|for|'
1686             r'while|do|switch|case|as|in|version|return|true|false|null)\b',
1687             Keyword),
1688            (r'include\b', Keyword, 'include'),
1689            (r'(cover)([ \t]+)(from)([ \t]+)([a-zA-Z0-9_]+[*@]?)',
1690             bygroups(Keyword, Text, Keyword, Text, Name.Class)),
1691            (r'(func)((?:[ \t]|\\\n)+)(~[a-z_][a-zA-Z0-9_]*)',
1692             bygroups(Keyword, Text, Name.Function)),
1693            (r'\bfunc\b', Keyword),
1694            # Note: %= and ^= not listed on http://ooc-lang.org/syntax
1695            (r'//.*', Comment),
1696            (r'(?s)/\*.*?\*/', Comment.Multiline),
1697            (r'(==?|\+=?|-[=>]?|\*=?|/=?|:=|!=?|%=?|\?|>{1,3}=?|<{1,3}=?|\.\.|'
1698             r'&&?|\|\|?|\^=?)', Operator),
1699            (r'(\.)([ \t]*)([a-z]\w*)', bygroups(Operator, Text,
1700                                                 Name.Function)),
1701            (r'[A-Z][A-Z0-9_]+', Name.Constant),
1702            (r'[A-Z][a-zA-Z0-9_]*([@*]|\[[ \t]*\])?', Name.Class),
1703
1704            (r'([a-z][a-zA-Z0-9_]*(?:~[a-z][a-zA-Z0-9_]*)?)((?:[ \t]|\\\n)*)(?=\()',
1705             bygroups(Name.Function, Text)),
1706            (r'[a-z][a-zA-Z0-9_]*', Name.Variable),
1707
1708            # : introduces types
1709            (r'[:(){}\[\];,]', Punctuation),
1710
1711            (r'0x[0-9a-fA-F]+', Number.Hex),
1712            (r'0c[0-9]+', Number.Octal),
1713            (r'0b[01]+', Number.Binary),
1714            (r'[0-9_]\.[0-9_]*(?!\.)', Number.Float),
1715            (r'[0-9_]+', Number.Decimal),
1716
1717            (r'"(?:\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\"])*"',
1718             String.Double),
1719            (r"'(?:\\.|\\[0-9]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
1720             String.Char),
1721            (r'@', Punctuation), # pointer dereference
1722            (r'\.', Punctuation), # imports or chain operator
1723
1724            (r'\\[ \t\n]', Text),
1725            (r'[ \t]+', Text),
1726        ],
1727        'include': [
1728            (r'[\w/]+', Name),
1729            (r',', Punctuation),
1730            (r'[ \t]', Text),
1731            (r'[;\n]', Text, '#pop'),
1732        ],
1733    }
1734
1735
1736class GoLexer(RegexLexer):
1737    """
1738    For `Go <http://golang.org>`_ source.
1739    """
1740    name = 'Go'
1741    filenames = ['*.go']
1742    aliases = ['go']
1743    mimetypes = ['text/x-gosrc']
1744
1745    tokens = {
1746        'root': [
1747            (r'\n', Text),
1748            (r'\s+', Text),
1749            (r'\\\n', Text), # line continuations
1750            (r'//(.*?)\n', Comment.Single),
1751            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
1752            (r'(break|default|func|interface|select'
1753             r'|case|defer|go|map|struct'
1754             r'|chan|else|goto|package|switch'
1755             r'|const|fallthrough|if|range|type'
1756             r'|continue|for|import|return|var)\b', Keyword
1757            ),
1758            # It seems the builtin types aren't actually keywords.
1759            (r'(uint8|uint16|uint32|uint64'
1760             r'|int8|int16|int32|int64'
1761             r'|float32|float64|byte'
1762             r'|uint|int|float|uintptr'
1763             r'|string|close|closed|len|cap|new|make)\b', Name.Builtin
1764            ),
1765            # float_lit
1766            (r'\d+(\.\d+[eE][+\-]?\d+|'
1767             r'\.\d*|[eE][+\-]?\d+)', Number.Float),
1768            (r'\.\d+([eE][+\-]?\d+)?', Number.Float),
1769            # int_lit
1770            # -- octal_lit
1771            (r'0[0-7]+', Number.Oct),
1772            # -- hex_lit
1773            (r'0[xX][0-9a-fA-F]+', Number.Hex),
1774            # -- decimal_lit
1775            (r'(0|[1-9][0-9]*)', Number.Integer),
1776            # char_lit
1777            (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
1778             r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""",
1779             String.Char
1780            ),
1781            # StringLiteral
1782            # -- raw_string_lit
1783            (r'`[^`]*`', String),
1784            # -- interpreted_string_lit
1785            (r'"(\\\\|\\"|[^"])*"', String),
1786            # Tokens
1787            (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\|'
1788             r'|<-|\+\+|--|==|!=|:=|\.\.\.)|[+\-*/%&|^<>=!()\[\]{}.,;:]',
1789             Punctuation
1790            ),
1791            # identifier
1792            (r'[a-zA-Z_]\w*', Name),
1793        ]
1794    }
1795
1796
1797class FelixLexer(RegexLexer):
1798    """
1799    For `Felix <http://www.felix-lang.org>`_ source code.
1800
1801    *New in Pygments 1.2.*
1802    """
1803
1804    name = 'Felix'
1805    aliases = ['felix', 'flx']
1806    filenames = ['*.flx', '*.flxh']
1807    mimetypes = ['text/x-felix']
1808
1809    preproc = [
1810        'elif', 'else', 'endif', 'if', 'ifdef', 'ifndef',
1811    ]
1812
1813    keywords = [
1814        '_', '_deref', 'all', 'as',
1815        'assert', 'attempt', 'call', 'callback', 'case', 'caseno', 'cclass',
1816        'code', 'compound', 'ctypes', 'do', 'done', 'downto', 'elif', 'else',
1817        'endattempt', 'endcase', 'endif', 'endmatch', 'enum', 'except',
1818        'exceptions', 'expect', 'finally', 'for', 'forall', 'forget', 'fork',
1819        'functor', 'goto', 'ident', 'if', 'incomplete', 'inherit', 'instance',
1820        'interface', 'jump', 'lambda', 'loop', 'match', 'module', 'namespace',
1821        'new', 'noexpand', 'nonterm', 'obj', 'of', 'open', 'parse', 'raise',
1822        'regexp', 'reglex', 'regmatch', 'rename', 'return', 'the', 'then',
1823        'to', 'type', 'typecase', 'typedef', 'typematch', 'typeof', 'upto',
1824        'when', 'whilst', 'with', 'yield',
1825    ]
1826
1827    keyword_directives = [
1828        '_gc_pointer', '_gc_type', 'body', 'comment', 'const', 'export',
1829        'header', 'inline', 'lval', 'macro', 'noinline', 'noreturn',
1830        'package', 'private', 'pod', 'property', 'public', 'publish',
1831        'requires', 'todo', 'virtual', 'use',
1832    ]
1833
1834    keyword_declarations = [
1835        'def', 'let', 'ref', 'val', 'var',
1836    ]
1837
1838    keyword_types = [
1839        'unit', 'void', 'any', 'bool',
1840        'byte',  'offset',
1841        'address', 'caddress', 'cvaddress', 'vaddress',
1842        'tiny', 'short', 'int', 'long', 'vlong',
1843        'utiny', 'ushort', 'vshort', 'uint', 'ulong', 'uvlong',
1844        'int8', 'int16', 'int32', 'int64',
1845        'uint8', 'uint16', 'uint32', 'uint64',
1846        'float', 'double', 'ldouble',
1847        'complex', 'dcomplex', 'lcomplex',
1848        'imaginary', 'dimaginary', 'limaginary',
1849        'char', 'wchar', 'uchar',
1850        'charp', 'charcp', 'ucharp', 'ucharcp',
1851        'string', 'wstring', 'ustring',
1852        'cont',
1853        'array', 'varray', 'list',
1854        'lvalue', 'opt', 'slice',
1855    ]
1856
1857    keyword_constants = [
1858        'false', 'true',
1859    ]
1860
1861    operator_words = [
1862        'and', 'not', 'in', 'is', 'isin', 'or', 'xor',
1863    ]
1864
1865    name_builtins = [
1866        '_svc', 'while',
1867    ]
1868
1869    name_pseudo = [
1870        'root', 'self', 'this',
1871    ]
1872
1873    decimal_suffixes = '([tTsSiIlLvV]|ll|LL|([iIuU])(8|16|32|64))?'
1874
1875    tokens = {
1876        'root': [
1877            include('whitespace'),
1878
1879            # Keywords
1880            (r'(axiom|ctor|fun|gen|proc|reduce|union)\b', Keyword,
1881             'funcname'),
1882            (r'(class|cclass|cstruct|obj|struct)\b', Keyword, 'classname'),
1883            (r'(instance|module|typeclass)\b', Keyword, 'modulename'),
1884
1885            (r'(%s)\b' % '|'.join(keywords), Keyword),
1886            (r'(%s)\b' % '|'.join(keyword_directives), Name.Decorator),
1887            (r'(%s)\b' % '|'.join(keyword_declarations), Keyword.Declaration),
1888            (r'(%s)\b' % '|'.join(keyword_types), Keyword.Type),
1889            (r'(%s)\b' % '|'.join(keyword_constants), Keyword.Constant),
1890
1891            # Operators
1892            include('operators'),
1893
1894            # Float Literal
1895            # -- Hex Float
1896            (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
1897             r'[pP][+\-]?[0-9_]+[lLfFdD]?', Number.Float),
1898            # -- DecimalFloat
1899            (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
1900             r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[lLfFdD]?', Number.Float),
1901            (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[lLfFdD]?',
1902             Number.Float),
1903
1904            # IntegerLiteral
1905            # -- Binary
1906            (r'0[Bb][01_]+%s' % decimal_suffixes, Number),
1907            # -- Octal
1908            (r'0[0-7_]+%s' % decimal_suffixes, Number.Oct),
1909            # -- Hexadecimal
1910            (r'0[xX][0-9a-fA-F_]+%s' % decimal_suffixes, Number.Hex),
1911            # -- Decimal
1912            (r'(0|[1-9][0-9_]*)%s' % decimal_suffixes, Number.Integer),
1913
1914            # Strings
1915            ('([rR][cC]?|[cC][rR])"""', String, 'tdqs'),
1916            ("([rR][cC]?|[cC][rR])'''", String, 'tsqs'),
1917            ('([rR][cC]?|[cC][rR])"', String, 'dqs'),
1918            ("([rR][cC]?|[cC][rR])'", String, 'sqs'),
1919            ('[cCfFqQwWuU]?"""', String, combined('stringescape', 'tdqs')),
1920            ("[cCfFqQwWuU]?'''", String, combined('stringescape', 'tsqs')),
1921            ('[cCfFqQwWuU]?"', String, combined('stringescape', 'dqs')),
1922            ("[cCfFqQwWuU]?'", String, combined('stringescape', 'sqs')),
1923
1924            # Punctuation
1925            (r'[\[\]{}:(),;?]', Punctuation),
1926
1927            # Labels
1928            (r'[a-zA-Z_]\w*:>', Name.Label),
1929
1930            # Identifiers
1931            (r'(%s)\b' % '|'.join(name_builtins), Name.Builtin),
1932            (r'(%s)\b' % '|'.join(name_pseudo), Name.Builtin.Pseudo),
1933            (r'[a-zA-Z_]\w*', Name),
1934        ],
1935        'whitespace': [
1936            (r'\n', Text),
1937            (r'\s+', Text),
1938
1939            include('comment'),
1940
1941            # Preprocessor
1942            (r'#\s*if\s+0', Comment.Preproc, 'if0'),
1943            (r'#', Comment.Preproc, 'macro'),
1944        ],
1945        'operators': [
1946            (r'(%s)\b' % '|'.join(operator_words), Operator.Word),
1947            (r'!=|==|<<|>>|\|\||&&|[-~+/*%=<>&^|.$]', Operator),
1948        ],
1949        'comment': [
1950            (r'//(.*?)\n', Comment.Single),
1951            (r'/[*]', Comment.Multiline, 'comment2'),
1952        ],
1953        'comment2': [
1954            (r'[^\/*]', Comment.Multiline),
1955            (r'/[*]', Comment.Multiline, '#push'),
1956            (r'[*]/', Comment.Multiline, '#pop'),
1957            (r'[\/*]', Comment.Multiline),
1958        ],
1959        'if0': [
1960            (r'^\s*#if.*?(?<!\\)\n', Comment, '#push'),
1961            (r'^\s*#endif.*?(?<!\\)\n', Comment, '#pop'),
1962            (r'.*?\n', Comment),
1963        ],
1964        'macro': [
1965            include('comment'),
1966            (r'(import|include)(\s+)(<[^>]*?>)',
1967             bygroups(Comment.Preproc, Text, String), '#pop'),
1968            (r'(import|include)(\s+)("[^"]*?")',
1969             bygroups(Comment.Preproc, Text, String), '#pop'),
1970            (r"(import|include)(\s+)('[^']*?')",
1971             bygroups(Comment.Preproc, Text, String), '#pop'),
1972            (r'[^/\n]+', Comment.Preproc),
1973            ##(r'/[*](.|\n)*?[*]/', Comment),
1974            ##(r'//.*?\n', Comment, '#pop'),
1975            (r'/', Comment.Preproc),
1976            (r'(?<=\\)\n', Comment.Preproc),
1977            (r'\n', Comment.Preproc, '#pop'),
1978        ],
1979        'funcname': [
1980            include('whitespace'),
1981            (r'[a-zA-Z_]\w*', Name.Function, '#pop'),
1982            # anonymous functions
1983            (r'(?=\()', Text, '#pop'),
1984        ],
1985        'classname': [
1986            include('whitespace'),
1987            (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
1988            # anonymous classes
1989            (r'(?=\{)', Text, '#pop'),
1990        ],
1991        'modulename': [
1992            include('whitespace'),
1993            (r'\[', Punctuation, ('modulename2', 'tvarlist')),
1994            (r'', Error, 'modulename2'),
1995        ],
1996        'modulename2': [
1997            include('whitespace'),
1998            (r'([a-zA-Z_]\w*)', Name.Namespace, '#pop:2'),
1999        ],
2000        'tvarlist': [
2001            include('whitespace'),
2002            include('operators'),
2003            (r'\[', Punctuation, '#push'),
2004            (r'\]', Punctuation, '#pop'),
2005            (r',', Punctuation),
2006            (r'(with|where)\b', Keyword),
2007            (r'[a-zA-Z_]\w*', Name),
2008        ],
2009        'stringescape': [
2010            (r'\\([\\abfnrtv"\']|\n|N{.*?}|u[a-fA-F0-9]{4}|'
2011             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
2012        ],
2013        'strings': [
2014            (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
2015             '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
2016            (r'[^\\\'"%\n]+', String),
2017            # quotes, percents and backslashes must be parsed one at a time
2018            (r'[\'"\\]', String),
2019            # unhandled string formatting sign
2020            (r'%', String)
2021            # newlines are an error (use "nl" state)
2022        ],
2023        'nl': [
2024            (r'\n', String)
2025        ],
2026        'dqs': [
2027            (r'"', String, '#pop'),
2028            # included here again for raw strings
2029            (r'\\\\|\\"|\\\n', String.Escape),
2030            include('strings')
2031        ],
2032        'sqs': [
2033            (r"'", String, '#pop'),
2034            # included here again for raw strings
2035            (r"\\\\|\\'|\\\n", String.Escape),
2036            include('strings')
2037        ],
2038        'tdqs': [
2039            (r'"""', String, '#pop'),
2040            include('strings'),
2041            include('nl')
2042        ],
2043        'tsqs': [
2044            (r"'''", String, '#pop'),
2045            include('strings'),
2046            include('nl')
2047        ],
2048     }
2049
2050
2051class AdaLexer(RegexLexer):
2052    """
2053    For Ada source code.
2054
2055    *New in Pygments 1.3.*
2056    """
2057
2058    name = 'Ada'
2059    aliases = ['ada', 'ada95' 'ada2005']
2060    filenames = ['*.adb', '*.ads', '*.ada']
2061    mimetypes = ['text/x-ada']
2062
2063    flags = re.MULTILINE | re.# Ignore case
2064
2065    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
2066
2067    tokens = {
2068        'root': [
2069            (r'[^\S\n]+', Text),
2070            (r'--.*?\n', Comment.Single),
2071            (r'[^\S\n]+', Text),
2072            (r'function|procedure|entry', Keyword.Declaration, 'subprogram'),
2073            (r'(subtype|type)(\s+)([a-z0-9_]+)',
2074             bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
2075            (r'task|protected', Keyword.Declaration),
2076            (r'(subtype)(\s+)', bygroups(Keyword.Declaration, Text)),
2077            (r'(end)(\s+)', bygroups(Keyword.Reserved, Text), 'end'),
2078            (r'(pragma)(\s+)([a-zA-Z0-9_]+)', bygroups(Keyword.Reserved, Text,
2079                                                       Comment.Preproc)),
2080            (r'(true|false|null)\b', Keyword.Constant),
2081            (r'(Byte|Character|Float|Integer|Long_Float|Long_Integer|'
2082             r'Long_Long_Float|Long_Long_Integer|Natural|Positive|Short_Float|'
2083             r'Short_Integer|Short_Short_Float|Short_Short_Integer|String|'
2084             r'Wide_String|Duration)\b', Keyword.Type),
2085            (r'(and(\s+then)?|in|mod|not|or(\s+else)|rem)\b', Operator.Word),
2086            (r'generic|private', Keyword.Declaration),
2087            (r'package', Keyword.Declaration, 'package'),
2088            (r'array\b', Keyword.Reserved, 'array_def'),
2089            (r'(with|use)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
2090            (r'([a-z0-9_]+)(\s*)(:)(\s*)(constant)',
2091             bygroups(Name.Constant, Text, Punctuation, Text,
2092                      Keyword.Reserved)),
2093            (r'<<[a-z0-9_]+>>', Name.Label),
2094            (r'([a-z0-9_]+)(\s*)(:)(\s*)(declare|begin|loop|for|while)',
2095             bygroups(Name.Label, Text, Punctuation, Text, Keyword.Reserved)),
2096            (r'\b(abort|abs|abstract|accept|access|aliased|all|array|at|begin|'
2097             r'body|case|constant|declare|delay|delta|digits|do|else|elsif|end|'
2098             r'entry|exception|exit|interface|for|goto|if|is|limited|loop|new|'
2099             r'null|of|or|others|out|overriding|pragma|protected|raise|range|'
2100             r'record|renames|requeue|return|reverse|select|separate|subtype|'
2101             r'synchronized|task|tagged|terminate|then|type|until|when|while|'
2102             r'xor)\b',
2103             Keyword.Reserved),
2104            (r'"[^"]*"', String),
2105            include('attribute'),
2106            include('numbers'),
2107            (r"'[^']'", String.Character),
2108            (r'([a-z0-9_]+)(\s*|[(,])', bygroups(Name, using(this))),
2109            (r"(<>|=>|:=|[\(\)\|:;,.'])", Punctuation),
2110            (r'[*<>+=/&-]', Operator),
2111            (r'\n+', Text),
2112        ],
2113        'numbers' : [
2114            (r'[0-9_]+#[0-9a-f]+#', Number.Hex),
2115            (r'[0-9_]+\.[0-9_]*', Number.Float),
2116            (r'[0-9_]+', Number.Integer),
2117        ],
2118        'attribute' : [
2119            (r"(')([a-zA-Z0-9_]+)", bygroups(Punctuation, Name.Attribute)),
2120        ],
2121        'subprogram' : [
2122            (r'\(', Punctuation, ('#pop', 'formal_part')),
2123            (r';', Punctuation, '#pop'),
2124            (r'is\b', Keyword.Reserved, '#pop'),
2125            (r'"[^"]+"|[a-z0-9_]+', Name.Function),
2126            include('root'),
2127        ],
2128        'end' : [
2129            ('(if|case|record|loop|select)', Keyword.Reserved),
2130            ('"[^"]+"|[a-zA-Z0-9_]+', Name.Function),
2131            ('[\n\s]+', Text),
2132            (';', Punctuation, '#pop'),
2133        ],
2134        'type_def': [
2135            (r';', Punctuation, '#pop'),
2136            (r'\(', Punctuation, 'formal_part'),
2137            (r'with|and|use', Keyword.Reserved),
2138            (r'array\b', Keyword.Reserved, ('#pop', 'array_def')),
2139            (r'record\b', Keyword.Reserved, ('formal_part')),
2140            include('root'),
2141        ],
2142        'array_def' : [
2143            (r';', Punctuation, '#pop'),
2144            (r'([a-z0-9_]+)(\s+)(range)', bygroups(Keyword.Type, Text,
2145                                                   Keyword.Reserved)),
2146            include('root'),
2147        ],
2148        'import': [
2149            (r'[a-z0-9_.]+', Name.Namespace, '#pop'),
2150        ],
2151        'formal_part' : [
2152            (r'\)', Punctuation, '#pop'),
2153            (r'([a-z0-9_]+)(\s*)(,|:[^=])', bygroups(Name.Variable,
2154                                                     Text, Punctuation)),
2155            (r'(in|not|null|out|access)\b', Keyword.Reserved),
2156            include('root'),
2157        ],
2158        'package': [
2159            ('body', Keyword.Declaration),
2160            ('is\s+new|renames', Keyword.Reserved),
2161            ('is', Keyword.Reserved, '#pop'),
2162            (';', Punctuation, '#pop'),
2163            ('\(', Punctuation, 'package_instantiation'),
2164            ('([a-zA-Z0-9_.]+)', Name.Class),
2165            include('root'),
2166        ],
2167        'package_instantiation': [
2168            (r'("[^"]+"|[a-z0-9_]+)(\s+)(=>)', bygroups(Name.Variable,
2169                                                        Text, Punctuation)),
2170            (r'[a-z0-9._\'"]', Text),
2171            (r'\)', Punctuation, '#pop'),
2172            include('root'),
2173        ],
2174    }
2175
2176
2177class Modula2Lexer(RegexLexer):
2178    """
2179    For `Modula-2 <http://www.modula2.org/>`_ source code.
2180
2181    Additional options that determine which keywords are highlighted:
2182
2183    `pim`
2184        Select PIM Modula-2 dialect (default: True).
2185    `iso`
2186        Select ISO Modula-2 dialect (default: False).
2187    `objm2`
2188        Select Objective Modula-2 dialect (default: False).
2189    `gm2ext`
2190        Also highlight GNU extensions (default: False).
2191
2192    *New in Pygments 1.3.*
2193    """
2194    name = 'Modula-2'
2195    aliases = ['modula2', 'm2']
2196    filenames = ['*.def', '*.mod']
2197    mimetypes = ['text/x-modula2']
2198
2199    flags = re.MULTILINE | re.DOTALL
2200
2201    tokens = {
2202        'whitespace': [
2203            (r'\n+', Text), # blank lines
2204            (r'\s+', Text), # whitespace
2205        ],
2206        'identifiers': [
2207            (r'([a-zA-Z_\$][a-zA-Z0-9_\$]*)', Name),
2208        ],
2209        'numliterals': [
2210            (r'[01]+B', Number.Binary),        # binary number (ObjM2)
2211            (r'[0-7]+B', Number.Oct),          # octal number (PIM + ISO)
2212            (r'[0-7]+C', Number.Oct),          # char code (PIM + ISO)
2213            (r'[0-9A-F]+C', Number.Hex),       # char code (ObjM2)
2214            (r'[0-9A-F]+H', Number.Hex),       # hexadecimal number
2215            (r'[0-9]+\.[0-9]+E[+-][0-9]+', Number.Float), # real number
2216            (r'[0-9]+\.[0-9]+', Number.Float), # real number
2217            (r'[0-9]+', Number.Integer),       # decimal whole number
2218        ],
2219        'strings': [
2220            (r"'(\\\\|\\'|[^'])*'", String), # single quoted string
2221            (r'"(\\\\|\\"|[^"])*"', String), # double quoted string
2222        ],
2223        'operators': [
2224            (r'[*/+=#~&<>\^-]', Operator),
2225            (r':=', Operator),   # assignment
2226            (r'@', Operator),    # pointer deref (ISO)
2227            (r'\.\.', Operator), # ellipsis or range
2228            (r'`', Operator),    # Smalltalk message (ObjM2)
2229            (r'::', Operator),   # type conversion (ObjM2)
2230        ],
2231        'punctuation': [
2232            (r'[\(\)\[\]{},.:;|]', Punctuation),
2233        ],
2234        'comments': [
2235            (r'//.*?\n', Comment.Single),       # ObjM2
2236            (r'/\*(.*?)\*/', Comment.Multiline), # ObjM2
2237            (r'\(\*([^\$].*?)\*\)', Comment.Multiline),
2238            # TO DO: nesting of (* ... *) comments
2239        ],
2240        'pragmas': [
2241            (r'\(\*\$(.*?)\*\)', Comment.Preproc), # PIM
2242            (r'<\*(.*?)\*>', Comment.Preproc),     # ISO + ObjM2
2243        ],
2244        'root': [
2245            include('whitespace'),
2246            include('comments'),
2247            include('pragmas'),
2248            include('identifiers'),
2249            include('numliterals'),
2250            include('strings'),
2251            include('operators'),
2252            include('punctuation'),
2253        ]
2254    }
2255</