xref: /vim-8.2.3635/runtime/syntax/python.vim (revision 9c102387)
1" Vim syntax file
2" Language:	Python
3" Maintainer:	Neil Schemenauer <[email protected]>
4" Updated:	2002-10-18
5"
6" Options to control Python syntax highlighting:
7"
8" For highlighted numbers:
9"
10"    let python_highlight_numbers = 1
11"
12" For highlighted builtin functions:
13"
14"    let python_highlight_builtins = 1
15"
16" For highlighted standard exceptions:
17"
18"    let python_highlight_exceptions = 1
19"
20" Highlight erroneous whitespace:
21"
22"    let python_highlight_space_errors = 1
23"
24" If you want all possible Python highlighting (the same as setting the
25" preceding options):
26"
27"    let python_highlight_all = 1
28"
29
30" For version 5.x: Clear all syntax items
31" For version 6.x: Quit when a syntax file was already loaded
32if version < 600
33  syntax clear
34elseif exists("b:current_syntax")
35  finish
36endif
37
38
39syn keyword pythonStatement	break continue del
40syn keyword pythonStatement	except exec finally
41syn keyword pythonStatement	pass print raise
42syn keyword pythonStatement	return try
43syn keyword pythonStatement	global assert
44syn keyword pythonStatement	lambda yield
45syn keyword pythonStatement	def class nextgroup=pythonFunction skipwhite
46syn match   pythonFunction	"[a-zA-Z_][a-zA-Z0-9_]*" contained
47syn keyword pythonRepeat	for while
48syn keyword pythonConditional	if elif else
49syn keyword pythonOperator	and in is not or
50" AS will be a keyword in Python 3
51syn keyword pythonPreCondit	import from as
52syn match   pythonComment	"#.*$" contains=pythonTodo
53syn keyword pythonTodo		TODO FIXME XXX contained
54
55" Decorators (new in Python 2.4)
56syn match   pythonDecorator	"@" display nextgroup=pythonFunction skipwhite
57
58" strings
59syn region pythonString		matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape
60syn region pythonString		matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape
61syn region pythonString		matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape
62syn region pythonString		matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape
63syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]'+ end=+'+ skip=+\\\\\|\\'+
64syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]"+ end=+"+ skip=+\\\\\|\\"+
65syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]"""+ end=+"""+
66syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]'''+ end=+'''+
67syn match  pythonEscape		+\\[abfnrtv'"\\]+ contained
68syn match  pythonEscape		"\\\o\{1,3}" contained
69syn match  pythonEscape		"\\x\x\{2}" contained
70syn match  pythonEscape		"\(\\u\x\{4}\|\\U\x\{8}\)" contained
71syn match  pythonEscape		"\\$"
72
73if exists("python_highlight_all")
74  let python_highlight_numbers = 1
75  let python_highlight_builtins = 1
76  let python_highlight_exceptions = 1
77  let python_highlight_space_errors = 1
78endif
79
80if exists("python_highlight_numbers")
81  " numbers (including longs and complex)
82  syn match   pythonNumber	"\<0x\x\+[Ll]\=\>"
83  syn match   pythonNumber	"\<\d\+[LljJ]\=\>"
84  syn match   pythonNumber	"\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
85  syn match   pythonNumber	"\<\d\+\.\([eE][+-]\=\d\+\)\=[jJ]\=\>"
86  syn match   pythonNumber	"\<\d\+\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
87endif
88
89if exists("python_highlight_builtins")
90  " builtin functions, types and objects, not really part of the syntax
91  syn keyword pythonBuiltin	True False bool enumerate set frozenset help
92  syn keyword pythonBuiltin	reversed sorted sum
93  syn keyword pythonBuiltin	Ellipsis None NotImplemented __import__ abs
94  syn keyword pythonBuiltin	apply buffer callable chr classmethod cmp
95  syn keyword pythonBuiltin	coerce compile complex delattr dict dir divmod
96  syn keyword pythonBuiltin	eval execfile file filter float getattr globals
97  syn keyword pythonBuiltin	hasattr hash hex id input int intern isinstance
98  syn keyword pythonBuiltin	issubclass iter len list locals long map max
99  syn keyword pythonBuiltin	min object oct open ord pow property range
100  syn keyword pythonBuiltin	raw_input reduce reload repr round setattr
101  syn keyword pythonBuiltin	slice staticmethod str super tuple type unichr
102  syn keyword pythonBuiltin	unicode vars xrange zip
103endif
104
105if exists("python_highlight_exceptions")
106  " builtin exceptions and warnings
107  syn keyword pythonException	ArithmeticError AssertionError AttributeError
108  syn keyword pythonException	DeprecationWarning EOFError EnvironmentError
109  syn keyword pythonException	Exception FloatingPointError IOError
110  syn keyword pythonException	ImportError IndentationError IndexError
111  syn keyword pythonException	KeyError KeyboardInterrupt LookupError
112  syn keyword pythonException	MemoryError NameError NotImplementedError
113  syn keyword pythonException	OSError OverflowError OverflowWarning
114  syn keyword pythonException	ReferenceError RuntimeError RuntimeWarning
115  syn keyword pythonException	StandardError StopIteration SyntaxError
116  syn keyword pythonException	SyntaxWarning SystemError SystemExit TabError
117  syn keyword pythonException	TypeError UnboundLocalError UnicodeError
118  syn keyword pythonException	UnicodeEncodeError UnicodeDecodeError
119  syn keyword pythonException	UnicodeTranslateError
120  syn keyword pythonException	UserWarning ValueError Warning WindowsError
121  syn keyword pythonException	ZeroDivisionError
122endif
123
124if exists("python_highlight_space_errors")
125  " trailing whitespace
126  syn match   pythonSpaceError   display excludenl "\S\s\+$"ms=s+1
127  " mixed tabs and spaces
128  syn match   pythonSpaceError   display " \+\t"
129  syn match   pythonSpaceError   display "\t\+ "
130endif
131
132" This is fast but code inside triple quoted strings screws it up. It
133" is impossible to fix because the only way to know if you are inside a
134" triple quoted string is to start from the beginning of the file. If
135" you have a fast machine you can try uncommenting the "sync minlines"
136" and commenting out the rest.
137syn sync match pythonSync grouphere NONE "):$"
138syn sync maxlines=200
139"syn sync minlines=2000
140
141if version >= 508 || !exists("did_python_syn_inits")
142  if version <= 508
143    let did_python_syn_inits = 1
144    command -nargs=+ HiLink hi link <args>
145  else
146    command -nargs=+ HiLink hi def link <args>
147  endif
148
149  " The default methods for highlighting.  Can be overridden later
150  HiLink pythonStatement	Statement
151  HiLink pythonFunction		Function
152  HiLink pythonConditional	Conditional
153  HiLink pythonRepeat		Repeat
154  HiLink pythonString		String
155  HiLink pythonRawString	String
156  HiLink pythonEscape		Special
157  HiLink pythonOperator		Operator
158  HiLink pythonPreCondit	PreCondit
159  HiLink pythonComment		Comment
160  HiLink pythonTodo		Todo
161  HiLink pythonDecorator	Define
162  if exists("python_highlight_numbers")
163    HiLink pythonNumber	Number
164  endif
165  if exists("python_highlight_builtins")
166    HiLink pythonBuiltin	Function
167  endif
168  if exists("python_highlight_exceptions")
169    HiLink pythonException	Exception
170  endif
171  if exists("python_highlight_space_errors")
172    HiLink pythonSpaceError	Error
173  endif
174
175  delcommand HiLink
176endif
177
178let b:current_syntax = "python"
179
180" vim: ts=8
181