Browse Source

Fully implement type annotation support

Joongi Kim 7 years ago
parent
commit
f544d3c90b
2 changed files with 174 additions and 136 deletions
  1. 143
    129
      syntax/python.vim
  2. 31
    7
      test.py

+ 143
- 129
syntax/python.vim View File

163
 " we provide a dummy group here to avoid crashing pyrex.vim.
163
 " we provide a dummy group here to avoid crashing pyrex.vim.
164
 syn keyword pythonInclude       import
164
 syn keyword pythonInclude       import
165
 syn keyword pythonImport        import
165
 syn keyword pythonImport        import
166
-syn match pythonIdentifier "\v[a-zA-Z_][a-zA-Z0-9_]*" nextgroup=FunctionParameters
166
+syn match   pythonIdentifier    "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs,pythonTypeAnno display
167
 syn keyword pythonException     try except finally
167
 syn keyword pythonException     try except finally
168
 syn keyword pythonOperator      and in is not or
168
 syn keyword pythonOperator      and in is not or
169
 
169
 
170
-" TODO: contain pythonTypeAnno in assignments and non-assigning variable type defs
171
-syn match pythonTypeAnno ":\s*[a-zA-Z0-9_\[\],\s]\+" display contained contains=pythonType
172
-syn keyword pythonType Any AnyStr Callable ClassVar Tuple Union Optional Type TypeVar contained
170
+syn region pythonDictSetExpr matchgroup=pythonDictSetExpr start='{' end='}' contains=@pythonExpression
171
+syn region pythonFuncArgs    matchgroup=pythonFuncArgs    start='(' end=')' contained contains=pythonTypeAnno,@pythonExpression
172
+
173
+syn match pythonTypeAnno ":\s*\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"hs=s+1 display contained contains=pythonType nextgroup=pythonTypeArgs
174
+syn match pythonTypeAnnoReturn "->\s*\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*:"hs=s+2,he=e-1 display contains=pythonType nextgroup=pythonTypeArgs
175
+syn region pythonTypeArgs matchgroup=pythonTypeArgs start='\[' end='\]' display contained contains=pythonType,pythonTypeArgs
176
+syn keyword pythonType Any AnyStr Callable ClassVar Tuple Union Optional Type TypeVar None contained
173
 syn keyword pythonType AbstractSet MutableSet Mapping MutableMapping Sequence MutableSequence ByteString Deque List contained
177
 syn keyword pythonType AbstractSet MutableSet Mapping MutableMapping Sequence MutableSequence ByteString Deque List contained
174
 syn keyword pythonType Set FrozenSet MappingView KeysView ItemsView ValuesView Awaitable Coroutine AsyncIterable contained
178
 syn keyword pythonType Set FrozenSet MappingView KeysView ItemsView ValuesView Awaitable Coroutine AsyncIterable contained
175
 syn keyword pythonType AsyncIterator ContextManager Dict DefaultDict Generator AsyncGenerator Text NamedTuple contained
179
 syn keyword pythonType AsyncIterator ContextManager Dict DefaultDict Generator AsyncGenerator Text NamedTuple contained
189
   syn keyword pythonStatement   as nonlocal
193
   syn keyword pythonStatement   as nonlocal
190
   syn match   pythonStatement   "\<yield\s\+from\>" display
194
   syn match   pythonStatement   "\<yield\s\+from\>" display
191
   syn keyword pythonBuiltinObj  None True False
195
   syn keyword pythonBuiltinObj  None True False
192
-  syn match   pythonFunction    "[a-zA-Z_][a-zA-Z0-9_]*" nextgroup=FunctionParameters display contained
196
+  syn match   pythonFunction    "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs display contained
193
   syn keyword pythonStatement   await async
197
   syn keyword pythonStatement   await async
194
-  syn match   pythonStatement   "\<async\s\+def\>" nextgroup=pythonFunction skipwhite
198
+  syn match   pythonStatement   "\<async\s\+def\>" display nextgroup=pythonFunction skipwhite
195
   syn match   pythonStatement   "\<async\s\+with\>" display
199
   syn match   pythonStatement   "\<async\s\+with\>" display
196
   syn match   pythonStatement   "\<async\s\+for\>" display
200
   syn match   pythonStatement   "\<async\s\+for\>" display
197
 endif
201
 endif
198
 
202
 
