49 ] |
49 ] |
50 |
50 |
51 tokens = { |
51 tokens = { |
52 'root': [ |
52 'root': [ |
53 (r'\n', Text), |
53 (r'\n', Text), |
54 (r'^(\s*)([rRuU]{,2}"""(?:.|\n)*?""")', bygroups(Text, String.Doc)), |
54 (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")', |
55 (r"^(\s*)([rRuU]{,2}'''(?:.|\n)*?''')", bygroups(Text, String.Doc)), |
55 bygroups(Text, String.Affix, String.Doc)), |
|
56 (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')", |
|
57 bygroups(Text, String.Affix, String.Doc)), |
56 (r'[^\S\n]+', Text), |
58 (r'[^\S\n]+', Text), |
57 (r'\A#!.+$', Comment.Hashbang), |
59 (r'\A#!.+$', Comment.Hashbang), |
58 (r'#.*$', Comment.Single), |
60 (r'#.*$', Comment.Single), |
59 (r'[]{}:(),;[]', Punctuation), |
61 (r'[]{}:(),;[]', Punctuation), |
60 (r'\\\n', Text), |
62 (r'\\\n', Text), |
67 (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
69 (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
68 'fromimport'), |
70 'fromimport'), |
69 (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
71 (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text), |
70 'import'), |
72 'import'), |
71 include('builtins'), |
73 include('builtins'), |
|
74 include('magicfuncs'), |
|
75 include('magicvars'), |
72 include('backtick'), |
76 include('backtick'), |
73 ('(?:[rR]|[uU][rR]|[rR][uU])"""', String.Double, 'tdqs'), |
77 ('([rR]|[uUbB][rR]|[rR][uUbB])(""")', |
74 ("(?:[rR]|[uU][rR]|[rR][uU])'''", String.Single, 'tsqs'), |
78 bygroups(String.Affix, String.Double), 'tdqs'), |
75 ('(?:[rR]|[uU][rR]|[rR][uU])"', String.Double, 'dqs'), |
79 ("([rR]|[uUbB][rR]|[rR][uUbB])(''')", |
76 ("(?:[rR]|[uU][rR]|[rR][uU])'", String.Single, 'sqs'), |
80 bygroups(String.Affix, String.Single), 'tsqs'), |
77 ('[uU]?"""', String.Double, combined('stringescape', 'tdqs')), |
81 ('([rR]|[uUbB][rR]|[rR][uUbB])(")', |
78 ("[uU]?'''", String.Single, combined('stringescape', 'tsqs')), |
82 bygroups(String.Affix, String.Double), 'dqs'), |
79 ('[uU]?"', String.Double, combined('stringescape', 'dqs')), |
83 ("([rR]|[uUbB][rR]|[rR][uUbB])(')", |
80 ("[uU]?'", String.Single, combined('stringescape', 'sqs')), |
84 bygroups(String.Affix, String.Single), 'sqs'), |
|
85 ('([uUbB]?)(""")', bygroups(String.Affix, String.Double), |
|
86 combined('stringescape', 'tdqs')), |
|
87 ("([uUbB]?)(''')", bygroups(String.Affix, String.Single), |
|
88 combined('stringescape', 'tsqs')), |
|
89 ('([uUbB]?)(")', bygroups(String.Affix, String.Double), |
|
90 combined('stringescape', 'dqs')), |
|
91 ("([uUbB]?)(')", bygroups(String.Affix, String.Single), |
|
92 combined('stringescape', 'sqs')), |
81 include('name'), |
93 include('name'), |
82 include('numbers'), |
94 include('numbers'), |
83 ], |
95 ], |
84 'keywords': [ |
96 'keywords': [ |
85 (words(( |
97 (words(( |
102 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', |
114 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', |
103 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', |
115 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', |
104 'unichr', 'unicode', 'vars', 'xrange', 'zip'), |
116 'unichr', 'unicode', 'vars', 'xrange', 'zip'), |
105 prefix=r'(?<!\.)', suffix=r'\b'), |
117 prefix=r'(?<!\.)', suffix=r'\b'), |
106 Name.Builtin), |
118 Name.Builtin), |
107 (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True' |
119 (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls' |
108 r')\b', Name.Builtin.Pseudo), |
120 r')\b', Name.Builtin.Pseudo), |
109 (words(( |
121 (words(( |
110 'ArithmeticError', 'AssertionError', 'AttributeError', |
122 'ArithmeticError', 'AssertionError', 'AttributeError', |
111 'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError', |
123 'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError', |
112 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', |
124 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', |
121 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
133 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
122 'ValueError', 'VMSError', 'Warning', 'WindowsError', |
134 'ValueError', 'VMSError', 'Warning', 'WindowsError', |
123 'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'), |
135 'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'), |
124 Name.Exception), |
136 Name.Exception), |
125 ], |
137 ], |
|
138 'magicfuncs': [ |
|
139 (words(( |
|
140 '__abs__', '__add__', '__and__', '__call__', '__cmp__', '__coerce__', |
|
141 '__complex__', '__contains__', '__del__', '__delattr__', '__delete__', |
|
142 '__delitem__', '__delslice__', '__div__', '__divmod__', '__enter__', |
|
143 '__eq__', '__exit__', '__float__', '__floordiv__', '__ge__', '__get__', |
|
144 '__getattr__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', |
|
145 '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__', |
|
146 '__ilshift__', '__imod__', '__imul__', '__index__', '__init__', |
|
147 '__instancecheck__', '__int__', '__invert__', '__iop__', '__ior__', |
|
148 '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', |
|
149 '__ixor__', '__le__', '__len__', '__long__', '__lshift__', '__lt__', |
|
150 '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', |
|
151 '__nonzero__', '__oct__', '__op__', '__or__', '__pos__', '__pow__', |
|
152 '__radd__', '__rand__', '__rcmp__', '__rdiv__', '__rdivmod__', '__repr__', |
|
153 '__reversed__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', |
|
154 '__rop__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', |
|
155 '__rtruediv__', '__rxor__', '__set__', '__setattr__', '__setitem__', |
|
156 '__setslice__', '__str__', '__sub__', '__subclasscheck__', '__truediv__', |
|
157 '__unicode__', '__xor__'), suffix=r'\b'), |
|
158 Name.Function.Magic), |
|
159 ], |
|
160 'magicvars': [ |
|
161 (words(( |
|
162 '__bases__', '__class__', '__closure__', '__code__', '__defaults__', |
|
163 '__dict__', '__doc__', '__file__', '__func__', '__globals__', |
|
164 '__metaclass__', '__module__', '__mro__', '__name__', '__self__', |
|
165 '__slots__', '__weakref__'), |
|
166 suffix=r'\b'), |
|
167 Name.Variable.Magic), |
|
168 ], |
126 'numbers': [ |
169 'numbers': [ |
127 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float), |
170 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float), |
128 (r'\d+[eE][+-]?[0-9]+j?', Number.Float), |
171 (r'\d+[eE][+-]?[0-9]+j?', Number.Float), |
129 (r'0[0-7]+j?', Number.Oct), |
172 (r'0[0-7]+j?', Number.Oct), |
130 (r'0[bB][01]+', Number.Bin), |
173 (r'0[bB][01]+', Number.Bin), |
215 |
260 |
216 def innerstring_rules(ttype): |
261 def innerstring_rules(ttype): |
217 return [ |
262 return [ |
218 # the old style '%s' % (...) string formatting (still valid in Py3) |
263 # the old style '%s' % (...) string formatting (still valid in Py3) |
219 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
264 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
220 '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol), |
265 '[hlL]?[E-GXc-giorsux%]', String.Interpol), |
221 # the new style '{}'.format(...) string formatting |
266 # the new style '{}'.format(...) string formatting |
222 (r'\{' |
267 (r'\{' |
223 '((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name |
268 '((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name |
224 '(\![sra])?' # conversion |
269 '(\![sra])?' # conversion |
225 '(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[bcdeEfFgGnosxX%]?)?' |
270 '(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?' |
226 '\}', String.Interpol), |
271 '\}', String.Interpol), |
227 |
272 |
228 # backslashes, quotes and formatting signs must be parsed one at a time |
273 # backslashes, quotes and formatting signs must be parsed one at a time |
229 (r'[^\\\'"%\{\n]+', ttype), |
274 (r'[^\\\'"%{\n]+', ttype), |
230 (r'[\'"\\]', ttype), |
275 (r'[\'"\\]', ttype), |
231 # unhandled string formatting sign |
276 # unhandled string formatting sign |
232 (r'%|(\{{1,2})', ttype) |
277 (r'%|(\{{1,2})', ttype) |
233 # newlines are an error (use "nl" state) |
278 # newlines are an error (use "nl" state) |
234 ] |
279 ] |
256 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', |
301 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', |
257 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', |
302 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', |
258 'sum', 'super', 'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)', |
303 'sum', 'super', 'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)', |
259 suffix=r'\b'), |
304 suffix=r'\b'), |
260 Name.Builtin), |
305 Name.Builtin), |
261 (r'(?<!\.)(self|Ellipsis|NotImplemented)\b', Name.Builtin.Pseudo), |
306 (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo), |
262 (words(( |
307 (words(( |
263 'ArithmeticError', 'AssertionError', 'AttributeError', |
308 'ArithmeticError', 'AssertionError', 'AttributeError', |
264 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', |
309 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', |
265 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', |
310 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', |
266 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
311 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
281 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', |
326 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', |
282 'PermissionError', 'ProcessLookupError', 'TimeoutError'), |
327 'PermissionError', 'ProcessLookupError', 'TimeoutError'), |
283 prefix=r'(?<!\.)', suffix=r'\b'), |
328 prefix=r'(?<!\.)', suffix=r'\b'), |
284 Name.Exception), |
329 Name.Exception), |
285 ] |
330 ] |
|
331 tokens['magicfuncs'] = [ |
|
332 (words(( |
|
333 '__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__', '__and__', |
|
334 '__anext__', '__await__', '__bool__', '__bytes__', '__call__', |
|
335 '__complex__', '__contains__', '__del__', '__delattr__', '__delete__', |
|
336 '__delitem__', '__dir__', '__divmod__', '__enter__', '__eq__', '__exit__', |
|
337 '__float__', '__floordiv__', '__format__', '__ge__', '__get__', |
|
338 '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', |
|
339 '__iadd__', '__iand__', '__ifloordiv__', '__ilshift__', '__imatmul__', |
|
340 '__imod__', '__import__', '__imul__', '__index__', '__init__', |
|
341 '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__', |
|
342 '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', |
|
343 '__le__', '__len__', '__length_hint__', '__lshift__', '__lt__', |
|
344 '__matmul__', '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', |
|
345 '__new__', '__next__', '__or__', '__pos__', '__pow__', '__prepare__', |
|
346 '__radd__', '__rand__', '__rdivmod__', '__repr__', '__reversed__', |
|
347 '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__', |
|
348 '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', |
|
349 '__rsub__', '__rtruediv__', '__rxor__', '__set__', '__setattr__', |
|
350 '__setitem__', '__str__', '__sub__', '__subclasscheck__', '__truediv__', |
|
351 '__xor__'), suffix=r'\b'), |
|
352 Name.Function.Magic), |
|
353 ] |
|
354 tokens['magicvars'] = [ |
|
355 (words(( |
|
356 '__annotations__', '__bases__', '__class__', '__closure__', '__code__', |
|
357 '__defaults__', '__dict__', '__doc__', '__file__', '__func__', |
|
358 '__globals__', '__kwdefaults__', '__module__', '__mro__', '__name__', |
|
359 '__objclass__', '__qualname__', '__self__', '__slots__', '__weakref__'), |
|
360 suffix=r'\b'), |
|
361 Name.Variable.Magic), |
|
362 ] |
286 tokens['numbers'] = [ |
363 tokens['numbers'] = [ |
287 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float), |
364 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float), |
|
365 (r'\d+[eE][+-]?[0-9]+j?', Number.Float), |
288 (r'0[oO][0-7]+', Number.Oct), |
366 (r'0[oO][0-7]+', Number.Oct), |
289 (r'0[bB][01]+', Number.Bin), |
367 (r'0[bB][01]+', Number.Bin), |
290 (r'0[xX][a-fA-F0-9]+', Number.Hex), |
368 (r'0[xX][a-fA-F0-9]+', Number.Hex), |
291 (r'\d+', Number.Integer) |
369 (r'\d+', Number.Integer) |
292 ] |
370 ] |
631 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
709 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
632 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
710 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
633 ], |
711 ], |
634 'strings': [ |
712 'strings': [ |
635 (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
713 (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
636 '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol), |
714 '[hlL]?[E-GXc-giorsux%]', String.Interpol), |
637 (r'[^\\\'"%\n]+', String), |
715 (r'[^\\\'"%\n]+', String), |
638 # quotes, percents and backslashes must be parsed one at a time |
716 # quotes, percents and backslashes must be parsed one at a time |
639 (r'[\'"\\]', String), |
717 (r'[\'"\\]', String), |
640 # unhandled string formatting sign |
718 # unhandled string formatting sign |
641 (r'%', String) |
719 (r'%', String) |
702 (r'[!$%&*+\-./:<-@\\^|~;,]+', Operator), |
780 (r'[!$%&*+\-./:<-@\\^|~;,]+', Operator), |
703 |
781 |
704 (words(( |
782 (words(( |
705 'bool', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'dict\'', |
783 'bool', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'dict\'', |
706 'float', 'frozenset', 'int', 'list', 'list\'', 'memoryview', 'object', |
784 'float', 'frozenset', 'int', 'list', 'list\'', 'memoryview', 'object', |
707 'property', 'range', 'set', 'set\'', 'slice', 'staticmethod', 'str', 'super', |
785 'property', 'range', 'set', 'set\'', 'slice', 'staticmethod', 'str', |
708 'tuple', 'tuple\'', 'type'), prefix=r'(?<!\.)', suffix=r'(?![\'\w])'), |
786 'super', 'tuple', 'tuple\'', 'type'), |
|
787 prefix=r'(?<!\.)', suffix=r'(?![\'\w])'), |
709 Name.Builtin), |
788 Name.Builtin), |
710 (words(( |
789 (words(( |
711 '__import__', 'abs', 'all', 'any', 'bin', 'bind', 'chr', 'cmp', 'compile', |
790 '__import__', 'abs', 'all', 'any', 'bin', 'bind', 'chr', 'cmp', 'compile', |
712 'complex', 'delattr', 'dir', 'divmod', 'drop', 'dropwhile', 'enumerate', |
791 'complex', 'delattr', 'dir', 'divmod', 'drop', 'dropwhile', 'enumerate', |
713 'eval', 'exhaust', 'filter', 'flip', 'foldl1?', 'format', 'fst', 'getattr', |
792 'eval', 'exhaust', 'filter', 'flip', 'foldl1?', 'format', 'fst', |
714 'globals', 'hasattr', 'hash', 'head', 'hex', 'id', 'init', 'input', |
793 'getattr', 'globals', 'hasattr', 'hash', 'head', 'hex', 'id', 'init', |
715 'isinstance', 'issubclass', 'iter', 'iterate', 'last', 'len', 'locals', |
794 'input', 'isinstance', 'issubclass', 'iter', 'iterate', 'last', 'len', |
716 'map', 'max', 'min', 'next', 'oct', 'open', 'ord', 'pow', 'print', 'repr', |
795 'locals', 'map', 'max', 'min', 'next', 'oct', 'open', 'ord', 'pow', |
717 'reversed', 'round', 'setattr', 'scanl1?', 'snd', 'sorted', 'sum', 'tail', |
796 'print', 'repr', 'reversed', 'round', 'setattr', 'scanl1?', 'snd', |
718 'take', 'takewhile', 'vars', 'zip'), prefix=r'(?<!\.)', suffix=r'(?![\'\w])'), |
797 'sorted', 'sum', 'tail', 'take', 'takewhile', 'vars', 'zip'), |
|
798 prefix=r'(?<!\.)', suffix=r'(?![\'\w])'), |
719 Name.Builtin), |
799 Name.Builtin), |
720 (r"(?<!\.)(self|Ellipsis|NotImplemented|None|True|False)(?!['\w])", |
800 (r"(?<!\.)(self|Ellipsis|NotImplemented|None|True|False)(?!['\w])", |
721 Name.Builtin.Pseudo), |
801 Name.Builtin.Pseudo), |
722 |
802 |
723 (r"(?<!\.)[A-Z]\w*(Error|Exception|Warning)'*(?!['\w])", |
803 (r"(?<!\.)[A-Z]\w*(Error|Exception|Warning)'*(?!['\w])", |
739 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
819 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
740 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
820 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
741 ], |
821 ], |
742 'string': [ |
822 'string': [ |
743 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
823 (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' |
744 '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol), |
824 '[hlL]?[E-GXc-giorsux%]', String.Interpol), |
745 (r'[^\\\'"%\n]+', String), |
825 (r'[^\\\'"%\n]+', String), |
746 # quotes, percents and backslashes must be parsed one at a time |
826 # quotes, percents and backslashes must be parsed one at a time |
747 (r'[\'"\\]', String), |
827 (r'[\'"\\]', String), |
748 # unhandled string formatting sign |
828 # unhandled string formatting sign |
749 (r'%', String), |
829 (r'%', String), |