Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
python.py1199 linesDownload Raw Back to lexers
1"""2    pygments.lexers.python3    ~~~~~~~~~~~~~~~~~~~~~~4 5    Lexers for Python and related languages.6 7    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11import keyword12 13from pygments.lexer import DelegatingLexer, RegexLexer, include, \14    bygroups, using, default, words, combined, this15from pygments.util import get_bool_opt, shebang_matches16from pygments.token import Text, Comment, Operator, Keyword, Name, String, \17    Number, Punctuation, Generic, Other, Error, Whitespace18from pygments import unistring as uni19 20__all__ = ['PythonLexer', 'PythonConsoleLexer', 'PythonTracebackLexer',21           'Python2Lexer', 'Python2TracebackLexer',22           'CythonLexer', 'DgLexer', 'NumPyLexer']23 24 25class PythonLexer(RegexLexer):26    """27    For Python source code (version 3.x).28 29    .. versionchanged:: 2.530       This is now the default ``PythonLexer``.  It is still available as the31       alias ``Python3Lexer``.32    """33 34    name = 'Python'35    url = 'https://www.python.org'36    aliases = ['python', 'py', 'sage', 'python3', 'py3', 'bazel', 'starlark']37    filenames = [38        '*.py',39        '*.pyw',40        # Type stubs41        '*.pyi',42        # Jython43        '*.jy',44        # Sage45        '*.sage',46        # SCons47        '*.sc',48        'SConstruct',49        'SConscript',50        # Skylark/Starlark (used by Bazel, Buck, and Pants)51        '*.bzl',52        'BUCK',53        'BUILD',54        'BUILD.bazel',55        'WORKSPACE',56        # Twisted Application infrastructure57        '*.tac',58    ]59    mimetypes = ['text/x-python', 'application/x-python',60                 'text/x-python3', 'application/x-python3']61    version_added = '0.10'62 63    uni_name = f"[{uni.xid_start}][{uni.xid_continue}]*"64 65    def innerstring_rules(ttype):66        return [67            # the old style '%s' % (...) string formatting (still valid in Py3)68            (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'69             '[hlL]?[E-GXc-giorsaux%]', String.Interpol),70            # the new style '{}'.format(...) string formatting71            (r'\{'72             r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?'  # field name73             r'(\![sra])?'                       # conversion74             r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?'75             r'\}', String.Interpol),76 77            # backslashes, quotes and formatting signs must be parsed one at a time78            (r'[^\\\'"%{\n]+', ttype),79            (r'[\'"\\]', ttype),80            # unhandled string formatting sign81            (r'%|(\{{1,2})', ttype)82            # newlines are an error (use "nl" state)83        ]84 85    def fstring_rules(ttype):86        return [87            # Assuming that a '}' is the closing brace after format specifier.88            # Sadly, this means that we won't detect syntax error. But it's89            # more important to parse correct syntax correctly, than to90            # highlight invalid syntax.91            (r'\}', String.Interpol),92            (r'\{', String.Interpol, 'expr-inside-fstring'),93            # backslashes, quotes and formatting signs must be parsed one at a time94            (r'[^\\\'"{}\n]+', ttype),95            (r'[\'"\\]', ttype),96            # newlines are an error (use "nl" state)97        ]98 99    tokens = {100        'root': [101            (r'\n', Whitespace),102            (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',103             bygroups(Whitespace, String.Affix, String.Doc)),104            (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",105             bygroups(Whitespace, String.Affix, String.Doc)),106            (r'\A#!.+$', Comment.Hashbang),107            (r'#.*$', Comment.Single),108            (r'\\\n', Text),109            (r'\\', Text),110            include('keywords'),111            include('soft-keywords'),112            (r'(def)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'funcname'),113            (r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'classname'),114            (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),115             'fromimport'),116            (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),117             'import'),118            include('expr'),119        ],120        'expr': [121            # raw f-strings122            ('(?i)(rf|fr)(""")',123             bygroups(String.Affix, String.Double),124             combined('rfstringescape', 'tdqf')),125            ("(?i)(rf|fr)(''')",126             bygroups(String.Affix, String.Single),127             combined('rfstringescape', 'tsqf')),128            ('(?i)(rf|fr)(")',129             bygroups(String.Affix, String.Double),130             combined('rfstringescape', 'dqf')),131            ("(?i)(rf|fr)(')",132             bygroups(String.Affix, String.Single),133             combined('rfstringescape', 'sqf')),134            # non-raw f-strings135            ('([fF])(""")', bygroups(String.Affix, String.Double),136             combined('fstringescape', 'tdqf')),137            ("([fF])(''')", bygroups(String.Affix, String.Single),138             combined('fstringescape', 'tsqf')),139            ('([fF])(")', bygroups(String.Affix, String.Double),140             combined('fstringescape', 'dqf')),141            ("([fF])(')", bygroups(String.Affix, String.Single),142             combined('fstringescape', 'sqf')),143            # raw bytes and strings144            ('(?i)(rb|br|r)(""")',145             bygroups(String.Affix, String.Double), 'tdqs'),146            ("(?i)(rb|br|r)(''')",147             bygroups(String.Affix, String.Single), 'tsqs'),148            ('(?i)(rb|br|r)(")',149             bygroups(String.Affix, String.Double), 'dqs'),150            ("(?i)(rb|br|r)(')",151             bygroups(String.Affix, String.Single), 'sqs'),152            # non-raw strings153            ('([uU]?)(""")', bygroups(String.Affix, String.Double),154             combined('stringescape', 'tdqs')),155            ("([uU]?)(''')", bygroups(String.Affix, String.Single),156             combined('stringescape', 'tsqs')),157            ('([uU]?)(")', bygroups(String.Affix, String.Double),158             combined('stringescape', 'dqs')),159            ("([uU]?)(')", bygroups(String.Affix, String.Single),160             combined('stringescape', 'sqs')),161            # non-raw bytes162            ('([bB])(""")', bygroups(String.Affix, String.Double),163             combined('bytesescape', 'tdqs')),164            ("([bB])(''')", bygroups(String.Affix, String.Single),165             combined('bytesescape', 'tsqs')),166            ('([bB])(")', bygroups(String.Affix, String.Double),167             combined('bytesescape', 'dqs')),168            ("([bB])(')", bygroups(String.Affix, String.Single),169             combined('bytesescape', 'sqs')),170 171            (r'[^\S\n]+', Text),172            include('numbers'),173            (r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator),174            (r'[]{}:(),;[]', Punctuation),175            (r'(in|is|and|or|not)\b', Operator.Word),176            include('expr-keywords'),177            include('builtins'),178            include('magicfuncs'),179            include('magicvars'),180            include('name'),181        ],182        'expr-inside-fstring': [183            (r'[{([]', Punctuation, 'expr-inside-fstring-inner'),184            # without format specifier185            (r'(=\s*)?'         # debug (https://bugs.python.org/issue36817)186             r'(\![sraf])?'     # conversion187             r'\}', String.Interpol, '#pop'),188            # with format specifier189            # we'll catch the remaining '}' in the outer scope190            (r'(=\s*)?'         # debug (https://bugs.python.org/issue36817)191             r'(\![sraf])?'     # conversion192             r':', String.Interpol, '#pop'),193            (r'\s+', Whitespace),  # allow new lines194            include('expr'),195        ],196        'expr-inside-fstring-inner': [197            (r'[{([]', Punctuation, 'expr-inside-fstring-inner'),198            (r'[])}]', Punctuation, '#pop'),199            (r'\s+', Whitespace),  # allow new lines200            include('expr'),201        ],202        'expr-keywords': [203            # Based on https://docs.python.org/3/reference/expressions.html204            (words((205                'async for', 'await', 'else', 'for', 'if', 'lambda',206                'yield', 'yield from'), suffix=r'\b'),207             Keyword),208            (words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant),209        ],210        'keywords': [211            (words((212                'assert', 'async', 'await', 'break', 'continue', 'del', 'elif',213                'else', 'except', 'finally', 'for', 'global', 'if', 'lambda',214                'pass', 'raise', 'nonlocal', 'return', 'try', 'while', 'yield',215                'yield from', 'as', 'with'), suffix=r'\b'),216             Keyword),217            (words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant),218        ],219        'soft-keywords': [220            # `match`, `case` and `_` soft keywords221            (r'(^[ \t]*)'              # at beginning of line + possible indentation222             r'(match|case)\b'         # a possible keyword223             r'(?![ \t]*(?:'           # not followed by...224             r'[:,;=^&|@~)\]}]|(?:' +  # characters and keywords that mean this isn't225                                       # pattern matching (but None/True/False is ok)226             r'|'.join(k for k in keyword.kwlist if k[0].islower()) + r')\b))',227             bygroups(Text, Keyword), 'soft-keywords-inner'),228        ],229        'soft-keywords-inner': [230            # optional `_` keyword231            (r'(\s+)([^\n_]*)(_\b)', bygroups(Whitespace, using(this), Keyword)),232            default('#pop')233        ],234        'builtins': [235            (words((236                '__import__', 'abs', 'aiter', 'all', 'any', 'bin', 'bool', 'bytearray',237                'breakpoint', 'bytes', 'callable', 'chr', 'classmethod', 'compile',238                'complex', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval',239                'filter', 'float', 'format', 'frozenset', 'getattr', 'globals',240                'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'isinstance',241                'issubclass', 'iter', 'len', 'list', 'locals', 'map', 'max',242                'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow',243                'print', 'property', 'range', 'repr', 'reversed', 'round', 'set',244                'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',245                'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)', suffix=r'\b'),246             Name.Builtin),247            (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo),248            (words((249                'ArithmeticError', 'AssertionError', 'AttributeError',250                'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning',251                'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError',252                'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',253                'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',254                'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',255                'NotImplementedError', 'OSError', 'OverflowError',256                'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning',257                'RuntimeError', 'RuntimeWarning', 'StopIteration',258                'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',259                'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',260                'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',261                'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError',262                'Warning', 'WindowsError', 'ZeroDivisionError',263                # new builtin exceptions from PEP 3151264                'BlockingIOError', 'ChildProcessError', 'ConnectionError',265                'BrokenPipeError', 'ConnectionAbortedError', 'ConnectionRefusedError',266                'ConnectionResetError', 'FileExistsError', 'FileNotFoundError',267                'InterruptedError', 'IsADirectoryError', 'NotADirectoryError',268                'PermissionError', 'ProcessLookupError', 'TimeoutError',269                # others new in Python 3270                'StopAsyncIteration', 'ModuleNotFoundError', 'RecursionError',271                'EncodingWarning'),272                prefix=r'(?<!\.)', suffix=r'\b'),273             Name.Exception),274        ],275        'magicfuncs': [276            (words((277                '__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__',278                '__and__', '__anext__', '__await__', '__bool__', '__bytes__',279                '__call__', '__complex__', '__contains__', '__del__', '__delattr__',280                '__delete__', '__delitem__', '__dir__', '__divmod__', '__enter__',281                '__eq__', '__exit__', '__float__', '__floordiv__', '__format__',282                '__ge__', '__get__', '__getattr__', '__getattribute__',283                '__getitem__', '__gt__', '__hash__', '__iadd__', '__iand__',284                '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__',285                '__imul__', '__index__', '__init__', '__instancecheck__',286                '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__',287                '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__',288                '__len__', '__length_hint__', '__lshift__', '__lt__', '__matmul__',289                '__missing__', '__mod__', '__mul__', '__ne__', '__neg__',290                '__new__', '__next__', '__or__', '__pos__', '__pow__',291                '__prepare__', '__radd__', '__rand__', '__rdivmod__', '__repr__',292                '__reversed__', '__rfloordiv__', '__rlshift__', '__rmatmul__',293                '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__',294                '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',295                '__rxor__', '__set__', '__setattr__', '__setitem__', '__str__',296                '__sub__', '__subclasscheck__', '__truediv__',297                '__xor__'), suffix=r'\b'),298             Name.Function.Magic),299        ],300        'magicvars': [301            (words((302                '__annotations__', '__bases__', '__class__', '__closure__',303                '__code__', '__defaults__', '__dict__', '__doc__', '__file__',304                '__func__', '__globals__', '__kwdefaults__', '__module__',305                '__mro__', '__name__', '__objclass__', '__qualname__',306                '__self__', '__slots__', '__weakref__'), suffix=r'\b'),307             Name.Variable.Magic),308        ],309        'numbers': [310            (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)'311             r'([eE][+-]?\d(?:_?\d)*)?', Number.Float),312            (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float),313            (r'0[oO](?:_?[0-7])+', Number.Oct),314            (r'0[bB](?:_?[01])+', Number.Bin),315            (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex),316            (r'\d(?:_?\d)*', Number.Integer),317        ],318        'name': [319            (r'@' + uni_name, Name.Decorator),320            (r'@', Operator),  # new matrix multiplication operator321            (uni_name, Name),322        ],323        'funcname': [324            include('magicfuncs'),325            (uni_name, Name.Function, '#pop'),326            default('#pop'),327        ],328        'classname': [329            (uni_name, Name.Class, '#pop'),330        ],331        'import': [332            (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),333            (r'\.', Name.Namespace),334            (uni_name, Name.Namespace),335            (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),336            default('#pop')  # all else: go back337        ],338        'fromimport': [339            (r'(\s+)(import)\b', bygroups(Text, Keyword.Namespace), '#pop'),340            (r'\.', Name.Namespace),341            # if None occurs here, it's "raise x from None", since None can342            # never be a module name343            (r'None\b', Keyword.Constant, '#pop'),344            (uni_name, Name.Namespace),345            default('#pop'),346        ],347        'rfstringescape': [348            (r'\{\{', String.Escape),349            (r'\}\}', String.Escape),350        ],351        'fstringescape': [352            include('rfstringescape'),353            include('stringescape'),354        ],355        'bytesescape': [356            (r'\\([\\abfnrtv"\']|\n|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)357        ],358        'stringescape': [359            (r'\\(N\{.*?\}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8})', String.Escape),360            include('bytesescape')361        ],362        'fstrings-single': fstring_rules(String.Single),363        'fstrings-double': fstring_rules(String.Double),364        'strings-single': innerstring_rules(String.Single),365        'strings-double': innerstring_rules(String.Double),366        'dqf': [367            (r'"', String.Double, '#pop'),368            (r'\\\\|\\"|\\\n', String.Escape),  # included here for raw strings369            include('fstrings-double')370        ],371        'sqf': [372            (r"'", String.Single, '#pop'),373            (r"\\\\|\\'|\\\n", String.Escape),  # included here for raw strings374            include('fstrings-single')375        ],376        'dqs': [377            (r'"', String.Double, '#pop'),378            (r'\\\\|\\"|\\\n', String.Escape),  # included here for raw strings379            include('strings-double')380        ],381        'sqs': [382            (r"'", String.Single, '#pop'),383            (r"\\\\|\\'|\\\n", String.Escape),  # included here for raw strings384            include('strings-single')385        ],386        'tdqf': [387            (r'"""', String.Double, '#pop'),388            include('fstrings-double'),389            (r'\n', String.Double)390        ],391        'tsqf': [392            (r"'''", String.Single, '#pop'),393            include('fstrings-single'),394            (r'\n', String.Single)395        ],396        'tdqs': [397            (r'"""', String.Double, '#pop'),398            include('strings-double'),399            (r'\n', String.Double)400        ],401        'tsqs': [402            (r"'''", String.Single, '#pop'),403            include('strings-single'),404            (r'\n', String.Single)405        ],406    }407 408    def analyse_text(text):409        return shebang_matches(text, r'pythonw?(3(\.\d)?)?') or \410            'import ' in text[:1000]411 412 413Python3Lexer = PythonLexer414 415 416class Python2Lexer(RegexLexer):417    """418    For Python 2.x source code.419 420    .. versionchanged:: 2.5421       This class has been renamed from ``PythonLexer``.  ``PythonLexer`` now422       refers to the Python 3 variant.  File name patterns like ``*.py`` have423       been moved to Python 3 as well.424    """425 426    name = 'Python 2.x'427    url = 'https://www.python.org'428    aliases = ['python2', 'py2']429    filenames = []  # now taken over by PythonLexer (3.x)430    mimetypes = ['text/x-python2', 'application/x-python2']431    version_added = ''432 433    def innerstring_rules(ttype):434        return [435            # the old style '%s' % (...) string formatting436            (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'437             '[hlL]?[E-GXc-giorsux%]', String.Interpol),438            # backslashes, quotes and formatting signs must be parsed one at a time439            (r'[^\\\'"%\n]+', ttype),440            (r'[\'"\\]', ttype),441            # unhandled string formatting sign442            (r'%', ttype),443            # newlines are an error (use "nl" state)444        ]445 446    tokens = {447        'root': [448            (r'\n', Whitespace),449            (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',450             bygroups(Whitespace, String.Affix, String.Doc)),451            (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",452             bygroups(Whitespace, String.Affix, String.Doc)),453            (r'[^\S\n]+', Text),454            (r'\A#!.+$', Comment.Hashbang),455            (r'#.*$', Comment.Single),456            (r'[]{}:(),;[]', Punctuation),457            (r'\\\n', Text),458            (r'\\', Text),459            (r'(in|is|and|or|not)\b', Operator.Word),460            (r'!=|==|<<|>>|[-~+/*%=<>&^|.]', Operator),461            include('keywords'),462            (r'(def)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'funcname'),463            (r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'classname'),464            (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),465             'fromimport'),466            (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),467             'import'),468            include('builtins'),469            include('magicfuncs'),470            include('magicvars'),471            include('backtick'),472            ('([rR]|[uUbB][rR]|[rR][uUbB])(""")',473             bygroups(String.Affix, String.Double), 'tdqs'),474            ("([rR]|[uUbB][rR]|[rR][uUbB])(''')",475             bygroups(String.Affix, String.Single), 'tsqs'),476            ('([rR]|[uUbB][rR]|[rR][uUbB])(")',477             bygroups(String.Affix, String.Double), 'dqs'),478            ("([rR]|[uUbB][rR]|[rR][uUbB])(')",479             bygroups(String.Affix, String.Single), 'sqs'),480            ('([uUbB]?)(""")', bygroups(String.Affix, String.Double),481             combined('stringescape', 'tdqs')),482            ("([uUbB]?)(''')", bygroups(String.Affix, String.Single),483             combined('stringescape', 'tsqs')),484            ('([uUbB]?)(")', bygroups(String.Affix, String.Double),485             combined('stringescape', 'dqs')),486            ("([uUbB]?)(')", bygroups(String.Affix, String.Single),487             combined('stringescape', 'sqs')),488            include('name'),489            include('numbers'),490        ],491        'keywords': [492            (words((493                'assert', 'break', 'continue', 'del', 'elif', 'else', 'except',494                'exec', 'finally', 'for', 'global', 'if', 'lambda', 'pass',495                'print', 'raise', 'return', 'try', 'while', 'yield',496                'yield from', 'as', 'with'), suffix=r'\b'),497             Keyword),498        ],499        'builtins': [500            (words((501                '__import__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin',502                'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod',503                'cmp', 'coerce', 'compile', 'complex', 'delattr', 'dict', 'dir', 'divmod',504                'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float',505                'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id',506                'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len',507                'list', 'locals', 'long', 'map', 'max', 'min', 'next', 'object',508                'oct', 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce',509                'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',510                'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type',511                'unichr', 'unicode', 'vars', 'xrange', 'zip'),512                prefix=r'(?<!\.)', suffix=r'\b'),513             Name.Builtin),514            (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls'515             r')\b', Name.Builtin.Pseudo),516            (words((517                'ArithmeticError', 'AssertionError', 'AttributeError',518                'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError',519                'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',520                'IOError', 'ImportError', 'ImportWarning', 'IndentationError',521                'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError',522                'MemoryError', 'NameError',523                'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning',524                'PendingDeprecationWarning', 'ReferenceError',525                'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',526                'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',527                'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',528                'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',529                'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError', 'Warning',530                'WindowsError', 'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'),531             Name.Exception),532        ],533        'magicfuncs': [534            (words((535                '__abs__', '__add__', '__and__', '__call__', '__cmp__', '__coerce__',536                '__complex__', '__contains__', '__del__', '__delattr__', '__delete__',537                '__delitem__', '__delslice__', '__div__', '__divmod__', '__enter__',538                '__eq__', '__exit__', '__float__', '__floordiv__', '__ge__', '__get__',539                '__getattr__', '__getattribute__', '__getitem__', '__getslice__', '__gt__',540                '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__',541                '__ilshift__', '__imod__', '__imul__', '__index__', '__init__',542                '__instancecheck__', '__int__', '__invert__', '__iop__', '__ior__',543                '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__',544                '__ixor__', '__le__', '__len__', '__long__', '__lshift__', '__lt__',545                '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__',546                '__nonzero__', '__oct__', '__op__', '__or__', '__pos__', '__pow__',547                '__radd__', '__rand__', '__rcmp__', '__rdiv__', '__rdivmod__', '__repr__',548                '__reversed__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',549                '__rop__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',550                '__rtruediv__', '__rxor__', '__set__', '__setattr__', '__setitem__',551                '__setslice__', '__str__', '__sub__', '__subclasscheck__', '__truediv__',552                '__unicode__', '__xor__'), suffix=r'\b'),553             Name.Function.Magic),554        ],555        'magicvars': [556            (words((557                '__bases__', '__class__', '__closure__', '__code__', '__defaults__',558                '__dict__', '__doc__', '__file__', '__func__', '__globals__',559                '__metaclass__', '__module__', '__mro__', '__name__', '__self__',560                '__slots__', '__weakref__'),561                suffix=r'\b'),562             Name.Variable.Magic),563        ],564        'numbers': [565            (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),566            (r'\d+[eE][+-]?[0-9]+j?', Number.Float),567            (r'0[0-7]+j?', Number.Oct),568            (r'0[bB][01]+', Number.Bin),569            (r'0[xX][a-fA-F0-9]+', Number.Hex),570            (r'\d+L', Number.Integer.Long),571            (r'\d+j?', Number.Integer)572        ],573        'backtick': [574            ('`.*?`', String.Backtick),575        ],576        'name': [577            (r'@[\w.]+', Name.Decorator),578            (r'[a-zA-Z_]\w*', Name),579        ],580        'funcname': [581            include('magicfuncs'),582            (r'[a-zA-Z_]\w*', Name.Function, '#pop'),583            default('#pop'),584        ],585        'classname': [586            (r'[a-zA-Z_]\w*', Name.Class, '#pop')587        ],588        'import': [589            (r'(?:[ \t]|\\\n)+', Text),590            (r'as\b', Keyword.Namespace),591            (r',', Operator),592            (r'[a-zA-Z_][\w.]*', Name.Namespace),593            default('#pop')  # all else: go back594        ],595        'fromimport': [596            (r'(?:[ \t]|\\\n)+', Text),597            (r'import\b', Keyword.Namespace, '#pop'),598            # if None occurs here, it's "raise x from None", since None can599            # never be a module name600            (r'None\b', Name.Builtin.Pseudo, '#pop'),601            # sadly, in "raise x from y" y will be highlighted as namespace too602            (r'[a-zA-Z_.][\w.]*', Name.Namespace),603            # anything else here also means "raise x from y" and is therefore604            # not an error605            default('#pop'),606        ],607        'stringescape': [608            (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'609             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)610        ],611        'strings-single': innerstring_rules(String.Single),612        'strings-double': innerstring_rules(String.Double),613        'dqs': [614            (r'"', String.Double, '#pop'),615            (r'\\\\|\\"|\\\n', String.Escape),  # included here for raw strings616            include('strings-double')617        ],618        'sqs': [619            (r"'", String.Single, '#pop'),620            (r"\\\\|\\'|\\\n", String.Escape),  # included here for raw strings621            include('strings-single')622        ],623        'tdqs': [624            (r'"""', String.Double, '#pop'),625            include('strings-double'),626            (r'\n', String.Double)627        ],628        'tsqs': [629            (r"'''", String.Single, '#pop'),630            include('strings-single'),631            (r'\n', String.Single)632        ],633    }634 635    def analyse_text(text):636        return shebang_matches(text, r'pythonw?2(\.\d)?')637 638class _PythonConsoleLexerBase(RegexLexer):639    name = 'Python console session'640    aliases = ['pycon', 'python-console']641    mimetypes = ['text/x-python-doctest']642 643    """Auxiliary lexer for `PythonConsoleLexer`.644 645    Code tokens are output as ``Token.Other.Code``, traceback tokens as646    ``Token.Other.Traceback``.647    """648    tokens = {649        'root': [650            (r'(>>> )(.*\n)', bygroups(Generic.Prompt, Other.Code), 'continuations'),651            # This happens, e.g., when tracebacks are embedded in documentation;652            # trailing whitespaces are often stripped in such contexts.653            (r'(>>>)(\n)', bygroups(Generic.Prompt, Whitespace)),654            (r'(\^C)?Traceback \(most recent call last\):\n', Other.Traceback, 'traceback'),655            # SyntaxError starts with this656            (r'  File "[^"]+", line \d+', Other.Traceback, 'traceback'),657            (r'.*\n', Generic.Output),658        ],659        'continuations': [660            (r'(\.\.\. )(.*\n)', bygroups(Generic.Prompt, Other.Code)),661            # See above.662            (r'(\.\.\.)(\n)', bygroups(Generic.Prompt, Whitespace)),663            default('#pop'),664        ],665        'traceback': [666            # As soon as we see a traceback, consume everything until the next667            # >>> prompt.668            (r'(?=>>>( |$))', Text, '#pop'),669            (r'(KeyboardInterrupt)(\n)', bygroups(Name.Class, Whitespace)),670            (r'.*\n', Other.Traceback),671        ],672    }673 674class PythonConsoleLexer(DelegatingLexer):675    """676    For Python console output or doctests, such as:677 678    .. sourcecode:: pycon679 680        >>> a = 'foo'681        >>> print(a)682        foo683        >>> 1 / 0684        Traceback (most recent call last):685          File "<stdin>", line 1, in <module>686        ZeroDivisionError: integer division or modulo by zero687 688    Additional options:689 690    `python3`691        Use Python 3 lexer for code.  Default is ``True``.692 693        .. versionadded:: 1.0694        .. versionchanged:: 2.5695           Now defaults to ``True``.696    """697 698    name = 'Python console session'699    aliases = ['pycon', 'python-console']700    mimetypes = ['text/x-python-doctest']701    url = 'https://python.org'702    version_added = ''703 704    def __init__(self, **options):705        python3 = get_bool_opt(options, 'python3', True)706        if python3:707            pylexer = PythonLexer708            tblexer = PythonTracebackLexer709        else:710            pylexer = Python2Lexer711            tblexer = Python2TracebackLexer712        # We have two auxiliary lexers. Use DelegatingLexer twice with713        # different tokens.  TODO: DelegatingLexer should support this714        # directly, by accepting a tuplet of auxiliary lexers and a tuple of715        # distinguishing tokens. Then we wouldn't need this intermediary716        # class.717        class _ReplaceInnerCode(DelegatingLexer):718            def __init__(self, **options):719                super().__init__(pylexer, _PythonConsoleLexerBase, Other.Code, **options)720        super().__init__(tblexer, _ReplaceInnerCode, Other.Traceback, **options)721 722class PythonTracebackLexer(RegexLexer):723    """724    For Python 3.x tracebacks, with support for chained exceptions.725 726    .. versionchanged:: 2.5727       This is now the default ``PythonTracebackLexer``.  It is still available728       as the alias ``Python3TracebackLexer``.729    """730 731    name = 'Python Traceback'732    aliases = ['pytb', 'py3tb']733    filenames = ['*.pytb', '*.py3tb']734    mimetypes = ['text/x-python-traceback', 'text/x-python3-traceback']735    url = 'https://python.org'736    version_added = '1.0'737 738    tokens = {739        'root': [740            (r'\n', Whitespace),741            (r'^(\^C)?Traceback \(most recent call last\):\n', Generic.Traceback, 'intb'),742            (r'^During handling of the above exception, another '743             r'exception occurred:\n\n', Generic.Traceback),744            (r'^The above exception was the direct cause of the '745             r'following exception:\n\n', Generic.Traceback),746            (r'^(?=  File "[^"]+", line \d+)', Generic.Traceback, 'intb'),747            (r'^.*\n', Other),748        ],749        'intb': [750            (r'^(  File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',751             bygroups(Text, Name.Builtin, Text, Number, Text, Name, Whitespace)),752            (r'^(  File )("[^"]+")(, line )(\d+)(\n)',753             bygroups(Text, Name.Builtin, Text, Number, Whitespace)),754            (r'^(    )(.+)(\n)',755             bygroups(Whitespace, using(PythonLexer), Whitespace), 'markers'),756            (r'^([ \t]*)(\.\.\.)(\n)',757             bygroups(Whitespace, Comment, Whitespace)),  # for doctests...758            (r'^([^:]+)(: )(.+)(\n)',759             bygroups(Generic.Error, Text, Name, Whitespace), '#pop'),760            (r'^([a-zA-Z_][\w.]*)(:?\n)',761             bygroups(Generic.Error, Whitespace), '#pop'),762            default('#pop'),763        ],764        'markers': [765            # Either `PEP 657 <https://www.python.org/dev/peps/pep-0657/>`766            # error locations in Python 3.11+, or single-caret markers767            # for syntax errors before that.768            (r'^( {4,})([~^]+)(\n)',769             bygroups(Whitespace, Punctuation.Marker, Whitespace),770             '#pop'),771            default('#pop'),772        ],773    }774 775 776Python3TracebackLexer = PythonTracebackLexer777 778 779class Python2TracebackLexer(RegexLexer):780    """781    For Python tracebacks.782 783    .. versionchanged:: 2.5784       This class has been renamed from ``PythonTracebackLexer``.785       ``PythonTracebackLexer`` now refers to the Python 3 variant.786    """787 788    name = 'Python 2.x Traceback'789    aliases = ['py2tb']790    filenames = ['*.py2tb']791    mimetypes = ['text/x-python2-traceback']792    url = 'https://python.org'793    version_added = '0.7'794 795    tokens = {796        'root': [797            # Cover both (most recent call last) and (innermost last)798            # The optional ^C allows us to catch keyboard interrupt signals.799            (r'^(\^C)?(Traceback.*\n)',800             bygroups(Text, Generic.Traceback), 'intb'),801            # SyntaxError starts with this.802            (r'^(?=  File "[^"]+", line \d+)', Generic.Traceback, 'intb'),803            (r'^.*\n', Other),804        ],805        'intb': [806            (r'^(  File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',807             bygroups(Text, Name.Builtin, Text, Number, Text, Name, Whitespace)),808            (r'^(  File )("[^"]+")(, line )(\d+)(\n)',809             bygroups(Text, Name.Builtin, Text, Number, Whitespace)),810            (r'^(    )(.+)(\n)',811             bygroups(Text, using(Python2Lexer), Whitespace), 'marker'),812            (r'^([ \t]*)(\.\.\.)(\n)',813             bygroups(Text, Comment, Whitespace)),  # for doctests...814            (r'^([^:]+)(: )(.+)(\n)',815             bygroups(Generic.Error, Text, Name, Whitespace), '#pop'),816            (r'^([a-zA-Z_]\w*)(:?\n)',817             bygroups(Generic.Error, Whitespace), '#pop')818        ],819        'marker': [820            # For syntax errors.821            (r'( {4,})(\^)', bygroups(Text, Punctuation.Marker), '#pop'),822            default('#pop'),823        ],824    }825 826 827class CythonLexer(RegexLexer):828    """829    For Pyrex and Cython source code.830    """831 832    name = 'Cython'833    url = 'https://cython.org'834    aliases = ['cython', 'pyx', 'pyrex']835    filenames = ['*.pyx', '*.pxd', '*.pxi']836    mimetypes = ['text/x-cython', 'application/x-cython']837    version_added = '1.1'838 839    tokens = {840        'root': [841            (r'\n', Whitespace),842            (r'^(\s*)("""(?:.|\n)*?""")', bygroups(Whitespace, String.Doc)),843            (r"^(\s*)('''(?:.|\n)*?''')", bygroups(Whitespace, String.Doc)),844            (r'[^\S\n]+', Text),845            (r'#.*$', Comment),846            (r'[]{}:(),;[]', Punctuation),847            (r'\\\n', Whitespace),848            (r'\\', Text),849            (r'(in|is|and|or|not)\b', Operator.Word),850            (r'(<)([a-zA-Z0-9.?]+)(>)',851             bygroups(Punctuation, Keyword.Type, Punctuation)),852            (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator),853            (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)',854             bygroups(Keyword, Number.Integer, Operator, Name, Operator,855                      Name, Punctuation)),856            include('keywords'),857            (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'),858            (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'),859            # (should actually start a block with only cdefs)860            (r'(cdef)(:)', bygroups(Keyword, Punctuation)),861            (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'),862            (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'),863            (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'),864            include('builtins'),865            include('backtick'),866            ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'),867            ("(?:[rR]|[uU][rR]|[rR][uU])'''", String, 'tsqs'),868            ('(?:[rR]|[uU][rR]|[rR][uU])"', String, 'dqs'),869            ("(?:[rR]|[uU][rR]|[rR][uU])'", String, 'sqs'),870            ('[uU]?"""', String, combined('stringescape', 'tdqs')),871            ("[uU]?'''", String, combined('stringescape', 'tsqs')),872            ('[uU]?"', String, combined('stringescape', 'dqs')),873            ("[uU]?'", String, combined('stringescape', 'sqs')),874            include('name'),875            include('numbers'),876        ],877        'keywords': [878            (words((879                'assert', 'async', 'await', 'break', 'by', 'continue', 'ctypedef', 'del', 'elif',880                'else', 'except', 'except?', 'exec', 'finally', 'for', 'fused', 'gil',881                'global', 'if', 'include', 'lambda', 'nogil', 'pass', 'print',882                'raise', 'return', 'try', 'while', 'yield', 'as', 'with'), suffix=r'\b'),883             Keyword),884            (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc),885        ],886        'builtins': [887            (words((888                '__import__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bint',889                'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr',890                'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'delattr',891                'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit',892                'file', 'filter', 'float', 'frozenset', 'getattr', 'globals',893                'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'intern', 'isinstance',894                'issubclass', 'iter', 'len', 'list', 'locals', 'long', 'map', 'max',895                'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'Py_ssize_t',896                'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed',897                'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod',898                'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'unsigned',899                'vars', 'xrange', 'zip'), prefix=r'(?<!\.)', suffix=r'\b'),900             Name.Builtin),901            (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'902             r')\b', Name.Builtin.Pseudo),903            (words((904                'ArithmeticError', 'AssertionError', 'AttributeError',905                'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError',906                'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',907                'IOError', 'ImportError', 'ImportWarning', 'IndentationError',908                'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError',909                'MemoryError', 'NameError', 'NotImplemented', 'NotImplementedError',910                'OSError', 'OverflowError', 'OverflowWarning',911                'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',912                'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError',913                'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',914                'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',915                'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',916                'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',917                'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'),918             Name.Exception),919        ],920        'numbers': [921            (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),922            (r'0\d+', Number.Oct),923            (r'0[xX][a-fA-F0-9]+', Number.Hex),924            (r'\d+L', Number.Integer.Long),925            (r'\d+', Number.Integer)926        ],927        'backtick': [928            ('`.*?`', String.Backtick),929        ],930        'name': [931            (r'@\w+', Name.Decorator),932            (r'[a-zA-Z_]\w*', Name),933        ],934        'funcname': [935            (r'[a-zA-Z_]\w*', Name.Function, '#pop')936        ],937        'cdef': [938            (r'(public|readonly|extern|api|inline)\b', Keyword.Reserved),939            (r'(struct|enum|union|class)\b', Keyword),940            (r'([a-zA-Z_]\w*)(\s*)(?=[(:#=]|$)',941             bygroups(Name.Function, Text), '#pop'),942            (r'([a-zA-Z_]\w*)(\s*)(,)',943             bygroups(Name.Function, Text, Punctuation)),944            (r'from\b', Keyword, '#pop'),945            (r'as\b', Keyword),946            (r':', Punctuation, '#pop'),947            (r'(?=["\'])', Text, '#pop'),948            (r'[a-zA-Z_]\w*', Keyword.Type),949            (r'.', Text),950        ],951        'classname': [952            (r'[a-zA-Z_]\w*', Name.Class, '#pop')953        ],954        'import': [955            (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),956            (r'[a-zA-Z_][\w.]*', Name.Namespace),957            (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),958            default('#pop')  # all else: go back959        ],960        'fromimport': [961            (r'(\s+)(c?import)\b', bygroups(Text, Keyword), '#pop'),962            (r'[a-zA-Z_.][\w.]*', Name.Namespace),963            # ``cdef foo from "header"``, or ``for foo from 0 < i < 10``964            default('#pop'),965        ],966        'stringescape': [967            (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'968             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)969        ],970        'strings': [971            (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'972             '[hlL]?[E-GXc-giorsux%]', String.Interpol),973            (r'[^\\\'"%\n]+', String),974            # quotes, percents and backslashes must be parsed one at a time975            (r'[\'"\\]', String),976            # unhandled string formatting sign977            (r'%', String)978            # newlines are an error (use "nl" state)979        ],980        'nl': [981            (r'\n', String)982        ],983        'dqs': [984            (r'"', String, '#pop'),985            (r'\\\\|\\"|\\\n', String.Escape),  # included here again for raw strings986            include('strings')987        ],988        'sqs': [989            (r"'", String, '#pop'),990            (r"\\\\|\\'|\\\n", String.Escape),  # included here again for raw strings991            include('strings')992        ],993        'tdqs': [994            (r'"""', String, '#pop'),995            include('strings'),996            include('nl')997        ],998        'tsqs': [999            (r"'''", String, '#pop'),1000            include('strings'),1001            include('nl')1002        ],1003    }1004 1005 1006class DgLexer(RegexLexer):1007    """1008    Lexer for dg,1009    a functional and object-oriented programming language1010    running on the CPython 3 VM.1011    """1012    name = 'dg'1013    aliases = ['dg']1014    filenames = ['*.dg']1015    mimetypes = ['text/x-dg']1016    url = 'http://pyos.github.io/dg'1017    version_added = '1.6'1018 1019    tokens = {1020        'root': [1021            (r'\s+', Text),1022            (r'#.*?$', Comment.Single),1023 1024            (r'(?i)0b[01]+', Number.Bin),1025            (r'(?i)0o[0-7]+', Number.Oct),1026            (r'(?i)0x[0-9a-f]+', Number.Hex),1027            (r'(?i)[+-]?[0-9]+\.[0-9]+(e[+-]?[0-9]+)?j?', Number.Float),1028            (r'(?i)[+-]?[0-9]+e[+-]?\d+j?', Number.Float),1029            (r'(?i)[+-]?[0-9]+j?', Number.Integer),1030 1031            (r"(?i)(br|r?b?)'''", String, combined('stringescape', 'tsqs', 'string')),1032            (r'(?i)(br|r?b?)"""', String, combined('stringescape', 'tdqs', 'string')),1033            (r"(?i)(br|r?b?)'", String, combined('stringescape', 'sqs', 'string')),1034            (r'(?i)(br|r?b?)"', String, combined('stringescape', 'dqs', 'string')),1035 1036            (r"`\w+'*`", Operator),1037            (r'\b(and|in|is|or|where)\b', Operator.Word),1038            (r'[!$%&*+\-./:<-@\\^|~;,]+', Operator),1039 1040            (words((1041                'bool', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'dict\'',1042                'float', 'frozenset', 'int', 'list', 'list\'', 'memoryview', 'object',1043                'property', 'range', 'set', 'set\'', 'slice', 'staticmethod', 'str',1044                'super', 'tuple', 'tuple\'', 'type'),1045                   prefix=r'(?<!\.)', suffix=r'(?![\'\w])'),1046             Name.Builtin),1047            (words((1048                '__import__', 'abs', 'all', 'any', 'bin', 'bind', 'chr', 'cmp', 'compile',1049                'complex', 'delattr', 'dir', 'divmod', 'drop', 'dropwhile', 'enumerate',1050                'eval', 'exhaust', 'filter', 'flip', 'foldl1?', 'format', 'fst',1051                'getattr', 'globals', 'hasattr', 'hash', 'head', 'hex', 'id', 'init',1052                'input', 'isinstance', 'issubclass', 'iter', 'iterate', 'last', 'len',1053                'locals', 'map', 'max', 'min', 'next', 'oct', 'open', 'ord', 'pow',1054                'print', 'repr', 'reversed', 'round', 'setattr', 'scanl1?', 'snd',1055                'sorted', 'sum', 'tail', 'take', 'takewhile', 'vars', 'zip'),1056                   prefix=r'(?<!\.)', suffix=r'(?![\'\w])'),1057             Name.Builtin),1058            (r"(?<!\.)(self|Ellipsis|NotImplemented|None|True|False)(?!['\w])",1059             Name.Builtin.Pseudo),1060 1061            (r"(?<!\.)[A-Z]\w*(Error|Exception|Warning)'*(?!['\w])",1062             Name.Exception),1063            (r"(?<!\.)(Exception|GeneratorExit|KeyboardInterrupt|StopIteration|"1064             r"SystemExit)(?!['\w])", Name.Exception),1065 1066            (r"(?<![\w.])(except|finally|for|if|import|not|otherwise|raise|"1067             r"subclass|while|with|yield)(?!['\w])", Keyword.Reserved),1068 1069            (r"[A-Z_]+'*(?!['\w])", Name),1070            (r"[A-Z]\w+'*(?!['\w])", Keyword.Type),1071            (r"\w+'*", Name),1072 1073            (r'[()]', Punctuation),1074            (r'.', Error),1075        ],1076        'stringescape': [1077            (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'1078             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)1079        ],1080        'string': [1081            (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'1082             '[hlL]?[E-GXc-giorsux%]', String.Interpol),1083            (r'[^\\\'"%\n]+', String),1084            # quotes, percents and backslashes must be parsed one at a time1085            (r'[\'"\\]', String),1086            # unhandled string formatting sign1087            (r'%', String),1088            (r'\n', String)1089        ],1090        'dqs': [1091            (r'"', String, '#pop')1092        ],1093        'sqs': [1094            (r"'", String, '#pop')1095        ],1096        'tdqs': [1097            (r'"""', String, '#pop')1098        ],1099        'tsqs': [1100            (r"'''", String, '#pop')1101        ],1102    }1103 1104 1105class NumPyLexer(PythonLexer):1106    """1107    A Python lexer recognizing Numerical Python builtins.1108    """1109 1110    name = 'NumPy'1111    url = 'https://numpy.org/'1112    aliases = ['numpy']1113    version_added = '0.10'1114 1115    # override the mimetypes to not inherit them from python1116    mimetypes = []1117    filenames = []1118 1119    EXTRA_KEYWORDS = {1120        'abs', 'absolute', 'accumulate', 'add', 'alen', 'all', 'allclose',1121        'alltrue', 'alterdot', 'amax', 'amin', 'angle', 'any', 'append',1122        'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh',1123        'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin',1124        'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal',1125        'array_equiv', 'array_repr', 'array_split', 'array_str', 'arrayrange',1126        'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray',1127        'asfarray', 'asfortranarray', 'asmatrix', 'asscalar', 'astype',1128        'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett',1129        'base_repr', 'beta', 'binary_repr', 'bincount', 'binomial',1130        'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman',1131        'bmat', 'broadcast', 'byte_bounds', 'bytes', 'byteswap', 'c_',1132        'can_cast', 'ceil', 'choose', 'clip', 'column_stack', 'common_type',1133        'compare_chararrays', 'compress', 'concatenate', 'conj', 'conjugate',1134        'convolve', 'copy', 'corrcoef', 'correlate', 'cos', 'cosh', 'cov',1135        'cross', 'cumprod', 'cumproduct', 'cumsum', 'delete', 'deprecate',1136        'diag', 'diagflat', 'diagonal', 'diff', 'digitize', 'disp', 'divide',1137        'dot', 'dsplit', 'dstack', 'dtype', 'dump', 'dumps', 'ediff1d', 'empty',1138        'empty_like', 'equal', 'exp', 'expand_dims', 'expm1', 'extract', 'eye',1139        'fabs', 'fastCopyAndTranspose', 'fft', 'fftfreq', 'fftshift', 'fill',1140        'finfo', 'fix', 'flat', 'flatnonzero', 'flatten', 'fliplr', 'flipud',1141        'floor', 'floor_divide', 'fmod', 'frexp', 'fromarrays', 'frombuffer',1142        'fromfile', 'fromfunction', 'fromiter', 'frompyfunc', 'fromstring',1143        'generic', 'get_array_wrap', 'get_include', 'get_numarray_include',1144        'get_numpy_include', 'get_printoptions', 'getbuffer', 'getbufsize',1145        'geterr', 'geterrcall', 'geterrobj', 'getfield', 'gradient', 'greater',1146        'greater_equal', 'gumbel', 'hamming', 'hanning', 'histogram',1147        'histogram2d', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0',1148        'identity', 'ifft', 'imag', 'index_exp', 'indices', 'inf', 'info',1149        'inner', 'insert', 'int_asbuffer', 'interp', 'intersect1d',1150        'intersect1d_nu', 'inv', 'invert', 'iscomplex', 'iscomplexobj',1151        'isfinite', 'isfortran', 'isinf', 'isnan', 'isneginf', 'isposinf',1152        'isreal', 'isrealobj', 'isscalar', 'issctype', 'issubclass_',1153        'issubdtype', 'issubsctype', 'item', 'itemset', 'iterable', 'ix_',1154        'kaiser', 'kron', 'ldexp', 'left_shift', 'less', 'less_equal', 'lexsort',1155        'linspace', 'load', 'loads', 'loadtxt', 'log', 'log10', 'log1p', 'log2',1156        'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'logspace',1157        'lstsq', 'mat', 'matrix', 'max', 'maximum', 'maximum_sctype',1158        'may_share_memory', 'mean', 'median', 'meshgrid', 'mgrid', 'min',1159        'minimum', 'mintypecode', 'mod', 'modf', 'msort', 'multiply', 'nan',1160        'nan_to_num', 'nanargmax', 'nanargmin', 'nanmax', 'nanmin', 'nansum',1161        'ndenumerate', 'ndim', 'ndindex', 'negative', 'newaxis', 'newbuffer',1162        'newbyteorder', 'nonzero', 'not_equal', 'obj2sctype', 'ogrid', 'ones',1163        'ones_like', 'outer', 'permutation', 'piecewise', 'pinv', 'pkgload',1164        'place', 'poisson', 'poly', 'poly1d', 'polyadd', 'polyder', 'polydiv',1165        'polyfit', 'polyint', 'polymul', 'polysub', 'polyval', 'power', 'prod',1166        'product', 'ptp', 'put', 'putmask', 'r_', 'randint', 'random_integers',1167        'random_sample', 'ranf', 'rank', 'ravel', 'real', 'real_if_close',1168        'recarray', 'reciprocal', 'reduce', 'remainder', 'repeat', 'require',1169        'reshape', 'resize', 'restoredot', 'right_shift', 'rint', 'roll',1170        'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack', 's_',1171        'sample', 'savetxt', 'sctype2char', 'searchsorted', 'seed', 'select',1172        'set_numeric_ops', 'set_printoptions', 'set_string_function',1173        'setbufsize', 'setdiff1d', 'seterr', 'seterrcall', 'seterrobj',1174        'setfield', 'setflags', 'setmember1d', 'setxor1d', 'shape',1175        'show_config', 'shuffle', 'sign', 'signbit', 'sin', 'sinc', 'sinh',1176        'size', 'slice', 'solve', 'sometrue', 'sort', 'sort_complex', 'source',1177        'split', 'sqrt', 'square', 'squeeze', 'standard_normal', 'std',1178        'subtract', 'sum', 'svd', 'swapaxes', 'take', 'tan', 'tanh', 'tensordot',1179        'test', 'tile', 'tofile', 'tolist', 'tostring', 'trace', 'transpose',1180        'trapz', 'tri', 'tril', 'trim_zeros', 'triu', 'true_divide', 'typeDict',1181        'typename', 'uniform', 'union1d', 'unique', 'unique1d', 'unravel_index',1182        'unwrap', 'vander', 'var', 'vdot', 'vectorize', 'view', 'vonmises',1183        'vsplit', 'vstack', 'weibull', 'where', 'who', 'zeros', 'zeros_like'1184    }1185 1186    def get_tokens_unprocessed(self, text):1187        for index, token, value in \1188                PythonLexer.get_tokens_unprocessed(self, text):1189            if token is Name and value in self.EXTRA_KEYWORDS:1190                yield index, Keyword.Pseudo, value1191            else:1192                yield index, token, value1193 1194    def analyse_text(text):1195        ltext = text[:1000]1196        return (shebang_matches(text, r'pythonw?(3(\.\d)?)?') or1197                'import ' in ltext) \1198            and ('import numpy' in ltext or 'from numpy import' in ltext)1199 
codekingpro/portable-devtools · Team Ai