199
-syn region FunctionParameters start='(' end=')' display contains=
200
-            \ FunctionParameters,
203
+syn cluster pythonExpression contains=
204
+            \ pythonFuncArgs,
205
+            \ pythonDictSetExpr,
206
+            \ pythonStatement,
201
             \ pythonRepeat,
207
             \ pythonRepeat,
202
             \ pythonConditional,
208
             \ pythonConditional,
203
             \ pythonComment,
209
             \ pythonComment,
206
             \ pythonNumberError,
212
             \ pythonNumberError,
207
             \ pythonFloat,
213
             \ pythonFloat,
208
             \ pythonHexNumber,
214
             \ pythonHexNumber,
209
-            \ pythonStatement,
210
             \ pythonOctNumber,
215
             \ pythonOctNumber,
216
+            \ pythonBytes,
211
             \ pythonString,
217
             \ pythonString,
212
             \ pythonRawString,
218
             \ pythonRawString,
213
             \ pythonUniString,
219
             \ pythonUniString,
214
-            \ pythonExClass,
215
             \ pythonUniRawString,
220
             \ pythonUniRawString,
221
+            \ pythonFString,
222
+            \ pythonExClass,
223
+            \ pythonBuiltinObj,
224
+            \ pythonBuiltinFunc
225
+
226
+syn cluster pythonFExpression contains=
227
+            \ pythonStatement,
228
+            \ pythonRepeat,
229
+            \ pythonConditional,
230
+            \ pythonOperator,
216
             \ pythonNumber,
231
             \ pythonNumber,
217
-            \ pythonRawString,
232
+            \ pythonHexNumber,
233
+            \ pythonOctNumber,
234
+            \ pythonBinNumber,
235
+            \ pythonFloat,
236
+            \ pythonString,
218
             \ pythonBytes,
237
             \ pythonBytes,
219
             \ pythonBuiltinObj,
238
             \ pythonBuiltinObj,
220
-            \ pythonNone,
221
-            \ pythonBuiltinFunc,
222
-            \ pythonTypeAnno,
223
-            \ pythonBoolean nextgroup=pythonRaiseFromStatement display contained
224
-
225
-syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonBytes,pythonBoolean,pythonBuiltinObj,pythonBuiltinFunc
239
+            \ pythonBuiltinFunc
226
 
240
 
227
 "
241
 "
228
 " Decorators (new in Python 2.4)
242
 " Decorators (new in Python 2.4)
229
 "
243
 "
230
 
244
 
231
-syn match   pythonDecorator	"^\s*\zs@" display nextgroup=pythonDottedName skipwhite
245
+syn match   pythonDecorator     "^\s*\zs@" display nextgroup=pythonDottedName skipwhite
232
 if s:Python2Syntax()
246
 if s:Python2Syntax()
233
   syn match   pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
247
   syn match   pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
234
 else
248
 else
240
 " Comments
254
 " Comments
241
 "
255
 "
242
 
256
 
243
-syn match   pythonComment	"#.*$" display contains=pythonTodo,@Spell
257
+syn match   pythonComment       "#.*$" display contains=pythonTodo,@Spell
244
 if !s:Enabled("g:python_highlight_file_headers_as_comments")
258
 if !s:Enabled("g:python_highlight_file_headers_as_comments")
245
-  syn match   pythonRun		"\%^#!.*$"
246
-  syn match   pythonCoding	"\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
259
+  syn match   pythonRun         "\%^#!.*$"
260
+  syn match   pythonCoding      "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
247
 endif
261
 endif
248
-syn keyword pythonTodo		TODO FIXME XXX contained
262
+syn keyword pythonTodo          TODO FIXME XXX contained
249
 
263
 
250
 "
264
 "
251
 " Errors
265
 " Errors
252
 "
266
 "
253
 
267
 
254
-syn match pythonError		"\<\d\+\D\+\>" display
255
-syn match pythonError		"[$?]" display
256
-syn match pythonError		"[&|]\{2,}" display
257
-syn match pythonError		"[=]\{3,}" display
268
+syn match pythonError           "\<\d\+\D\+\>" display
269
+syn match pythonError           "[$?]" display
270
+syn match pythonError           "[&|]\{2,}" display
271
+syn match pythonError           "[=]\{3,}" display
258
 
272
 
259
 " Mixing spaces and tabs also may be used for pretty formatting multiline
