15 bygroups, using, default, words, combined, do_insertions, this, line_re
18 Number, Punctuation, Generic, Other, Error, Whitespace
28 For Python source code (version 3.x).
30 .. versionadded:: 0.10
32 .. versionchanged:: 2.5
33 This is now the default ``PythonLexer``. It is still available as the
34 alias ``Python3Lexer``.
38 url =
'http://www.python.org'
39 aliases = [
'python',
'py',
'sage',
'python3',
'py3']
62 mimetypes = [
'text/x-python',
'application/x-python',
63 'text/x-python3',
'application/x-python3']
70 (
r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
74 r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?'
76 r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?'
80 (
r'[^\\\'"%{\n]+', ttype),
83 (
r'%|(\{{1,2})', ttype)
96 (
r'[^\\\'"{}\n]+', ttype),
104 (
r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
106 (
r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
114 (
r'(def)((?:\s|\\\s)+)', bygroups(Keyword, Text),
'funcname'),
115 (
r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text),
'classname'),
126 combined(
'rfstringescape',
'tdqf')),
129 combined(
'rfstringescape',
'tsqf')),
146 (
'(?i)(rb|br|r)(""")',
148 (
"(?i)(rb|br|r)(''')",
175 (
r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator),
176 (
r'[]{}:(),;[]', Punctuation),
184 'expr-inside-fstring': [
185 (
r'[{([]', Punctuation,
'expr-inside-fstring-inner'),
195 (
r'\s+', Whitespace),
198 'expr-inside-fstring-inner': [
199 (
r'[{([]', Punctuation,
'expr-inside-fstring-inner'),
200 (
r'[])}]', Punctuation,
'#pop'),
201 (
r'\s+', Whitespace),
207 'async for',
'await',
'else',
'for',
'if',
'lambda',
208 'yield',
'yield from'), suffix=
r'\b'),
214 'assert',
'async',
'await',
'break',
'continue',
'del',
'elif',
215 'else',
'except',
'finally',
'for',
'global',
'if',
'lambda',
216 'pass',
'raise',
'nonlocal',
'return',
'try',
'while',
'yield',
217 'yield from',
'as',
'with'), suffix=
r'\b'),
226 r'[:,;=^&|@~)\]}]|(?:' +
228 bygroups(Text, Keyword),
'soft-keywords-inner'),
230 'soft-keywords-inner': [
232 (
r'(\s+)([^\n_]*)(_\b)', bygroups(Whitespace, using(this), Keyword)),
237 '__import__',
'abs',
'aiter',
'all',
'any',
'bin',
'bool',
'bytearray',
238 'breakpoint',
'bytes',
'callable',
'chr',
'classmethod',
'compile',
239 'complex',
'delattr',
'dict',
'dir',
'divmod',
'enumerate',
'eval',
240 'filter',
'float',
'format',
'frozenset',
'getattr',
'globals',
241 'hasattr',
'hash',
'hex',
'id',
'input',
'int',
'isinstance',
242 'issubclass',
'iter',
'len',
'list',
'locals',
'map',
'max',
243 'memoryview',
'min',
'next',
'object',
'oct',
'open',
'ord',
'pow',
244 'print',
'property',
'range',
'repr',
'reversed',
'round',
'set',
245 'setattr',
'slice',
'sorted',
'staticmethod',
'str',
'sum',
'super',
246 'tuple',
'type',
'vars',
'zip'), prefix=
r'(?<!\.)', suffix=
r'\b'),
250 'ArithmeticError',
'AssertionError',
'AttributeError',
251 'BaseException',
'BufferError',
'BytesWarning',
'DeprecationWarning',
252 'EOFError',
'EnvironmentError',
'Exception',
'FloatingPointError',
253 'FutureWarning',
'GeneratorExit',
'IOError',
'ImportError',
254 'ImportWarning',
'IndentationError',
'IndexError',
'KeyError',
255 'KeyboardInterrupt',
'LookupError',
'MemoryError',
'NameError',
256 'NotImplementedError',
'OSError',
'OverflowError',
257 'PendingDeprecationWarning',
'ReferenceError',
'ResourceWarning',
258 'RuntimeError',
'RuntimeWarning',
'StopIteration',
259 'SyntaxError',
'SyntaxWarning',
'SystemError',
'SystemExit',
260 'TabError',
'TypeError',
'UnboundLocalError',
'UnicodeDecodeError',
261 'UnicodeEncodeError',
'UnicodeError',
'UnicodeTranslateError',
262 'UnicodeWarning',
'UserWarning',
'ValueError',
'VMSError',
263 'Warning',
'WindowsError',
'ZeroDivisionError',
265 'BlockingIOError',
'ChildProcessError',
'ConnectionError',
266 'BrokenPipeError',
'ConnectionAbortedError',
'ConnectionRefusedError',
267 'ConnectionResetError',
'FileExistsError',
'FileNotFoundError',
268 'InterruptedError',
'IsADirectoryError',
'NotADirectoryError',
269 'PermissionError',
'ProcessLookupError',
'TimeoutError',
271 'StopAsyncIteration',
'ModuleNotFoundError',
'RecursionError',
273 prefix=
r'(?<!\.)', suffix=
r'\b'),
278 '__abs__',
'__add__',
'__aenter__',
'__aexit__',
'__aiter__',
279 '__and__',
'__anext__',
'__await__',
'__bool__',
'__bytes__',
280 '__call__',
'__complex__',
'__contains__',
'__del__',
'__delattr__',
281 '__delete__',
'__delitem__',
'__dir__',
'__divmod__',
'__enter__',
282 '__eq__',
'__exit__',
'__float__',
'__floordiv__',
'__format__',
283 '__ge__',
'__get__',
'__getattr__',
'__getattribute__',
284 '__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__iand__',
285 '__ifloordiv__',
'__ilshift__',
'__imatmul__',
'__imod__',
286 '__imul__',
'__index__',
'__init__',
'__instancecheck__',
287 '__int__',
'__invert__',
'__ior__',
'__ipow__',
'__irshift__',
288 '__isub__',
'__iter__',
'__itruediv__',
'__ixor__',
'__le__',
289 '__len__',
'__length_hint__',
'__lshift__',
'__lt__',
'__matmul__',
290 '__missing__',
'__mod__',
'__mul__',
'__ne__',
'__neg__',
291 '__new__',
'__next__',
'__or__',
'__pos__',
'__pow__',
292 '__prepare__',
'__radd__',
'__rand__',
'__rdivmod__',
'__repr__',
293 '__reversed__',
'__rfloordiv__',
'__rlshift__',
'__rmatmul__',
294 '__rmod__',
'__rmul__',
'__ror__',
'__round__',
'__rpow__',
295 '__rrshift__',
'__rshift__',
'__rsub__',
'__rtruediv__',
296 '__rxor__',
'__set__',
'__setattr__',
'__setitem__',
'__str__',
297 '__sub__',
'__subclasscheck__',
'__truediv__',
298 '__xor__'), suffix=
r'\b'),
303 '__annotations__',
'__bases__',
'__class__',
'__closure__',
304 '__code__',
'__defaults__',
'__dict__',
'__doc__',
'__file__',
305 '__func__',
'__globals__',
'__kwdefaults__',
'__module__',
306 '__mro__',
'__name__',
'__objclass__',
'__qualname__',
307 '__self__',
'__slots__',
'__weakref__'), suffix=
r'\b'),
311 (
r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)'
333 (
r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
336 (
r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),
357 (
r'\\([\\abfnrtv"\']|\n|x[a-fA-F0-9]{2}|[0-7]{1,3})',
String.Escape)
360 (
r'\\(N\{.*?\}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8})',
String.Escape),
410 return shebang_matches(text,
r'pythonw?(3(\.\d)?)?')
or \
411 'import ' in text[:1000]
722 For Python 3.x tracebacks, with support for chained exceptions.
724 .. versionadded:: 1.0
726 .. versionchanged:: 2.5
727 This is now the default ``PythonTracebackLexer``. It is still available
728 as the alias ``Python3TracebackLexer``.
731 name =
'Python Traceback'
732 aliases = [
'pytb',
'py3tb']
733 filenames = [
'*.pytb',
'*.py3tb']
734 mimetypes = [
'text/x-python-traceback',
'text/x-python3-traceback']
740 (
r'^During handling of the above exception, another '
742 (
r'^The above exception was the direct cause of the '
748 (
r'^( File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',
749 bygroups(Text,
Name.Builtin, Text, Number, Text, Name, Whitespace)),
750 (
r'^( File )("[^"]+")(, line )(\d+)(\n)',
751 bygroups(Text,
Name.Builtin, Text, Number, Whitespace)),
753 bygroups(Whitespace, using(PythonLexer), Whitespace),
'markers'),
754 (
r'^([ \t]*)(\.\.\.)(\n)',
755 bygroups(Whitespace, Comment, Whitespace)),
756 (
r'^([^:]+)(: )(.+)(\n)',
758 (
r'^([a-zA-Z_][\w.]*)(:?\n)',
766 (
r'^( {4,})([~^]+)(\n)',
779 For Python tracebacks.
781 .. versionadded:: 0.7
783 .. versionchanged:: 2.5
784 This class has been renamed from ``PythonTracebackLexer``.
785 ``PythonTracebackLexer`` now refers to the Python 3 variant.
788 name =
'Python 2.x Traceback'
790 filenames = [
'*.py2tb']
791 mimetypes = [
'text/x-python2-traceback']
797 (
r'^(\^C)?(Traceback.*\n)',
804 (
r'^( File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',
805 bygroups(Text,
Name.Builtin, Text, Number, Text, Name, Whitespace)),
806 (
r'^( File )("[^"]+")(, line )(\d+)(\n)',
807 bygroups(Text,
Name.Builtin, Text, Number, Whitespace)),
809 bygroups(Text, using(Python2Lexer), Whitespace),
'marker'),
810 (
r'^([ \t]*)(\.\.\.)(\n)',
811 bygroups(Text, Comment, Whitespace)),
812 (
r'^([^:]+)(: )(.+)(\n)',
814 (
r'^([a-zA-Z_]\w*)(:?\n)',
827 For Pyrex and Cython source code.
829 .. versionadded:: 1.1
833 url =
'http://cython.org'
834 aliases = [
'cython',
'pyx',
'pyrex']
835 filenames = [
'*.pyx',
'*.pxd',
'*.pxi']
836 mimetypes = [
'text/x-cython',
'application/x-cython']
841 (
r'^(\s*)("""(?:.|\n)*?""")', bygroups(Whitespace,
String.Doc)),
842 (
r"^(\s*)('''(?:.|\n)*?''')", bygroups(Whitespace,
String.Doc)),
845 (
r'[]{}:(),;[]', Punctuation),
846 (
r'\\\n', Whitespace),
849 (
r'(<)([a-zA-Z0-9.?]+)(>)',
851 (
r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator),
852 (
r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)',
856 (
r'(def|property)(\s+)', bygroups(Keyword, Text),
'funcname'),
857 (
r'(cp?def)(\s+)', bygroups(Keyword, Text),
'cdef'),
859 (
r'(cdef)(:)', bygroups(Keyword, Punctuation)),
860 (
r'(class|struct)(\s+)', bygroups(Keyword, Text),
'classname'),
861 (
r'(from)(\s+)', bygroups(Keyword, Text),
'fromimport'),
862 (
r'(c?import)(\s+)', bygroups(Keyword, Text),
'import'),
865 (
'(?:[rR]|[uU][rR]|[rR][uU])"""', String,
'tdqs'),
866 (
"(?:[rR]|[uU][rR]|[rR][uU])'''", String,
'tsqs'),
867 (
'(?:[rR]|[uU][rR]|[rR][uU])"', String,
'dqs'),
868 (
"(?:[rR]|[uU][rR]|[rR][uU])'", String,
'sqs'),
869 (
'[uU]?"""', String,
combined(
'stringescape',
'tdqs')),
870 (
"[uU]?'''", String,
combined(
'stringescape',
'tsqs')),
871 (
'[uU]?"', String,
combined(
'stringescape',
'dqs')),
872 (
"[uU]?'", String,
combined(
'stringescape',
'sqs')),
878 'assert',
'async',
'await',
'break',
'by',
'continue',
'ctypedef',
'del',
'elif',
879 'else',
'except',
'except?',
'exec',
'finally',
'for',
'fused',
'gil',
880 'global',
'if',
'include',
'lambda',
'nogil',
'pass',
'print',
881 'raise',
'return',
'try',
'while',
'yield',
'as',
'with'), suffix=
r'\b'),
887 '__import__',
'abs',
'all',
'any',
'apply',
'basestring',
'bin',
'bint',
888 'bool',
'buffer',
'bytearray',
'bytes',
'callable',
'chr',
889 'classmethod',
'cmp',
'coerce',
'compile',
'complex',
'delattr',
890 'dict',
'dir',
'divmod',
'enumerate',
'eval',
'execfile',
'exit',
891 'file',
'filter',
'float',
'frozenset',
'getattr',
'globals',
892 'hasattr',
'hash',
'hex',
'id',
'input',
'int',
'intern',
'isinstance',
893 'issubclass',
'iter',
'len',
'list',
'locals',
'long',
'map',
'max',
894 'min',
'next',
'object',
'oct',
'open',
'ord',
'pow',
'property',
'Py_ssize_t',
895 'range',
'raw_input',
'reduce',
'reload',
'repr',
'reversed',
896 'round',
'set',
'setattr',
'slice',
'sorted',
'staticmethod',
897 'str',
'sum',
'super',
'tuple',
'type',
'unichr',
'unicode',
'unsigned',
898 'vars',
'xrange',
'zip'), prefix=
r'(?<!\.)', suffix=
r'\b'),
900 (
r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'
903 'ArithmeticError',
'AssertionError',
'AttributeError',
904 'BaseException',
'DeprecationWarning',
'EOFError',
'EnvironmentError',
905 'Exception',
'FloatingPointError',
'FutureWarning',
'GeneratorExit',
906 'IOError',
'ImportError',
'ImportWarning',
'IndentationError',
907 'IndexError',
'KeyError',
'KeyboardInterrupt',
'LookupError',
908 'MemoryError',
'NameError',
'NotImplemented',
'NotImplementedError',
909 'OSError',
'OverflowError',
'OverflowWarning',
910 'PendingDeprecationWarning',
'ReferenceError',
'RuntimeError',
911 'RuntimeWarning',
'StandardError',
'StopIteration',
'SyntaxError',
912 'SyntaxWarning',
'SystemError',
'SystemExit',
'TabError',
913 'TypeError',
'UnboundLocalError',
'UnicodeDecodeError',
914 'UnicodeEncodeError',
'UnicodeError',
'UnicodeTranslateError',
915 'UnicodeWarning',
'UserWarning',
'ValueError',
'Warning',
916 'ZeroDivisionError'), prefix=
r'(?<!\.)', suffix=
r'\b'),
920 (
r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?',
Number.Float),
931 (
r'[a-zA-Z_]\w*', Name),
938 (
r'(struct|enum|union|class)\b', Keyword),
939 (
r'([a-zA-Z_]\w*)(\s*)(?=[(:#=]|$)',
941 (
r'([a-zA-Z_]\w*)(\s*)(,)',
943 (
r'from\b', Keyword,
'#pop'),
945 (
r':', Punctuation,
'#pop'),
946 (
r'(?=["\'])', Text,
'#pop'),
954 (
r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
956 (
r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),
960 (
r'(\s+)(c?import)\b', bygroups(Text, Keyword),
'#pop'),
966 (
r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
967 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})',
String.Escape)
970 (
r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
972 (
r'[^\\\'"%\n]+', String),
974 (
r'[\'"\\]', String),
983 (
r'"', String,
'#pop'),
988 (
r"'", String,
'#pop'),
993 (
r'"""', String,
'#pop'),
998 (
r"'''", String,
'#pop'),