3 " Maintainer: Dmitry Vasiliev <dima at hlabs dot org>
4 " URL: https://github.com/hdima/vim-scripts/blob/master/syntax/python/python3.0.vim
5 " Last Change: 2012-02-11
9 " Based on python.vim (from Vim 6.1 distribution)
10 " by Neil Schemenauer <nas at python dot ca>
14 " Jeroen Ruigrok van der Werven
15 " for the idea to highlight erroneous operators
17 " for the patch to enable spell checking only for the right spots
18 " (strings and comments)
20 " for the patch fixing small typo
22 " for the patch fixing highlighting for decorators
24 " for the patch with new configuration options
26 " for the patch fixing bytes literals highlighting
27 " for the patch fixing str.format syntax highlighting
32 " For set option do: let OPTION_NAME = 1
33 " For clear option do: let OPTION_NAME = 0
37 " For highlight builtin functions:
38 " python_highlight_builtins
40 " For highlight builtin objects:
41 " python_highlight_builtin_objs
43 " For highlight builtin funtions:
44 " python_highlight_builtin_funcs
46 " For highlight standard exceptions:
47 " python_highlight_exceptions
49 " For highlight string formatting:
50 " python_highlight_string_formatting
52 " For highlight str.format syntax:
53 " python_highlight_string_format
55 " For highlight string.Template syntax:
56 " python_highlight_string_templates
58 " For highlight indentation errors:
59 " python_highlight_indent_errors
61 " For highlight trailing spaces:
62 " python_highlight_space_errors
64 " For highlight doc-tests:
65 " python_highlight_doctests
67 " If you want all Python highlightings above:
68 " python_highlight_all
69 " (This option not override previously set options)
74 " For version 5.x: Clear all syntax items
75 " For version 6.x: Quit when a syntax file was already loaded
78 elseif exists("b:current_syntax")
82 if exists("python_highlight_all") && python_highlight_all != 0
83 " Not override previously set options
84 if !exists("python_highlight_builtins")
85 if !exists("python_highlight_builtin_objs")
86 let python_highlight_builtin_objs = 1
88 if !exists("python_highlight_builtin_funcs")
89 let python_highlight_builtin_funcs = 1
92 if !exists("python_highlight_exceptions")
93 let python_highlight_exceptions = 1
95 if !exists("python_highlight_string_formatting")
96 let python_highlight_string_formatting = 1
98 if !exists("python_highlight_string_format")
99 let python_highlight_string_format = 1
101 if !exists("python_highlight_string_templates")
102 let python_highlight_string_templates = 1
104 if !exists("python_highlight_indent_errors")
105 let python_highlight_indent_errors = 1
107 if !exists("python_highlight_space_errors")
108 let python_highlight_space_errors = 1
110 if !exists("python_highlight_doctests")
111 let python_highlight_doctests = 1
116 syn keyword pythonStatement break continue del
117 syn keyword pythonStatement exec return as
118 syn keyword pythonStatement pass raise
119 syn keyword pythonStatement global assert
120 syn keyword pythonStatement lambda yield
121 syn keyword pythonStatement with nonlocal
122 syn keyword pythonStatement False None True
123 syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
124 syn match pythonFunction "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
125 syn keyword pythonRepeat for while
126 syn keyword pythonConditional if elif else
127 syn keyword pythonPreCondit import from
128 syn keyword pythonException try except finally
129 syn keyword pythonOperator and in is not or
131 " Decorators (new in Python 2.4)
132 syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite
133 syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
134 syn match pythonDot "\." display containedin=pythonDottedName
137 syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
138 syn match pythonRun "\%^#!.*$"
139 syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
140 syn keyword pythonTodo TODO FIXME XXX contained
143 syn match pythonError "\<\d\+\D\+\>" display
144 syn match pythonError "[$?]" display
145 syn match pythonError "[&|]\{2,}" display
146 syn match pythonError "[=]\{3,}" display
148 " TODO: Mixing spaces and tabs also may be used for pretty formatting multiline
149 " statements. For now I don't know how to work around this.
150 if exists("python_highlight_indent_errors") && python_highlight_indent_errors != 0
151 syn match pythonIndentError "^\s*\%( \t\|\t \)\s*\S"me=e-1 display
154 " Trailing space errors
155 if exists("python_highlight_space_errors") && python_highlight_space_errors != 0
156 syn match pythonSpaceError "\s\+$" display
160 syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
161 syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell
162 syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell
163 syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell
165 syn match pythonEscape +\\[abfnrtv'"\\]+ display contained
166 syn match pythonEscape "\\\o\o\=\o\=" display contained
167 syn match pythonEscapeError "\\\o\{,2}[89]" display contained
168 syn match pythonEscape "\\x\x\{2}" display contained
169 syn match pythonEscapeError "\\x\x\=\X" display contained
170 syn match pythonEscape "\\$"
171 syn match pythonEscape "\\u\x\{4}" display contained
172 syn match pythonEscapeError "\\u\x\{,3}\X" display contained
173 syn match pythonEscape "\\U\x\{8}" display contained
174 syn match pythonEscapeError "\\U\x\{,7}\X" display contained
175 syn match pythonEscape "\\N{[A-Z ]\+}" display contained
176 syn match pythonEscapeError "\\N{[^A-Z ]\+}" display contained
179 syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
180 syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
181 syn region pythonRawString start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
182 syn region pythonRawString start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
184 syn match pythonRawEscape +\\['"]+ display transparent contained
187 syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
188 syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
189 syn region pythonBytes start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
190 syn region pythonBytes start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
192 syn match pythonBytesError ".\+" display contained
193 syn match pythonBytesContent "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
195 syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained
196 syn match pythonBytesEscape "\\\o\o\=\o\=" display contained
197 syn match pythonBytesEscapeError "\\\o\{,2}[89]" display contained
198 syn match pythonBytesEscape "\\x\x\{2}" display contained
199 syn match pythonBytesEscapeError "\\x\x\=\X" display contained
200 syn match pythonBytesEscape "\\$"
202 if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0
204 syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
205 syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
208 if exists("python_highlight_string_format") && python_highlight_string_format != 0
210 syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString
211 syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
214 if exists("python_highlight_string_templates") && python_highlight_string_templates != 0
216 syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString
217 syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
218 syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
221 if exists("python_highlight_doctests") && python_highlight_doctests != 0
223 syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
224 syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
227 " Numbers (ints, longs, floats, complex)
228 syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*\>" display
230 syn match pythonHexNumber "\<0[xX]\x\+\>" display
231 syn match pythonOctNumber "\<0[oO]\o\+\>" display
232 syn match pythonBinNumber "\<0[bB][01]\+\>" display
234 syn match pythonNumberError "\<\d\+\D\>" display
235 syn match pythonNumberError "\<0\d\+\>" display
236 syn match pythonNumber "\<\d\>" display
237 syn match pythonNumber "\<[1-9]\d\+\>" display
238 syn match pythonNumber "\<\d\+[jJ]\>" display
240 syn match pythonFloat "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
241 syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
242 syn match pythonFloat "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
244 syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*\>" display
245 syn match pythonBinError "\<0[bB][01]*[2-9]\d*\>" display
247 if exists("python_highlight_builtin_objs") && python_highlight_builtin_objs != 0
248 " Builtin objects and types
249 syn keyword pythonBuiltinObj Ellipsis NotImplemented
250 syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
253 if exists("python_highlight_builtin_funcs") && python_highlight_builtin_funcs != 0
255 syn keyword pythonBuiltinFunc __import__ abs all any ascii
256 syn keyword pythonBuiltinFunc bin bool bytearray bytes
257 syn keyword pythonBuiltinFunc chr classmethod cmp compile complex
258 syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
259 syn keyword pythonBuiltinFunc exec filter float format frozenset getattr
260 syn keyword pythonBuiltinFunc globals hasattr hash hex id
261 syn keyword pythonBuiltinFunc input int isinstance
262 syn keyword pythonBuiltinFunc issubclass iter len list locals map max
263 syn keyword pythonBuiltinFunc memoryview min next object oct open ord
264 syn keyword pythonBuiltinFunc pow print property range
265 syn keyword pythonBuiltinFunc repr reversed round set setattr
266 syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
267 syn keyword pythonBuiltinFunc type vars zip
270 if exists("python_highlight_exceptions") && python_highlight_exceptions != 0
271 " Builtin exceptions and warnings
272 syn keyword pythonExClass BaseException
273 syn keyword pythonExClass Exception ArithmeticError
274 syn keyword pythonExClass LookupError EnvironmentError
276 syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
277 syn keyword pythonExClass FloatingPointError GeneratorExit IOError
278 syn keyword pythonExClass ImportError IndexError KeyError
279 syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
280 syn keyword pythonExClass NotImplementedError OSError OverflowError
281 syn keyword pythonExClass ReferenceError RuntimeError StopIteration
282 syn keyword pythonExClass SyntaxError IndentationError TabError
283 syn keyword pythonExClass SystemError SystemExit TypeError
284 syn keyword pythonExClass UnboundLocalError UnicodeError
285 syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
286 syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
287 syn keyword pythonExClass WindowsError ZeroDivisionError
289 syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
290 syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
291 syn keyword pythonExClass RuntimeWarning FutureWarning
292 syn keyword pythonExClass ImportWarning UnicodeWarning
295 if exists("python_slow_sync") && python_slow_sync != 0
296 syn sync minlines=2000
298 " This is fast but code inside triple quoted strings screws it up. It
299 " is impossible to fix because the only way to know if you are inside a
300 " triple quoted string is to start from the beginning of the file.
301 syn sync match pythonSync grouphere NONE "):$"
302 syn sync maxlines=200
305 if version >= 508 || !exists("did_python_syn_inits")
307 let did_python_syn_inits = 1
308 command -nargs=+ HiLink hi link <args>
310 command -nargs=+ HiLink hi def link <args>
313 HiLink pythonStatement Statement
314 HiLink pythonPreCondit Statement
315 HiLink pythonFunction Function
316 HiLink pythonConditional Conditional
317 HiLink pythonRepeat Repeat
318 HiLink pythonException Exception
319 HiLink pythonOperator Operator
321 HiLink pythonDecorator Define
322 HiLink pythonDottedName Function
323 HiLink pythonDot Normal
325 HiLink pythonComment Comment
326 HiLink pythonCoding Special
327 HiLink pythonRun Special
328 HiLink pythonTodo Todo
330 HiLink pythonError Error
331 HiLink pythonIndentError Error
332 HiLink pythonSpaceError Error
334 HiLink pythonString String
335 HiLink pythonRawString String
336 HiLink pythonEscape Special
337 HiLink pythonEscapeError Error
339 HiLink pythonBytes String
340 HiLink pythonBytesContent String
341 HiLink pythonBytesError Error
342 HiLink pythonBytesEscape Special
343 HiLink pythonBytesEscapeError Error
345 HiLink pythonStrFormatting Special
346 HiLink pythonStrFormat Special
347 HiLink pythonStrTemplate Special
349 HiLink pythonDocTest Special
350 HiLink pythonDocTest2 Special
352 HiLink pythonNumber Number
353 HiLink pythonHexNumber Number
354 HiLink pythonOctNumber Number
355 HiLink pythonBinNumber Number
356 HiLink pythonFloat Float
357 HiLink pythonNumberError Error
358 HiLink pythonOctError Error
359 HiLink pythonHexError Error
360 HiLink pythonBinError Error
362 HiLink pythonBuiltinObj Structure
363 HiLink pythonBuiltinFunc Function
365 HiLink pythonExClass Structure
370 let b:current_syntax = "python"