273
 " Mixing spaces and tabs also may be used for pretty formatting multiline
260
 " statements
274
 " statements
261
 if s:Enabled("g:python_highlight_indent_errors")
275
 if s:Enabled("g:python_highlight_indent_errors")
262
-  syn match pythonIndentError	"^\s*\%( \t\|\t \)\s*\S"me=e-1 display
276
+  syn match pythonIndentError   "^\s*\%( \t\|\t \)\s*\S"me=e-1 display
263
 endif
277
 endif
264
 
278
 
265
 " Trailing space errors
279
 " Trailing space errors
266
 if s:Enabled("g:python_highlight_space_errors")
280
 if s:Enabled("g:python_highlight_space_errors")
267
-  syn match pythonSpaceError	"\s\+$" display
281
+  syn match pythonSpaceError    "\s\+$" display
268
 endif
282
 endif
269
 
283
 
270
 "
284
 "
279
   syn region pythonString   start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
293
   syn region pythonString   start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
280
 else
294
 else
281
   " Python 3 byte strings
295
   " Python 3 byte strings
282
-  syn region pythonBytes		start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
283
-  syn region pythonBytes		start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
284
-  syn region pythonBytes		start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
285
-  syn region pythonBytes		start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
296
+  syn region pythonBytes                start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
297
+  syn region pythonBytes                start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
298
+  syn region pythonBytes                start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
299
+  syn region pythonBytes                start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
286
 
300
 
287
   syn match pythonBytesError    ".\+" display contained
301
   syn match pythonBytesError    ".\+" display contained
288
   syn match pythonBytesContent  "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
302
   syn match pythonBytesContent  "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
355
 if s:Enabled("g:python_highlight_string_formatting")
369
 if s:Enabled("g:python_highlight_string_formatting")
356
   " % operator string formatting
370
   " % operator string formatting
357
   if s:Python2Syntax()
371
   if s:Python2Syntax()
358
-    syn match pythonStrFormatting	"%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
359
-    syn match pythonStrFormatting	"%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
372
+    syn match pythonStrFormatting       "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
373
+    syn match pythonStrFormatting       "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
360
   else
374
   else
361
-    syn match pythonStrFormatting	"%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
362
-    syn match pythonStrFormatting	"%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
375
+    syn match pythonStrFormatting       "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
376
+    syn match pythonStrFormatting       "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
363
   endif
377
   endif
364
 endif
378
 endif
365
 
379
 
367
   " str.format syntax
381
   " str.format syntax
368
   if s:Python2Syntax()
382
   if s:Python2Syntax()
369
     syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
383
     syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
370
-    syn match pythonStrFormat	"{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
384
+    syn match pythonStrFormat   "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
371
   else
385
   else
372
     syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString,pythonFString
386
     syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString,pythonFString
373
     syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
387
     syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
374
-    syn region pythonStrInterpRegion start="{"he=e+1,rs=e+1 end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}"hs=s-1,re=s-1 extend contained containedin=pythonFString contains=pythonStrInterpRegion,@pythonExpression
388
+    syn region pythonStrInterpRegion start="{"he=e+1,rs=e+1 end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}"hs=s-1,re=s-1 extend contained containedin=pythonFString contains=pythonStrInterpRegion,@pythonFExpression
375
   endif
389
   endif
376
 endif
390
 endif
377
 
391
 
378
 if s:Enabled("g:python_highlight_string_templates")
392
 if s:Enabled("g:python_highlight_string_templates")
379
   " string.Template format
393
   " string.Template format
380
   if s:Python2Syntax()
394
   if s:Python2Syntax()
381
-    syn match pythonStrTemplate	"\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
382
-    syn match pythonStrTemplate	"\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
383
-    syn match pythonStrTemplate	"\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
395
+    syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
396
+    syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
397
+    syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
384
   else
398
   else
385
-    syn match pythonStrTemplate	"\$\$" contained containedin=pythonString,pythonRawString
386
-    syn match pythonStrTemplate	"\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
387
-    syn match pythonStrTemplate	"\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
399
+    syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString
400
+    syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
401
+    syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
388
   endif
402
   endif
389
 endif
403
 endif
390
 
404
 
391
 if s:Enabled("g:python_highlight_doctests")
405
 if s:Enabled("g:python_highlight_doctests")
