|
| 1 | +package p |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/alecthomas/chroma" // nolint |
| 5 | + "github.com/alecthomas/chroma/lexers/internal" |
| 6 | +) |
| 7 | + |
| 8 | +// Python2 lexer. |
| 9 | +var Python2 = internal.Register(MustNewLazyLexer( |
| 10 | + &Config{ |
| 11 | + Name: "Python 2", |
| 12 | + Aliases: []string{"python2", "py2"}, |
| 13 | + Filenames: []string{}, |
| 14 | + MimeTypes: []string{"text/x-python", "application/x-python"}, |
| 15 | + }, |
| 16 | + python2Rules, |
| 17 | +)) |
| 18 | + |
| 19 | +func python2Rules() Rules { |
| 20 | + return Rules{ |
| 21 | + "root": { |
| 22 | + {`\n`, Text, nil}, |
| 23 | + {`^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil}, |
| 24 | + {`^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil}, |
| 25 | + {`[^\S\n]+`, Text, nil}, |
| 26 | + {`\A#!.+$`, CommentHashbang, nil}, |
| 27 | + {`#.*$`, CommentSingle, nil}, |
| 28 | + {`[]{}:(),;[]`, Punctuation, nil}, |
| 29 | + {`\\\n`, Text, nil}, |
| 30 | + {`\\`, Text, nil}, |
| 31 | + {`(in|is|and|or|not)\b`, OperatorWord, nil}, |
| 32 | + {`!=|==|<<|>>|[-~+/*%=<>&^|.]`, Operator, nil}, |
| 33 | + Include("keywords"), |
| 34 | + {`(def)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("funcname")}, |
| 35 | + {`(class)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("classname")}, |
| 36 | + {`(from)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("fromimport")}, |
| 37 | + {`(import)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("import")}, |
| 38 | + Include("builtins"), |
| 39 | + Include("magicfuncs"), |
| 40 | + Include("magicvars"), |
| 41 | + Include("backtick"), |
| 42 | + {`([rR]|[uUbB][rR]|[rR][uUbB])(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("tdqs")}, |
| 43 | + {`([rR]|[uUbB][rR]|[rR][uUbB])(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("tsqs")}, |
| 44 | + {`([rR]|[uUbB][rR]|[rR][uUbB])(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("dqs")}, |
| 45 | + {`([rR]|[uUbB][rR]|[rR][uUbB])(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("sqs")}, |
| 46 | + {`([uUbB]?)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "tdqs")}, |
| 47 | + {`([uUbB]?)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "tsqs")}, |
| 48 | + {`([uUbB]?)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "dqs")}, |
| 49 | + {`([uUbB]?)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "sqs")}, |
| 50 | + Include("name"), |
| 51 | + Include("numbers"), |
| 52 | + }, |
| 53 | + "keywords": { |
| 54 | + {Words(``, `\b`, `assert`, `break`, `continue`, `del`, `elif`, `else`, `except`, `exec`, `finally`, `for`, `global`, `if`, `lambda`, `pass`, `print`, `raise`, `return`, `try`, `while`, `yield`, `yield from`, `as`, `with`), Keyword, nil}, |
| 55 | + }, |
| 56 | + "builtins": { |
| 57 | + {Words(`(?<!\.)`, `\b`, `__import__`, `abs`, `all`, `any`, `apply`, `basestring`, `bin`, `bool`, `buffer`, `bytearray`, `bytes`, `callable`, `chr`, `classmethod`, `cmp`, `coerce`, `compile`, `complex`, `delattr`, `dict`, `dir`, `divmod`, `enumerate`, `eval`, `execfile`, `exit`, `file`, `filter`, `float`, `frozenset`, `getattr`, `globals`, `hasattr`, `hash`, `hex`, `id`, `input`, `int`, `intern`, `isinstance`, `issubclass`, `iter`, `len`, `list`, `locals`, `long`, `map`, `max`, `min`, `next`, `object`, `oct`, `open`, `ord`, `pow`, `property`, `range`, `raw_input`, `reduce`, `reload`, `repr`, `reversed`, `round`, `set`, `setattr`, `slice`, `sorted`, `staticmethod`, `str`, `sum`, `super`, `tuple`, `type`, `unichr`, `unicode`, `vars`, `xrange`, `zip`), NameBuiltin, nil}, |
| 58 | + {`(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls)\b`, NameBuiltinPseudo, nil}, |
| 59 | + {Words(`(?<!\.)`, `\b`, `ArithmeticError`, `AssertionError`, `AttributeError`, `BaseException`, `DeprecationWarning`, `EOFError`, `EnvironmentError`, `Exception`, `FloatingPointError`, `FutureWarning`, `GeneratorExit`, `IOError`, `ImportError`, `ImportWarning`, `IndentationError`, `IndexError`, `KeyError`, `KeyboardInterrupt`, `LookupError`, `MemoryError`, `NameError`, `NotImplemented`, `NotImplementedError`, `OSError`, `OverflowError`, `OverflowWarning`, `PendingDeprecationWarning`, `ReferenceError`, `RuntimeError`, `RuntimeWarning`, `StandardError`, `StopIteration`, `SyntaxError`, `SyntaxWarning`, `SystemError`, `SystemExit`, `TabError`, `TypeError`, `UnboundLocalError`, `UnicodeDecodeError`, `UnicodeEncodeError`, `UnicodeError`, `UnicodeTranslateError`, `UnicodeWarning`, `UserWarning`, `ValueError`, `VMSError`, `Warning`, `WindowsError`, `ZeroDivisionError`), NameException, nil}, |
| 60 | + }, |
| 61 | + "magicfuncs": { |
| 62 | + {Words(``, `\b`, `__abs__`, `__add__`, `__and__`, `__call__`, `__cmp__`, `__coerce__`, `__complex__`, `__contains__`, `__del__`, `__delattr__`, `__delete__`, `__delitem__`, `__delslice__`, `__div__`, `__divmod__`, `__enter__`, `__eq__`, `__exit__`, `__float__`, `__floordiv__`, `__ge__`, `__get__`, `__getattr__`, `__getattribute__`, `__getitem__`, `__getslice__`, `__gt__`, `__hash__`, `__hex__`, `__iadd__`, `__iand__`, `__idiv__`, `__ifloordiv__`, `__ilshift__`, `__imod__`, `__imul__`, `__index__`, `__init__`, `__instancecheck__`, `__int__`, `__invert__`, `__iop__`, `__ior__`, `__ipow__`, `__irshift__`, `__isub__`, `__iter__`, `__itruediv__`, `__ixor__`, `__le__`, `__len__`, `__long__`, `__lshift__`, `__lt__`, `__missing__`, `__mod__`, `__mul__`, `__ne__`, `__neg__`, `__new__`, `__nonzero__`, `__oct__`, `__op__`, `__or__`, `__pos__`, `__pow__`, `__radd__`, `__rand__`, `__rcmp__`, `__rdiv__`, `__rdivmod__`, `__repr__`, `__reversed__`, `__rfloordiv__`, `__rlshift__`, `__rmod__`, `__rmul__`, `__rop__`, `__ror__`, `__rpow__`, `__rrshift__`, `__rshift__`, `__rsub__`, `__rtruediv__`, `__rxor__`, `__set__`, `__setattr__`, `__setitem__`, `__setslice__`, `__str__`, `__sub__`, `__subclasscheck__`, `__truediv__`, `__unicode__`, `__xor__`), NameFunctionMagic, nil}, |
| 63 | + }, |
| 64 | + "magicvars": { |
| 65 | + {Words(``, `\b`, `__bases__`, `__class__`, `__closure__`, `__code__`, `__defaults__`, `__dict__`, `__doc__`, `__file__`, `__func__`, `__globals__`, `__metaclass__`, `__module__`, `__mro__`, `__name__`, `__self__`, `__slots__`, `__weakref__`), NameVariableMagic, nil}, |
| 66 | + }, |
| 67 | + "numbers": { |
| 68 | + {`(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?`, LiteralNumberFloat, nil}, |
| 69 | + {`\d+[eE][+-]?[0-9]+j?`, LiteralNumberFloat, nil}, |
| 70 | + {`0[0-7]+j?`, LiteralNumberOct, nil}, |
| 71 | + {`0[bB][01]+`, LiteralNumberBin, nil}, |
| 72 | + {`0[xX][a-fA-F0-9_]+`, LiteralNumberHex, nil}, |
| 73 | + {`\d+L`, LiteralNumberIntegerLong, nil}, |
| 74 | + {`[\d_]+j?`, LiteralNumberInteger, nil}, |
| 75 | + }, |
| 76 | + "backtick": { |
| 77 | + {"`.*?`", LiteralStringBacktick, nil}, |
| 78 | + }, |
| 79 | + "name": { |
| 80 | + {`@[\w.]+`, NameDecorator, nil}, |
| 81 | + {`[a-zA-Z_]\w*`, Name, nil}, |
| 82 | + }, |
| 83 | + "funcname": { |
| 84 | + Include("magicfuncs"), |
| 85 | + {`[a-zA-Z_]\w*`, NameFunction, Pop(1)}, |
| 86 | + Default(Pop(1)), |
| 87 | + }, |
| 88 | + "classname": { |
| 89 | + {`[a-zA-Z_]\w*`, NameClass, Pop(1)}, |
| 90 | + }, |
| 91 | + "import": { |
| 92 | + {`(?:[ \t]|\\\n)+`, Text, nil}, |
| 93 | + {`as\b`, KeywordNamespace, nil}, |
| 94 | + {`,`, Operator, nil}, |
| 95 | + {`[a-zA-Z_][\w.]*`, NameNamespace, nil}, |
| 96 | + Default(Pop(1)), |
| 97 | + }, |
| 98 | + "fromimport": { |
| 99 | + {`(?:[ \t]|\\\n)+`, Text, nil}, |
| 100 | + {`import\b`, KeywordNamespace, Pop(1)}, |
| 101 | + {`None\b`, NameBuiltinPseudo, Pop(1)}, |
| 102 | + {`[a-zA-Z_.][\w.]*`, NameNamespace, nil}, |
| 103 | + Default(Pop(1)), |
| 104 | + }, |
| 105 | + "stringescape": { |
| 106 | + {`\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})`, LiteralStringEscape, nil}, |
| 107 | + }, |
| 108 | + "strings-single": { |
| 109 | + {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil}, |
| 110 | + {`[^\\\'"%\n]+`, LiteralStringSingle, nil}, |
| 111 | + {`[\'"\\]`, LiteralStringSingle, nil}, |
| 112 | + {`%`, LiteralStringSingle, nil}, |
| 113 | + }, |
| 114 | + "strings-double": { |
| 115 | + {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil}, |
| 116 | + {`[^\\\'"%\n]+`, LiteralStringDouble, nil}, |
| 117 | + {`[\'"\\]`, LiteralStringDouble, nil}, |
| 118 | + {`%`, LiteralStringDouble, nil}, |
| 119 | + }, |
| 120 | + "dqs": { |
| 121 | + {`"`, LiteralStringDouble, Pop(1)}, |
| 122 | + {`\\\\|\\"|\\\n`, LiteralStringEscape, nil}, |
| 123 | + Include("strings-double"), |
| 124 | + }, |
| 125 | + "sqs": { |
| 126 | + {`'`, LiteralStringSingle, Pop(1)}, |
| 127 | + {`\\\\|\\'|\\\n`, LiteralStringEscape, nil}, |
| 128 | + Include("strings-single"), |
| 129 | + }, |
| 130 | + "tdqs": { |
| 131 | + {`"""`, LiteralStringDouble, Pop(1)}, |
| 132 | + Include("strings-double"), |
| 133 | + {`\n`, LiteralStringDouble, nil}, |
| 134 | + }, |
| 135 | + "tsqs": { |
| 136 | + {`'''`, LiteralStringSingle, Pop(1)}, |
| 137 | + Include("strings-single"), |
| 138 | + {`\n`, LiteralStringSingle, nil}, |
| 139 | + }, |
| 140 | + } |
| 141 | +} |
0 commit comments