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