392
   " DocTests
406
   " DocTests
393
-  syn region pythonDocTest	start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
394
-  syn region pythonDocTest2	start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
407
+  syn region pythonDocTest      start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
408
+  syn region pythonDocTest2     start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
395
 endif
409
 endif
396
 
410
 
397
 "
411
 "
399
 "
413
 "
400
 
414
 
401
 if s:Python2Syntax()
415
 if s:Python2Syntax()
402
-  syn match   pythonHexError	"\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
403
-  syn match   pythonOctError	"\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
404
-  syn match   pythonBinError	"\<0[bB][01]*\D\+\d*[lL]\=\>" display
416
+  syn match   pythonHexError    "\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
417
+  syn match   pythonOctError    "\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
418
+  syn match   pythonBinError    "\<0[bB][01]*\D\+\d*[lL]\=\>" display
405
 
419
 
406
-  syn match   pythonHexNumber	"\<0[xX]\x\+[lL]\=\>" display
420
+  syn match   pythonHexNumber   "\<0[xX]\x\+[lL]\=\>" display
407
   syn match   pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
421
   syn match   pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
408
   syn match   pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
422
   syn match   pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
409
 
423
 
410
-  syn match   pythonNumberError	"\<\d\+\D[lL]\=\>" display
411
-  syn match   pythonNumber	"\<\d[lL]\=\>" display
412
-  syn match   pythonNumber	"\<[0-9]\d\+[lL]\=\>" display
413
-  syn match   pythonNumber	"\<\d\+[lLjJ]\>" display
424
+  syn match   pythonNumberError "\<\d\+\D[lL]\=\>" display
425
+  syn match   pythonNumber      "\<\d[lL]\=\>" display
426
+  syn match   pythonNumber      "\<[0-9]\d\+[lL]\=\>" display
427
+  syn match   pythonNumber      "\<\d\+[lLjJ]\>" display
414
 
428
 
415
-  syn match   pythonOctError	"\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
416
-  syn match   pythonBinError	"\<0[bB][01]*[2-9]\d*[lL]\=\>" display
429
+  syn match   pythonOctError    "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
430
+  syn match   pythonBinError    "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
417
 
431
 
418
-  syn match   pythonFloat	"\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
419
-  syn match   pythonFloat	"\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
420
-  syn match   pythonFloat	"\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
432
+  syn match   pythonFloat       "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
433
+  syn match   pythonFloat       "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
434
+  syn match   pythonFloat       "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
421
 else
435
 else
422
-  syn match   pythonHexError	"\<0[xX]\x*[g-zG-Z]\x*\>" display
423
-  syn match   pythonOctError	"\<0[oO]\=\o*\D\+\d*\>" display
424
-  syn match   pythonBinError	"\<0[bB][01]*\D\+\d*\>" display
436
+  syn match   pythonHexError    "\<0[xX]\x*[g-zG-Z]\x*\>" display
437
+  syn match   pythonOctError    "\<0[oO]\=\o*\D\+\d*\>" display
438
+  syn match   pythonBinError    "\<0[bB][01]*\D\+\d*\>" display
425
 
439
 
426
-  syn match   pythonHexNumber	"\<0[xX][_0-9a-fA-F]*\x\>" display
440
+  syn match   pythonHexNumber   "\<0[xX][_0-9a-fA-F]*\x\>" display
427
   syn match   pythonOctNumber "\<0[oO][_0-7]*\o\>" display
441
   syn match   pythonOctNumber "\<0[oO][_0-7]*\o\>" display
428
   syn match   pythonBinNumber "\<0[bB][_01]*[01]\>" display
442
   syn match   pythonBinNumber "\<0[bB][_01]*[01]\>" display
429
 
443
 
430
-  syn match   pythonNumberError	"\<\d[_0-9]*\D\>" display
431
-  syn match   pythonNumberError	"\<0[_0-9]\+\>" display
432
-  syn match   pythonNumberError	"\<\d[_0-9]*_\>" display
433
-  syn match   pythonNumber	"\<\d\>" display
434
-  syn match   pythonNumber	"\<[1-9][_0-9]*\d\>" display
435
-  syn match   pythonNumber	"\<\d[jJ]\>" display
436
-  syn match   pythonNumber	"\<[1-9][_0-9]*\d[jJ]\>" display
444
+  syn match   pythonNumberError "\<\d[_0-9]*\D\>" display
445
+  syn match   pythonNumberError "\<0[_0-9]\+\>" display
446
+  syn match   pythonNumberError "\<\d[_0-9]*_\>" display
447
+  syn match   pythonNumber      "\<\d\>" display
448
+  syn match   pythonNumber      "\<[1-9][_0-9]*\d\>" display
449
+  syn match   pythonNumber      "\<\d[jJ]\>" display
450
+  syn match   pythonNumber      "\<[1-9][_0-9]*\d[jJ]\>" display
437
 
