blob: f99153f4cd03ce75f946e415821cff62c8087322 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: Python
Bram Moolenaar541f92d2015-06-19 13:27:23 +02003" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>
Bram Moolenaarf9132812015-07-21 19:19:13 +02004" Last Change: 2015 Jul 14
Bram Moolenaar541f92d2015-06-19 13:27:23 +02005" Credits: Neil Schemenauer <nas@python.ca>
Bram Moolenaar5c736222010-01-06 20:54:52 +01006" Dmitry Vasiliev
Bram Moolenaar071d4272004-06-13 20:20:40 +00007"
Bram Moolenaar5c736222010-01-06 20:54:52 +01008" This version is a major rewrite by Zvezdan Petkovic.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009"
Bram Moolenaar5c736222010-01-06 20:54:52 +010010" - introduced highlighting of doctests
11" - updated keywords, built-ins, and exceptions
12" - corrected regular expressions for
Bram Moolenaar071d4272004-06-13 20:20:40 +000013"
Bram Moolenaar5c736222010-01-06 20:54:52 +010014" * functions
15" * decorators
16" * strings
17" * escapes
18" * numbers
19" * space error
Bram Moolenaar071d4272004-06-13 20:20:40 +000020"
Bram Moolenaar5c736222010-01-06 20:54:52 +010021" - corrected synchronization
22" - more highlighting is ON by default, except
23" - space error highlighting is OFF by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000024"
Bram Moolenaar5c736222010-01-06 20:54:52 +010025" Optional highlighting can be controlled using these variables.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026"
Bram Moolenaar5c736222010-01-06 20:54:52 +010027" let python_no_builtin_highlight = 1
28" let python_no_doctest_code_highlight = 1
29" let python_no_doctest_highlight = 1
30" let python_no_exception_highlight = 1
31" let python_no_number_highlight = 1
32" let python_space_error_highlight = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000033"
Bram Moolenaar5c736222010-01-06 20:54:52 +010034" All the options above can be switched on together.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035"
Bram Moolenaar5c736222010-01-06 20:54:52 +010036" let python_highlight_all = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000037"
38
Bram Moolenaar5c736222010-01-06 20:54:52 +010039" For version 5.x: Clear all syntax items.
40" For version 6.x: Quit when a syntax file was already loaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041if version < 600
42 syntax clear
43elseif exists("b:current_syntax")
44 finish
45endif
46
Bram Moolenaar00659062010-09-21 22:34:02 +020047" We need nocompatible mode in order to continue lines with backslashes.
48" Original setting will be restored.
49let s:cpo_save = &cpo
50set cpo&vim
51
Bram Moolenaar5c736222010-01-06 20:54:52 +010052" Keep Python keywords in alphabetical order inside groups for easy
53" comparison with the table in the 'Python Language Reference'
Bram Moolenaarf9132812015-07-21 19:19:13 +020054" https://docs.python.org/2/reference/lexical_analysis.html#keywords,
55" https://docs.python.org/3/reference/lexical_analysis.html#keywords.
Bram Moolenaar5c736222010-01-06 20:54:52 +010056" Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
57" Exceptions come last at the end of each group (class and def below).
58"
59" Keywords 'with' and 'as' are new in Python 2.6
60" (use 'from __future__ import with_statement' in Python 2.5).
61"
Bram Moolenaarf9132812015-07-21 19:19:13 +020062" Some compromises had to be made to support both Python 3 and 2.
63" We include Python 3 features, but when a definition is duplicated,
Bram Moolenaar5c736222010-01-06 20:54:52 +010064" the last definition takes precedence.
65"
Bram Moolenaarf9132812015-07-21 19:19:13 +020066" - 'False', 'None', and 'True' are keywords in Python 3 but they are
67" built-ins in 2 and will be highlighted as built-ins below.
68" - 'exec' is a built-in in Python 3 and will be highlighted as
Bram Moolenaar5c736222010-01-06 20:54:52 +010069" built-in below.
Bram Moolenaarf9132812015-07-21 19:19:13 +020070" - 'nonlocal' is a keyword in Python 3 and will be highlighted.
71" - 'print' is a built-in in Python 3 and will be highlighted as
72" built-in below (use 'from __future__ import print_function' in 2)
Bram Moolenaar5c736222010-01-06 20:54:52 +010073"
74syn keyword pythonStatement False, None, True
75syn keyword pythonStatement as assert break continue del exec global
76syn keyword pythonStatement lambda nonlocal pass print return with yield
77syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
78syn keyword pythonConditional elif else if
Bram Moolenaar071d4272004-06-13 20:20:40 +000079syn keyword pythonRepeat for while
Bram Moolenaar071d4272004-06-13 20:20:40 +000080syn keyword pythonOperator and in is not or
Bram Moolenaar5c736222010-01-06 20:54:52 +010081syn keyword pythonException except finally raise try
82syn keyword pythonInclude from import
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar9c102382006-05-03 21:26:49 +000084" Decorators (new in Python 2.4)
85syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
Bram Moolenaar5c736222010-01-06 20:54:52 +010086" The zero-length non-grouping match before the function name is
87" extremely important in pythonFunction. Without it, everything is
88" interpreted as a function inside the contained environment of
89" doctests.
90" A dot must be allowed because of @MyClass.myfunc decorators.
91syn match pythonFunction
92 \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
Bram Moolenaar9c102382006-05-03 21:26:49 +000093
Bram Moolenaar5c736222010-01-06 20:54:52 +010094syn match pythonComment "#.*$" contains=pythonTodo,@Spell
95syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
96
97" Triple-quoted strings can contain doctests.
Bram Moolenaar541f92d2015-06-19 13:27:23 +020098syn region pythonString matchgroup=pythonQuotes
Bram Moolenaar5c736222010-01-06 20:54:52 +010099 \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
100 \ contains=pythonEscape,@Spell
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200101syn region pythonString matchgroup=pythonTripleQuotes
Bram Moolenaar5c736222010-01-06 20:54:52 +0100102 \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
103 \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200104syn region pythonRawString matchgroup=pythonQuotes
Bram Moolenaar5c736222010-01-06 20:54:52 +0100105 \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
106 \ contains=@Spell
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200107syn region pythonRawString matchgroup=pythonTripleQuotes
Bram Moolenaar5c736222010-01-06 20:54:52 +0100108 \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
109 \ contains=pythonSpaceError,pythonDoctest,@Spell
110
111syn match pythonEscape +\\[abfnrtv'"\\]+ contained
112syn match pythonEscape "\\\o\{1,3}" contained
113syn match pythonEscape "\\x\x\{2}" contained
114syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
115" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200116syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
Bram Moolenaar5c736222010-01-06 20:54:52 +0100117syn match pythonEscape "\\$"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119if exists("python_highlight_all")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100120 if exists("python_no_builtin_highlight")
121 unlet python_no_builtin_highlight
122 endif
123 if exists("python_no_doctest_code_highlight")
124 unlet python_no_doctest_code_highlight
125 endif
126 if exists("python_no_doctest_highlight")
127 unlet python_no_doctest_highlight
128 endif
129 if exists("python_no_exception_highlight")
130 unlet python_no_exception_highlight
131 endif
132 if exists("python_no_number_highlight")
133 unlet python_no_number_highlight
134 endif
135 let python_space_error_highlight = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136endif
137
Bram Moolenaar5c736222010-01-06 20:54:52 +0100138" It is very important to understand all details before changing the
139" regular expressions below or their order.
140" The word boundaries are *not* the floating-point number boundaries
141" because of a possible leading or trailing decimal point.
142" The expressions below ensure that all valid number literals are
143" highlighted, and invalid number literals are not. For example,
144"
145" - a decimal point in '4.' at the end of a line is highlighted,
146" - a second dot in 1.0.0 is not highlighted,
147" - 08 is not highlighted,
148" - 08e0 or 08j are highlighted,
149"
150" and so on, as specified in the 'Python Language Reference'.
Bram Moolenaarf9132812015-07-21 19:19:13 +0200151" https://docs.python.org/2/reference/lexical_analysis.html#numeric-literals
152" https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals
Bram Moolenaar5c736222010-01-06 20:54:52 +0100153if !exists("python_no_number_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 " numbers (including longs and complex)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100155 syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
156 syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
157 syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
158 syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
159 syn match pythonNumber "\<\d\+[jJ]\>"
160 syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
161 syn match pythonNumber
162 \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
163 syn match pythonNumber
Bram Moolenaarf9132812015-07-21 19:19:13 +0200164 \ "\%(^\|\W\)\zs\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165endif
166
Bram Moolenaar5c736222010-01-06 20:54:52 +0100167" Group the built-ins in the order in the 'Python Library Reference' for
168" easier comparison.
Bram Moolenaarf9132812015-07-21 19:19:13 +0200169" https://docs.python.org/2/library/constants.html
170" https://docs.python.org/3/library/constants.html
171" http://docs.python.org/2/library/functions.html
172" http://docs.python.org/3/library/functions.html
173" http://docs.python.org/2/library/functions.html#non-essential-built-in-functions
174" http://docs.python.org/3/library/functions.html#non-essential-built-in-functions
Bram Moolenaar5c736222010-01-06 20:54:52 +0100175" Python built-in functions are in alphabetical order.
176if !exists("python_no_builtin_highlight")
177 " built-in constants
Bram Moolenaarf9132812015-07-21 19:19:13 +0200178 " 'False', 'True', and 'None' are also reserved words in Python 3
Bram Moolenaar5c736222010-01-06 20:54:52 +0100179 syn keyword pythonBuiltin False True None
180 syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
181 " built-in functions
Bram Moolenaarf9132812015-07-21 19:19:13 +0200182 syn keyword pythonBuiltin abs all any bin bool bytearray callable chr
183 syn keyword pythonBuiltin classmethod compile complex delattr dict dir
184 syn keyword pythonBuiltin divmod enumerate eval filter float format
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185 syn keyword pythonBuiltin frozenset getattr globals hasattr hash
186 syn keyword pythonBuiltin help hex id input int isinstance
187 syn keyword pythonBuiltin issubclass iter len list locals map max
Bram Moolenaarf9132812015-07-21 19:19:13 +0200188 syn keyword pythonBuiltin memoryview min next object oct open ord pow
189 syn keyword pythonBuiltin print property range repr reversed round set
Bram Moolenaar5c736222010-01-06 20:54:52 +0100190 syn keyword pythonBuiltin setattr slice sorted staticmethod str
191 syn keyword pythonBuiltin sum super tuple type vars zip __import__
Bram Moolenaarf9132812015-07-21 19:19:13 +0200192 " Python 2 only
193 syn keyword pythonBuiltin basestring cmp execfile file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100194 syn keyword pythonBuiltin long raw_input reduce reload unichr
195 syn keyword pythonBuiltin unicode xrange
Bram Moolenaarf9132812015-07-21 19:19:13 +0200196 " Python 3 only
197 syn keyword pythonBuiltin ascii bytes exec
198 " non-essential built-in functions; Python 2 only
Bram Moolenaar5c736222010-01-06 20:54:52 +0100199 syn keyword pythonBuiltin apply buffer coerce intern
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200endif
201
Bram Moolenaar5c736222010-01-06 20:54:52 +0100202" From the 'Python Library Reference' class hierarchy at the bottom.
Bram Moolenaarf9132812015-07-21 19:19:13 +0200203" http://docs.python.org/2/library/exceptions.html
204" http://docs.python.org/3/library/exceptions.html
Bram Moolenaar5c736222010-01-06 20:54:52 +0100205if !exists("python_no_exception_highlight")
Bram Moolenaarf9132812015-07-21 19:19:13 +0200206 " builtin base exceptions (used mostly as base classes for other exceptions)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100207 syn keyword pythonExceptions BaseException Exception
Bram Moolenaarf9132812015-07-21 19:19:13 +0200208 syn keyword pythonExceptions ArithmeticError BufferError
Bram Moolenaar5c736222010-01-06 20:54:52 +0100209 syn keyword pythonExceptions LookupError
Bram Moolenaarf9132812015-07-21 19:19:13 +0200210 " builtin base exceptions removed in Python 3
211 syn keyword pythonExceptions EnvironmentError StandardError
Bram Moolenaar5c736222010-01-06 20:54:52 +0100212 " builtin exceptions (actually raised)
Bram Moolenaarf9132812015-07-21 19:19:13 +0200213 syn keyword pythonExceptions AssertionError AttributeError
Bram Moolenaar5c736222010-01-06 20:54:52 +0100214 syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
Bram Moolenaarf9132812015-07-21 19:19:13 +0200215 syn keyword pythonExceptions ImportError IndentationError
Bram Moolenaar5c736222010-01-06 20:54:52 +0100216 syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
217 syn keyword pythonExceptions MemoryError NameError NotImplementedError
218 syn keyword pythonExceptions OSError OverflowError ReferenceError
219 syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
220 syn keyword pythonExceptions SystemError SystemExit TabError TypeError
221 syn keyword pythonExceptions UnboundLocalError UnicodeError
222 syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
Bram Moolenaarf9132812015-07-21 19:19:13 +0200223 syn keyword pythonExceptions UnicodeTranslateError ValueError
224 syn keyword pythonExceptions ZeroDivisionError
225 " builtin OS exceptions in Python 3
226 syn keyword pythonExceptions BlockingIOError BrokenPipeError
227 syn keyword pythonExceptions ChildProcessError ConnectionAbortedError
228 syn keyword pythonExceptions ConnectionError ConnectionRefusedError
229 syn keyword pythonExceptions ConnectionResetError FileExistsError
230 syn keyword pythonExceptions FileNotFoundError InterruptedError
231 syn keyword pythonExceptions IsADirectoryError NotADirectoryError
232 syn keyword pythonExceptions PermissionError ProcessLookupError
233 syn keyword pythonExceptions TimeoutError
234 " builtin exceptions deprecated/removed in Python 3
235 syn keyword pythonExceptions IOError VMSError WindowsError
Bram Moolenaar5c736222010-01-06 20:54:52 +0100236 " builtin warnings
237 syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
238 syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
239 syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
240 syn keyword pythonExceptions UserWarning Warning
Bram Moolenaarf9132812015-07-21 19:19:13 +0200241 " builtin warnings in Python 3
242 syn keyword pythonExceptions ResourceWarning
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243endif
244
Bram Moolenaar5c736222010-01-06 20:54:52 +0100245if exists("python_space_error_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 " trailing whitespace
Bram Moolenaar5c736222010-01-06 20:54:52 +0100247 syn match pythonSpaceError display excludenl "\s\+$"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 " mixed tabs and spaces
Bram Moolenaar5c736222010-01-06 20:54:52 +0100249 syn match pythonSpaceError display " \+\t"
250 syn match pythonSpaceError display "\t\+ "
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251endif
252
Bram Moolenaar5c736222010-01-06 20:54:52 +0100253" Do not spell doctests inside strings.
254" Notice that the end of a string, either ''', or """, will end the contained
255" doctest too. Thus, we do *not* need to have it as an end pattern.
256if !exists("python_no_doctest_highlight")
Bram Moolenaar34700a62013-03-07 13:20:54 +0100257 if !exists("python_no_doctest_code_highlight")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100258 syn region pythonDoctest
259 \ start="^\s*>>>\s" end="^\s*$"
260 \ contained contains=ALLBUT,pythonDoctest,@Spell
261 syn region pythonDoctestValue
262 \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
263 \ contained
264 else
265 syn region pythonDoctest
266 \ start="^\s*>>>" end="^\s*$"
267 \ contained contains=@NoSpell
268 endif
269endif
270
271" Sync at the beginning of class, function, or method definition.
272syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274if version >= 508 || !exists("did_python_syn_inits")
275 if version <= 508
276 let did_python_syn_inits = 1
277 command -nargs=+ HiLink hi link <args>
278 else
279 command -nargs=+ HiLink hi def link <args>
280 endif
281
Bram Moolenaar5c736222010-01-06 20:54:52 +0100282 " The default highlight links. Can be overridden later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 HiLink pythonStatement Statement
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 HiLink pythonConditional Conditional
285 HiLink pythonRepeat Repeat
Bram Moolenaar5c736222010-01-06 20:54:52 +0100286 HiLink pythonOperator Operator
287 HiLink pythonException Exception
288 HiLink pythonInclude Include
289 HiLink pythonDecorator Define
290 HiLink pythonFunction Function
291 HiLink pythonComment Comment
292 HiLink pythonTodo Todo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 HiLink pythonString String
294 HiLink pythonRawString String
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200295 HiLink pythonQuotes String
296 HiLink pythonTripleQuotes pythonQuotes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 HiLink pythonEscape Special
Bram Moolenaar5c736222010-01-06 20:54:52 +0100298 if !exists("python_no_number_highlight")
299 HiLink pythonNumber Number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100301 if !exists("python_no_builtin_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 HiLink pythonBuiltin Function
303 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100304 if !exists("python_no_exception_highlight")
305 HiLink pythonExceptions Structure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100307 if exists("python_space_error_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 HiLink pythonSpaceError Error
309 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100310 if !exists("python_no_doctest_highlight")
311 HiLink pythonDoctest Special
312 HiLink pythonDoctestValue Define
313 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315 delcommand HiLink
316endif
317
318let b:current_syntax = "python"
319
Bram Moolenaar00659062010-09-21 22:34:02 +0200320let &cpo = s:cpo_save
321unlet s:cpo_save
322
Bram Moolenaar5c736222010-01-06 20:54:52 +0100323" vim:set sw=2 sts=2 ts=8 noet: