python.vim 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. " Vim syntax file
  2. " Language: Python
  3. " Current Maintainer: Dmitry Vasiliev <dima at hlabs dot org>
  4. " Previous Maintainer: Neil Schemenauer <nas at python dot ca>
  5. " URL: https://github.com/hdima/python-syntax
  6. " Last Change: 2015-11-01
  7. " Filenames: *.py
  8. " Version: 3.6.0
  9. "
  10. " Based on python.vim (from Vim 6.1 distribution)
  11. " by Neil Schemenauer <nas at python dot ca>
  12. "
  13. " Please use the following channels for reporting bugs, offering suggestions or
  14. " feedback:
  15. " - python.vim issue tracker: https://github.com/hdima/python-syntax/issues
  16. " - Email: Dmitry Vasiliev (dima at hlabs.org)
  17. " - Send a message or follow me for updates on Twitter: `@hdima
  18. " <https://twitter.com/hdima>`__
  19. "
  20. " Contributors
  21. " ============
  22. "
  23. " List of the contributors in alphabetical order:
  24. "
  25. " Andrea Riciputi
  26. " Anton Butanaev
  27. " Antony Lee
  28. " Caleb Adamantine
  29. " David Briscoe
  30. " Elizabeth Myers
  31. " Ihor Gorobets
  32. " Jeroen Ruigrok van der Werven
  33. " John Eikenberry
  34. " Joongi Kim
  35. " Marc Weber
  36. " Pedro Algarvio
  37. " Victor Salgado
  38. " Will Gray
  39. " Yuri Habrusiev
  40. "
  41. " Options
  42. " =======
  43. "
  44. " :let OPTION_NAME = 1 Enable option
  45. " :let OPTION_NAME = 0 Disable option
  46. "
  47. "
  48. " Option to select Python version
  49. " -------------------------------
  50. "
  51. " python_version_2 Enable highlighting for Python 2
  52. " (Python 3 highlighting is enabled
  53. " by default). Can also be set as
  54. " a buffer (b:python_version_2)
  55. " variable.
  56. "
  57. " You can also use the following local to buffer commands to switch
  58. " between two highlighting modes:
  59. "
  60. " :Python2Syntax Switch to Python 2 highlighting
  61. " mode
  62. " :Python3Syntax Switch to Python 3 highlighting
  63. " mode
  64. "
  65. " Option names used by the script
  66. " -------------------------------
  67. "
  68. " python_highlight_builtins Highlight builtin functions and
  69. " objects
  70. " python_highlight_builtin_objs Highlight builtin objects only
  71. " python_highlight_builtin_funcs Highlight builtin functions only
  72. " python_highlight_exceptions Highlight standard exceptions
  73. " python_highlight_string_formatting Highlight % string formatting
  74. " python_highlight_string_format Highlight str.format syntax
  75. " python_highlight_string_templates Highlight string.Template syntax
  76. " python_highlight_indent_errors Highlight indentation errors
  77. " python_highlight_space_errors Highlight trailing spaces
  78. " python_highlight_doctests Highlight doc-tests
  79. " python_print_as_function Highlight 'print' statement as
  80. " function for Python 2
  81. " python_highlight_file_headers_as_comments
  82. " Highlight shebang and coding
  83. " headers as comments
  84. "
  85. " python_highlight_all Enable all the options above
  86. " NOTE: This option don't override
  87. " any previously set options
  88. "
  89. " python_slow_sync Can be set to 0 for slow machines
  90. "
  91. " For version 5.x: Clear all syntax items
  92. " For versions greater than 6.x: Quit when a syntax file was already loaded
  93. if version < 600
  94. syntax clear
  95. elseif exists("b:current_syntax")
  96. finish
  97. endif
  98. "
  99. " Commands
  100. "
  101. command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax
  102. command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax
  103. " Enable option if it's not defined
  104. function! s:EnableByDefault(name)
  105. if !exists(a:name)
  106. let {a:name} = 1
  107. endif
  108. endfunction
  109. " Check if option is enabled
  110. function! s:Enabled(name)
  111. return exists(a:name) && {a:name}
  112. endfunction
  113. " Is it Python 2 syntax?
  114. function! s:Python2Syntax()
  115. if exists("b:python_version_2")
  116. return b:python_version_2
  117. endif
  118. return s:Enabled("g:python_version_2")
  119. endfunction
  120. "
  121. " Default options
  122. "
  123. call s:EnableByDefault("g:python_slow_sync")
  124. if s:Enabled("g:python_highlight_all")
  125. call s:EnableByDefault("g:python_highlight_builtins")
  126. if s:Enabled("g:python_highlight_builtins")
  127. call s:EnableByDefault("g:python_highlight_builtin_objs")
  128. call s:EnableByDefault("g:python_highlight_builtin_funcs")
  129. endif
  130. call s:EnableByDefault("g:python_highlight_exceptions")
  131. call s:EnableByDefault("g:python_highlight_string_formatting")
  132. call s:EnableByDefault("g:python_highlight_string_format")
  133. call s:EnableByDefault("g:python_highlight_string_templates")
  134. call s:EnableByDefault("g:python_highlight_indent_errors")
  135. call s:EnableByDefault("g:python_highlight_space_errors")
  136. call s:EnableByDefault("g:python_highlight_doctests")
  137. call s:EnableByDefault("g:python_print_as_function")
  138. endif
  139. "
  140. " Keywords
  141. "
  142. syn keyword pythonStatement break continue del
  143. syn keyword pythonStatement exec return
  144. syn keyword pythonStatement pass raise
  145. syn keyword pythonStatement global assert
  146. syn keyword pythonStatement lambda
  147. syn keyword pythonStatement with
  148. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  149. syn keyword pythonRepeat for while
  150. syn keyword pythonConditional if elif else
  151. " The standard pyrex.vim unconditionally removes the pythonInclude group, so
  152. " we provide a dummy group here to avoid crashing pyrex.vim.
  153. syn keyword pythonInclude import
  154. syn keyword pythonImport import
  155. syn match pythonIdentifier "\v[a-zA-Z_][a-zA-Z0-9_]*" nextgroup=FunctionParameters
  156. syn keyword pythonException try except finally
  157. syn keyword pythonOperator and in is not or
  158. " TODO: contain pythonTypeAnno in assignments and non-assigning variable type defs
  159. syn match pythonTypeAnno ":\s*[a-zA-Z0-9_\[\],\s]\+" display contained contains=pythonType
  160. syn keyword pythonType Any AnyStr Callable ClassVar Tuple Union Optional Type TypeVar contained
  161. syn keyword pythonType AbstractSet MutableSet Mapping MutableMapping Sequence MutableSequence ByteString Deque List contained
  162. syn keyword pythonType Set FrozenSet MappingView KeysView ItemsView ValuesView Awaitable Coroutine AsyncIterable contained
  163. syn keyword pythonType AsyncIterator ContextManager Dict DefaultDict Generator AsyncGenerator Text NamedTuple contained
  164. syn keyword pythonType Iterable Iterator Reversible SupportsInt SupportsFloat SupportsAbs SupportsRound Container contained
  165. syn keyword pythonType Hashable Sized Collection contained
  166. syn match pythonStatement "\<yield\>" display
  167. syn match pythonImport "\<from\>" display
  168. if s:Python2Syntax()
  169. if !s:Enabled("g:python_print_as_function")
  170. syn keyword pythonStatement print
  171. endif
  172. syn keyword pythonImport as
  173. syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained
  174. else
  175. syn keyword pythonStatement as nonlocal
  176. syn match pythonStatement "\<yield\s\+from\>" display
  177. syn keyword pythonBuiltinObj None True False
  178. syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" nextgroup=FunctionParameters display contained
  179. syn keyword pythonStatement await async
  180. syn match pythonStatement "\<async\s\+def\>" nextgroup=pythonFunction skipwhite
  181. syn match pythonStatement "\<async\s\+with\>" display
  182. syn match pythonStatement "\<async\s\+for\>" display
  183. endif
  184. syn region FunctionParameters start='(' end=')' display contains=
  185. \ FunctionParameters,
  186. \ pythonRepeat,
  187. \ pythonConditional,
  188. \ pythonComment,
  189. \ pythonOperator,
  190. \ pythonNumber,
  191. \ pythonNumberError,
  192. \ pythonFloat,
  193. \ pythonHexNumber,
  194. \ pythonStatement,
  195. \ pythonOctNumber,
  196. \ pythonString,
  197. \ pythonRawString,
  198. \ pythonUniString,
  199. \ pythonExClass,
  200. \ pythonUniRawString,
  201. \ pythonNumber,
  202. \ pythonRawString,
  203. \ pythonBytes,
  204. \ pythonBuiltinObj,
  205. \ pythonNone,
  206. \ pythonBuiltinFunc,
  207. \ pythonTypeAnno,
  208. \ pythonBoolean nextgroup=pythonRaiseFromStatement display contained
  209. syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonBytes,pythonBoolean,pythonBuiltinObj,pythonBuiltinFunc
  210. "
  211. " Decorators (new in Python 2.4)
  212. "
  213. syn match pythonDecorator "^\s*\zs@" display nextgroup=pythonDottedName skipwhite
  214. if s:Python2Syntax()
  215. syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
  216. else
  217. syn match pythonDottedName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*" display contained
  218. endif
  219. syn match pythonDot "\." display containedin=pythonDottedName
  220. "
  221. " Comments
  222. "
  223. syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
  224. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  225. syn match pythonRun "\%^#!.*$"
  226. syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  227. endif
  228. syn keyword pythonTodo TODO FIXME XXX contained
  229. "
  230. " Errors
  231. "
  232. syn match pythonError "\<\d\+\D\+\>" display
  233. syn match pythonError "[$?]" display
  234. syn match pythonError "[&|]\{2,}" display
  235. syn match pythonError "[=]\{3,}" display
  236. " Mixing spaces and tabs also may be used for pretty formatting multiline
  237. " statements
  238. if s:Enabled("g:python_highlight_indent_errors")
  239. syn match pythonIndentError "^\s*\%( \t\|\t \)\s*\S"me=e-1 display
  240. endif
  241. " Trailing space errors
  242. if s:Enabled("g:python_highlight_space_errors")
  243. syn match pythonSpaceError "\s\+$" display
  244. endif
  245. "
  246. " Strings
  247. "
  248. if s:Python2Syntax()
  249. " Python 2 strings
  250. syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  251. syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  252. syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  253. syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  254. else
  255. " Python 3 byte strings
  256. syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  257. syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  258. syn region pythonBytes start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
  259. syn region pythonBytes start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
  260. syn match pythonBytesError ".\+" display contained
  261. syn match pythonBytesContent "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
  262. endif
  263. syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained
  264. syn match pythonBytesEscape "\\\o\o\=\o\=" display contained
  265. syn match pythonBytesEscapeError "\\\o\{,2}[89]" display contained
  266. syn match pythonBytesEscape "\\x\x\{2}" display contained
  267. syn match pythonBytesEscapeError "\\x\x\=\X" display contained
  268. syn match pythonBytesEscape "\\$"
  269. syn match pythonUniEscape "\\u\x\{4}" display contained
  270. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  271. syn match pythonUniEscape "\\U\x\{8}" display contained
  272. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  273. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  274. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  275. if s:Python2Syntax()
  276. " Python 2 Unicode strings
  277. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  278. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  279. syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  280. syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  281. else
  282. " Python 3 strings
  283. syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  284. syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  285. syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  286. syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  287. syn region pythonFString start=+[fF]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  288. syn region pythonFString start=+[fF]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  289. syn region pythonFString start=+[fF]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  290. syn region pythonFString start=+[fF]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  291. endif
  292. if s:Python2Syntax()
  293. " Python 2 Unicode raw strings
  294. syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  295. syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  296. syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  297. syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
  298. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  299. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  300. endif
  301. " Python 2/3 raw strings
  302. if s:Python2Syntax()
  303. syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  304. syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  305. syn region pythonRawString start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  306. syn region pythonRawString start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  307. else
  308. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  309. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  310. syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  311. syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  312. syn region pythonRawBytes start=+[bB][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  313. syn region pythonRawBytes start=+[bB][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  314. syn region pythonRawBytes start=+[bB][rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  315. syn region pythonRawBytes start=+[bB][rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  316. endif
  317. syn match pythonRawEscape +\\['"]+ display transparent contained
  318. if s:Enabled("g:python_highlight_string_formatting")
  319. " % operator string formatting
  320. if s:Python2Syntax()
  321. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  322. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  323. else
  324. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  325. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  326. endif
  327. endif
  328. if s:Enabled("g:python_highlight_string_format")
  329. " str.format syntax
  330. if s:Python2Syntax()
  331. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  332. 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
  333. else
  334. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString,pythonFString
  335. 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
  336. 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
  337. endif
  338. endif
  339. if s:Enabled("g:python_highlight_string_templates")
  340. " string.Template format
  341. if s:Python2Syntax()
  342. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  343. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  344. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  345. else
  346. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString
  347. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
  348. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
  349. endif
  350. endif
  351. if s:Enabled("g:python_highlight_doctests")
  352. " DocTests
  353. syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
  354. syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
  355. endif
  356. "
  357. " Numbers (ints, longs, floats, complex)
  358. "
  359. if s:Python2Syntax()
  360. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
  361. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
  362. syn match pythonBinError "\<0[bB][01]*\D\+\d*[lL]\=\>" display
  363. syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
  364. syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
  365. syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
  366. syn match pythonNumberError "\<\d\+\D[lL]\=\>" display
  367. syn match pythonNumber "\<\d[lL]\=\>" display
  368. syn match pythonNumber "\<[0-9]\d\+[lL]\=\>" display
  369. syn match pythonNumber "\<\d\+[lLjJ]\>" display
  370. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
  371. syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
  372. syn match pythonFloat "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  373. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  374. syn match pythonFloat "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
  375. else
  376. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*\>" display
  377. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*\>" display
  378. syn match pythonBinError "\<0[bB][01]*\D\+\d*\>" display
  379. syn match pythonHexNumber "\<0[xX][_0-9a-fA-F]*\x\>" display
  380. syn match pythonOctNumber "\<0[oO][_0-7]*\o\>" display
  381. syn match pythonBinNumber "\<0[bB][_01]*[01]\>" display
  382. syn match pythonNumberError "\<\d[_0-9]*\D\>" display
  383. syn match pythonNumberError "\<0[_0-9]\+\>" display
  384. syn match pythonNumberError "\<\d[_0-9]*_\>" display
  385. syn match pythonNumber "\<\d\>" display
  386. syn match pythonNumber "\<[1-9][_0-9]*\d\>" display
  387. syn match pythonNumber "\<\d[jJ]\>" display
  388. syn match pythonNumber "\<[1-9][_0-9]*\d[jJ]\>" display
  389. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*\>" display
  390. syn match pythonBinError "\<0[bB][01]*[2-9]\d*\>" display
  391. syn match pythonFloat "\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>" display
  392. syn match pythonFloat "\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>" display
  393. syn match pythonFloat "\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=" display
  394. endif
  395. "
  396. " Builtin objects and types
  397. "
  398. if s:Enabled("g:python_highlight_builtin_objs")
  399. if s:Python2Syntax()
  400. syn keyword pythonBuiltinObj None False True
  401. endif
  402. syn keyword pythonBuiltinObj Ellipsis NotImplemented self cls
  403. syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
  404. endif
  405. "
  406. " Builtin functions
  407. "
  408. if s:Enabled("g:python_highlight_builtin_funcs")
  409. if s:Python2Syntax()
  410. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(apply|basestring|buffer|callable|coerce)>\ze\(' nextgroup=FunctionParameters
  411. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(execfile|file|help|intern|long|raw_input)>\ze\(' nextgroup=FunctionParameters
  412. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(reduce|reload|unichr|unicode|xrange)>\ze\(' nextgroup=FunctionParameters
  413. if s:Enabled("g:python_print_as_function")
  414. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(print)>\ze\(' nextgroup=FunctionParameters
  415. endif
  416. else
  417. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(ascii|exec|memoryview|print)\ze\(>' nextgroup=FunctionParameters
  418. endif
  419. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(__import__|abs|all|any)>\ze\(' nextgroup=FunctionParameters
  420. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(bin|bool|bytearray|bytes)>\ze\(' nextgroup=FunctionParameters
  421. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(chr|classmethod|cmp|compile|complex)>\ze\(' nextgroup=FunctionParameters
  422. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(delattr|dict|dir|divmod|enumerate|eval)>\ze\(' nextgroup=FunctionParameters
  423. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(filter|float|format|frozenset|getattr)>\ze\(' nextgroup=FunctionParameters
  424. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(globals|hasattr|hash|hex|id)>\ze\(' nextgroup=FunctionParameters
  425. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(input|int|isinstance)>\ze\(' nextgroup=FunctionParameters
  426. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(issubclass|iter|len|list|locals|map|max)>\ze\(' nextgroup=FunctionParameters
  427. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(min|next|object|oct|open|ord)>\ze\(' nextgroup=FunctionParameters
  428. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(pow|property|range)>\ze\(' nextgroup=FunctionParameters
  429. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(repr|reversed|round|set|setattr)>\ze\(' nextgroup=FunctionParameters
  430. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(slice|sorted|staticmethod|str|sum|super|tuple)>\ze\(' nextgroup=FunctionParameters
  431. syn match pythonBuiltinFunc '\v(\.)@<!\zs<(type|vars|zip)>\ze\(' nextgroup=FunctionParameters
  432. endif
  433. "
  434. " Builtin exceptions and warnings
  435. "
  436. if s:Enabled("g:python_highlight_exceptions")
  437. if s:Python2Syntax()
  438. syn keyword pythonExClass StandardError nextgroup=FunctionParameters
  439. else
  440. syn keyword pythonExClass BlockingIOError ChildProcessError nextgroup=FunctionParameters
  441. syn keyword pythonExClass ConnectionError BrokenPipeError nextgroup=FunctionParameters
  442. syn keyword pythonExClass ConnectionAbortedError ConnectionRefusedError nextgroup=FunctionParameters
  443. syn keyword pythonExClass ConnectionResetError FileExistsError nextgroup=FunctionParameters
  444. syn keyword pythonExClass FileNotFoundError InterruptedError nextgroup=FunctionParameters
  445. syn keyword pythonExClass IsADirectoryError NotADirectoryError nextgroup=FunctionParameters
  446. syn keyword pythonExClass PermissionError ProcessLookupError TimeoutError nextgroup=FunctionParameters
  447. syn keyword pythonExClass ResourceWarning nextgroup=FunctionParameters
  448. endif
  449. syn keyword pythonExClass BaseException nextgroup=FunctionParameters
  450. syn keyword pythonExClass Exception ArithmeticError nextgroup=FunctionParameters
  451. syn keyword pythonExClass LookupError EnvironmentError nextgroup=FunctionParameters
  452. syn keyword pythonExClass AssertionError AttributeError BufferError EOFError nextgroup=FunctionParameters
  453. syn keyword pythonExClass FloatingPointError GeneratorExit IOError nextgroup=FunctionParameters
  454. syn keyword pythonExClass ImportError IndexError KeyError nextgroup=FunctionParameters
  455. syn keyword pythonExClass KeyboardInterrupt MemoryError NameError nextgroup=FunctionParameters
  456. syn keyword pythonExClass NotImplementedError OSError OverflowError nextgroup=FunctionParameters
  457. syn keyword pythonExClass ReferenceError RuntimeError StopIteration nextgroup=FunctionParameters
  458. syn keyword pythonExClass SyntaxError IndentationError TabError nextgroup=FunctionParameters
  459. syn keyword pythonExClass SystemError SystemExit TypeError nextgroup=FunctionParameters
  460. syn keyword pythonExClass UnboundLocalError UnicodeError nextgroup=FunctionParameters
  461. syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError nextgroup=FunctionParameters
  462. syn keyword pythonExClass UnicodeTranslateError ValueError VMSError nextgroup=FunctionParameters
  463. syn keyword pythonExClass WindowsError ZeroDivisionError nextgroup=FunctionParameters
  464. syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning nextgroup=FunctionParameters
  465. syn keyword pythonExClass PendingDepricationWarning SyntaxWarning nextgroup=FunctionParameters
  466. syn keyword pythonExClass RuntimeWarning FutureWarning nextgroup=FunctionParameters
  467. syn keyword pythonExClass ImportWarning UnicodeWarning nextgroup=FunctionParameters
  468. endif
  469. if s:Enabled("g:python_slow_sync")
  470. syn sync minlines=2000
  471. else
  472. " This is fast but code inside triple quoted strings screws it up. It
  473. " is impossible to fix because the only way to know if you are inside a
  474. " triple quoted string is to start from the beginning of the file.
  475. syn sync match pythonSync grouphere NONE "):$"
  476. syn sync maxlines=200
  477. endif
  478. if version >= 508 || !exists("did_python_syn_inits")
  479. if version <= 508
  480. let did_python_syn_inits = 1
  481. command -nargs=+ HiLink hi link <args>
  482. else
  483. command -nargs=+ HiLink hi def link <args>
  484. endif
  485. HiLink pythonStatement Statement
  486. HiLink pythonImport Include
  487. HiLink pythonFunction Function
  488. HiLink pythonConditional Conditional
  489. HiLink pythonRepeat Repeat
  490. HiLink pythonException Exception
  491. HiLink pythonOperator Operator
  492. HiLink pythonDecorator Define
  493. HiLink pythonDottedName Function
  494. HiLink pythonDot Normal
  495. HiLink pythonComment Comment
  496. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  497. HiLink pythonCoding Special
  498. HiLink pythonRun Special
  499. endif
  500. HiLink pythonTodo Todo
  501. HiLink pythonError Error
  502. HiLink pythonIndentError Error
  503. HiLink pythonSpaceError Error
  504. HiLink pythonString String
  505. HiLink pythonRawString String
  506. HiLink pythonUniEscape Special
  507. HiLink pythonUniEscapeError Error
  508. if s:Python2Syntax()
  509. HiLink pythonUniString String
  510. HiLink pythonUniRawString String
  511. HiLink pythonUniRawEscape Special
  512. HiLink pythonUniRawEscapeError Error
  513. else
  514. HiLink pythonBytes String
  515. HiLink pythonRawBytes String
  516. HiLink pythonBytesContent String
  517. HiLink pythonBytesError Error
  518. HiLink pythonBytesEscape Special
  519. HiLink pythonBytesEscapeError Error
  520. HiLink pythonFString String
  521. HiLink pythonStrInterpRegion Special
  522. endif
  523. HiLink pythonStrFormatting Special
  524. HiLink pythonStrFormat Special
  525. HiLink pythonStrTemplate Special
  526. HiLink pythonDocTest Special
  527. HiLink pythonDocTest2 Special
  528. HiLink pythonNumber Number
  529. HiLink pythonHexNumber Number
  530. HiLink pythonOctNumber Number
  531. HiLink pythonBinNumber Number
  532. HiLink pythonFloat Float
  533. HiLink pythonNumberError Error
  534. HiLink pythonOctError Error
  535. HiLink pythonHexError Error
  536. HiLink pythonBinError Error
  537. HiLink pythonBoolean Boolean
  538. HiLink pythonBuiltinObj Structure
  539. HiLink pythonBuiltinFunc Function
  540. HiLink pythonExClass Structure
  541. HiLink pythonTypeAnno Optional
  542. HiLink pythonType Special
  543. delcommand HiLink
  544. endif
  545. let b:current_syntax = "python"