|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.compiled |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for compiled languages. |
|
7 |
|
8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 try: |
|
14 set |
|
15 except NameError: |
|
16 from sets import Set as set |
|
17 |
|
18 from pygments.scanner import Scanner |
|
19 from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \ |
|
20 this, combined |
|
21 from pygments.util import get_bool_opt, get_list_opt |
|
22 from pygments.token import \ |
|
23 Text, Comment, Operator, Keyword, Name, String, Number, Punctuation, \ |
|
24 Error |
|
25 |
|
26 # backwards compatibility |
|
27 from pygments.lexers.functional import OcamlLexer |
|
28 |
|
29 __all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer', |
|
30 'ScalaLexer', 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer', |
|
31 'FortranLexer', 'GLShaderLexer', 'PrologLexer', 'CythonLexer', |
|
32 'ValaLexer'] |
|
33 |
|
34 |
|
35 class CLexer(RegexLexer): |
|
36 """ |
|
37 For C source code with preprocessor directives. |
|
38 """ |
|
39 name = 'C' |
|
40 aliases = ['c'] |
|
41 filenames = ['*.c', '*.h'] |
|
42 mimetypes = ['text/x-chdr', 'text/x-csrc'] |
|
43 |
|
44 #: optional Comment or Whitespace |
|
45 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' |
|
46 |
|
47 tokens = { |
|
48 'whitespace': [ |
|
49 (r'^\s*#if\s+0', Comment.Preproc, 'if0'), |
|
50 (r'^\s*#', Comment.Preproc, 'macro'), |
|
51 (r'\n', Text), |
|
52 (r'\s+', Text), |
|
53 (r'\\\n', Text), # line continuation |
|
54 (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single), |
|
55 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
56 ], |
|
57 'statements': [ |
|
58 (r'L?"', String, 'string'), |
|
59 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char), |
|
60 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float), |
|
61 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
|
62 (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex), |
|
63 (r'0[0-7]+[Ll]?', Number.Oct), |
|
64 (r'\d+[Ll]?', Number.Integer), |
|
65 (r'[~!%^&*+=|?:<>/-]', Operator), |
|
66 (r'[()\[\],.]', Punctuation), |
|
67 (r'\b(case)(.+?)(:)', bygroups(Keyword, using(this), Text)), |
|
68 (r'(auto|break|case|const|continue|default|do|else|enum|extern|' |
|
69 r'for|goto|if|register|restricted|return|sizeof|static|struct|' |
|
70 r'switch|typedef|union|volatile|virtual|while)\b', Keyword), |
|
71 (r'(int|long|float|short|double|char|unsigned|signed|void)\b', |
|
72 Keyword.Type), |
|
73 (r'(_{0,2}inline|naked|restrict|thread|typename)\b', Keyword.Reserved), |
|
74 (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|' |
|
75 r'declspec|finally|int64|try|leave)\b', Keyword.Reserved), |
|
76 (r'(true|false|NULL)\b', Name.Builtin), |
|
77 ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label), |
|
78 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), |
|
79 ], |
|
80 'root': [ |
|
81 include('whitespace'), |
|
82 # functions |
|
83 (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments |
|
84 r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name |
|
85 r'(\s*\([^;]*?\))' # signature |
|
86 r'(' + _ws + r')({)', |
|
87 bygroups(using(this), Name.Function, using(this), using(this), |
|
88 Punctuation), |
|
89 'function'), |
|
90 # function declarations |
|
91 (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments |
|
92 r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name |
|
93 r'(\s*\([^;]*?\))' # signature |
|
94 r'(' + _ws + r')(;)', |
|
95 bygroups(using(this), Name.Function, using(this), using(this), |
|
96 Punctuation)), |
|
97 ('', Text, 'statement'), |
|
98 ], |
|
99 'statement' : [ |
|
100 include('whitespace'), |
|
101 include('statements'), |
|
102 ('[{}]', Punctuation), |
|
103 (';', Punctuation, '#pop'), |
|
104 ], |
|
105 'function': [ |
|
106 include('whitespace'), |
|
107 include('statements'), |
|
108 (';', Punctuation), |
|
109 ('{', Punctuation, '#push'), |
|
110 ('}', Punctuation, '#pop'), |
|
111 ], |
|
112 'string': [ |
|
113 (r'"', String, '#pop'), |
|
114 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape), |
|
115 (r'[^\\"\n]+', String), # all other characters |
|
116 (r'\\\n', String), # line continuation |
|
117 (r'\\', String), # stray backslash |
|
118 ], |
|
119 'macro': [ |
|
120 (r'[^/\n]+', Comment.Preproc), |
|
121 (r'/[*](.|\n)*?[*]/', Comment.Multiline), |
|
122 (r'//.*?\n', Comment.Single, '#pop'), |
|
123 (r'/', Comment.Preproc), |
|
124 (r'(?<=\\)\n', Comment.Preproc), |
|
125 (r'\n', Comment.Preproc, '#pop'), |
|
126 ], |
|
127 'if0': [ |
|
128 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
|
129 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), |
|
130 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
|
131 (r'.*?\n', Comment), |
|
132 ] |
|
133 } |
|
134 |
|
135 stdlib_types = ['size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', |
|
136 'sig_atomic_t', 'fpos_t', 'clock_t', 'time_t', 'va_list', |
|
137 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 'mbstate_t', |
|
138 'wctrans_t', 'wint_t', 'wctype_t'] |
|
139 c99_types = ['_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t', |
|
140 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', |
|
141 'int_least16_t', 'int_least32_t', 'int_least64_t', |
|
142 'uint_least8_t', 'uint_least16_t', 'uint_least32_t', |
|
143 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', |
|
144 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', |
|
145 'uint_fast64_t', 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'] |
|
146 |
|
147 def __init__(self, **options): |
|
148 self.stdlibhighlighting = get_bool_opt(options, |
|
149 'stdlibhighlighting', True) |
|
150 self.c99highlighting = get_bool_opt(options, |
|
151 'c99highlighting', True) |
|
152 RegexLexer.__init__(self, **options) |
|
153 |
|
154 def get_tokens_unprocessed(self, text): |
|
155 for index, token, value in \ |
|
156 RegexLexer.get_tokens_unprocessed(self, text): |
|
157 if token is Name: |
|
158 if self.stdlibhighlighting and value in self.stdlib_types: |
|
159 token = Keyword.Type |
|
160 elif self.c99highlighting and value in self.c99_types: |
|
161 token = Keyword.Type |
|
162 yield index, token, value |
|
163 |
|
164 class CppLexer(RegexLexer): |
|
165 """ |
|
166 For C++ source code with preprocessor directives. |
|
167 """ |
|
168 name = 'C++' |
|
169 aliases = ['cpp', 'c++'] |
|
170 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', '*.cc', '*.hh', '*.cxx', '*.hxx'] |
|
171 mimetypes = ['text/x-c++hdr', 'text/x-c++src'] |
|
172 |
|
173 tokens = { |
|
174 'root': [ |
|
175 (r'^\s*#if\s+0', Comment.Preproc, 'if0'), |
|
176 (r'^\s*#', Comment.Preproc, 'macro'), |
|
177 (r'\n', Text), |
|
178 (r'\s+', Text), |
|
179 (r'\\\n', Text), # line continuation |
|
180 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single), |
|
181 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
182 (r'[{}]', Punctuation), |
|
183 (r'L?"', String, 'string'), |
|
184 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char), |
|
185 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float), |
|
186 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
|
187 (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex), |
|
188 (r'0[0-7]+[Ll]?', Number.Oct), |
|
189 (r'\d+[Ll]?', Number.Integer), |
|
190 (r'[~!%^&*+=|?:<>/-]', Operator), |
|
191 (r'[()\[\],.;]', Punctuation), |
|
192 (r'(asm|auto|break|case|catch|const|const_cast|continue|' |
|
193 r'default|delete|do|dynamic_cast|else|enum|explicit|export|' |
|
194 r'extern|for|friend|goto|if|mutable|namespace|new|operator|' |
|
195 r'private|protected|public|register|reinterpret_cast|return|' |
|
196 r'restrict|sizeof|static|static_cast|struct|switch|template|' |
|
197 r'this|throw|throws|try|typedef|typeid|typename|union|using|' |
|
198 r'volatile|virtual|while)\b', Keyword), |
|
199 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), |
|
200 (r'(bool|int|long|float|short|double|char|unsigned|signed|' |
|
201 r'void|wchar_t)\b', Keyword.Type), |
|
202 (r'(_{0,2}inline|naked|thread)\b', Keyword.Reserved), |
|
203 (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|' |
|
204 r'declspec|finally|int64|try|leave|wchar_t|w64|virtual_inheritance|' |
|
205 r'uuidof|unaligned|super|single_inheritance|raise|noop|' |
|
206 r'multiple_inheritance|m128i|m128d|m128|m64|interface|' |
|
207 r'identifier|forceinline|event|assume)\b', Keyword.Reserved), |
|
208 (r'(true|false)\b', Keyword.Constant), |
|
209 (r'NULL\b', Name.Builtin), |
|
210 ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label), |
|
211 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), |
|
212 ], |
|
213 'classname': [ |
|
214 (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop'), |
|
215 # template specification |
|
216 (r'\s*(?=>)', Text, '#pop'), |
|
217 ], |
|
218 'string': [ |
|
219 (r'"', String, '#pop'), |
|
220 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape), |
|
221 (r'[^\\"\n]+', String), # all other characters |
|
222 (r'\\\n', String), # line continuation |
|
223 (r'\\', String), # stray backslash |
|
224 ], |
|
225 'macro': [ |
|
226 (r'[^/\n]+', Comment.Preproc), |
|
227 (r'/[*](.|\n)*?[*]/', Comment.Multiline), |
|
228 (r'//.*?\n', Comment.Single, '#pop'), |
|
229 (r'/', Comment.Preproc), |
|
230 (r'(?<=\\)\n', Comment.Preproc), |
|
231 (r'\n', Comment.Preproc, '#pop'), |
|
232 ], |
|
233 'if0': [ |
|
234 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
|
235 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
|
236 (r'.*?\n', Comment), |
|
237 ] |
|
238 } |
|
239 |
|
240 |
|
241 class DLexer(RegexLexer): |
|
242 """ |
|
243 For D source. |
|
244 """ |
|
245 name = 'D' |
|
246 filenames = ['*.d', '*.di'] |
|
247 aliases = ['d'] |
|
248 mimetypes = ['text/x-dsrc'] |
|
249 |
|
250 tokens = { |
|
251 'root': [ |
|
252 (r'\n', Text), |
|
253 (r'\s+', Text), |
|
254 #(r'\\\n', Text), # line continuations |
|
255 # Comments |
|
256 (r'//(.*?)\n', Comment.Single), |
|
257 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
258 (r'/\+', Comment.Multiline, 'nested_comment'), |
|
259 # Keywords |
|
260 (r'(abstract|alias|align|asm|assert|auto|body|break|case|cast' |
|
261 r'|catch|class|const|continue|debug|default|delegate|delete' |
|
262 r'|deprecated|do|else|enum|export|extern|finally|final' |
|
263 r'|foreach_reverse|foreach|for|function|goto|if|import|inout' |
|
264 r'|interface|invariant|in|is|lazy|mixin|module|new|nothrow|out' |
|
265 r'|override|package|pragma|private|protected|public|pure|ref|return' |
|
266 r'|scope|static|struct|super|switch|synchronized|template|this' |
|
267 r'|throw|try|typedef|typeid|typeof|union|unittest|version|volatile' |
|
268 r'|while|with|__traits)\b', Keyword |
|
269 ), |
|
270 (r'(bool|byte|cdouble|cent|cfloat|char|creal|dchar|double|float' |
|
271 r'|idouble|ifloat|int|ireal|long|real|short|ubyte|ucent|uint|ulong' |
|
272 r'|ushort|void|wchar)\b', Keyword.Type |
|
273 ), |
|
274 (r'(false|true|null)\b', Keyword.Constant), |
|
275 (r'macro\b', Keyword.Reserved), |
|
276 (r'(string|wstring|dstring)\b', Name.Builtin), |
|
277 # FloatLiteral |
|
278 # -- HexFloat |
|
279 (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)' |
|
280 r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float), |
|
281 # -- DecimalFloat |
|
282 (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
|
283 r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float), |
|
284 (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float), |
|
285 # IntegerLiteral |
|
286 # -- Binary |
|
287 (r'0[Bb][01_]+', Number), |
|
288 # -- Octal |
|
289 (r'0[0-7_]+', Number.Oct), |
|
290 # -- Hexadecimal |
|
291 (r'0[xX][0-9a-fA-F_]+', Number.Hex), |
|
292 # -- Decimal |
|
293 (r'(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?', Number.Integer), |
|
294 # CharacterLiteral |
|
295 (r"""'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" |
|
296 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'""", |
|
297 String.Char |
|
298 ), |
|
299 # StringLiteral |
|
300 # -- WysiwygString |
|
301 (r'r"[^"]*"[cwd]?', String), |
|
302 # -- AlternateWysiwygString |
|
303 (r'`[^`]*`[cwd]?', String), |
|
304 # -- DoubleQuotedString |
|
305 (r'"(\\\\|\\"|[^"])*"[cwd]?', String), |
|
306 # -- EscapeSequence |
|
307 (r"""\\(['"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}""" |
|
308 r"""|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)""", |
|
309 String |
|
310 ), |
|
311 # -- HexString |
|
312 (r'x"[0-9a-fA-F_\s]*"[cwd]?', String), |
|
313 # -- DelimitedString |
|
314 (r'q"\[', String, 'delimited_bracket'), |
|
315 (r'q"\(', String, 'delimited_parenthesis'), |
|
316 (r'q"<', String, 'delimited_angle'), |
|
317 (r'q"{', String, 'delimited_curly'), |
|
318 (r'q"([a-zA-Z_]\w*)\n.*?\n\1"', String), |
|
319 (r'q"(.).*?\1"', String), |
|
320 # -- TokenString |
|
321 (r'q{', String, 'token_string'), |
|
322 # Tokens |
|
323 (r'(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>=' |
|
324 r'|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)' |
|
325 r'|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]', Punctuation |
|
326 ), |
|
327 # Identifier |
|
328 (r'[a-zA-Z_]\w*', Name), |
|
329 ], |
|
330 'nested_comment': [ |
|
331 (r'[^+/]+', Comment.Multiline), |
|
332 (r'/\+', Comment.Multiline, '#push'), |
|
333 (r'\+/', Comment.Multiline, '#pop'), |
|
334 (r'[+/]', Comment.Multiline), |
|
335 ], |
|
336 'token_string': [ |
|
337 (r'{', Punctuation, 'token_string_nest'), |
|
338 (r'}', String, '#pop'), |
|
339 include('root'), |
|
340 ], |
|
341 'token_string_nest': [ |
|
342 (r'{', Punctuation, '#push'), |
|
343 (r'}', Punctuation, '#pop'), |
|
344 include('root'), |
|
345 ], |
|
346 'delimited_bracket': [ |
|
347 (r'[^\[\]]+', String), |
|
348 (r'\[', String, 'delimited_inside_bracket'), |
|
349 (r'\]"', String, '#pop'), |
|
350 ], |
|
351 'delimited_inside_bracket': [ |
|
352 (r'[^\[\]]+', String), |
|
353 (r'\[', String, '#push'), |
|
354 (r'\]', String, '#pop'), |
|
355 ], |
|
356 'delimited_parenthesis': [ |
|
357 (r'[^\(\)]+', String), |
|
358 (r'\(', String, 'delimited_inside_parenthesis'), |
|
359 (r'\)"', String, '#pop'), |
|
360 ], |
|
361 'delimited_inside_parenthesis': [ |
|
362 (r'[^\(\)]+', String), |
|
363 (r'\(', String, '#push'), |
|
364 (r'\)', String, '#pop'), |
|
365 ], |
|
366 'delimited_angle': [ |
|
367 (r'[^<>]+', String), |
|
368 (r'<', String, 'delimited_inside_angle'), |
|
369 (r'>"', String, '#pop'), |
|
370 ], |
|
371 'delimited_inside_angle': [ |
|
372 (r'[^<>]+', String), |
|
373 (r'<', String, '#push'), |
|
374 (r'>', String, '#pop'), |
|
375 ], |
|
376 'delimited_curly': [ |
|
377 (r'[^{}]+', String), |
|
378 (r'{', String, 'delimited_inside_curly'), |
|
379 (r'}"', String, '#pop'), |
|
380 ], |
|
381 'delimited_inside_curly': [ |
|
382 (r'[^{}]+', String), |
|
383 (r'{', String, '#push'), |
|
384 (r'}', String, '#pop'), |
|
385 ], |
|
386 } |
|
387 |
|
388 |
|
389 class DelphiLexer(Lexer): |
|
390 """ |
|
391 For `Delphi <http://www.borland.com/delphi/>`_ (Borland Object Pascal), |
|
392 Turbo Pascal and Free Pascal source code. |
|
393 |
|
394 Additional options accepted: |
|
395 |
|
396 `turbopascal` |
|
397 Highlight Turbo Pascal specific keywords (default: ``True``). |
|
398 `delphi` |
|
399 Highlight Borland Delphi specific keywords (default: ``True``). |
|
400 `freepascal` |
|
401 Highlight Free Pascal specific keywords (default: ``True``). |
|
402 `units` |
|
403 A list of units that should be considered builtin, supported are |
|
404 ``System``, ``SysUtils``, ``Classes`` and ``Math``. |
|
405 Default is to consider all of them builtin. |
|
406 """ |
|
407 name = 'Delphi' |
|
408 aliases = ['delphi', 'pas', 'pascal', 'objectpascal'] |
|
409 filenames = ['*.pas'] |
|
410 mimetypes = ['text/x-pascal'] |
|
411 |
|
412 TURBO_PASCAL_KEYWORDS = [ |
|
413 'absolute', 'and', 'array', 'asm', 'begin', 'break', 'case', |
|
414 'const', 'constructor', 'continue', 'destructor', 'div', 'do', |
|
415 'downto', 'else', 'end', 'file', 'for', 'function', 'goto', |
|
416 'if', 'implementation', 'in', 'inherited', 'inline', 'interface', |
|
417 'label', 'mod', 'nil', 'not', 'object', 'of', 'on', 'operator', |
|
418 'or', 'packed', 'procedure', 'program', 'record', 'reintroduce', |
|
419 'repeat', 'self', 'set', 'shl', 'shr', 'string', 'then', 'to', |
|
420 'type', 'unit', 'until', 'uses', 'var', 'while', 'with', 'xor' |
|
421 ] |
|
422 |
|
423 DELPHI_KEYWORDS = [ |
|
424 'as', 'class', 'except', 'exports', 'finalization', 'finally', |
|
425 'initialization', 'is', 'library', 'on', 'property', 'raise', |
|
426 'threadvar', 'try' |
|
427 ] |
|
428 |
|
429 FREE_PASCAL_KEYWORDS = [ |
|
430 'dispose', 'exit', 'false', 'new', 'true' |
|
431 ] |
|
432 |
|
433 BLOCK_KEYWORDS = set([ |
|
434 'begin', 'class', 'const', 'constructor', 'destructor', 'end', |
|
435 'finalization', 'function', 'implementation', 'initialization', |
|
436 'label', 'library', 'operator', 'procedure', 'program', 'property', |
|
437 'record', 'threadvar', 'type', 'unit', 'uses', 'var' |
|
438 ]) |
|
439 |
|
440 FUNCTION_MODIFIERS = set([ |
|
441 'alias', 'cdecl', 'export', 'inline', 'interrupt', 'nostackframe', |
|
442 'pascal', 'register', 'safecall', 'softfloat', 'stdcall', |
|
443 'varargs', 'name', 'dynamic', 'near', 'virtual', 'external', |
|
444 'override', 'assembler' |
|
445 ]) |
|
446 |
|
447 # XXX: those aren't global. but currently we know no way for defining |
|
448 # them just for the type context. |
|
449 DIRECTIVES = set([ |
|
450 'absolute', 'abstract', 'assembler', 'cppdecl', 'default', 'far', |
|
451 'far16', 'forward', 'index', 'oldfpccall', 'private', 'protected', |
|
452 'published', 'public' |
|
453 ]) |
|
454 |
|
455 BUILTIN_TYPES = set([ |
|
456 'ansichar', 'ansistring', 'bool', 'boolean', 'byte', 'bytebool', |
|
457 'cardinal', 'char', 'comp', 'currency', 'double', 'dword', |
|
458 'extended', 'int64', 'integer', 'iunknown', 'longbool', 'longint', |
|
459 'longword', 'pansichar', 'pansistring', 'pbool', 'pboolean', |
|
460 'pbyte', 'pbytearray', 'pcardinal', 'pchar', 'pcomp', 'pcurrency', |
|
461 'pdate', 'pdatetime', 'pdouble', 'pdword', 'pextended', 'phandle', |
|
462 'pint64', 'pinteger', 'plongint', 'plongword', 'pointer', |
|
463 'ppointer', 'pshortint', 'pshortstring', 'psingle', 'psmallint', |
|
464 'pstring', 'pvariant', 'pwidechar', 'pwidestring', 'pword', |
|
465 'pwordarray', 'pwordbool', 'real', 'real48', 'shortint', |
|
466 'shortstring', 'single', 'smallint', 'string', 'tclass', 'tdate', |
|
467 'tdatetime', 'textfile', 'thandle', 'tobject', 'ttime', 'variant', |
|
468 'widechar', 'widestring', 'word', 'wordbool' |
|
469 ]) |
|
470 |
|
471 BUILTIN_UNITS = { |
|
472 'System': [ |
|
473 'abs', 'acquireexceptionobject', 'addr', 'ansitoutf8', |
|
474 'append', 'arctan', 'assert', 'assigned', 'assignfile', |
|
475 'beginthread', 'blockread', 'blockwrite', 'break', 'chdir', |
|
476 'chr', 'close', 'closefile', 'comptocurrency', 'comptodouble', |
|
477 'concat', 'continue', 'copy', 'cos', 'dec', 'delete', |
|
478 'dispose', 'doubletocomp', 'endthread', 'enummodules', |
|
479 'enumresourcemodules', 'eof', 'eoln', 'erase', 'exceptaddr', |
|
480 'exceptobject', 'exclude', 'exit', 'exp', 'filepos', 'filesize', |
|
481 'fillchar', 'finalize', 'findclasshinstance', 'findhinstance', |
|
482 'findresourcehinstance', 'flush', 'frac', 'freemem', |
|
483 'get8087cw', 'getdir', 'getlasterror', 'getmem', |
|
484 'getmemorymanager', 'getmodulefilename', 'getvariantmanager', |
|
485 'halt', 'hi', 'high', 'inc', 'include', 'initialize', 'insert', |
|
486 'int', 'ioresult', 'ismemorymanagerset', 'isvariantmanagerset', |
|
487 'length', 'ln', 'lo', 'low', 'mkdir', 'move', 'new', 'odd', |
|
488 'olestrtostring', 'olestrtostrvar', 'ord', 'paramcount', |
|
489 'paramstr', 'pi', 'pos', 'pred', 'ptr', 'pucs4chars', 'random', |
|
490 'randomize', 'read', 'readln', 'reallocmem', |
|
491 'releaseexceptionobject', 'rename', 'reset', 'rewrite', 'rmdir', |
|
492 'round', 'runerror', 'seek', 'seekeof', 'seekeoln', |
|
493 'set8087cw', 'setlength', 'setlinebreakstyle', |
|
494 'setmemorymanager', 'setstring', 'settextbuf', |
|
495 'setvariantmanager', 'sin', 'sizeof', 'slice', 'sqr', 'sqrt', |
|
496 'str', 'stringofchar', 'stringtoolestr', 'stringtowidechar', |
|
497 'succ', 'swap', 'trunc', 'truncate', 'typeinfo', |
|
498 'ucs4stringtowidestring', 'unicodetoutf8', 'uniquestring', |
|
499 'upcase', 'utf8decode', 'utf8encode', 'utf8toansi', |
|
500 'utf8tounicode', 'val', 'vararrayredim', 'varclear', |
|
501 'widecharlentostring', 'widecharlentostrvar', |
|
502 'widechartostring', 'widechartostrvar', |
|
503 'widestringtoucs4string', 'write', 'writeln' |
|
504 ], |
|
505 'SysUtils': [ |
|
506 'abort', 'addexitproc', 'addterminateproc', 'adjustlinebreaks', |
|
507 'allocmem', 'ansicomparefilename', 'ansicomparestr', |
|
508 'ansicomparetext', 'ansidequotedstr', 'ansiextractquotedstr', |
|
509 'ansilastchar', 'ansilowercase', 'ansilowercasefilename', |
|
510 'ansipos', 'ansiquotedstr', 'ansisamestr', 'ansisametext', |
|
511 'ansistrcomp', 'ansistricomp', 'ansistrlastchar', 'ansistrlcomp', |
|
512 'ansistrlicomp', 'ansistrlower', 'ansistrpos', 'ansistrrscan', |
|
513 'ansistrscan', 'ansistrupper', 'ansiuppercase', |
|
514 'ansiuppercasefilename', 'appendstr', 'assignstr', 'beep', |
|
515 'booltostr', 'bytetocharindex', 'bytetocharlen', 'bytetype', |
|
516 'callterminateprocs', 'changefileext', 'charlength', |
|
517 'chartobyteindex', 'chartobytelen', 'comparemem', 'comparestr', |
|
518 'comparetext', 'createdir', 'createguid', 'currentyear', |
|
519 'currtostr', 'currtostrf', 'date', 'datetimetofiledate', |
|
520 'datetimetostr', 'datetimetostring', 'datetimetosystemtime', |
|
521 'datetimetotimestamp', 'datetostr', 'dayofweek', 'decodedate', |
|
522 'decodedatefully', 'decodetime', 'deletefile', 'directoryexists', |
|
523 'diskfree', 'disksize', 'disposestr', 'encodedate', 'encodetime', |
|
524 'exceptionerrormessage', 'excludetrailingbackslash', |
|
525 'excludetrailingpathdelimiter', 'expandfilename', |
|
526 'expandfilenamecase', 'expanduncfilename', 'extractfiledir', |
|
527 'extractfiledrive', 'extractfileext', 'extractfilename', |
|
528 'extractfilepath', 'extractrelativepath', 'extractshortpathname', |
|
529 'fileage', 'fileclose', 'filecreate', 'filedatetodatetime', |
|
530 'fileexists', 'filegetattr', 'filegetdate', 'fileisreadonly', |
|
531 'fileopen', 'fileread', 'filesearch', 'fileseek', 'filesetattr', |
|
532 'filesetdate', 'filesetreadonly', 'filewrite', 'finalizepackage', |
|
533 'findclose', 'findcmdlineswitch', 'findfirst', 'findnext', |
|
534 'floattocurr', 'floattodatetime', 'floattodecimal', 'floattostr', |
|
535 'floattostrf', 'floattotext', 'floattotextfmt', 'fmtloadstr', |
|
536 'fmtstr', 'forcedirectories', 'format', 'formatbuf', 'formatcurr', |
|
537 'formatdatetime', 'formatfloat', 'freeandnil', 'getcurrentdir', |
|
538 'getenvironmentvariable', 'getfileversion', 'getformatsettings', |
|
539 'getlocaleformatsettings', 'getmodulename', 'getpackagedescription', |
|
540 'getpackageinfo', 'gettime', 'guidtostring', 'incamonth', |
|
541 'includetrailingbackslash', 'includetrailingpathdelimiter', |
|
542 'incmonth', 'initializepackage', 'interlockeddecrement', |
|
543 'interlockedexchange', 'interlockedexchangeadd', |
|
544 'interlockedincrement', 'inttohex', 'inttostr', 'isdelimiter', |
|
545 'isequalguid', 'isleapyear', 'ispathdelimiter', 'isvalidident', |
|
546 'languages', 'lastdelimiter', 'loadpackage', 'loadstr', |
|
547 'lowercase', 'msecstotimestamp', 'newstr', 'nextcharindex', 'now', |
|
548 'outofmemoryerror', 'quotedstr', 'raiselastoserror', |
|
549 'raiselastwin32error', 'removedir', 'renamefile', 'replacedate', |
|
550 'replacetime', 'safeloadlibrary', 'samefilename', 'sametext', |
|
551 'setcurrentdir', 'showexception', 'sleep', 'stralloc', 'strbufsize', |
|
552 'strbytetype', 'strcat', 'strcharlength', 'strcomp', 'strcopy', |
|
553 'strdispose', 'strecopy', 'strend', 'strfmt', 'stricomp', |
|
554 'stringreplace', 'stringtoguid', 'strlcat', 'strlcomp', 'strlcopy', |
|
555 'strlen', 'strlfmt', 'strlicomp', 'strlower', 'strmove', 'strnew', |
|
556 'strnextchar', 'strpas', 'strpcopy', 'strplcopy', 'strpos', |
|
557 'strrscan', 'strscan', 'strtobool', 'strtobooldef', 'strtocurr', |
|
558 'strtocurrdef', 'strtodate', 'strtodatedef', 'strtodatetime', |
|
559 'strtodatetimedef', 'strtofloat', 'strtofloatdef', 'strtoint', |
|
560 'strtoint64', 'strtoint64def', 'strtointdef', 'strtotime', |
|
561 'strtotimedef', 'strupper', 'supports', 'syserrormessage', |
|
562 'systemtimetodatetime', 'texttofloat', 'time', 'timestamptodatetime', |
|
563 'timestamptomsecs', 'timetostr', 'trim', 'trimleft', 'trimright', |
|
564 'tryencodedate', 'tryencodetime', 'tryfloattocurr', 'tryfloattodatetime', |
|
565 'trystrtobool', 'trystrtocurr', 'trystrtodate', 'trystrtodatetime', |
|
566 'trystrtofloat', 'trystrtoint', 'trystrtoint64', 'trystrtotime', |
|
567 'unloadpackage', 'uppercase', 'widecomparestr', 'widecomparetext', |
|
568 'widefmtstr', 'wideformat', 'wideformatbuf', 'widelowercase', |
|
569 'widesamestr', 'widesametext', 'wideuppercase', 'win32check', |
|
570 'wraptext' |
|
571 ], |
|
572 'Classes': [ |
|
573 'activateclassgroup', 'allocatehwnd', 'bintohex', 'checksynchronize', |
|
574 'collectionsequal', 'countgenerations', 'deallocatehwnd', 'equalrect', |
|
575 'extractstrings', 'findclass', 'findglobalcomponent', 'getclass', |
|
576 'groupdescendantswith', 'hextobin', 'identtoint', |
|
577 'initinheritedcomponent', 'inttoident', 'invalidpoint', |
|
578 'isuniqueglobalcomponentname', 'linestart', 'objectbinarytotext', |
|
579 'objectresourcetotext', 'objecttexttobinary', 'objecttexttoresource', |
|
580 'pointsequal', 'readcomponentres', 'readcomponentresex', |
|
581 'readcomponentresfile', 'rect', 'registerclass', 'registerclassalias', |
|
582 'registerclasses', 'registercomponents', 'registerintegerconsts', |
|
583 'registernoicon', 'registernonactivex', 'smallpoint', 'startclassgroup', |
|
584 'teststreamformat', 'unregisterclass', 'unregisterclasses', |
|
585 'unregisterintegerconsts', 'unregistermoduleclasses', |
|
586 'writecomponentresfile' |
|
587 ], |
|
588 'Math': [ |
|
589 'arccos', 'arccosh', 'arccot', 'arccoth', 'arccsc', 'arccsch', 'arcsec', |
|
590 'arcsech', 'arcsin', 'arcsinh', 'arctan2', 'arctanh', 'ceil', |
|
591 'comparevalue', 'cosecant', 'cosh', 'cot', 'cotan', 'coth', 'csc', |
|
592 'csch', 'cycletodeg', 'cycletograd', 'cycletorad', 'degtocycle', |
|
593 'degtograd', 'degtorad', 'divmod', 'doubledecliningbalance', |
|
594 'ensurerange', 'floor', 'frexp', 'futurevalue', 'getexceptionmask', |
|
595 'getprecisionmode', 'getroundmode', 'gradtocycle', 'gradtodeg', |
|
596 'gradtorad', 'hypot', 'inrange', 'interestpayment', 'interestrate', |
|
597 'internalrateofreturn', 'intpower', 'isinfinite', 'isnan', 'iszero', |
|
598 'ldexp', 'lnxp1', 'log10', 'log2', 'logn', 'max', 'maxintvalue', |
|
599 'maxvalue', 'mean', 'meanandstddev', 'min', 'minintvalue', 'minvalue', |
|
600 'momentskewkurtosis', 'netpresentvalue', 'norm', 'numberofperiods', |
|
601 'payment', 'periodpayment', 'poly', 'popnstddev', 'popnvariance', |
|
602 'power', 'presentvalue', 'radtocycle', 'radtodeg', 'radtograd', |
|
603 'randg', 'randomrange', 'roundto', 'samevalue', 'sec', 'secant', |
|
604 'sech', 'setexceptionmask', 'setprecisionmode', 'setroundmode', |
|
605 'sign', 'simpleroundto', 'sincos', 'sinh', 'slndepreciation', 'stddev', |
|
606 'sum', 'sumint', 'sumofsquares', 'sumsandsquares', 'syddepreciation', |
|
607 'tan', 'tanh', 'totalvariance', 'variance' |
|
608 ] |
|
609 } |
|
610 |
|
611 ASM_REGISTERS = set([ |
|
612 'ah', 'al', 'ax', 'bh', 'bl', 'bp', 'bx', 'ch', 'cl', 'cr0', |
|
613 'cr1', 'cr2', 'cr3', 'cr4', 'cs', 'cx', 'dh', 'di', 'dl', 'dr0', |
|
614 'dr1', 'dr2', 'dr3', 'dr4', 'dr5', 'dr6', 'dr7', 'ds', 'dx', |
|
615 'eax', 'ebp', 'ebx', 'ecx', 'edi', 'edx', 'es', 'esi', 'esp', |
|
616 'fs', 'gs', 'mm0', 'mm1', 'mm2', 'mm3', 'mm4', 'mm5', 'mm6', |
|
617 'mm7', 'si', 'sp', 'ss', 'st0', 'st1', 'st2', 'st3', 'st4', 'st5', |
|
618 'st6', 'st7', 'xmm0', 'xmm1', 'xmm2', 'xmm3', 'xmm4', 'xmm5', |
|
619 'xmm6', 'xmm7' |
|
620 ]) |
|
621 |
|
622 ASM_INSTRUCTIONS = set([ |
|
623 'aaa', 'aad', 'aam', 'aas', 'adc', 'add', 'and', 'arpl', 'bound', |
|
624 'bsf', 'bsr', 'bswap', 'bt', 'btc', 'btr', 'bts', 'call', 'cbw', |
|
625 'cdq', 'clc', 'cld', 'cli', 'clts', 'cmc', 'cmova', 'cmovae', |
|
626 'cmovb', 'cmovbe', 'cmovc', 'cmovcxz', 'cmove', 'cmovg', |
|
627 'cmovge', 'cmovl', 'cmovle', 'cmovna', 'cmovnae', 'cmovnb', |
|
628 'cmovnbe', 'cmovnc', 'cmovne', 'cmovng', 'cmovnge', 'cmovnl', |
|
629 'cmovnle', 'cmovno', 'cmovnp', 'cmovns', 'cmovnz', 'cmovo', |
|
630 'cmovp', 'cmovpe', 'cmovpo', 'cmovs', 'cmovz', 'cmp', 'cmpsb', |
|
631 'cmpsd', 'cmpsw', 'cmpxchg', 'cmpxchg486', 'cmpxchg8b', 'cpuid', |
|
632 'cwd', 'cwde', 'daa', 'das', 'dec', 'div', 'emms', 'enter', 'hlt', |
|
633 'ibts', 'icebp', 'idiv', 'imul', 'in', 'inc', 'insb', 'insd', |
|
634 'insw', 'int', 'int01', 'int03', 'int1', 'int3', 'into', 'invd', |
|
635 'invlpg', 'iret', 'iretd', 'iretw', 'ja', 'jae', 'jb', 'jbe', |
|
636 'jc', 'jcxz', 'jcxz', 'je', 'jecxz', 'jg', 'jge', 'jl', 'jle', |
|
637 'jmp', 'jna', 'jnae', 'jnb', 'jnbe', 'jnc', 'jne', 'jng', 'jnge', |
|
638 'jnl', 'jnle', 'jno', 'jnp', 'jns', 'jnz', 'jo', 'jp', 'jpe', |
|
639 'jpo', 'js', 'jz', 'lahf', 'lar', 'lcall', 'lds', 'lea', 'leave', |
|
640 'les', 'lfs', 'lgdt', 'lgs', 'lidt', 'ljmp', 'lldt', 'lmsw', |
|
641 'loadall', 'loadall286', 'lock', 'lodsb', 'lodsd', 'lodsw', |
|
642 'loop', 'loope', 'loopne', 'loopnz', 'loopz', 'lsl', 'lss', 'ltr', |
|
643 'mov', 'movd', 'movq', 'movsb', 'movsd', 'movsw', 'movsx', |
|
644 'movzx', 'mul', 'neg', 'nop', 'not', 'or', 'out', 'outsb', 'outsd', |
|
645 'outsw', 'pop', 'popa', 'popad', 'popaw', 'popf', 'popfd', 'popfw', |
|
646 'push', 'pusha', 'pushad', 'pushaw', 'pushf', 'pushfd', 'pushfw', |
|
647 'rcl', 'rcr', 'rdmsr', 'rdpmc', 'rdshr', 'rdtsc', 'rep', 'repe', |
|
648 'repne', 'repnz', 'repz', 'ret', 'retf', 'retn', 'rol', 'ror', |
|
649 'rsdc', 'rsldt', 'rsm', 'sahf', 'sal', 'salc', 'sar', 'sbb', |
|
650 'scasb', 'scasd', 'scasw', 'seta', 'setae', 'setb', 'setbe', |
|
651 'setc', 'setcxz', 'sete', 'setg', 'setge', 'setl', 'setle', |
|
652 'setna', 'setnae', 'setnb', 'setnbe', 'setnc', 'setne', 'setng', |
|
653 'setnge', 'setnl', 'setnle', 'setno', 'setnp', 'setns', 'setnz', |
|
654 'seto', 'setp', 'setpe', 'setpo', 'sets', 'setz', 'sgdt', 'shl', |
|
655 'shld', 'shr', 'shrd', 'sidt', 'sldt', 'smi', 'smint', 'smintold', |
|
656 'smsw', 'stc', 'std', 'sti', 'stosb', 'stosd', 'stosw', 'str', |
|
657 'sub', 'svdc', 'svldt', 'svts', 'syscall', 'sysenter', 'sysexit', |
|
658 'sysret', 'test', 'ud1', 'ud2', 'umov', 'verr', 'verw', 'wait', |
|
659 'wbinvd', 'wrmsr', 'wrshr', 'xadd', 'xbts', 'xchg', 'xlat', |
|
660 'xlatb', 'xor' |
|
661 ]) |
|
662 |
|
663 def __init__(self, **options): |
|
664 Lexer.__init__(self, **options) |
|
665 self.keywords = set() |
|
666 if get_bool_opt(options, 'turbopascal', True): |
|
667 self.keywords.update(self.TURBO_PASCAL_KEYWORDS) |
|
668 if get_bool_opt(options, 'delphi', True): |
|
669 self.keywords.update(self.DELPHI_KEYWORDS) |
|
670 if get_bool_opt(options, 'freepascal', True): |
|
671 self.keywords.update(self.FREE_PASCAL_KEYWORDS) |
|
672 self.builtins = set() |
|
673 for unit in get_list_opt(options, 'units', self.BUILTIN_UNITS.keys()): |
|
674 self.builtins.update(self.BUILTIN_UNITS[unit]) |
|
675 |
|
676 def get_tokens_unprocessed(self, text): |
|
677 scanner = Scanner(text, re.DOTALL | re.MULTILINE | re.IGNORECASE) |
|
678 stack = ['initial'] |
|
679 in_function_block = False |
|
680 in_property_block = False |
|
681 was_dot = False |
|
682 next_token_is_function = False |
|
683 next_token_is_property = False |
|
684 collect_labels = False |
|
685 block_labels = set() |
|
686 brace_balance = [0, 0] |
|
687 |
|
688 while not scanner.eos: |
|
689 token = Error |
|
690 |
|
691 if stack[-1] == 'initial': |
|
692 if scanner.scan(r'\s+'): |
|
693 token = Text |
|
694 elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'): |
|
695 if scanner.match.startswith('$'): |
|
696 token = Comment.Preproc |
|
697 else: |
|
698 token = Comment.Multiline |
|
699 elif scanner.scan(r'//.*?$'): |
|
700 token = Comment.Single |
|
701 elif scanner.scan(r'[-+*\/=<>:;,.@\^]'): |
|
702 token = Operator |
|
703 # stop label highlighting on next ";" |
|
704 if collect_labels and scanner.match == ';': |
|
705 collect_labels = False |
|
706 elif scanner.scan(r'[\(\)\[\]]+'): |
|
707 token = Punctuation |
|
708 # abort function naming ``foo = Function(...)`` |
|
709 next_token_is_function = False |
|
710 # if we are in a function block we count the open |
|
711 # braces because ootherwise it's impossible to |
|
712 # determine the end of the modifier context |
|
713 if in_function_block or in_property_block: |
|
714 if scanner.match == '(': |
|
715 brace_balance[0] += 1 |
|
716 elif scanner.match == ')': |
|
717 brace_balance[0] -= 1 |
|
718 elif scanner.match == '[': |
|
719 brace_balance[1] += 1 |
|
720 elif scanner.match == ']': |
|
721 brace_balance[1] -= 1 |
|
722 elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'): |
|
723 lowercase_name = scanner.match.lower() |
|
724 if lowercase_name == 'result': |
|
725 token = Name.Builtin.Pseudo |
|
726 elif lowercase_name in self.keywords: |
|
727 token = Keyword |
|
728 # if we are in a special block and a |
|
729 # block ending keyword occours (and the parenthesis |
|
730 # is balanced) we end the current block context |
|
731 if (in_function_block or in_property_block) and \ |
|
732 lowercase_name in self.BLOCK_KEYWORDS and \ |
|
733 brace_balance[0] <= 0 and \ |
|
734 brace_balance[1] <= 0: |
|
735 in_function_block = False |
|
736 in_property_block = False |
|
737 brace_balance = [0, 0] |
|
738 block_labels = set() |
|
739 if lowercase_name in ('label', 'goto'): |
|
740 collect_labels = True |
|
741 elif lowercase_name == 'asm': |
|
742 stack.append('asm') |
|
743 elif lowercase_name == 'property': |
|
744 in_property_block = True |
|
745 next_token_is_property = True |
|
746 elif lowercase_name in ('procedure', 'operator', |
|
747 'function', 'constructor', |
|
748 'destructor'): |
|
749 in_function_block = True |
|
750 next_token_is_function = True |
|
751 # we are in a function block and the current name |
|
752 # is in the set of registered modifiers. highlight |
|
753 # it as pseudo keyword |
|
754 elif in_function_block and \ |
|
755 lowercase_name in self.FUNCTION_MODIFIERS: |
|
756 token = Keyword.Pseudo |
|
757 # if we are in a property highlight some more |
|
758 # modifiers |
|
759 elif in_property_block and \ |
|
760 lowercase_name in ('read', 'write'): |
|
761 token = Keyword.Pseudo |
|
762 next_token_is_function = True |
|
763 # if the last iteration set next_token_is_function |
|
764 # to true we now want this name highlighted as |
|
765 # function. so do that and reset the state |
|
766 elif next_token_is_function: |
|
767 # Look if the next token is a dot. If yes it's |
|
768 # not a function, but a class name and the |
|
769 # part after the dot a function name |
|
770 if scanner.test(r'\s*\.\s*'): |
|
771 token = Name.Class |
|
772 # it's not a dot, our job is done |
|
773 else: |
|
774 token = Name.Function |
|
775 next_token_is_function = False |
|
776 # same for properties |
|
777 elif next_token_is_property: |
|
778 token = Name.Property |
|
779 next_token_is_property = False |
|
780 # Highlight this token as label and add it |
|
781 # to the list of known labels |
|
782 elif collect_labels: |
|
783 token = Name.Label |
|
784 block_labels.add(scanner.match.lower()) |
|
785 # name is in list of known labels |
|
786 elif lowercase_name in block_labels: |
|
787 token = Name.Label |
|
788 elif lowercase_name in self.BUILTIN_TYPES: |
|
789 token = Keyword.Type |
|
790 elif lowercase_name in self.DIRECTIVES: |
|
791 token = Keyword.Pseudo |
|
792 # builtins are just builtins if the token |
|
793 # before isn't a dot |
|
794 elif not was_dot and lowercase_name in self.builtins: |
|
795 token = Name.Builtin |
|
796 else: |
|
797 token = Name |
|
798 elif scanner.scan(r"'"): |
|
799 token = String |
|
800 stack.append('string') |
|
801 elif scanner.scan(r'\#(\d+|\$[0-9A-Fa-f]+)'): |
|
802 token = String.Char |
|
803 elif scanner.scan(r'\$[0-9A-Fa-f]+'): |
|
804 token = Number.Hex |
|
805 elif scanner.scan(r'\d+(?![eE]|\.[^.])'): |
|
806 token = Number.Integer |
|
807 elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'): |
|
808 token = Number.Float |
|
809 else: |
|
810 # if the stack depth is deeper than once, pop |
|
811 if len(stack) > 1: |
|
812 stack.pop() |
|
813 scanner.get_char() |
|
814 |
|
815 elif stack[-1] == 'string': |
|
816 if scanner.scan(r"''"): |
|
817 token = String.Escape |
|
818 elif scanner.scan(r"'"): |
|
819 token = String |
|
820 stack.pop() |
|
821 elif scanner.scan(r"[^']*"): |
|
822 token = String |
|
823 else: |
|
824 scanner.get_char() |
|
825 stack.pop() |
|
826 |
|
827 elif stack[-1] == 'asm': |
|
828 if scanner.scan(r'\s+'): |
|
829 token = Text |
|
830 elif scanner.scan(r'end'): |
|
831 token = Keyword |
|
832 stack.pop() |
|
833 elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'): |
|
834 if scanner.match.startswith('$'): |
|
835 token = Comment.Preproc |
|
836 else: |
|
837 token = Comment.Multiline |
|
838 elif scanner.scan(r'//.*?$'): |
|
839 token = Comment.Single |
|
840 elif scanner.scan(r"'"): |
|
841 token = String |
|
842 stack.append('string') |
|
843 elif scanner.scan(r'@@[A-Za-z_][A-Za-z_0-9]*'): |
|
844 token = Name.Label |
|
845 elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'): |
|
846 lowercase_name = scanner.match.lower() |
|
847 if lowercase_name in self.ASM_INSTRUCTIONS: |
|
848 token = Keyword |
|
849 elif lowercase_name in self.ASM_REGISTERS: |
|
850 token = Name.Builtin |
|
851 else: |
|
852 token = Name |
|
853 elif scanner.scan(r'[-+*\/=<>:;,.@\^]+'): |
|
854 token = Operator |
|
855 elif scanner.scan(r'[\(\)\[\]]+'): |
|
856 token = Punctuation |
|
857 elif scanner.scan(r'\$[0-9A-Fa-f]+'): |
|
858 token = Number.Hex |
|
859 elif scanner.scan(r'\d+(?![eE]|\.[^.])'): |
|
860 token = Number.Integer |
|
861 elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'): |
|
862 token = Number.Float |
|
863 else: |
|
864 scanner.get_char() |
|
865 stack.pop() |
|
866 |
|
867 # save the dot!!!11 |
|
868 if scanner.match.strip(): |
|
869 was_dot = scanner.match == '.' |
|
870 yield scanner.start_pos, token, scanner.match or '' |
|
871 |
|
872 |
|
873 class JavaLexer(RegexLexer): |
|
874 """ |
|
875 For `Java <http://www.sun.com/java/>`_ source code. |
|
876 """ |
|
877 |
|
878 name = 'Java' |
|
879 aliases = ['java'] |
|
880 filenames = ['*.java'] |
|
881 mimetypes = ['text/x-java'] |
|
882 |
|
883 flags = re.MULTILINE | re.DOTALL |
|
884 |
|
885 #: optional Comment or Whitespace |
|
886 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' |
|
887 |
|
888 tokens = { |
|
889 'root': [ |
|
890 # method names |
|
891 (r'^(\s*(?:[a-zA-Z_][a-zA-Z0-9_\.\[\]]*\s+)+?)' # return arguments |
|
892 r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name |
|
893 r'(\s*)(\()', # signature start |
|
894 bygroups(using(this), Name.Function, Text, Operator)), |
|
895 (r'[^\S\n]+', Text), |
|
896 (r'//.*?\n', Comment.Single), |
|
897 (r'/\*.*?\*/', Comment.Multiline), |
|
898 (r'@[a-zA-Z_][a-zA-Z0-9_\.]*', Name.Decorator), |
|
899 (r'(assert|break|case|catch|continue|default|do|else|finally|for|' |
|
900 r'if|goto|instanceof|new|return|switch|this|throw|try|while)\b', |
|
901 Keyword), |
|
902 (r'(abstract|const|enum|extends|final|implements|native|private|' |
|
903 r'protected|public|static|strictfp|super|synchronized|throws|' |
|
904 r'transient|volatile)\b', Keyword.Declaration), |
|
905 (r'(boolean|byte|char|double|float|int|long|short|void)\b', |
|
906 Keyword.Type), |
|
907 (r'(package)(\s+)', bygroups(Keyword.Namespace, Text)), |
|
908 (r'(true|false|null)\b', Keyword.Constant), |
|
909 (r'(class|interface)(\s+)', bygroups(Keyword.Declaration, Text), 'class'), |
|
910 (r'(import)(\s+)', bygroups(Keyword.Namespace, Text), 'import'), |
|
911 (r'"(\\\\|\\"|[^"])*"', String), |
|
912 (r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char), |
|
913 (r'(\.)([a-zA-Z_][a-zA-Z0-9_]*)', bygroups(Operator, Name.Attribute)), |
|
914 (r'[a-zA-Z_][a-zA-Z0-9_]*:', Name.Label), |
|
915 (r'[a-zA-Z_\$][a-zA-Z0-9_]*', Name), |
|
916 (r'[~\^\*!%&\[\]\(\)\{\}<>\|+=:;,./?-]', Operator), |
|
917 (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
|
918 (r'0x[0-9a-f]+', Number.Hex), |
|
919 (r'[0-9]+L?', Number.Integer), |
|
920 (r'\n', Text) |
|
921 ], |
|
922 'class': [ |
|
923 (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop') |
|
924 ], |
|
925 'import': [ |
|
926 (r'[a-zA-Z0-9_.]+\*?', Name.Namespace, '#pop') |
|
927 ], |
|
928 } |
|
929 |
|
930 class ScalaLexer(RegexLexer): |
|
931 """ |
|
932 For `Scala <http://www.scala-lang.org>`_ source code. |
|
933 """ |
|
934 |
|
935 name = 'Scala' |
|
936 aliases = ['scala'] |
|
937 filenames = ['*.scala'] |
|
938 mimetypes = ['text/x-scala'] |
|
939 |
|
940 flags = re.MULTILINE | re.DOTALL |
|
941 |
|
942 #: optional Comment or Whitespace |
|
943 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' |
|
944 |
|
945 # don't use raw unicode strings! |
|
946 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]+' |
|
947 |
|
948 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]' |
|
949 |
|
950 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]' |
|
951 |
|
952 idrest = ur'%s(?:%s|[0-9])*(?:(?<=_)%s)?' % (letter, letter, op) |
|
953 |
|
954 tokens = { |
|
955 'root': [ |
|
956 # method names |
|
957 (r'(class|trait|object)(\s+)', bygroups(Keyword, Text), 'class'), |
|
958 (ur"'%s" % idrest, Text.Symbol), |
|
959 (r'[^\S\n]+', Text), |
|
960 (r'//.*?\n', Comment.Single), |
|
961 (r'/\*', Comment.Multiline, 'comment'), |
|
962 (ur'@%s' % idrest, Name.Decorator), |
|
963 (ur'(abstract|ca(?:se|tch)|d(?:ef|o)|e(?:lse|xtends)|' |
|
964 ur'f(?:inal(?:ly)?|or(?:Some)?)|i(?:f|mplicit)|' |
|
965 ur'lazy|match|new|override|pr(?:ivate|otected)' |
|
966 ur'|re(?:quires|turn)|s(?:ealed|uper)|' |
|
967 ur't(?:h(?:is|row)|ry)|va[lr]|w(?:hile|ith)|yield)\b|' |
|
968 u'(<[%:-]|=>|>:|[#=@_\u21D2\u2190])(\b|(?=\\s)|$)', Keyword), |
|
969 (ur':(?!%s)' % op, Keyword, 'type'), |
|
970 (ur'%s%s\b' % (upper, idrest), Name.Class), |
|
971 (r'(true|false|null)\b', Keyword.Constant), |
|
972 (r'(import|package)(\s+)', bygroups(Keyword, Text), 'import'), |
|
973 (r'(type)(\s+)', bygroups(Keyword, Text), 'type'), |
|
974 (r'"""(?:.|\n)*?"""', String), |
|
975 (r'"(\\\\|\\"|[^"])*"', String), |
|
976 (ur"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char), |
|
977 # (ur'(\.)(%s|%s|`[^`]+`)' % (idrest, op), bygroups(Operator, |
|
978 # Name.Attribute)), |
|
979 (idrest, Name), |
|
980 (r'`[^`]+`', Name), |
|
981 (r'\[', Operator, 'typeparam'), |
|
982 (r'[\(\)\{\};,.]', Operator), |
|
983 (op, Operator), |
|
984 (ur'([0-9][0-9]*\.[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?[fFdD]?', |
|
985 Number.Float), |
|
986 (r'0x[0-9a-f]+', Number.Hex), |
|
987 (r'[0-9]+L?', Number.Integer), |
|
988 (r'\n', Text) |
|
989 ], |
|
990 'class': [ |
|
991 (ur'(%s|%s|`[^`]+`)(\s*)(\[)' % (idrest, op), |
|
992 bygroups(Name.Class, Text, Operator), 'typeparam'), |
|
993 (r'[\s\n]+', Text), |
|
994 (r'{', Operator, '#pop'), |
|
995 (r'\(', Operator, '#pop'), |
|
996 (ur'%s|%s|`[^`]+`' % (idrest, op), Name.Class, '#pop'), |
|
997 ], |
|
998 'type': [ |
|
999 (r'\s+', Text), |
|
1000 (u'<[%:]|>:|[#_\u21D2]|forSome|type', Keyword), |
|
1001 (r'([,\);}]|=>|=)([\s\n]*)', bygroups(Operator, Text), '#pop'), |
|
1002 (r'[\(\{]', Operator, '#push'), |
|
1003 (ur'((?:%s|%s|`[^`]+`)(?:\.(?:%s|%s|`[^`]+`))*)(\s*)(\[)' % |
|
1004 (idrest, op, idrest, op), |
|
1005 bygroups(Keyword.Type, Text, Operator), ('#pop', 'typeparam')), |
|
1006 (ur'((?:%s|%s|`[^`]+`)(?:\.(?:%s|%s|`[^`]+`))*)(\s*)$' % |
|
1007 (idrest, op, idrest, op), |
|
1008 bygroups(Keyword.Type, Text), '#pop'), |
|
1009 (ur'\.|%s|%s|`[^`]+`' % (idrest, op), Keyword.Type) |
|
1010 ], |
|
1011 'typeparam': [ |
|
1012 (r'[\s\n,]+', Text), |
|
1013 (u'<[%:]|=>|>:|[#_\u21D2]|forSome|type', Keyword), |
|
1014 (r'([\]\)\}])', Operator, '#pop'), |
|
1015 (r'[\(\[\{]', Operator, '#push'), |
|
1016 (ur'\.|%s|%s|`[^`]+`' % (idrest, op), Keyword.Type) |
|
1017 ], |
|
1018 'comment': [ |
|
1019 (r'[^/\*]+', Comment.Multiline), |
|
1020 (r'/\*', Comment.Multiline, '#push'), |
|
1021 (r'\*/', Comment.Multiline, '#pop'), |
|
1022 (r'[*/]', Comment.Multiline) |
|
1023 ], |
|
1024 'import': [ |
|
1025 (ur'(%s|\.)+' % idrest, Name.Namespace, '#pop') |
|
1026 ], |
|
1027 } |
|
1028 |
|
1029 |
|
1030 class DylanLexer(RegexLexer): |
|
1031 """ |
|
1032 For the `Dylan <http://www.opendylan.org/>`_ language. |
|
1033 |
|
1034 *New in Pygments 0.7.* |
|
1035 """ |
|
1036 |
|
1037 name = 'Dylan' |
|
1038 aliases = ['dylan'] |
|
1039 filenames = ['*.dylan'] |
|
1040 mimetypes = ['text/x-dylan'] |
|
1041 |
|
1042 flags = re.DOTALL |
|
1043 |
|
1044 tokens = { |
|
1045 'root': [ |
|
1046 (r'\b(subclass|abstract|block|c(on(crete|stant)|lass)|domain' |
|
1047 r'|ex(c(eption|lude)|port)|f(unction(|al))|generic|handler' |
|
1048 r'|i(n(herited|line|stance|terface)|mport)|library|m(acro|ethod)' |
|
1049 r'|open|primary|sealed|si(deways|ngleton)|slot' |
|
1050 r'|v(ariable|irtual))\b', Name.Builtin), |
|
1051 (r'<\w+>', Keyword.Type), |
|
1052 (r'#?"(?:\\.|[^"])+?"', String.Double), |
|
1053 (r'//.*?\n', Comment.Single), |
|
1054 (r'/\*[\w\W]*?\*/', Comment.Multiline), |
|
1055 (r'\'.*?\'', String.Single), |
|
1056 (r'=>|\b(a(bove|fterwards)|b(e(gin|low)|y)|c(ase|leanup|reate)' |
|
1057 r'|define|else(|if)|end|f(inally|or|rom)|i[fn]|l(et|ocal)|otherwise' |
|
1058 r'|rename|s(elect|ignal)|t(hen|o)|u(n(less|til)|se)|wh(en|ile))\b', |
|
1059 Keyword), |
|
1060 (r'([ \t])([!\$%&\*\/:<=>\?~_^a-zA-Z0-9.+\-]*:)', |
|
1061 bygroups(Text, Name.Variable)), |
|
1062 (r'([ \t]*)(\S+[^:])([ \t]*)(\()([ \t]*)', |
|
1063 bygroups(Text, Name.Function, Text, Punctuation, Text)), |
|
1064 (r'-?[0-9.]+', Number), |
|
1065 (r'[(),;]', Punctuation), |
|
1066 (r'\$[a-zA-Z0-9-]+', Name.Constant), |
|
1067 (r'[!$%&*/:<>=?~^.+\[\]{}-]+', Operator), |
|
1068 (r'\s+', Text), |
|
1069 (r'#[a-zA-Z0-9-]+', Keyword), |
|
1070 (r'[a-zA-Z0-9-]+', Name.Variable), |
|
1071 ], |
|
1072 } |
|
1073 |
|
1074 |
|
1075 class ObjectiveCLexer(RegexLexer): |
|
1076 """ |
|
1077 For Objective-C source code with preprocessor directives. |
|
1078 """ |
|
1079 |
|
1080 name = 'Objective-C' |
|
1081 aliases = ['objective-c', 'objectivec', 'obj-c', 'objc'] |
|
1082 #XXX: objc has .h files too :-/ |
|
1083 filenames = ['*.m'] |
|
1084 mimetypes = ['text/x-objective-c'] |
|
1085 |
|
1086 #: optional Comment or Whitespace |
|
1087 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' |
|
1088 |
|
1089 tokens = { |
|
1090 'whitespace': [ |
|
1091 (r'^(\s*)(#if\s+0)', bygroups(Text, Comment.Preproc), 'if0'), |
|
1092 (r'^(\s*)(#)', bygroups(Text, Comment.Preproc), 'macro'), |
|
1093 (r'\n', Text), |
|
1094 (r'\s+', Text), |
|
1095 (r'\\\n', Text), # line continuation |
|
1096 (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single), |
|
1097 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
1098 ], |
|
1099 'statements': [ |
|
1100 (r'(L|@)?"', String, 'string'), |
|
1101 (r"(L|@)?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", |
|
1102 String.Char), |
|
1103 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float), |
|
1104 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
|
1105 (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex), |
|
1106 (r'0[0-7]+[Ll]?', Number.Oct), |
|
1107 (r'\d+[Ll]?', Number.Integer), |
|
1108 (r'[~!%^&*+=|?:<>/-]', Operator), |
|
1109 (r'[()\[\],.]', Punctuation), |
|
1110 (r'(auto|break|case|const|continue|default|do|else|enum|extern|' |
|
1111 r'for|goto|if|register|restricted|return|sizeof|static|struct|' |
|
1112 r'switch|typedef|union|volatile|virtual|while|in|@selector|' |
|
1113 r'@private|@protected|@public|@encode|' |
|
1114 r'@synchronized|@try|@throw|@catch|@finally|@end|@property|' |
|
1115 r'@synthesize|@dynamic)\b', Keyword), |
|
1116 (r'(int|long|float|short|double|char|unsigned|signed|void|' |
|
1117 r'id|BOOL|IBOutlet|IBAction|SEL)\b', Keyword.Type), |
|
1118 (r'(_{0,2}inline|naked|restrict|thread|typename)\b', |
|
1119 Keyword.Reserved), |
|
1120 (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|' |
|
1121 r'declspec|finally|int64|try|leave)\b', Keyword.Reserved), |
|
1122 (r'(TRUE|FALSE|nil|NULL)\b', Name.Builtin), |
|
1123 ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label), |
|
1124 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), |
|
1125 ], |
|
1126 'root': [ |
|
1127 include('whitespace'), |
|
1128 # functions |
|
1129 (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments |
|
1130 r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name |
|
1131 r'(\s*\([^;]*?\))' # signature |
|
1132 r'(' + _ws + r')({)', |
|
1133 bygroups(using(this), Name.Function, |
|
1134 using(this), Text, Punctuation), |
|
1135 'function'), |
|
1136 # function declarations |
|
1137 (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments |
|
1138 r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name |
|
1139 r'(\s*\([^;]*?\))' # signature |
|
1140 r'(' + _ws + r')(;)', |
|
1141 bygroups(using(this), Name.Function, |
|
1142 using(this), Text, Punctuation)), |
|
1143 (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text), |
|
1144 'classname'), |
|
1145 (r'(@class|@protocol)(\s+)', bygroups(Keyword, Text), |
|
1146 'forward_classname'), |
|
1147 (r'(\s*)(@end)(\s*)', bygroups(Text, Keyword, Text)), |
|
1148 ('', Text, 'statement'), |
|
1149 ], |
|
1150 'classname' : [ |
|
1151 # interface definition that inherits |
|
1152 ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*:\s*)([a-zA-Z_][a-zA-Z0-9_]*)?', |
|
1153 bygroups(Name.Class, Text, Name.Class), '#pop'), |
|
1154 # interface definition for a category |
|
1155 ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\([a-zA-Z_][a-zA-Z0-9_]*\))', |
|
1156 bygroups(Name.Class, Text, Name.Label), '#pop'), |
|
1157 # simple interface / implementation |
|
1158 ('([a-zA-Z_][a-zA-Z0-9_]*)', Name.Class, '#pop') |
|
1159 ], |
|
1160 'forward_classname' : [ |
|
1161 ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*,\s*)', |
|
1162 bygroups(Name.Class, Text), 'forward_classname'), |
|
1163 ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*;?)', |
|
1164 bygroups(Name.Class, Text), '#pop') |
|
1165 ], |
|
1166 'statement' : [ |
|
1167 include('whitespace'), |
|
1168 include('statements'), |
|
1169 ('[{}]', Punctuation), |
|
1170 (';', Punctuation, '#pop'), |
|
1171 ], |
|
1172 'function': [ |
|
1173 include('whitespace'), |
|
1174 include('statements'), |
|
1175 (';', Punctuation), |
|
1176 ('{', Punctuation, '#push'), |
|
1177 ('}', Punctuation, '#pop'), |
|
1178 ], |
|
1179 'string': [ |
|
1180 (r'"', String, '#pop'), |
|
1181 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape), |
|
1182 (r'[^\\"\n]+', String), # all other characters |
|
1183 (r'\\\n', String), # line continuation |
|
1184 (r'\\', String), # stray backslash |
|
1185 ], |
|
1186 'macro': [ |
|
1187 (r'[^/\n]+', Comment.Preproc), |
|
1188 (r'/[*](.|\n)*?[*]/', Comment.Multiline), |
|
1189 (r'//.*?\n', Comment.Single, '#pop'), |
|
1190 (r'/', Comment.Preproc), |
|
1191 (r'(?<=\\)\n', Comment.Preproc), |
|
1192 (r'\n', Comment.Preproc, '#pop'), |
|
1193 ], |
|
1194 'if0': [ |
|
1195 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
|
1196 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
|
1197 (r'.*?\n', Comment), |
|
1198 ] |
|
1199 } |
|
1200 |
|
1201 def analyse_text(text): |
|
1202 if '@"' in text: # strings |
|
1203 return True |
|
1204 if re.match(r'\[[a-zA-Z0-9.]:', text): # message |
|
1205 return True |
|
1206 return False |
|
1207 |
|
1208 class FortranLexer(RegexLexer): |
|
1209 ''' |
|
1210 Lexer for FORTRAN 90 code. |
|
1211 |
|
1212 *New in Pygments 0.10.* |
|
1213 ''' |
|
1214 name = 'Fortran' |
|
1215 aliases = ['fortran'] |
|
1216 filenames = ['*.f', '*.f90'] |
|
1217 mimetypes = ['text/x-fortran'] |
|
1218 flags = re.IGNORECASE |
|
1219 |
|
1220 # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and DOUBLE PRECISION |
|
1221 # Operators: **, *, +, -, /, <, >, <=, >=, ==, /= |
|
1222 # Logical (?): NOT, AND, OR, EQV, NEQV |
|
1223 |
|
1224 # Builtins: |
|
1225 # http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Table-of-Intrinsic-Functions.html |
|
1226 |
|
1227 tokens = { |
|
1228 'root': [ |
|
1229 (r'!.*\n', Comment), |
|
1230 include('strings'), |
|
1231 include('core'), |
|
1232 (r'[a-z][a-z0-9_]*', Name.Variable), |
|
1233 include('nums'), |
|
1234 (r'[\s]+', Text), |
|
1235 ], |
|
1236 'core': [ |
|
1237 # Statements |
|
1238 (r'\b(ACCEPT|ALLOCATABLE|ALLOCATE|ARRAY|ASSIGN|BACKSPACE|BLOCK DATA|' |
|
1239 r'BYTE|CALL|CASE|CLOSE|COMMON|CONTAINS|CONTINUE|CYCLE|DATA|' |
|
1240 r'DEALLOCATE|DECODE|DIMENSION|DO|ENCODE|END FILE|ENDIF|END|ENTRY|' |
|
1241 r'EQUIVALENCE|EXIT|EXTERNAL|EXTRINSIC|FORALL|FORMAT|FUNCTION|GOTO|' |
|
1242 r'IF|IMPLICIT|INCLUDE|INQUIRE|INTENT|INTERFACE|INTRINSIC|MODULE|' |
|
1243 r'NAMELIST|NULLIFY|NONE|OPEN|OPTIONAL|OPTIONS|PARAMETER|PAUSE|' |
|
1244 r'POINTER|PRINT|PRIVATE|PROGRAM|PUBLIC|PURE|READ|RECURSIVE|RETURN|' |
|
1245 r'REWIND|SAVE|SELECT|SEQUENCE|STOP|SUBROUTINE|TARGET|TYPE|USE|' |
|
1246 r'VOLATILE|WHERE|WRITE|WHILE|THEN|ELSE|ENDIF)\s*\b', |
|
1247 Keyword), |
|
1248 |
|
1249 # Data Types |
|
1250 (r'\b(CHARACTER|COMPLEX|DOUBLE PRECISION|DOUBLE COMPLEX|INTEGER|' |
|
1251 r'LOGICAL|REAL)\s*\b', |
|
1252 Keyword.Type), |
|
1253 |
|
1254 # Operators |
|
1255 (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator), |
|
1256 |
|
1257 (r'(::)', Keyword.Declaration), |
|
1258 |
|
1259 (r'[(),:&%;]', Punctuation), |
|
1260 |
|
1261 # Intrinsics |
|
1262 (r'\b(Abort|Abs|Access|AChar|ACos|AdjustL|AdjustR|AImag|AInt|Alarm|' |
|
1263 r'All|Allocated|ALog|AMax|AMin|AMod|And|ANInt|Any|' |
|
1264 r'ASin|Associated|ATan|BesJ|BesJN|BesY|BesYN|' |
|
1265 r'Bit_Size|BTest|CAbs|CCos|Ceiling|CExp|Char|ChDir|ChMod|CLog|' |
|
1266 r'Cmplx|Complex|Conjg|Cos|CosH|Count|CPU_Time|CShift|CSin|CSqRt|' |
|
1267 r'CTime|DAbs|DACos|DASin|DATan|Date_and_Time|DbesJ|' |
|
1268 r'DbesJ|DbesJN|DbesY|DbesY|DbesYN|Dble|DCos|DCosH|DDiM|DErF|DErFC|' |
|
1269 r'DExp|Digits|DiM|DInt|DLog|DLog|DMax|DMin|DMod|DNInt|Dot_Product|' |
|
1270 r'DProd|DSign|DSinH|DSin|DSqRt|DTanH|DTan|DTime|EOShift|Epsilon|' |
|
1271 r'ErF|ErFC|ETime|Exit|Exp|Exponent|FDate|FGet|FGetC|Float|' |
|
1272 r'Floor|Flush|FNum|FPutC|FPut|Fraction|FSeek|FStat|FTell|' |
|
1273 r'GError|GetArg|GetCWD|GetEnv|GetGId|GetLog|GetPId|GetUId|' |
|
1274 r'GMTime|HostNm|Huge|IAbs|IAChar|IAnd|IArgC|IBClr|IBits|' |
|
1275 r'IBSet|IChar|IDate|IDiM|IDInt|IDNInt|IEOr|IErrNo|IFix|Imag|' |
|
1276 r'ImagPart|Index|Int|IOr|IRand|IsaTty|IShft|IShftC|ISign|' |
|
1277 r'ITime|Kill|Kind|LBound|Len|Len_Trim|LGe|LGt|Link|LLe|LLt|LnBlnk|' |
|
1278 r'Loc|Log|Log|Logical|Long|LShift|LStat|LTime|MatMul|Max|' |
|
1279 r'MaxExponent|MaxLoc|MaxVal|MClock|Merge|Min|MinExponent|MinLoc|' |
|
1280 r'MinVal|Mod|Modulo|MvBits|Nearest|NInt|Not|Or|Pack|PError|' |
|
1281 r'Precision|Present|Product|Radix|Rand|Random_Number|Random_Seed|' |
|
1282 r'Range|Real|RealPart|Rename|Repeat|Reshape|RRSpacing|RShift|Scale|' |
|
1283 r'Scan|Second|Selected_Int_Kind|Selected_Real_Kind|Set_Exponent|' |
|
1284 r'Shape|Short|Sign|Signal|SinH|Sin|Sleep|Sngl|Spacing|Spread|SqRt|' |
|
1285 r'SRand|Stat|Sum|SymLnk|System|System_Clock|Tan|TanH|Time|' |
|
1286 r'Tiny|Transfer|Transpose|Trim|TtyNam|UBound|UMask|Unlink|Unpack|' |
|
1287 r'Verify|XOr|ZAbs|ZCos|ZExp|ZLog|ZSin|ZSqRt)\s*\b', |
|
1288 Name.Builtin), |
|
1289 |
|
1290 # Booleans |
|
1291 (r'\.(true|false)\.', Name.Builtin), |
|
1292 # Comparing Operators |
|
1293 (r'\.(eq|ne|lt|le|gt|ge|not|and|or|eqv|neqv)\.', Operator.Word), |
|
1294 ], |
|
1295 |
|
1296 'strings': [ |
|
1297 (r'"(\\\\|\\[0-7]+|\\.|[^"])*"', String.Double), |
|
1298 (r"'(\\\\|\\[0-7]+|\\.|[^'])*'", String.Single), |
|
1299 ], |
|
1300 |
|
1301 'nums': [ |
|
1302 (r'\d+(?![.Ee])', Number.Integer), |
|
1303 (r'[+-]?\d*\.\d+([eE][-+]?\d+)?', Number.Float), |
|
1304 (r'[+-]?\d+\.\d*([eE][-+]?\d+)?', Number.Float), |
|
1305 ], |
|
1306 } |
|
1307 |
|
1308 |
|
1309 class GLShaderLexer(RegexLexer): |
|
1310 """ |
|
1311 GLSL (OpenGL Shader) lexer. |
|
1312 |
|
1313 *New in Pygments 1.1.* |
|
1314 """ |
|
1315 name = 'GLSL' |
|
1316 aliases = ['glsl'] |
|
1317 filenames = ['*.vert', '*.frag', '*.geo'] |
|
1318 mimetypes = ['text/x-glslsrc'] |
|
1319 |
|
1320 tokens = { |
|
1321 'root': [ |
|
1322 (r'^#.*', Comment.Preproc), |
|
1323 (r'//.*', Comment.Single), |
|
1324 (r'/\*[\w\W]*\*/', Comment.Multiline), |
|
1325 (r'\+|-|~|!=?|\*|/|%|<<|>>|<=?|>=?|==?|&&?|\^|\|\|?', |
|
1326 Operator), |
|
1327 (r'[?:]', Operator), # quick hack for ternary |
|
1328 (r'\bdefined\b', Operator), |
|
1329 (r'[;{}(),\[\]]', Punctuation), |
|
1330 #FIXME when e is present, no decimal point needed |
|
1331 (r'[+-]?\d*\.\d+([eE][-+]?\d+)?', Number.Float), |
|
1332 (r'[+-]?\d+\.\d*([eE][-+]?\d+)?', Number.Float), |
|
1333 (r'0[xX][0-9a-fA-F]*', Number.Hex), |
|
1334 (r'0[0-7]*', Number.Octal), |
|
1335 (r'[1-9][0-9]*', Number.Integer), |
|
1336 (r'\b(attribute|const|uniform|varying|centroid|break|continue|' |
|
1337 r'do|for|while|if|else|in|out|inout|float|int|void|bool|true|' |
|
1338 r'false|invariant|discard|return|mat[234]|mat[234]x[234]|' |
|
1339 r'vec[234]|[ib]vec[234]|sampler[123]D|samplerCube|' |
|
1340 r'sampler[12]DShadow|struct)\b', Keyword), |
|
1341 (r'\b(asm|class|union|enum|typedef|template|this|packed|goto|' |
|
1342 r'switch|default|inline|noinline|volatile|public|static|extern|' |
|
1343 r'external|interface|long|short|double|half|fixed|unsigned|' |
|
1344 r'lowp|mediump|highp|precision|input|output|hvec[234]|' |
|
1345 r'[df]vec[234]|sampler[23]DRect|sampler2DRectShadow|sizeof|' |
|
1346 r'cast|namespace|using)\b', Keyword), #future use |
|
1347 (r'[a-zA-Z_][a-zA-Z_0-9]*', Name.Variable), |
|
1348 (r'\.', Punctuation), |
|
1349 (r'\s+', Text), |
|
1350 ], |
|
1351 } |
|
1352 |
|
1353 class PrologLexer(RegexLexer): |
|
1354 """ |
|
1355 Lexer for Prolog files. |
|
1356 """ |
|
1357 name = 'Prolog' |
|
1358 aliases = ['prolog'] |
|
1359 filenames = ['*.prolog', '*.pro', '*.pl'] |
|
1360 mimetypes = ['text/x-prolog'] |
|
1361 |
|
1362 flags = re.UNICODE |
|
1363 |
|
1364 tokens = { |
|
1365 'root': [ |
|
1366 (r'^#.*', Comment.Single), |
|
1367 (r'/\*', Comment.Multiline, 'nested-comment'), |
|
1368 (r'%.*', Comment.Single), |
|
1369 (r'[0-9]+', Number), |
|
1370 (r'[\[\](){}|.,;!]', Punctuation), |
|
1371 (r':-|-->', Punctuation), |
|
1372 (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\U[0-9a-fA-F]{8}|' |
|
1373 r'\\[0-7]+\\|\\[\w\W]|[^"])*"', String.Double), |
|
1374 (r"'(?:''|[^'])*'", String.Atom), # quoted atom |
|
1375 # Needs to not be followed by an atom. |
|
1376 #(r'=(?=\s|[a-zA-Z\[])', Operator), |
|
1377 (r'(is|<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])', |
|
1378 Operator), |
|
1379 (r'(mod|div|not)\b', Operator), |
|
1380 (r'_', Keyword), # The don't-care variable |
|
1381 (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)), |
|
1382 (u'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' |
|
1383 u'[a-zA-Z0-9_$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)' |
|
1384 u'(\\s*)(:-|-->)', |
|
1385 bygroups(Name.Function, Text, Operator)), # function defn |
|
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, Punctuation)), |
|
1390 (u'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' |
|
1391 u'[a-zA-Z0-9_$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*', |
|
1392 String.Atom), # atom, characters |
|
1393 # This one includes ! |
|
1394 (u'[#&*+\\-./:<=>?@\\\\^~\u00a1-\u00bf\u2010-\u303f]+', |
|
1395 String.Atom), # atom, graphics |
|
1396 (r'[A-Z_][A-Za-z0-9_]*', Name.Variable), |
|
1397 (u'\\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text), |
|
1398 ], |
|
1399 'nested-comment': [ |
|
1400 (r'\*/', Comment.Multiline, '#pop'), |
|
1401 (r'/\*', Comment.Multiline, '#push'), |
|
1402 (r'[^*/]+', Comment.Multiline), |
|
1403 (r'[*/]', Comment.Multiline), |
|
1404 ], |
|
1405 } |
|
1406 |
|
1407 def analyse_text(text): |
|
1408 return ':-' in text |
|
1409 |
|
1410 |
|
1411 class CythonLexer(RegexLexer): |
|
1412 """ |
|
1413 For Pyrex and `Cython <http://cython.org>`_ source code. |
|
1414 |
|
1415 *New in Pygments 1.1.* |
|
1416 """ |
|
1417 |
|
1418 name = 'Cython' |
|
1419 aliases = ['cython', 'pyx'] |
|
1420 filenames = ['*.pyx', '*.pxd', '*.pxi'] |
|
1421 mimetypes = ['text/x-cython', 'application/x-cython'] |
|
1422 |
|
1423 tokens = { |
|
1424 'root': [ |
|
1425 (r'\n', Text), |
|
1426 (r'^(\s*)("""(?:.|\n)*?""")', bygroups(Text, String.Doc)), |
|
1427 (r"^(\s*)('''(?:.|\n)*?''')", bygroups(Text, String.Doc)), |
|
1428 (r'[^\S\n]+', Text), |
|
1429 (r'#.*$', Comment), |
|
1430 (r'[]{}:(),;[]', Punctuation), |
|
1431 (r'\\\n', Text), |
|
1432 (r'\\', Text), |
|
1433 (r'(in|is|and|or|not)\b', Operator.Word), |
|
1434 (r'(<)([a-zA-Z0-9.?]+)(>)', |
|
1435 bygroups(Punctuation, Keyword.Type, Punctuation)), |
|
1436 (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator), |
|
1437 (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)', |
|
1438 bygroups(Keyword, Number.Integer, Operator, Name, Operator, |
|
1439 Name, Punctuation)), |
|
1440 include('keywords'), |
|
1441 (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'), |
|
1442 (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'), |
|
1443 (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'), |
|
1444 (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'), |
|
1445 (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'), |
|
1446 include('builtins'), |
|
1447 include('backtick'), |
|
1448 ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'), |
|
1449 ("(?:[rR]|[uU][rR]|[rR][uU])'''", String, 'tsqs'), |
|
1450 ('(?:[rR]|[uU][rR]|[rR][uU])"', String, 'dqs'), |
|
1451 ("(?:[rR]|[uU][rR]|[rR][uU])'", String, 'sqs'), |
|
1452 ('[uU]?"""', String, combined('stringescape', 'tdqs')), |
|
1453 ("[uU]?'''", String, combined('stringescape', 'tsqs')), |
|
1454 ('[uU]?"', String, combined('stringescape', 'dqs')), |
|
1455 ("[uU]?'", String, combined('stringescape', 'sqs')), |
|
1456 include('name'), |
|
1457 include('numbers'), |
|
1458 ], |
|
1459 'keywords': [ |
|
1460 (r'(assert|break|by|continue|ctypedef|del|elif|else|except\??|exec|' |
|
1461 r'finally|for|gil|global|if|include|lambda|nogil|pass|print|raise|' |
|
1462 r'return|try|while|yield|as|with)\b', Keyword), |
|
1463 (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc), |
|
1464 ], |
|
1465 'builtins': [ |
|
1466 (r'(?<!\.)(__import__|abs|all|any|apply|basestring|bin|bool|buffer|' |
|
1467 r'bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|' |
|
1468 r'complex|delattr|dict|dir|divmod|enumerate|eval|execfile|exit|' |
|
1469 r'file|filter|float|frozenset|getattr|globals|hasattr|hash|hex|id|' |
|
1470 r'input|int|intern|isinstance|issubclass|iter|len|list|locals|' |
|
1471 r'long|map|max|min|next|object|oct|open|ord|pow|property|range|' |
|
1472 r'raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|' |
|
1473 r'sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|' |
|
1474 r'vars|xrange|zip)\b', Name.Builtin), |
|
1475 (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL' |
|
1476 r')\b', Name.Builtin.Pseudo), |
|
1477 (r'(?<!\.)(ArithmeticError|AssertionError|AttributeError|' |
|
1478 r'BaseException|DeprecationWarning|EOFError|EnvironmentError|' |
|
1479 r'Exception|FloatingPointError|FutureWarning|GeneratorExit|IOError|' |
|
1480 r'ImportError|ImportWarning|IndentationError|IndexError|KeyError|' |
|
1481 r'KeyboardInterrupt|LookupError|MemoryError|NameError|' |
|
1482 r'NotImplemented|NotImplementedError|OSError|OverflowError|' |
|
1483 r'OverflowWarning|PendingDeprecationWarning|ReferenceError|' |
|
1484 r'RuntimeError|RuntimeWarning|StandardError|StopIteration|' |
|
1485 r'SyntaxError|SyntaxWarning|SystemError|SystemExit|TabError|' |
|
1486 r'TypeError|UnboundLocalError|UnicodeDecodeError|' |
|
1487 r'UnicodeEncodeError|UnicodeError|UnicodeTranslateError|' |
|
1488 r'UnicodeWarning|UserWarning|ValueError|Warning|ZeroDivisionError' |
|
1489 r')\b', Name.Exception), |
|
1490 ], |
|
1491 'numbers': [ |
|
1492 (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float), |
|
1493 (r'0\d+', Number.Oct), |
|
1494 (r'0[xX][a-fA-F0-9]+', Number.Hex), |
|
1495 (r'\d+L', Number.Integer.Long), |
|
1496 (r'\d+', Number.Integer) |
|
1497 ], |
|
1498 'backtick': [ |
|
1499 ('`.*?`', String.Backtick), |
|
1500 ], |
|
1501 'name': [ |
|
1502 (r'@[a-zA-Z0-9_]+', Name.Decorator), |
|
1503 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), |
|
1504 ], |
|
1505 'funcname': [ |
|
1506 ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop') |
|
1507 ], |
|
1508 'cdef': [ |
|
1509 (r'(public|readonly|extern|api|inline)\b', Keyword.Reserved), |
|
1510 (r'(struct|enum|union|class)\b', Keyword), |
|
1511 (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(?=[(:#=]|$)', |
|
1512 bygroups(Name.Function, Text), '#pop'), |
|
1513 (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(,)', |
|
1514 bygroups(Name.Function, Text, Punctuation)), |
|
1515 (r'from\b', Keyword, '#pop'), |
|
1516 (r'as\b', Keyword), |
|
1517 (r':', Punctuation, '#pop'), |
|
1518 (r'(?=["\'])', Text, '#pop'), |
|
1519 (r'[a-zA-Z_][a-zA-Z0-9_]*', Keyword.Type), |
|
1520 (r'.', Text), |
|
1521 ], |
|
1522 'classname': [ |
|
1523 ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop') |
|
1524 ], |
|
1525 'import': [ |
|
1526 (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)), |
|
1527 (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name.Namespace), |
|
1528 (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)), |
|
1529 (r'', Text, '#pop') # all else: go back |
|
1530 ], |
|
1531 'fromimport': [ |
|
1532 (r'(\s+)(c?import)\b', bygroups(Text, Keyword), '#pop'), |
|
1533 (r'[a-zA-Z_.][a-zA-Z0-9_.]*', Name.Namespace), |
|
1534 # ``cdef foo from "header"``, or ``for foo from 0 < i < 10`` |
|
1535 (r'', Text, '#pop'), |
|
1536 ], |
|
1537 'stringescape': [ |
|
1538 (r'\\([\\abfnrtv"\']|\n|N{.*?}|u[a-fA-F0-9]{4}|' |
|
1539 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
|
1540 ], |
|
1541 'strings': [ |
|
1542 (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
|
1543 '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol), |
|
1544 (r'[^\\\'"%\n]+', String), |
|
1545 # quotes, percents and backslashes must be parsed one at a time |
|
1546 (r'[\'"\\]', String), |
|
1547 # unhandled string formatting sign |
|
1548 (r'%', String) |
|
1549 # newlines are an error (use "nl" state) |
|
1550 ], |
|
1551 'nl': [ |
|
1552 (r'\n', String) |
|
1553 ], |
|
1554 'dqs': [ |
|
1555 (r'"', String, '#pop'), |
|
1556 (r'\\\\|\\"|\\\n', String.Escape), # included here again for raw strings |
|
1557 include('strings') |
|
1558 ], |
|
1559 'sqs': [ |
|
1560 (r"'", String, '#pop'), |
|
1561 (r"\\\\|\\'|\\\n", String.Escape), # included here again for raw strings |
|
1562 include('strings') |
|
1563 ], |
|
1564 'tdqs': [ |
|
1565 (r'"""', String, '#pop'), |
|
1566 include('strings'), |
|
1567 include('nl') |
|
1568 ], |
|
1569 'tsqs': [ |
|
1570 (r"'''", String, '#pop'), |
|
1571 include('strings'), |
|
1572 include('nl') |
|
1573 ], |
|
1574 } |
|
1575 |
|
1576 |
|
1577 class ValaLexer(RegexLexer): |
|
1578 """ |
|
1579 For Vala source code with preprocessor directives. |
|
1580 |
|
1581 *New in Pygments 1.1.* |
|
1582 """ |
|
1583 name = 'Vala' |
|
1584 aliases = ['vala', 'vapi'] |
|
1585 filenames = ['*.vala', '*.vapi'] |
|
1586 mimetypes = ['text/x-vala'] |
|
1587 |
|
1588 tokens = { |
|
1589 'whitespace': [ |
|
1590 (r'^\s*#if\s+0', Comment.Preproc, 'if0'), |
|
1591 (r'\n', Text), |
|
1592 (r'\s+', Text), |
|
1593 (r'\\\n', Text), # line continuation |
|
1594 (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single), |
|
1595 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
1596 ], |
|
1597 'statements': [ |
|
1598 (r'L?"', String, 'string'), |
|
1599 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", |
|
1600 String.Char), |
|
1601 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float), |
|
1602 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
|
1603 (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex), |
|
1604 (r'0[0-7]+[Ll]?', Number.Oct), |
|
1605 (r'\d+[Ll]?', Number.Integer), |
|
1606 (r'[~!%^&*+=|?:<>/-]', Operator), |
|
1607 (r'(\[)(Compact|Immutable|(?:Boolean|Simple)Type)(\])', |
|
1608 bygroups(Punctuation, Name.Decorator, Punctuation)), |
|
1609 # TODO: "correctly" parse complex code attributes |
|
1610 (r'(\[)(CCode|(?:Integer|Floating)Type)', |
|
1611 bygroups(Punctuation, Name.Decorator)), |
|
1612 (r'[()\[\],.]', Punctuation), |
|
1613 (r'(as|base|break|case|catch|construct|continue|default|delete|do|' |
|
1614 r'else|enum|finally|for|foreach|get|if|in|is|lock|new|out|params|' |
|
1615 r'return|set|sizeof|switch|this|throw|try|typeof|while|yield)\b', |
|
1616 Keyword), |
|
1617 (r'(abstract|const|delegate|dynamic|ensures|extern|inline|internal|' |
|
1618 r'override|owned|private|protected|public|ref|requires|signal|' |
|
1619 r'static|throws|unowned|var|virtual|volatile|weak|yields)\b', |
|
1620 Keyword.Declaration), |
|
1621 (r'(namespace|using)(\s+)', bygroups(Keyword.Namespace, Text), |
|
1622 'namespace'), |
|
1623 (r'(class|errordomain|interface|struct)(\s+)', |
|
1624 bygroups(Keyword.Declaration, Text), 'class'), |
|
1625 (r'(\.)([a-zA-Z_][a-zA-Z0-9_]*)', |
|
1626 bygroups(Operator, Name.Attribute)), |
|
1627 # void is an actual keyword, others are in glib-2.0.vapi |
|
1628 (r'(void|bool|char|double|float|int|int8|int16|int32|int64|long|' |
|
1629 r'short|size_t|ssize_t|string|time_t|uchar|uint|uint8|uint16|' |
|
1630 r'uint32|uint64|ulong|unichar|ushort)\b', Keyword.Type), |
|
1631 (r'(true|false|null)\b', Name.Builtin), |
|
1632 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), |
|
1633 ], |
|
1634 'root': [ |
|
1635 include('whitespace'), |
|
1636 ('', Text, 'statement'), |
|
1637 ], |
|
1638 'statement' : [ |
|
1639 include('whitespace'), |
|
1640 include('statements'), |
|
1641 ('[{}]', Punctuation), |
|
1642 (';', Punctuation, '#pop'), |
|
1643 ], |
|
1644 'string': [ |
|
1645 (r'"', String, '#pop'), |
|
1646 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape), |
|
1647 (r'[^\\"\n]+', String), # all other characters |
|
1648 (r'\\\n', String), # line continuation |
|
1649 (r'\\', String), # stray backslash |
|
1650 ], |
|
1651 'if0': [ |
|
1652 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
|
1653 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), |
|
1654 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
|
1655 (r'.*?\n', Comment), |
|
1656 ], |
|
1657 'class': [ |
|
1658 (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop') |
|
1659 ], |
|
1660 'namespace': [ |
|
1661 (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name.Namespace, '#pop') |
|
1662 ], |
|
1663 } |