1 package p
2
3 import (
4 . "github.com/alecthomas/chroma"
5 "github.com/alecthomas/chroma/lexers/internal"
6 )
7
8
9 var Python = internal.Register(MustNewLazyLexer(
10 &Config{
11 Name: "Python",
12 Aliases: []string{"python", "py", "sage", "python3", "py3"},
13 Filenames: []string{"*.py", "*.pyi", "*.pyw", "*.jy", "*.sage", "*.sc", "SConstruct", "SConscript", "*.bzl", "BUCK", "BUILD", "BUILD.bazel", "WORKSPACE", "*.tac"},
14 MimeTypes: []string{"text/x-python", "application/x-python", "text/x-python3", "application/x-python3"},
15 },
16 pythonRules,
17 ))
18
19 func pythonRules() Rules {
20 const pythonIdentifier = `[_\p{L}][_\p{L}\p{N}]*`
21
22 return Rules{
23 "root": {
24 {`\n`, Text, nil},
25 {`^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil},
26 {`^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil},
27 {`\A#!.+$`, CommentHashbang, nil},
28 {`#.*$`, CommentSingle, nil},
29 {`\\\n`, Text, nil},
30 {`\\`, Text, nil},
31 Include("keywords"),
32 {`(def)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("funcname")},
33 {`(class)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("classname")},
34 {`(from)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("fromimport")},
35 {`(import)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("import")},
36 Include("expr"),
37 },
38 "expr": {
39 {`(?i)(rf|fr)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("rfstringescape", "tdqf")},
40 {`(?i)(rf|fr)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("rfstringescape", "tsqf")},
41 {`(?i)(rf|fr)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("rfstringescape", "dqf")},
42 {`(?i)(rf|fr)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("rfstringescape", "sqf")},
43 {`([fF])(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("fstringescape", "tdqf")},
44 {`([fF])(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("fstringescape", "tsqf")},
45 {`([fF])(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("fstringescape", "dqf")},
46 {`([fF])(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("fstringescape", "sqf")},
47 {`(?i)(rb|br|r)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("tdqs")},
48 {`(?i)(rb|br|r)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("tsqs")},
49 {`(?i)(rb|br|r)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("dqs")},
50 {`(?i)(rb|br|r)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("sqs")},
51 {`([uUbB]?)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "tdqs")},
52 {`([uUbB]?)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "tsqs")},
53 {`([uUbB]?)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "dqs")},
54 {`([uUbB]?)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "sqs")},
55 {`[^\S\n]+`, Text, nil},
56 Include("numbers"),
57 {`!=|==|<<|>>|:=|[-~+/*%=<>&^|.]`, Operator, nil},
58 {`[]{}:(),;[]`, Punctuation, nil},
59 {`(in|is|and|or|not)\b`, OperatorWord, nil},
60 Include("expr-keywords"),
61 Include("builtins"),
62 Include("magicfuncs"),
63 Include("magicvars"),
64 Include("name"),
65 },
66 "expr-inside-fstring": {
67 {`[{([]`, Punctuation, Push("expr-inside-fstring-inner")},
68 {`(=\s*)?(\![sraf])?\}`, LiteralStringInterpol, Pop(1)},
69 {`(=\s*)?(\![sraf])?:`, LiteralStringInterpol, Pop(1)},
70 {`\s+`, Text, nil},
71 Include("expr"),
72 },
73 "expr-inside-fstring-inner": {
74 {`[{([]`, Punctuation, Push("expr-inside-fstring-inner")},
75 {`[])}]`, Punctuation, Pop(1)},
76 {`\s+`, Text, nil},
77 Include("expr"),
78 },
79 "expr-keywords": {
80 {Words(``, `\b`, `async for`, `await`, `else`, `for`, `if`, `lambda`, `yield`, `yield from`), Keyword, nil},
81 {Words(``, `\b`, `True`, `False`, `None`), KeywordConstant, nil},
82 },
83 "keywords": {
84 {Words(``, `\b`, `assert`, `async`, `await`, `break`, `continue`, `del`, `elif`, `else`, `except`, `finally`, `for`, `global`, `if`, `lambda`, `pass`, `raise`, `nonlocal`, `return`, `try`, `while`, `yield`, `yield from`, `as`, `with`), Keyword, nil},
85 {Words(``, `\b`, `True`, `False`, `None`), KeywordConstant, nil},
86 },
87 "builtins": {
88 {Words(`(?<!\.)`, `\b`, `__import__`, `abs`, `all`, `any`, `bin`, `bool`, `bytearray`, `bytes`, `chr`, `classmethod`, `compile`, `complex`, `delattr`, `dict`, `dir`, `divmod`, `enumerate`, `eval`, `filter`, `float`, `format`, `frozenset`, `getattr`, `globals`, `hasattr`, `hash`, `hex`, `id`, `input`, `int`, `isinstance`, `issubclass`, `iter`, `len`, `list`, `locals`, `map`, `max`, `memoryview`, `min`, `next`, `object`, `oct`, `open`, `ord`, `pow`, `print`, `property`, `range`, `repr`, `reversed`, `round`, `set`, `setattr`, `slice`, `sorted`, `staticmethod`, `str`, `sum`, `super`, `tuple`, `type`, `vars`, `zip`), NameBuiltin, nil},
89 {`(?<!\.)(self|Ellipsis|NotImplemented|cls)\b`, NameBuiltinPseudo, nil},
90 {Words(`(?<!\.)`, `\b`, `ArithmeticError`, `AssertionError`, `AttributeError`, `BaseException`, `BufferError`, `BytesWarning`, `DeprecationWarning`, `EOFError`, `EnvironmentError`, `Exception`, `FloatingPointError`, `FutureWarning`, `GeneratorExit`, `IOError`, `ImportError`, `ImportWarning`, `IndentationError`, `IndexError`, `KeyError`, `KeyboardInterrupt`, `LookupError`, `MemoryError`, `NameError`, `NotImplementedError`, `OSError`, `OverflowError`, `PendingDeprecationWarning`, `ReferenceError`, `ResourceWarning`, `RuntimeError`, `RuntimeWarning`, `StopIteration`, `SyntaxError`, `SyntaxWarning`, `SystemError`, `SystemExit`, `TabError`, `TypeError`, `UnboundLocalError`, `UnicodeDecodeError`, `UnicodeEncodeError`, `UnicodeError`, `UnicodeTranslateError`, `UnicodeWarning`, `UserWarning`, `ValueError`, `VMSError`, `Warning`, `WindowsError`, `ZeroDivisionError`, `BlockingIOError`, `ChildProcessError`, `ConnectionError`, `BrokenPipeError`, `ConnectionAbortedError`, `ConnectionRefusedError`, `ConnectionResetError`, `FileExistsError`, `FileNotFoundError`, `InterruptedError`, `IsADirectoryError`, `NotADirectoryError`, `PermissionError`, `ProcessLookupError`, `TimeoutError`, `StopAsyncIteration`, `ModuleNotFoundError`, `RecursionError`), NameException, nil},
91 },
92 "magicfuncs": {
93 {Words(``, `\b`, `__abs__`, `__add__`, `__aenter__`, `__aexit__`, `__aiter__`, `__and__`, `__anext__`, `__await__`, `__bool__`, `__bytes__`, `__call__`, `__complex__`, `__contains__`, `__del__`, `__delattr__`, `__delete__`, `__delitem__`, `__dir__`, `__divmod__`, `__enter__`, `__eq__`, `__exit__`, `__float__`, `__floordiv__`, `__format__`, `__ge__`, `__get__`, `__getattr__`, `__getattribute__`, `__getitem__`, `__gt__`, `__hash__`, `__iadd__`, `__iand__`, `__ifloordiv__`, `__ilshift__`, `__imatmul__`, `__imod__`, `__imul__`, `__index__`, `__init__`, `__instancecheck__`, `__int__`, `__invert__`, `__ior__`, `__ipow__`, `__irshift__`, `__isub__`, `__iter__`, `__itruediv__`, `__ixor__`, `__le__`, `__len__`, `__length_hint__`, `__lshift__`, `__lt__`, `__matmul__`, `__missing__`, `__mod__`, `__mul__`, `__ne__`, `__neg__`, `__new__`, `__next__`, `__or__`, `__pos__`, `__pow__`, `__prepare__`, `__radd__`, `__rand__`, `__rdivmod__`, `__repr__`, `__reversed__`, `__rfloordiv__`, `__rlshift__`, `__rmatmul__`, `__rmod__`, `__rmul__`, `__ror__`, `__round__`, `__rpow__`, `__rrshift__`, `__rshift__`, `__rsub__`, `__rtruediv__`, `__rxor__`, `__set__`, `__setattr__`, `__setitem__`, `__str__`, `__sub__`, `__subclasscheck__`, `__truediv__`, `__xor__`), NameFunctionMagic, nil},
94 },
95 "magicvars": {
96 {Words(``, `\b`, `__annotations__`, `__bases__`, `__class__`, `__closure__`, `__code__`, `__defaults__`, `__dict__`, `__doc__`, `__file__`, `__func__`, `__globals__`, `__kwdefaults__`, `__module__`, `__mro__`, `__name__`, `__objclass__`, `__qualname__`, `__self__`, `__slots__`, `__weakref__`), NameVariableMagic, nil},
97 },
98 "numbers": {
99 {`(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)([eE][+-]?\d(?:_?\d)*)?`, LiteralNumberFloat, nil},
100 {`\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?`, LiteralNumberFloat, nil},
101 {`0[oO](?:_?[0-7])+`, LiteralNumberOct, nil},
102 {`0[bB](?:_?[01])+`, LiteralNumberBin, nil},
103 {`0[xX](?:_?[a-fA-F0-9])+`, LiteralNumberHex, nil},
104 {`\d(?:_?\d)*`, LiteralNumberInteger, nil},
105 },
106 "name": {
107 {`@` + pythonIdentifier, NameDecorator, nil},
108 {`@`, Operator, nil},
109 {pythonIdentifier, Name, nil},
110 },
111 "funcname": {
112 Include("magicfuncs"),
113 {pythonIdentifier, NameFunction, Pop(1)},
114 Default(Pop(1)),
115 },
116 "classname": {
117 {pythonIdentifier, NameClass, Pop(1)},
118 },
119 "import": {
120 {`(\s+)(as)(\s+)`, ByGroups(Text, Keyword, Text), nil},
121 {`\.`, NameNamespace, nil},
122 {pythonIdentifier, NameNamespace, nil},
123 {`(\s*)(,)(\s*)`, ByGroups(Text, Operator, Text), nil},
124 Default(Pop(1)),
125 },
126 "fromimport": {
127 {`(\s+)(import)\b`, ByGroups(Text, KeywordNamespace), Pop(1)},
128 {`\.`, NameNamespace, nil},
129 {`None\b`, NameBuiltinPseudo, Pop(1)},
130 {pythonIdentifier, NameNamespace, nil},
131 Default(Pop(1)),
132 },
133 "rfstringescape": {
134 {`\{\{`, LiteralStringEscape, nil},
135 {`\}\}`, LiteralStringEscape, nil},
136 },
137 "fstringescape": {
138 Include("rfstringescape"),
139 Include("stringescape"),
140 },
141 "stringescape": {
142 {`\\([\\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},
143 },
144 "fstrings-single": {
145 {`\}`, LiteralStringInterpol, nil},
146 {`\{`, LiteralStringInterpol, Push("expr-inside-fstring")},
147 {`[^\\\'"{}\n]+`, LiteralStringSingle, nil},
148 {`[\'"\\]`, LiteralStringSingle, nil},
149 },
150 "fstrings-double": {
151 {`\}`, LiteralStringInterpol, nil},
152 {`\{`, LiteralStringInterpol, Push("expr-inside-fstring")},
153 {`[^\\\'"{}\n]+`, LiteralStringDouble, nil},
154 {`[\'"\\]`, LiteralStringDouble, nil},
155 },
156 "strings-single": {
157 {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsaux%]`, LiteralStringInterpol, nil},
158 {`\{((\w+)((\.\w+)|(\[[^\]]+\]))*)?(\![sra])?(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?\}`, LiteralStringInterpol, nil},
159 {`[^\\\'"%{\n]+`, LiteralStringSingle, nil},
160 {`[\'"\\]`, LiteralStringSingle, nil},
161 {`%|(\{{1,2})`, LiteralStringSingle, nil},
162 },
163 "strings-double": {
164 {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsaux%]`, LiteralStringInterpol, nil},
165 {`\{((\w+)((\.\w+)|(\[[^\]]+\]))*)?(\![sra])?(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?\}`, LiteralStringInterpol, nil},
166 {`[^\\\'"%{\n]+`, LiteralStringDouble, nil},
167 {`[\'"\\]`, LiteralStringDouble, nil},
168 {`%|(\{{1,2})`, LiteralStringDouble, nil},
169 },
170 "dqf": {
171 {`"`, LiteralStringDouble, Pop(1)},
172 {`\\\\|\\"|\\\n`, LiteralStringEscape, nil},
173 Include("fstrings-double"),
174 },
175 "sqf": {
176 {`'`, LiteralStringSingle, Pop(1)},
177 {`\\\\|\\'|\\\n`, LiteralStringEscape, nil},
178 Include("fstrings-single"),
179 },
180 "dqs": {
181 {`"`, LiteralStringDouble, Pop(1)},
182 {`\\\\|\\"|\\\n`, LiteralStringEscape, nil},
183 Include("strings-double"),
184 },
185 "sqs": {
186 {`'`, LiteralStringSingle, Pop(1)},
187 {`\\\\|\\'|\\\n`, LiteralStringEscape, nil},
188 Include("strings-single"),
189 },
190 "tdqf": {
191 {`"""`, LiteralStringDouble, Pop(1)},
192 Include("fstrings-double"),
193 {`\n`, LiteralStringDouble, nil},
194 },
195 "tsqf": {
196 {`'''`, LiteralStringSingle, Pop(1)},
197 Include("fstrings-single"),
198 {`\n`, LiteralStringSingle, nil},
199 },
200 "tdqs": {
201 {`"""`, LiteralStringDouble, Pop(1)},
202 Include("strings-double"),
203 {`\n`, LiteralStringDouble, nil},
204 },
205 "tsqs": {
206 {`'''`, LiteralStringSingle, Pop(1)},
207 Include("strings-single"),
208 {`\n`, LiteralStringSingle, nil},
209 },
210 }
211 }
212
View as plain text