17 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
17 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
18 Number, Punctuation, Generic, Other, Error |
18 Number, Punctuation, Generic, Other, Error |
19 from pygments import unistring as uni |
19 from pygments import unistring as uni |
20 |
20 |
21 __all__ = ['PythonLexer', 'PythonConsoleLexer', 'PythonTracebackLexer', |
21 __all__ = ['PythonLexer', 'PythonConsoleLexer', 'PythonTracebackLexer', |
22 'Python3Lexer', 'Python3TracebackLexer', 'CythonLexer', |
22 'Python2Lexer', 'Python2TracebackLexer', |
23 'DgLexer', 'NumPyLexer'] |
23 'CythonLexer', 'DgLexer', 'NumPyLexer'] |
24 |
24 |
25 line_re = re.compile('.*?\n') |
25 line_re = re.compile('.*?\n') |
26 |
26 |
27 |
27 |
28 class PythonLexer(RegexLexer): |
28 class PythonLexer(RegexLexer): |
29 """ |
29 """ |
30 For `Python <http://www.python.org>`_ source code. |
30 For `Python <http://www.python.org>`_ source code (version 3.x). |
|
31 |
|
32 .. versionadded:: 0.10 |
|
33 |
|
34 .. versionchanged:: 2.5 |
|
35 This is now the default ``PythonLexer``. It is still available as the |
|
36 alias ``Python3Lexer``. |
31 """ |
37 """ |
32 |
38 |
33 name = 'Python' |
39 name = 'Python' |
34 aliases = ['python', 'py', 'sage'] |
40 aliases = ['python', 'py', 'sage', 'python3', 'py3'] |
35 filenames = ['*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac', '*.sage'] |
41 filenames = [ |
36 mimetypes = ['text/x-python', 'application/x-python'] |
42 '*.py', |
|
43 '*.pyw', |
|
44 # Jython |
|
45 '*.jy', |
|
46 # Sage |
|
47 '*.sage', |
|
48 # SCons |
|
49 '*.sc', |
|
50 'SConstruct', |
|
51 'SConscript', |
|
52 # Skylark/Starlark (used by Bazel, Buck, and Pants) |
|
53 '*.bzl', |
|
54 'BUCK', |
|
55 'BUILD', |
|
56 'BUILD.bazel', |
|
57 'WORKSPACE', |
|
58 # Twisted Application infrastructure |
|
59 '*.tac', |
|
60 ] |
|
61 mimetypes = ['text/x-python', 'application/x-python', |
|
62 'text/x-python3', 'application/x-python3'] |
|
63 |
|
64 flags = re.MULTILINE | re.UNICODE |
|
65 |
|
66 uni_name = "[%s][%s]*" % (uni.xid_start, uni.xid_continue) |
|
67 |
|
68 def innerstring_rules(ttype): |
|
69 return [ |
|
70 # the old style '%s' % (...) string formatting (still valid in Py3) |
|
71 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
|
72 '[hlL]?[E-GXc-giorsaux%]', String.Interpol), |
|
73 # the new style '{}'.format(...) string formatting |
|
74 (r'\{' |
|
75 r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name |
|
76 r'(\![sra])?' # conversion |
|
77 r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?' |
|
78 r'\}', String.Interpol), |
|
79 |
|
80 # backslashes, quotes and formatting signs must be parsed one at a time |
|
81 (r'[^\\\'"%{\n]+', ttype), |
|
82 (r'[\'"\\]', ttype), |
|
83 # unhandled string formatting sign |
|
84 (r'%|(\{{1,2})', ttype) |
|
85 # newlines are an error (use "nl" state) |
|
86 ] |
|
87 |
|
88 def fstring_rules(ttype): |
|
89 return [ |
|
90 # Assuming that a '}' is the closing brace after format specifier. |
|
91 # Sadly, this means that we won't detect syntax error. But it's |
|
92 # more important to parse correct syntax correctly, than to |
|
93 # highlight invalid syntax. |
|
94 (r'\}', String.Interpol), |
|
95 (r'\{', String.Interpol, 'expr-inside-fstring'), |
|
96 # backslashes, quotes and formatting signs must be parsed one at a time |
|
97 (r'[^\\\'"{}\n]+', ttype), |
|
98 (r'[\'"\\]', ttype), |
|
99 # newlines are an error (use "nl" state) |
|
100 ] |
|
101 |
|
102 tokens = { |
|
103 'root': [ |
|
104 (r'\n', Text), |
|
105 (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")', |
|
106 bygroups(Text, String.Affix, String.Doc)), |
|
107 (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')", |
|
108 bygroups(Text, String.Affix, String.Doc)), |
|
109 (r'\A#!.+$', Comment.Hashbang), |
|
110 (r'#.*$', Comment.Single), |
|
111 (r'\\\n', Text), |
|
112 (r'\\', Text), |
|
113 include('keywords'), |
|
114 (r'(def)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'funcname'), |
|
115 (r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'classname'), |
|
116 (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
|
117 'fromimport'), |
|
118 (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
|
119 'import'), |
|
120 include('expr'), |
|
121 ], |
|
122 'expr': [ |
|
123 # raw f-strings |
|
124 ('(?i)(rf|fr)(""")', |
|
125 bygroups(String.Affix, String.Double), 'tdqf'), |
|
126 ("(?i)(rf|fr)(''')", |
|
127 bygroups(String.Affix, String.Single), 'tsqf'), |
|
128 ('(?i)(rf|fr)(")', |
|
129 bygroups(String.Affix, String.Double), 'dqf'), |
|
130 ("(?i)(rf|fr)(')", |
|
131 bygroups(String.Affix, String.Single), 'sqf'), |
|
132 # non-raw f-strings |
|
133 ('([fF])(""")', bygroups(String.Affix, String.Double), |
|
134 combined('fstringescape', 'tdqf')), |
|
135 ("([fF])(''')", bygroups(String.Affix, String.Single), |
|
136 combined('fstringescape', 'tsqf')), |
|
137 ('([fF])(")', bygroups(String.Affix, String.Double), |
|
138 combined('fstringescape', 'dqf')), |
|
139 ("([fF])(')", bygroups(String.Affix, String.Single), |
|
140 combined('fstringescape', 'sqf')), |
|
141 # raw strings |
|
142 ('(?i)(rb|br|r)(""")', |
|
143 bygroups(String.Affix, String.Double), 'tdqs'), |
|
144 ("(?i)(rb|br|r)(''')", |
|
145 bygroups(String.Affix, String.Single), 'tsqs'), |
|
146 ('(?i)(rb|br|r)(")', |
|
147 bygroups(String.Affix, String.Double), 'dqs'), |
|
148 ("(?i)(rb|br|r)(')", |
|
149 bygroups(String.Affix, String.Single), 'sqs'), |
|
150 # non-raw strings |
|
151 ('([uUbB]?)(""")', bygroups(String.Affix, String.Double), |
|
152 combined('stringescape', 'tdqs')), |
|
153 ("([uUbB]?)(''')", bygroups(String.Affix, String.Single), |
|
154 combined('stringescape', 'tsqs')), |
|
155 ('([uUbB]?)(")', bygroups(String.Affix, String.Double), |
|
156 combined('stringescape', 'dqs')), |
|
157 ("([uUbB]?)(')", bygroups(String.Affix, String.Single), |
|
158 combined('stringescape', 'sqs')), |
|
159 (r'[^\S\n]+', Text), |
|
160 (r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator), |
|
161 (r'[]{}:(),;[]', Punctuation), |
|
162 (r'(in|is|and|or|not)\b', Operator.Word), |
|
163 include('expr-keywords'), |
|
164 include('builtins'), |
|
165 include('magicfuncs'), |
|
166 include('magicvars'), |
|
167 include('name'), |
|
168 include('numbers'), |
|
169 ], |
|
170 'expr-inside-fstring': [ |
|
171 (r'[{([]', Punctuation, 'expr-inside-fstring-inner'), |
|
172 # without format specifier |
|
173 (r'(=\s*)?' # debug (https://bugs.python.org/issue36817) |
|
174 r'(\![sraf])?' # conversion |
|
175 r'}', String.Interpol, '#pop'), |
|
176 # with format specifier |
|
177 # we'll catch the remaining '}' in the outer scope |
|
178 (r'(=\s*)?' # debug (https://bugs.python.org/issue36817) |
|
179 r'(\![sraf])?' # conversion |
|
180 r':', String.Interpol, '#pop'), |
|
181 (r'[^\S]+', Text), # allow new lines |
|
182 include('expr'), |
|
183 ], |
|
184 'expr-inside-fstring-inner': [ |
|
185 (r'[{([]', Punctuation, 'expr-inside-fstring-inner'), |
|
186 (r'[])}]', Punctuation, '#pop'), |
|
187 (r'[^\S]+', Text), # allow new lines |
|
188 include('expr'), |
|
189 ], |
|
190 'expr-keywords': [ |
|
191 # Based on https://docs.python.org/3/reference/expressions.html |
|
192 (words(( |
|
193 'async for', 'await', 'else', 'for', 'if', 'lambda', |
|
194 'yield', 'yield from'), suffix=r'\b'), |
|
195 Keyword), |
|
196 (words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant), |
|
197 ], |
|
198 'keywords': [ |
|
199 (words(( |
|
200 'assert', 'async', 'await', 'break', 'continue', 'del', 'elif', |
|
201 'else', 'except', 'finally', 'for', 'global', 'if', 'lambda', |
|
202 'pass', 'raise', 'nonlocal', 'return', 'try', 'while', 'yield', |
|
203 'yield from', 'as', 'with'), suffix=r'\b'), |
|
204 Keyword), |
|
205 (words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant), |
|
206 ], |
|
207 'builtins': [ |
|
208 (words(( |
|
209 '__import__', 'abs', 'all', 'any', 'bin', 'bool', 'bytearray', |
|
210 'bytes', 'chr', 'classmethod', 'cmp', 'compile', 'complex', |
|
211 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'filter', |
|
212 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', |
|
213 'hash', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', |
|
214 'iter', 'len', 'list', 'locals', 'map', 'max', 'memoryview', |
|
215 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', |
|
216 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', |
|
217 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', |
|
218 'type', 'vars', 'zip'), prefix=r'(?<!\.)', suffix=r'\b'), |
|
219 Name.Builtin), |
|
220 (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo), |
|
221 (words(( |
|
222 'ArithmeticError', 'AssertionError', 'AttributeError', |
|
223 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', |
|
224 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', |
|
225 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
|
226 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
|
227 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
|
228 'NotImplementedError', 'OSError', 'OverflowError', |
|
229 'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning', |
|
230 'RuntimeError', 'RuntimeWarning', 'StopIteration', |
|
231 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
|
232 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', |
|
233 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', |
|
234 'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError', |
|
235 'Warning', 'WindowsError', 'ZeroDivisionError', |
|
236 # new builtin exceptions from PEP 3151 |
|
237 'BlockingIOError', 'ChildProcessError', 'ConnectionError', |
|
238 'BrokenPipeError', 'ConnectionAbortedError', 'ConnectionRefusedError', |
|
239 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError', |
|
240 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', |
|
241 'PermissionError', 'ProcessLookupError', 'TimeoutError', |
|
242 # others new in Python 3 |
|
243 'StopAsyncIteration', 'ModuleNotFoundError', 'RecursionError'), |
|
244 prefix=r'(?<!\.)', suffix=r'\b'), |
|
245 Name.Exception), |
|
246 ], |
|
247 'magicfuncs': [ |
|
248 (words(( |
|
249 '__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__', |
|
250 '__and__', '__anext__', '__await__', '__bool__', '__bytes__', |
|
251 '__call__', '__complex__', '__contains__', '__del__', '__delattr__', |
|
252 '__delete__', '__delitem__', '__dir__', '__divmod__', '__enter__', |
|
253 '__eq__', '__exit__', '__float__', '__floordiv__', '__format__', |
|
254 '__ge__', '__get__', '__getattr__', '__getattribute__', |
|
255 '__getitem__', '__gt__', '__hash__', '__iadd__', '__iand__', |
|
256 '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', |
|
257 '__imul__', '__index__', '__init__', '__instancecheck__', |
|
258 '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', |
|
259 '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', |
|
260 '__len__', '__length_hint__', '__lshift__', '__lt__', '__matmul__', |
|
261 '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', |
|
262 '__new__', '__next__', '__or__', '__pos__', '__pow__', |
|
263 '__prepare__', '__radd__', '__rand__', '__rdivmod__', '__repr__', |
|
264 '__reversed__', '__rfloordiv__', '__rlshift__', '__rmatmul__', |
|
265 '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', |
|
266 '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', |
|
267 '__rxor__', '__set__', '__setattr__', '__setitem__', '__str__', |
|
268 '__sub__', '__subclasscheck__', '__truediv__', |
|
269 '__xor__'), suffix=r'\b'), |
|
270 Name.Function.Magic), |
|
271 ], |
|
272 'magicvars': [ |
|
273 (words(( |
|
274 '__annotations__', '__bases__', '__class__', '__closure__', |
|
275 '__code__', '__defaults__', '__dict__', '__doc__', '__file__', |
|
276 '__func__', '__globals__', '__kwdefaults__', '__module__', |
|
277 '__mro__', '__name__', '__objclass__', '__qualname__', |
|
278 '__self__', '__slots__', '__weakref__'), suffix=r'\b'), |
|
279 Name.Variable.Magic), |
|
280 ], |
|
281 'numbers': [ |
|
282 (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)' |
|
283 r'([eE][+-]?\d(?:_?\d)*)?', Number.Float), |
|
284 (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float), |
|
285 (r'0[oO](?:_?[0-7])+', Number.Oct), |
|
286 (r'0[bB](?:_?[01])+', Number.Bin), |
|
287 (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex), |
|
288 (r'\d(?:_?\d)*', Number.Integer), |
|
289 ], |
|
290 'name': [ |
|
291 (r'@' + uni_name, Name.Decorator), |
|
292 (r'@', Operator), # new matrix multiplication operator |
|
293 (uni_name, Name), |
|
294 ], |
|
295 'funcname': [ |
|
296 include('magicfuncs'), |
|
297 (uni_name, Name.Function, '#pop'), |
|
298 default('#pop'), |
|
299 ], |
|
300 'classname': [ |
|
301 (uni_name, Name.Class, '#pop'), |
|
302 ], |
|
303 'import': [ |
|
304 (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)), |
|
305 (r'\.', Name.Namespace), |
|
306 (uni_name, Name.Namespace), |
|
307 (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)), |
|
308 default('#pop') # all else: go back |
|
309 ], |
|
310 'fromimport': [ |
|
311 (r'(\s+)(import)\b', bygroups(Text, Keyword.Namespace), '#pop'), |
|
312 (r'\.', Name.Namespace), |
|
313 # if None occurs here, it's "raise x from None", since None can |
|
314 # never be a module name |
|
315 (r'None\b', Name.Builtin.Pseudo, '#pop'), |
|
316 (uni_name, Name.Namespace), |
|
317 default('#pop'), |
|
318 ], |
|
319 'fstringescape': [ |
|
320 ('{{', String.Escape), |
|
321 ('}}', String.Escape), |
|
322 include('stringescape'), |
|
323 ], |
|
324 'stringescape': [ |
|
325 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
|
326 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
|
327 ], |
|
328 'fstrings-single': fstring_rules(String.Single), |
|
329 'fstrings-double': fstring_rules(String.Double), |
|
330 'strings-single': innerstring_rules(String.Single), |
|
331 'strings-double': innerstring_rules(String.Double), |
|
332 'dqf': [ |
|
333 (r'"', String.Double, '#pop'), |
|
334 (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings |
|
335 include('fstrings-double') |
|
336 ], |
|
337 'sqf': [ |
|
338 (r"'", String.Single, '#pop'), |
|
339 (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings |
|
340 include('fstrings-single') |
|
341 ], |
|
342 'dqs': [ |
|
343 (r'"', String.Double, '#pop'), |
|
344 (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings |
|
345 include('strings-double') |
|
346 ], |
|
347 'sqs': [ |
|
348 (r"'", String.Single, '#pop'), |
|
349 (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings |
|
350 include('strings-single') |
|
351 ], |
|
352 'tdqf': [ |
|
353 (r'"""', String.Double, '#pop'), |
|
354 include('fstrings-double'), |
|
355 (r'\n', String.Double) |
|
356 ], |
|
357 'tsqf': [ |
|
358 (r"'''", String.Single, '#pop'), |
|
359 include('fstrings-single'), |
|
360 (r'\n', String.Single) |
|
361 ], |
|
362 'tdqs': [ |
|
363 (r'"""', String.Double, '#pop'), |
|
364 include('strings-double'), |
|
365 (r'\n', String.Double) |
|
366 ], |
|
367 'tsqs': [ |
|
368 (r"'''", String.Single, '#pop'), |
|
369 include('strings-single'), |
|
370 (r'\n', String.Single) |
|
371 ], |
|
372 } |
|
373 |
|
374 def analyse_text(text): |
|
375 return shebang_matches(text, r'pythonw?(3(\.\d)?)?') |
|
376 |
|
377 |
|
378 Python3Lexer = PythonLexer |
|
379 |
|
380 |
|
381 class Python2Lexer(RegexLexer): |
|
382 """ |
|
383 For `Python 2.x <http://www.python.org>`_ source code. |
|
384 |
|
385 .. versionchanged:: 2.5 |
|
386 This class has been renamed from ``PythonLexer``. ``PythonLexer`` now |
|
387 refers to the Python 3 variant. File name patterns like ``*.py`` have |
|
388 been moved to Python 3 as well. |
|
389 """ |
|
390 |
|
391 name = 'Python 2.x' |
|
392 aliases = ['python2', 'py2'] |
|
393 filenames = [] # now taken over by PythonLexer (3.x) |
|
394 mimetypes = ['text/x-python2', 'application/x-python2'] |
37 |
395 |
38 def innerstring_rules(ttype): |
396 def innerstring_rules(ttype): |
39 return [ |
397 return [ |
40 # the old style '%s' % (...) string formatting |
398 # the old style '%s' % (...) string formatting |
41 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
399 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
236 (r'\n', String.Single) |
594 (r'\n', String.Single) |
237 ], |
595 ], |
238 } |
596 } |
239 |
597 |
240 def analyse_text(text): |
598 def analyse_text(text): |
241 return shebang_matches(text, r'pythonw?(2(\.\d)?)?') or \ |
599 return shebang_matches(text, r'pythonw?2(\.\d)?') or \ |
242 'import ' in text[:1000] |
600 'import ' in text[:1000] |
243 |
|
244 |
|
245 class Python3Lexer(RegexLexer): |
|
246 """ |
|
247 For `Python <http://www.python.org>`_ source code (version 3.0). |
|
248 |
|
249 .. versionadded:: 0.10 |
|
250 """ |
|
251 |
|
252 name = 'Python 3' |
|
253 aliases = ['python3', 'py3'] |
|
254 filenames = [] # Nothing until Python 3 gets widespread |
|
255 mimetypes = ['text/x-python3', 'application/x-python3'] |
|
256 |
|
257 flags = re.MULTILINE | re.UNICODE |
|
258 |
|
259 uni_name = "[%s][%s]*" % (uni.xid_start, uni.xid_continue) |
|
260 |
|
261 def innerstring_rules(ttype): |
|
262 return [ |
|
263 # the old style '%s' % (...) string formatting (still valid in Py3) |
|
264 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
|
265 '[hlL]?[E-GXc-giorsaux%]', String.Interpol), |
|
266 # the new style '{}'.format(...) string formatting |
|
267 (r'\{' |
|
268 r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name |
|
269 r'(\![sra])?' # conversion |
|
270 r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?' |
|
271 r'\}', String.Interpol), |
|
272 |
|
273 # backslashes, quotes and formatting signs must be parsed one at a time |
|
274 (r'[^\\\'"%{\n]+', ttype), |
|
275 (r'[\'"\\]', ttype), |
|
276 # unhandled string formatting sign |
|
277 (r'%|(\{{1,2})', ttype) |
|
278 # newlines are an error (use "nl" state) |
|
279 ] |
|
280 |
|
281 tokens = PythonLexer.tokens.copy() |
|
282 tokens['keywords'] = [ |
|
283 (words(( |
|
284 'assert', 'async', 'await', 'break', 'continue', 'del', 'elif', |
|
285 'else', 'except', 'finally', 'for', 'global', 'if', 'lambda', 'pass', |
|
286 'raise', 'nonlocal', 'return', 'try', 'while', 'yield', 'yield from', |
|
287 'as', 'with'), suffix=r'\b'), |
|
288 Keyword), |
|
289 (words(( |
|
290 'True', 'False', 'None'), suffix=r'\b'), |
|
291 Keyword.Constant), |
|
292 ] |
|
293 tokens['builtins'] = [ |
|
294 (words(( |
|
295 '__import__', 'abs', 'all', 'any', 'bin', 'bool', 'bytearray', 'bytes', |
|
296 'chr', 'classmethod', 'cmp', 'compile', 'complex', 'delattr', 'dict', |
|
297 'dir', 'divmod', 'enumerate', 'eval', 'filter', 'float', 'format', |
|
298 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', |
|
299 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'list', |
|
300 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', |
|
301 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', |
|
302 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', |
|
303 'sum', 'super', 'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)', |
|
304 suffix=r'\b'), |
|
305 Name.Builtin), |
|
306 (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo), |
|
307 (words(( |
|
308 'ArithmeticError', 'AssertionError', 'AttributeError', |
|
309 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', |
|
310 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', |
|
311 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
|
312 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
|
313 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
|
314 'NotImplementedError', 'OSError', 'OverflowError', |
|
315 'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning', |
|
316 'RuntimeError', 'RuntimeWarning', 'StopIteration', |
|
317 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', |
|
318 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', |
|
319 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', |
|
320 'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError', 'Warning', |
|
321 'WindowsError', 'ZeroDivisionError', |
|
322 # new builtin exceptions from PEP 3151 |
|
323 'BlockingIOError', 'ChildProcessError', 'ConnectionError', |
|
324 'BrokenPipeError', 'ConnectionAbortedError', 'ConnectionRefusedError', |
|
325 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError', |
|
326 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', |
|
327 'PermissionError', 'ProcessLookupError', 'TimeoutError'), |
|
328 prefix=r'(?<!\.)', suffix=r'\b'), |
|
329 Name.Exception), |
|
330 ] |
|
331 tokens['magicfuncs'] = [ |
|
332 (words(( |
|
333 '__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__', '__and__', |
|
334 '__anext__', '__await__', '__bool__', '__bytes__', '__call__', |
|
335 '__complex__', '__contains__', '__del__', '__delattr__', '__delete__', |
|
336 '__delitem__', '__dir__', '__divmod__', '__enter__', '__eq__', '__exit__', |
|
337 '__float__', '__floordiv__', '__format__', '__ge__', '__get__', |
|
338 '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', |
|
339 '__iadd__', '__iand__', '__ifloordiv__', '__ilshift__', '__imatmul__', |
|
340 '__imod__', '__import__', '__imul__', '__index__', '__init__', |
|
341 '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__', |
|
342 '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', |
|
343 '__le__', '__len__', '__length_hint__', '__lshift__', '__lt__', |
|
344 '__matmul__', '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', |
|
345 '__new__', '__next__', '__or__', '__pos__', '__pow__', '__prepare__', |
|
346 '__radd__', '__rand__', '__rdivmod__', '__repr__', '__reversed__', |
|
347 '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__', |
|
348 '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', |
|
349 '__rsub__', '__rtruediv__', '__rxor__', '__set__', '__setattr__', |
|
350 '__setitem__', '__str__', '__sub__', '__subclasscheck__', '__truediv__', |
|
351 '__xor__'), suffix=r'\b'), |
|
352 Name.Function.Magic), |
|
353 ] |
|
354 tokens['magicvars'] = [ |
|
355 (words(( |
|
356 '__annotations__', '__bases__', '__class__', '__closure__', '__code__', |
|
357 '__defaults__', '__dict__', '__doc__', '__file__', '__func__', |
|
358 '__globals__', '__kwdefaults__', '__module__', '__mro__', '__name__', |
|
359 '__objclass__', '__qualname__', '__self__', '__slots__', '__weakref__'), |
|
360 suffix=r'\b'), |
|
361 Name.Variable.Magic), |
|
362 ] |
|
363 tokens['numbers'] = [ |
|
364 (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)([eE][+-]?\d(?:_?\d)*)?', Number.Float), |
|
365 (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float), |
|
366 (r'0[oO](?:_?[0-7])+', Number.Oct), |
|
367 (r'0[bB](?:_?[01])+', Number.Bin), |
|
368 (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex), |
|
369 (r'\d(?:_?\d)*', Number.Integer) |
|
370 ] |
|
371 tokens['backtick'] = [] |
|
372 tokens['name'] = [ |
|
373 (r'@\w+', Name.Decorator), |
|
374 (r'@', Operator), # new matrix multiplication operator |
|
375 (uni_name, Name), |
|
376 ] |
|
377 tokens['funcname'] = [ |
|
378 (uni_name, Name.Function, '#pop') |
|
379 ] |
|
380 tokens['classname'] = [ |
|
381 (uni_name, Name.Class, '#pop') |
|
382 ] |
|
383 tokens['import'] = [ |
|
384 (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)), |
|
385 (r'\.', Name.Namespace), |
|
386 (uni_name, Name.Namespace), |
|
387 (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)), |
|
388 default('#pop') # all else: go back |
|
389 ] |
|
390 tokens['fromimport'] = [ |
|
391 (r'(\s+)(import)\b', bygroups(Text, Keyword), '#pop'), |
|
392 (r'\.', Name.Namespace), |
|
393 (uni_name, Name.Namespace), |
|
394 default('#pop'), |
|
395 ] |
|
396 tokens['strings-single'] = innerstring_rules(String.Single) |
|
397 tokens['strings-double'] = innerstring_rules(String.Double) |
|
398 |
|
399 |
|
400 def analyse_text(text): |
|
401 return shebang_matches(text, r'pythonw?3(\.\d)?') |
|
402 |
601 |
403 |
602 |
404 class PythonConsoleLexer(Lexer): |
603 class PythonConsoleLexer(Lexer): |
405 """ |
604 """ |
406 For Python console output or doctests, such as: |
605 For Python console output or doctests, such as: |