1package p
2
3import (
4 . "github.com/alecthomas/chroma" // nolint
5 "github.com/alecthomas/chroma/lexers/internal"
6)
7
8// Python lexer.
9var Python = internal.Register(MustNewLexer(
10 &Config{
11 Name: "Python",
12 Aliases: []string{"python", "py", "sage"},
13 Filenames: []string{"*.py", "*.pyw", "*.sc", "SConstruct", "SConscript", "*.tac", "*.sage"},
14 MimeTypes: []string{"text/x-python", "application/x-python"},
15 },
16 Rules{
17 "root": {
18 {`\n`, Text, nil},
19 {`^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil},
20 {`^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil},
21 {`[^\S\n]+`, Text, nil},
22 {`\A#!.+$`, CommentHashbang, nil},
23 {`#.*$`, CommentSingle, nil},
24 {`[]{}:(),;[]`, Punctuation, nil},
25 {`\\\n`, Text, nil},
26 {`\\`, Text, nil},
27 {`(in|is|and|or|not)\b`, OperatorWord, nil},
28 {`!=|==|<<|>>|[-~+/*%=<>&^|.]`, Operator, nil},
29 Include("keywords"),
30 {`(def)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("funcname")},
31 {`(class)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("classname")},
32 {`(from)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("fromimport")},
33 {`(import)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("import")},
34 Include("builtins"),
35 Include("magicfuncs"),
36 Include("magicvars"),
37 Include("backtick"),
38 {`([rR]|[uUbB][rR]|[rR][uUbB])(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("tdqs")},
39 {`([rR]|[uUbB][rR]|[rR][uUbB])(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("tsqs")},
40 {`([rR]|[uUbB][rR]|[rR][uUbB])(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("dqs")},
41 {`([rR]|[uUbB][rR]|[rR][uUbB])(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("sqs")},
42 {`([uUbB]?)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "tdqs")},
43 {`([uUbB]?)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "tsqs")},
44 {`([uUbB]?)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "dqs")},
45 {`([uUbB]?)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "sqs")},
46 Include("name"),
47 Include("numbers"),
48 },
49 "keywords": {
50 {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},
51 },
52 "builtins": {
53 {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},
54 {`(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls)\b`, NameBuiltinPseudo, nil},
55 {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},
56 },
57 "magicfuncs": {
58 {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},
59 },
60 "magicvars": {
61 {Words(``, `\b`, `__bases__`, `__class__`, `__closure__`, `__code__`, `__defaults__`, `__dict__`, `__doc__`, `__file__`, `__func__`, `__globals__`, `__metaclass__`, `__module__`, `__mro__`, `__name__`, `__self__`, `__slots__`, `__weakref__`), NameVariableMagic, nil},
62 },
63 "numbers": {
64 {`(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?`, LiteralNumberFloat, nil},
65 {`\d+[eE][+-]?[0-9]+j?`, LiteralNumberFloat, nil},
66 {`0[0-7]+j?`, LiteralNumberOct, nil},
67 {`0[bB][01]+`, LiteralNumberBin, nil},
68 {`0[xX][a-fA-F0-9]+`, LiteralNumberHex, nil},
69 {`\d+L`, LiteralNumberIntegerLong, nil},
70 {`\d+j?`, LiteralNumberInteger, nil},
71 },
72 "backtick": {
73 {"`.*?`", LiteralStringBacktick, nil},
74 },
75 "name": {
76 {`@[\w.]+`, NameDecorator, nil},
77 {`[a-zA-Z_]\w*`, Name, nil},
78 },
79 "funcname": {
80 Include("magicfuncs"),
81 {`[a-zA-Z_]\w*`, NameFunction, Pop(1)},
82 Default(Pop(1)),
83 },
84 "classname": {
85 {`[a-zA-Z_]\w*`, NameClass, Pop(1)},
86 },
87 "import": {
88 {`(?:[ \t]|\\\n)+`, Text, nil},
89 {`as\b`, KeywordNamespace, nil},
90 {`,`, Operator, nil},
91 {`[a-zA-Z_][\w.]*`, NameNamespace, nil},
92 Default(Pop(1)),
93 },
94 "fromimport": {
95 {`(?:[ \t]|\\\n)+`, Text, nil},
96 {`import\b`, KeywordNamespace, Pop(1)},
97 {`None\b`, NameBuiltinPseudo, Pop(1)},
98 {`[a-zA-Z_.][\w.]*`, NameNamespace, nil},
99 Default(Pop(1)),
100 },
101 "stringescape": {
102 {`\\([\\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},
103 },
104 "strings-single": {
105 {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil},
106 {`[^\\\'"%\n]+`, LiteralStringSingle, nil},
107 {`[\'"\\]`, LiteralStringSingle, nil},
108 {`%`, LiteralStringSingle, nil},
109 },
110 "strings-double": {
111 {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil},
112 {`[^\\\'"%\n]+`, LiteralStringDouble, nil},
113 {`[\'"\\]`, LiteralStringDouble, nil},
114 {`%`, LiteralStringDouble, nil},
115 },
116 "dqs": {
117 {`"`, LiteralStringDouble, Pop(1)},
118 {`\\\\|\\"|\\\n`, LiteralStringEscape, nil},
119 Include("strings-double"),
120 },
121 "sqs": {
122 {`'`, LiteralStringSingle, Pop(1)},
123 {`\\\\|\\'|\\\n`, LiteralStringEscape, nil},
124 Include("strings-single"),
125 },
126 "tdqs": {
127 {`"""`, LiteralStringDouble, Pop(1)},
128 Include("strings-double"),
129 {`\n`, LiteralStringDouble, nil},
130 },
131 "tsqs": {
132 {`'''`, LiteralStringSingle, Pop(1)},
133 Include("strings-single"),
134 {`\n`, LiteralStringSingle, nil},
135 },
136 },
137))