451
 
438
-  syn match   pythonOctError	"\<0[oO]\=\o*[8-9]\d*\>" display
439
-  syn match   pythonBinError	"\<0[bB][01]*[2-9]\d*\>" display
452
+  syn match   pythonOctError    "\<0[oO]\=\o*[8-9]\d*\>" display
453
+  syn match   pythonBinError    "\<0[bB][01]*[2-9]\d*\>" display
440
 
454
 
441
-  syn match   pythonFloat	"\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>" display
442
-  syn match   pythonFloat	"\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>" display
443
-  syn match   pythonFloat	"\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=" display
455
+  syn match   pythonFloat       "\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>" display
456
+  syn match   pythonFloat       "\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>" display
457
+  syn match   pythonFloat       "\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=" display
444
 endif
458
 endif
445
 
459
 
446
 "
460
 "
449
 
463
 
450
 if s:Enabled("g:python_highlight_builtin_objs")
464
 if s:Enabled("g:python_highlight_builtin_objs")
451
   if s:Python2Syntax()
465
   if s:Python2Syntax()
452
-    syn keyword pythonBuiltinObj	None False True
466
+    syn keyword pythonBuiltinObj        None False True
453
   endif
467
   endif
454
-  syn keyword pythonBuiltinObj	Ellipsis NotImplemented self cls
455
-  syn keyword pythonBuiltinObj	__debug__ __doc__ __file__ __name__ __package__
468
+  syn keyword pythonBuiltinObj  Ellipsis NotImplemented self cls
469
+  syn keyword pythonBuiltinObj  __debug__ __doc__ __file__ __name__ __package__
456
 endif
470
 endif
457
 
471
 
458
 "
472
 "
461
 
475
 
462
 if s:Enabled("g:python_highlight_builtin_funcs")
476
 if s:Enabled("g:python_highlight_builtin_funcs")
463
   if s:Python2Syntax()
477
   if s:Python2Syntax()
464
-    syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(apply|basestring|buffer|callable|coerce)>\ze\(' nextgroup=FunctionParameters
465
-    syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(execfile|file|help|intern|long|raw_input)>\ze\(' nextgroup=FunctionParameters
466
-    syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(reduce|reload|unichr|unicode|xrange)>\ze\(' nextgroup=FunctionParameters
478
+    syn match pythonBuiltinFunc '\v(\.)@<!\zs<(apply|basestring|buffer|callable|coerce)>\ze\(' nextgroup=pythonFuncArgs
479
+    syn match pythonBuiltinFunc '\v(\.)@<!\zs<(execfile|file|help|intern|long|raw_input)>\ze\(' nextgroup=pythonFuncArgs
480
+    syn match pythonBuiltinFunc '\v(\.)@<!\zs<(reduce|reload|unichr|unicode|xrange)>\ze\(' nextgroup=pythonFuncArgs
467
     if s:Enabled("g:python_print_as_function")
481
     if s:Enabled("g:python_print_as_function")
468
-      syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(print)>\ze\(' nextgroup=FunctionParameters
482
+      syn match pythonBuiltinFunc       '\v(\.)@<!\zs<(print)>\ze\(' nextgroup=pythonFuncArgs
469
     endif
483
     endif
470
   else
484
   else
471
-    syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(ascii|exec|memoryview|print)\ze\(>' nextgroup=FunctionParameters
485
+    syn match pythonBuiltinFunc '\v(\.)@<!\zs<(ascii|exec|memoryview|print)\ze\(>' nextgroup=pythonFuncArgs
472
   endif
486
   endif
473
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(__import__|abs|all|any)>\ze\(' nextgroup=FunctionParameters
474
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(bin|bool|bytearray|bytes)>\ze\(' nextgroup=FunctionParameters
475
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(chr|classmethod|cmp|compile|complex)>\ze\(' nextgroup=FunctionParameters
476
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(delattr|dict|dir|divmod|enumerate|eval)>\ze\(' nextgroup=FunctionParameters
477
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(filter|float|format|frozenset|getattr)>\ze\(' nextgroup=FunctionParameters
478
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(globals|hasattr|hash|hex|id)>\ze\(' nextgroup=FunctionParameters
479
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(input|int|isinstance)>\ze\(' nextgroup=FunctionParameters
480
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(issubclass|iter|len|list|locals|map|max)>\ze\(' nextgroup=FunctionParameters
481
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(min|next|object|oct|open|ord)>\ze\(' nextgroup=FunctionParameters
482
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(pow|property|range)>\ze\(' nextgroup=FunctionParameters
483
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(repr|reversed|round|set|setattr)>\ze\(' nextgroup=FunctionParameters
484
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(slice|sorted|staticmethod|str|sum|super|tuple)>\ze\(' nextgroup=FunctionParameters
485
-  syn match pythonBuiltinFunc	'\v(\.)@<!\zs<(type|vars|zip)>\ze\(' nextgroup=FunctionParameters
487
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(__import__|abs|all|any)>\ze\(' nextgroup=pythonFuncArgs
488
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(bin|bool|bytearray|bytes)>\ze\(' nextgroup=pythonFuncArgs
489
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(chr|classmethod|cmp|compile|complex)>\ze\(' nextgroup=pythonFuncArgs
490
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(delattr|dict|dir|divmod|enumerate|eval)>\ze\(' nextgroup=pythonFuncArgs
491
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(filter|float|format|frozenset|getattr)>\ze\(' nextgroup=pythonFuncArgs
492
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(globals|hasattr|hash|hex|id)>\ze\(' nextgroup=pythonFuncArgs
493
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(input|int|isinstance)>\ze\(' nextgroup=pythonFuncArgs
494
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(issubclass|iter|len|list|locals|map|max)>\ze\(' nextgroup=pythonFuncArgs
495
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(min|next|object|oct|open|ord)>\ze\(' nextgroup=pythonFuncArgs
496
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(pow|property|range)>\ze\(' nextgroup=pythonFuncArgs
497
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(repr|reversed|round|set|setattr)>\ze\(' nextgroup=pythonFuncArgs
498
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(slice|sorted|staticmethod|str|sum|super|tuple)>\ze\(' nextgroup=pythonFuncArgs
499
+  syn match pythonBuiltinFunc   '\v(\.)@<!\zs<(type|vars|zip)>\ze\(' nextgroup=pythonFuncArgs
486
 endif
500
 endif
487
 
501
 
488
 "
502
 "
491
 
505
 
492
 if s:Enabled("g:python_highlight_exceptions")
506
 if s:Enabled("g:python_highlight_exceptions")
493
   if s:Python2Syntax()
507
   if s:Python2Syntax()
494
-    syn keyword pythonExClass	StandardError nextgroup=FunctionParameters
508
+    syn keyword pythonExClass   StandardError nextgroup=pythonFuncArgs
495
   else
509
   else
496
-    syn keyword pythonExClass	BlockingIOError ChildProcessError nextgroup=FunctionParameters
497
-    syn keyword pythonExClass	ConnectionError BrokenPipeError nextgroup=FunctionParameters
498
-    syn keyword pythonExClass	ConnectionAbortedError ConnectionRefusedError nextgroup=FunctionParameters
499
-    syn keyword pythonExClass	ConnectionResetError FileExistsError nextgroup=FunctionParameters
500
-    syn keyword pythonExClass	FileNotFoundError InterruptedError nextgroup=FunctionParameters
501
-    syn keyword pythonExClass	IsADirectoryError NotADirectoryError nextgroup=FunctionParameters
502
-    syn keyword pythonExClass	PermissionError ProcessLookupError TimeoutError nextgroup=FunctionParameters
503
-
504
-    syn keyword pythonExClass	ResourceWarning nextgroup=FunctionParameters
510
+    syn keyword pythonExClass   BlockingIOError ChildProcessError nextgroup=pythonFuncArgs
511
+    syn keyword pythonExClass   ConnectionError BrokenPipeError nextgroup=pythonFuncArgs
512
+    syn keyword pythonExClass   ConnectionAbortedError ConnectionRefusedError nextgroup=pythonFuncArgs
513
+    syn keyword pythonExClass   ConnectionResetError FileExistsError nextgroup=pythonFuncArgs
514
+    syn keyword pythonExClass   FileNotFoundError InterruptedError nextgroup=pythonFuncArgs
515
+    syn keyword pythonExClass   IsADirectoryError NotADirectoryError nextgroup=pythonFuncArgs
516
+    syn keyword pythonExClass   PermissionError ProcessLookupError TimeoutError nextgroup=pythonFuncArgs
517
+
518
+    syn keyword pythonExClass   ResourceWarning nextgroup=pythonFuncArgs
505
   endif
519
   endif
506
-  syn keyword pythonExClass	BaseException nextgroup=FunctionParameters
507
-  syn keyword pythonExClass	Exception ArithmeticError nextgroup=FunctionParameters
508
-  syn keyword pythonExClass	LookupError EnvironmentError nextgroup=FunctionParameters
509
-
510
-  syn keyword pythonExClass	AssertionError AttributeError BufferError EOFError nextgroup=FunctionParameters
511
-  syn keyword pythonExClass	FloatingPointError GeneratorExit IOError nextgroup=FunctionParameters
512
-  syn keyword pythonExClass	ImportError IndexError KeyError nextgroup=FunctionParameters
513
-  syn keyword pythonExClass	KeyboardInterrupt MemoryError NameError nextgroup=FunctionParameters
514
-  syn keyword pythonExClass	NotImplementedError OSError OverflowError nextgroup=FunctionParameters
515
-  syn keyword pythonExClass	ReferenceError RuntimeError StopIteration nextgroup=FunctionParameters
516
-  syn keyword pythonExClass	SyntaxError IndentationError TabError nextgroup=FunctionParameters
517
-  syn keyword pythonExClass	SystemError SystemExit TypeError nextgroup=FunctionParameters
518
-  syn keyword pythonExClass	UnboundLocalError UnicodeError nextgroup=FunctionParameters
519
-  syn keyword pythonExClass	UnicodeEncodeError UnicodeDecodeError nextgroup=FunctionParameters
520
-  syn keyword pythonExClass	UnicodeTranslateError ValueError VMSError nextgroup=FunctionParameters
521
-  syn keyword pythonExClass	WindowsError ZeroDivisionError nextgroup=FunctionParameters
522
-
523
-  syn keyword pythonExClass	Warning UserWarning BytesWarning DeprecationWarning nextgroup=FunctionParameters
524
-  syn keyword pythonExClass	PendingDepricationWarning SyntaxWarning nextgroup=FunctionParameters
525
-  syn keyword pythonExClass	RuntimeWarning FutureWarning nextgroup=FunctionParameters
526
-  syn keyword pythonExClass	ImportWarning UnicodeWarning nextgroup=FunctionParameters
520
+  syn keyword pythonExClass     BaseException nextgroup=pythonFuncArgs
521
+  syn keyword pythonExClass     Exception ArithmeticError nextgroup=pythonFuncArgs
522
+  syn keyword pythonExClass     LookupError EnvironmentError nextgroup=pythonFuncArgs
523
+
524
+  syn keyword pythonExClass     AssertionError AttributeError BufferError EOFError nextgroup=pythonFuncArgs
525
+  syn keyword pythonExClass     FloatingPointError GeneratorExit IOError nextgroup=pythonFuncArgs
526
+  syn keyword pythonExClass     ImportError IndexError KeyError nextgroup=pythonFuncArgs
527
+  syn keyword pythonExClass     KeyboardInterrupt MemoryError NameError nextgroup=pythonFuncArgs
528
+  syn keyword pythonExClass     NotImplementedError OSError OverflowError nextgroup=pythonFuncArgs
529
+  syn keyword pythonExClass     ReferenceError RuntimeError StopIteration nextgroup=pythonFuncArgs
530
+  syn keyword pythonExClass     SyntaxError IndentationError TabError nextgroup=pythonFuncArgs
531
+  syn keyword pythonExClass     SystemError SystemExit TypeError nextgroup=pythonFuncArgs
532
+  syn keyword pythonExClass     UnboundLocalError UnicodeError nextgroup=pythonFuncArgs
533
+  syn keyword pythonExClass     UnicodeEncodeError UnicodeDecodeError nextgroup=pythonFuncArgs
534
+  syn keyword pythonExClass     UnicodeTranslateError ValueError VMSError nextgroup=pythonFuncArgs
535
+  syn keyword pythonExClass     WindowsError ZeroDivisionError nextgroup=pythonFuncArgs
536
+
537
+  syn keyword pythonExClass     Warning UserWarning BytesWarning DeprecationWarning nextgroup=pythonFuncArgs
538
+  syn keyword pythonExClass     PendingDepricationWarning SyntaxWarning nextgroup=pythonFuncArgs
539
+  syn keyword pythonExClass     RuntimeWarning FutureWarning nextgroup=pythonFuncArgs
540
+  syn keyword pythonExClass     ImportWarning UnicodeWarning nextgroup=pythonFuncArgs
527
 endif
541
 endif
528
 
542
 
529
 if s:Enabled("g:python_slow_sync")
543
 if s:Enabled("g:python_slow_sync")
606
   HiLink pythonHexError         Error
620
   HiLink pythonHexError         Error
607
   HiLink pythonBinError         Error
621
   HiLink pythonBinError         Error
608
 
622
 
609
-  HiLink pythonBoolean          Boolean
610
-
611
   HiLink pythonBuiltinObj       Structure
623
   HiLink pythonBuiltinObj       Structure
612
   HiLink pythonBuiltinFunc      Function
624
   HiLink pythonBuiltinFunc      Function
613
 
625
 
614
   HiLink pythonExClass          Structure
626
   HiLink pythonExClass          Structure
615
 
627
 
616
   HiLink pythonTypeAnno         Optional
628
   HiLink pythonTypeAnno         Optional
629
+  HiLink pythonTypeAnnoReturn   Optional
630
+  HiLink pythonTypeArgs         Optional
617
   HiLink pythonType             Special
631
   HiLink pythonType             Special
618
 
632
 
619
   delcommand HiLink
633
   delcommand HiLink

+ 31
- 7
test.py View File

23
 async with
23
 async with
24
 async for
24
 async for
25
 
25
 
26
+# Type annotations
27
+
28
+def myfunc(a: str, something_other,
29
+           b: Callable[[str, str], int]) -> Any:
30
+    myval: float
31
+    mygood: Optional[int, Any] = b('wow', 'oops')
32
+    mykey = a
33
+    wow = {
34
+        mykey: this_should_not_be_type_anno[Any],
35
+        'b': some_data,
36
+    }
37
+    call_with_dict(a={
38
+        'a': asdf,
39
+        'b': 'zxcb',
40
+        mykey: this_should_not_be_type_anno[Any],
41
+    }, b=mydata['a'])
42
+
43
+
26
 # Builtin objects.
44
 # Builtin objects.
27
 
45
 
28
 True False Ellipsis None NotImplemented
46
 True False Ellipsis None NotImplemented
29
 
47
 
30
 # Builtin function and types.
48
 # Builtin function and types.
31
 
49
 
32
-__import__ abs all any apply basestring bool buffer callable chr classmethod
33
-cmp coerce compile complex delattr dict dir divmod enumerate eval execfile file
34
-filter float frozenset getattr globals hasattr hash help hex id input int
35
-intern isinstance issubclass iter len list locals long map max min object oct
36
-open ord pow property range raw_input reduce reload repr reversed round set
37
-setattr slice sorted staticmethod str sum super tuple type unichr unicode vars
38
-xrange zip
50
+__import__() abs() all() any() apply() basestring() bool() buffer() callable() chr() classmethod()
51
+cmp() coerce() compile() complex() delattr() dict() dir() divmod() enumerate() eval() execfile() file()
52
+filter() float() frozenset() getattr() globals() hasattr() hash() help() hex() id() input() int()
53
+intern() isinstance() issubclass() iter() len() list() locals() long() map() max() min() object() oct()
54
+open() ord() pow() property() range() raw_input() reduce() reload() repr() reversed() round() set()
55
+setattr() slice() sorted() staticmethod() str() sum() super() tuple() type() unichr() unicode() vars()
56
+xrange() zip()
57
+
58
+when_we_dont_call = a.float
59
+float = when_we_dont_call
60
+
61
+when_we_call = float(x)
62
+when_we_call = min(a, b)
39
 
63
 
40
 # Builtin exceptions and warnings.
64
 # Builtin exceptions and warnings.
41
 
65