blob: d59074321a4c7677afda8f76a862a1780adecfea [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: Python
3" Maintainer: Neil Schemenauer <nas@python.ca>
Bram Moolenaar5c736222010-01-06 20:54:52 +01004" Last Change: 2009-10-13
5" Credits: Zvezdan Petkovic <zpetkovic@acm.org>
6" Neil Schemenauer <nas@python.ca>
7" Dmitry Vasiliev
Bram Moolenaar071d4272004-06-13 20:20:40 +00008"
Bram Moolenaar5c736222010-01-06 20:54:52 +01009" This version is a major rewrite by Zvezdan Petkovic.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010"
Bram Moolenaar5c736222010-01-06 20:54:52 +010011" - introduced highlighting of doctests
12" - updated keywords, built-ins, and exceptions
13" - corrected regular expressions for
Bram Moolenaar071d4272004-06-13 20:20:40 +000014"
Bram Moolenaar5c736222010-01-06 20:54:52 +010015" * functions
16" * decorators
17" * strings
18" * escapes
19" * numbers
20" * space error
Bram Moolenaar071d4272004-06-13 20:20:40 +000021"
Bram Moolenaar5c736222010-01-06 20:54:52 +010022" - corrected synchronization
23" - more highlighting is ON by default, except
24" - space error highlighting is OFF by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000025"
Bram Moolenaar5c736222010-01-06 20:54:52 +010026" Optional highlighting can be controlled using these variables.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027"
Bram Moolenaar5c736222010-01-06 20:54:52 +010028" let python_no_builtin_highlight = 1
29" let python_no_doctest_code_highlight = 1
30" let python_no_doctest_highlight = 1
31" let python_no_exception_highlight = 1
32" let python_no_number_highlight = 1
33" let python_space_error_highlight = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000034"
Bram Moolenaar5c736222010-01-06 20:54:52 +010035" All the options above can be switched on together.
Bram Moolenaar071d4272004-06-13 20:20:40 +000036"
Bram Moolenaar5c736222010-01-06 20:54:52 +010037" let python_highlight_all = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000038"
39
Bram Moolenaar5c736222010-01-06 20:54:52 +010040" For version 5.x: Clear all syntax items.
41" For version 6.x: Quit when a syntax file was already loaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042if version < 600
43 syntax clear
44elseif exists("b:current_syntax")
45 finish
46endif
47
Bram Moolenaar5c736222010-01-06 20:54:52 +010048" Keep Python keywords in alphabetical order inside groups for easy
49" comparison with the table in the 'Python Language Reference'
50" http://docs.python.org/reference/lexical_analysis.html#keywords.
51" Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
52" Exceptions come last at the end of each group (class and def below).
53"
54" Keywords 'with' and 'as' are new in Python 2.6
55" (use 'from __future__ import with_statement' in Python 2.5).
56"
57" Some compromises had to be made to support both Python 3.0 and 2.6.
58" We include Python 3.0 features, but when a definition is duplicated,
59" the last definition takes precedence.
60"
61" - 'False', 'None', and 'True' are keywords in Python 3.0 but they are
62" built-ins in 2.6 and will be highlighted as built-ins below.
63" - 'exec' is a built-in in Python 3.0 and will be highlighted as
64" built-in below.
65" - 'nonlocal' is a keyword in Python 3.0 and will be highlighted.
66" - 'print' is a built-in in Python 3.0 and will be highlighted as
67" built-in below (use 'from __future__ import print_function' in 2.6)
68"
69syn keyword pythonStatement False, None, True
70syn keyword pythonStatement as assert break continue del exec global
71syn keyword pythonStatement lambda nonlocal pass print return with yield
72syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
73syn keyword pythonConditional elif else if
Bram Moolenaar071d4272004-06-13 20:20:40 +000074syn keyword pythonRepeat for while
Bram Moolenaar071d4272004-06-13 20:20:40 +000075syn keyword pythonOperator and in is not or
Bram Moolenaar5c736222010-01-06 20:54:52 +010076syn keyword pythonException except finally raise try
77syn keyword pythonInclude from import
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar9c102382006-05-03 21:26:49 +000079" Decorators (new in Python 2.4)
80syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
Bram Moolenaar5c736222010-01-06 20:54:52 +010081" The zero-length non-grouping match before the function name is
82" extremely important in pythonFunction. Without it, everything is
83" interpreted as a function inside the contained environment of
84" doctests.
85" A dot must be allowed because of @MyClass.myfunc decorators.
86syn match pythonFunction
87 \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
Bram Moolenaar9c102382006-05-03 21:26:49 +000088
Bram Moolenaar5c736222010-01-06 20:54:52 +010089syn match pythonComment "#.*$" contains=pythonTodo,@Spell
90syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
91
92" Triple-quoted strings can contain doctests.
93syn region pythonString
94 \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
95 \ contains=pythonEscape,@Spell
96syn region pythonString
97 \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
98 \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
99syn region pythonRawString
100 \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
101 \ contains=@Spell
102syn region pythonRawString
103 \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
104 \ contains=pythonSpaceError,pythonDoctest,@Spell
105
106syn match pythonEscape +\\[abfnrtv'"\\]+ contained
107syn match pythonEscape "\\\o\{1,3}" contained
108syn match pythonEscape "\\x\x\{2}" contained
109syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
110" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
111syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
112syn match pythonEscape "\\$"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114if exists("python_highlight_all")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100115 if exists("python_no_builtin_highlight")
116 unlet python_no_builtin_highlight
117 endif
118 if exists("python_no_doctest_code_highlight")
119 unlet python_no_doctest_code_highlight
120 endif
121 if exists("python_no_doctest_highlight")
122 unlet python_no_doctest_highlight
123 endif
124 if exists("python_no_exception_highlight")
125 unlet python_no_exception_highlight
126 endif
127 if exists("python_no_number_highlight")
128 unlet python_no_number_highlight
129 endif
130 let python_space_error_highlight = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131endif
132
Bram Moolenaar5c736222010-01-06 20:54:52 +0100133" It is very important to understand all details before changing the
134" regular expressions below or their order.
135" The word boundaries are *not* the floating-point number boundaries
136" because of a possible leading or trailing decimal point.
137" The expressions below ensure that all valid number literals are
138" highlighted, and invalid number literals are not. For example,
139"
140" - a decimal point in '4.' at the end of a line is highlighted,
141" - a second dot in 1.0.0 is not highlighted,
142" - 08 is not highlighted,
143" - 08e0 or 08j are highlighted,
144"
145" and so on, as specified in the 'Python Language Reference'.
146" http://docs.python.org/reference/lexical_analysis.html#numeric-literals
147if !exists("python_no_number_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 " numbers (including longs and complex)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100149 syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
150 syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
151 syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
152 syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
153 syn match pythonNumber "\<\d\+[jJ]\>"
154 syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
155 syn match pythonNumber
156 \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
157 syn match pythonNumber
158 \ "\%(^\|\W\)\@<=\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159endif
160
Bram Moolenaar5c736222010-01-06 20:54:52 +0100161" Group the built-ins in the order in the 'Python Library Reference' for
162" easier comparison.
163" http://docs.python.org/library/constants.html
164" http://docs.python.org/library/functions.html
165" http://docs.python.org/library/functions.html#non-essential-built-in-functions
166" Python built-in functions are in alphabetical order.
167if !exists("python_no_builtin_highlight")
168 " built-in constants
169 " 'False', 'True', and 'None' are also reserved words in Python 3.0
170 syn keyword pythonBuiltin False True None
171 syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
172 " built-in functions
173 syn keyword pythonBuiltin abs all any bin bool chr classmethod
174 syn keyword pythonBuiltin compile complex delattr dict dir divmod
175 syn keyword pythonBuiltin enumerate eval filter float format
176 syn keyword pythonBuiltin frozenset getattr globals hasattr hash
177 syn keyword pythonBuiltin help hex id input int isinstance
178 syn keyword pythonBuiltin issubclass iter len list locals map max
179 syn keyword pythonBuiltin min next object oct open ord pow print
180 syn keyword pythonBuiltin property range repr reversed round set
181 syn keyword pythonBuiltin setattr slice sorted staticmethod str
182 syn keyword pythonBuiltin sum super tuple type vars zip __import__
183 " Python 2.6 only
184 syn keyword pythonBuiltin basestring callable cmp execfile file
185 syn keyword pythonBuiltin long raw_input reduce reload unichr
186 syn keyword pythonBuiltin unicode xrange
187 " Python 3.0 only
188 syn keyword pythonBuiltin ascii bytearray bytes exec memoryview
189 " non-essential built-in functions; Python 2.6 only
190 syn keyword pythonBuiltin apply buffer coerce intern
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191endif
192
Bram Moolenaar5c736222010-01-06 20:54:52 +0100193" From the 'Python Library Reference' class hierarchy at the bottom.
194" http://docs.python.org/library/exceptions.html
195if !exists("python_no_exception_highlight")
196 " builtin base exceptions (only used as base classes for other exceptions)
197 syn keyword pythonExceptions BaseException Exception
198 syn keyword pythonExceptions ArithmeticError EnvironmentError
199 syn keyword pythonExceptions LookupError
200 " builtin base exception removed in Python 3.0
201 syn keyword pythonExceptions StandardError
202 " builtin exceptions (actually raised)
203 syn keyword pythonExceptions AssertionError AttributeError BufferError
204 syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
205 syn keyword pythonExceptions IOError ImportError IndentationError
206 syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
207 syn keyword pythonExceptions MemoryError NameError NotImplementedError
208 syn keyword pythonExceptions OSError OverflowError ReferenceError
209 syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
210 syn keyword pythonExceptions SystemError SystemExit TabError TypeError
211 syn keyword pythonExceptions UnboundLocalError UnicodeError
212 syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
213 syn keyword pythonExceptions UnicodeTranslateError ValueError VMSError
214 syn keyword pythonExceptions WindowsError ZeroDivisionError
215 " builtin warnings
216 syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
217 syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
218 syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
219 syn keyword pythonExceptions UserWarning Warning
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220endif
221
Bram Moolenaar5c736222010-01-06 20:54:52 +0100222if exists("python_space_error_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 " trailing whitespace
Bram Moolenaar5c736222010-01-06 20:54:52 +0100224 syn match pythonSpaceError display excludenl "\s\+$"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 " mixed tabs and spaces
Bram Moolenaar5c736222010-01-06 20:54:52 +0100226 syn match pythonSpaceError display " \+\t"
227 syn match pythonSpaceError display "\t\+ "
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228endif
229
Bram Moolenaar5c736222010-01-06 20:54:52 +0100230" Do not spell doctests inside strings.
231" Notice that the end of a string, either ''', or """, will end the contained
232" doctest too. Thus, we do *not* need to have it as an end pattern.
233if !exists("python_no_doctest_highlight")
234 if !exists("python_no_doctest_code_higlight")
235 syn region pythonDoctest
236 \ start="^\s*>>>\s" end="^\s*$"
237 \ contained contains=ALLBUT,pythonDoctest,@Spell
238 syn region pythonDoctestValue
239 \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
240 \ contained
241 else
242 syn region pythonDoctest
243 \ start="^\s*>>>" end="^\s*$"
244 \ contained contains=@NoSpell
245 endif
246endif
247
248" Sync at the beginning of class, function, or method definition.
249syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251if version >= 508 || !exists("did_python_syn_inits")
252 if version <= 508
253 let did_python_syn_inits = 1
254 command -nargs=+ HiLink hi link <args>
255 else
256 command -nargs=+ HiLink hi def link <args>
257 endif
258
Bram Moolenaar5c736222010-01-06 20:54:52 +0100259 " The default highlight links. Can be overridden later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 HiLink pythonStatement Statement
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 HiLink pythonConditional Conditional
262 HiLink pythonRepeat Repeat
Bram Moolenaar5c736222010-01-06 20:54:52 +0100263 HiLink pythonOperator Operator
264 HiLink pythonException Exception
265 HiLink pythonInclude Include
266 HiLink pythonDecorator Define
267 HiLink pythonFunction Function
268 HiLink pythonComment Comment
269 HiLink pythonTodo Todo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 HiLink pythonString String
271 HiLink pythonRawString String
272 HiLink pythonEscape Special
Bram Moolenaar5c736222010-01-06 20:54:52 +0100273 if !exists("python_no_number_highlight")
274 HiLink pythonNumber Number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100276 if !exists("python_no_builtin_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 HiLink pythonBuiltin Function
278 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100279 if !exists("python_no_exception_highlight")
280 HiLink pythonExceptions Structure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100282 if exists("python_space_error_highlight")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 HiLink pythonSpaceError Error
284 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100285 if !exists("python_no_doctest_highlight")
286 HiLink pythonDoctest Special
287 HiLink pythonDoctestValue Define
288 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290 delcommand HiLink
291endif
292
293let b:current_syntax = "python"
294
Bram Moolenaar5c736222010-01-06 20:54:52 +0100295" vim:set sw=2 sts=2 ts=8 noet: