xref: /vim-8.2.3635/runtime/syntax/python.vim (revision e2f98b95)
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" strings
56syn region pythonString		matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape
57syn region pythonString		matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape
58syn region pythonString		matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape
59syn region pythonString		matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape
60syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]'+ end=+'+ skip=+\\\\\|\\'+
61syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]"+ end=+"+ skip=+\\\\\|\\"+
62syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]"""+ end=+"""+
63syn region pythonRawString	matchgroup=Normal start=+[uU]\=[rR]'''+ end=+'''+
64syn match  pythonEscape		+\\[abfnrtv'"\\]+ contained
65syn match  pythonEscape		"\\\o\{1,3}" contained
66syn match  pythonEscape		"\\x\x\{2}" contained
67syn match  pythonEscape		"\(\\u\x\{4}\|\\U\x\{8}\)" contained
68syn match  pythonEscape		"\\$"
69
70if exists("python_highlight_all")
71  let python_highlight_numbers = 1
72  let python_highlight_builtins = 1
73  let python_highlight_exceptions = 1
74  let python_highlight_space_errors = 1
75endif
76
77if exists("python_highlight_numbers")
78  " numbers (including longs and complex)
79  syn match   pythonNumber	"\<0x\x\+[Ll]\=\>"
80  syn match   pythonNumber	"\<\d\+[LljJ]\=\>"
81  syn match   pythonNumber	"\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
82  syn match   pythonNumber	"\<\d\+\.\([eE][+-]\=\d\+\)\=[jJ]\=\>"
83  syn match   pythonNumber	"\<\d\+\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
84endif
85
86if exists("python_highlight_builtins")
87  " builtin functions, types and objects, not really part of the syntax
88  syn keyword pythonBuiltin	Ellipsis None NotImplemented __import__ abs
89  syn keyword pythonBuiltin	apply buffer callable chr classmethod cmp
90  syn keyword pythonBuiltin	coerce compile complex delattr dict dir divmod
91  syn keyword pythonBuiltin	eval execfile file filter float getattr globals
92  syn keyword pythonBuiltin	hasattr hash hex id input int intern isinstance
93  syn keyword pythonBuiltin	issubclass iter len list locals long map max
94  syn keyword pythonBuiltin	min object oct open ord pow property range
95  syn keyword pythonBuiltin	raw_input reduce reload repr round setattr
96  syn keyword pythonBuiltin	slice staticmethod str super tuple type unichr
97  syn keyword pythonBuiltin	unicode vars xrange zip
98endif
99
100if exists("python_highlight_exceptions")
101  " builtin exceptions and warnings
102  syn keyword pythonException	ArithmeticError AssertionError AttributeError
103  syn keyword pythonException	DeprecationWarning EOFError EnvironmentError
104  syn keyword pythonException	Exception FloatingPointError IOError
105  syn keyword pythonException	ImportError IndentationError IndexError
106  syn keyword pythonException	KeyError KeyboardInterrupt LookupError
107  syn keyword pythonException	MemoryError NameError NotImplementedError
108  syn keyword pythonException	OSError OverflowError OverflowWarning
109  syn keyword pythonException	ReferenceError RuntimeError RuntimeWarning
110  syn keyword pythonException	StandardError StopIteration SyntaxError
111  syn keyword pythonException	SyntaxWarning SystemError SystemExit TabError
112  syn keyword pythonException	TypeError UnboundLocalError UnicodeError
113  syn keyword pythonException	UserWarning ValueError Warning WindowsError
114  syn keyword pythonException	ZeroDivisionError
115endif
116
117if exists("python_highlight_space_errors")
118  " trailing whitespace
119  syn match   pythonSpaceError   display excludenl "\S\s\+$"ms=s+1
120  " mixed tabs and spaces
121  syn match   pythonSpaceError   display " \+\t"
122  syn match   pythonSpaceError   display "\t\+ "
123endif
124
125" This is fast but code inside triple quoted strings screws it up. It
126" is impossible to fix because the only way to know if you are inside a
127" triple quoted string is to start from the beginning of the file. If
128" you have a fast machine you can try uncommenting the "sync minlines"
129" and commenting out the rest.
130syn sync match pythonSync grouphere NONE "):$"
131syn sync maxlines=200
132"syn sync minlines=2000
133
134if version >= 508 || !exists("did_python_syn_inits")
135  if version <= 508
136    let did_python_syn_inits = 1
137    command -nargs=+ HiLink hi link <args>
138  else
139    command -nargs=+ HiLink hi def link <args>
140  endif
141
142  " The default methods for highlighting.  Can be overridden later
143  HiLink pythonStatement	Statement
144  HiLink pythonFunction		Function
145  HiLink pythonConditional	Conditional
146  HiLink pythonRepeat		Repeat
147  HiLink pythonString		String
148  HiLink pythonRawString	String
149  HiLink pythonEscape		Special
150  HiLink pythonOperator		Operator
151  HiLink pythonPreCondit	PreCondit
152  HiLink pythonComment		Comment
153  HiLink pythonTodo		Todo
154  if exists("python_highlight_numbers")
155    HiLink pythonNumber	Number
156  endif
157  if exists("python_highlight_builtins")
158    HiLink pythonBuiltin	Function
159  endif
160  if exists("python_highlight_exceptions")
161    HiLink pythonException	Exception
162  endif
163  if exists("python_highlight_space_errors")
164    HiLink pythonSpaceError	Error
165  endif
166
167  delcommand HiLink
168endif
169
170let b:current_syntax = "python"
171
172" vim: ts=8
173