python.vim 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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_default_punctuation Highlight punctuation that does not
  69. " fall into any syntax group
  70. " python_highlight_builtins Highlight builtin functions and
  71. " objects
  72. " python_highlight_builtin_objs Highlight builtin objects only
  73. " python_highlight_builtin_funcs Highlight builtin functions only
  74. " python_highlight_exceptions Highlight standard exceptions
  75. " python_highlight_string_formatting Highlight % string formatting
  76. " python_highlight_string_format Highlight str.format syntax
  77. " python_highlight_string_templates Highlight string.Template syntax
  78. " python_highlight_indent_errors Highlight indentation errors
  79. " python_highlight_space_errors Highlight trailing spaces
  80. " python_highlight_doctests Highlight doc-tests
  81. " python_print_as_function Highlight 'print' statement as
  82. " function for Python 2
  83. " python_highlight_file_headers_as_comments
  84. " Highlight shebang and coding
  85. " headers as comments
  86. "
  87. " python_highlight_all Enable all the options above
  88. " NOTE: This option don't override
  89. " any previously set options
  90. "
  91. " python_slow_sync Can be set to 0 for slow machines
  92. "
  93. " For version 5.x: Clear all syntax items
  94. " For versions greater than 6.x: Quit when a syntax file was already loaded
  95. if version < 600
  96. syntax clear
  97. elseif exists("b:current_syntax")
  98. finish
  99. endif
  100. "
  101. " Commands
  102. "
  103. command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax
  104. command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax
  105. " Enable option if it's not defined
  106. function! s:EnableByDefault(name)
  107. if !exists(a:name)
  108. let {a:name} = 1
  109. endif
  110. endfunction
  111. " Check if option is enabled
  112. function! s:Enabled(name)
  113. return exists(a:name) && {a:name}
  114. endfunction
  115. " Is it Python 2 syntax?
  116. function! s:Python2Syntax()
  117. if exists("b:python_version_2")
  118. return b:python_version_2
  119. endif
  120. return s:Enabled("g:python_version_2")
  121. endfunction
  122. "
  123. " Default options
  124. "
  125. call s:EnableByDefault("g:python_slow_sync")
  126. if s:Enabled("g:python_highlight_all")
  127. call s:EnableByDefault("g:python_highlight_default_punctuation")
  128. call s:EnableByDefault("g:python_highlight_builtins")
  129. if s:Enabled("g:python_highlight_builtins")
  130. call s:EnableByDefault("g:python_highlight_builtin_objs")
  131. call s:EnableByDefault("g:python_highlight_builtin_funcs")
  132. endif
  133. call s:EnableByDefault("g:python_highlight_exceptions")
  134. call s:EnableByDefault("g:python_highlight_string_formatting")
  135. call s:EnableByDefault("g:python_highlight_string_format")
  136. call s:EnableByDefault("g:python_highlight_string_templates")
  137. call s:EnableByDefault("g:python_highlight_indent_errors")
  138. call s:EnableByDefault("g:python_highlight_space_errors")
  139. call s:EnableByDefault("g:python_highlight_doctests")
  140. call s:EnableByDefault("g:python_print_as_function")
  141. endif
  142. "
  143. " Default punctuation
  144. "
  145. if s:Enabled("g:python_highlight_default_punctuation")
  146. syn match pythonPunctuation "[^A-Za-z0-9_]"
  147. endif
  148. "
  149. " Keywords
  150. "
  151. syn keyword pythonStatement break continue del
  152. syn keyword pythonStatement exec return
  153. syn keyword pythonStatement pass raise
  154. syn keyword pythonStatement global assert
  155. syn keyword pythonStatement lambda
  156. syn keyword pythonStatement with
  157. syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
  158. syn keyword pythonRepeat for while
  159. syn keyword pythonConditional if elif else
  160. " The standard pyrex.vim unconditionally removes the pythonInclude group, so
  161. " we provide a dummy group here to avoid crashing pyrex.vim.
  162. syn keyword pythonInclude import
  163. syn keyword pythonImport import
  164. syn keyword pythonException try except finally
  165. syn keyword pythonOperator and in is not or
  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 None
  176. syn match pythonStatement "\<yield\s\+from\>" display
  177. syn keyword pythonBoolean True False
  178. syn match pythonFunction "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
  179. syn keyword pythonStatement await
  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. "
  185. " Decorators (new in Python 2.4)
  186. "
  187. syn match pythonDecorator "@" display nextgroup=pythonDottedName skipwhite
  188. if s:Python2Syntax()
  189. syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained
  190. else
  191. syn match pythonDottedName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*" display contained
  192. endif
  193. syn match pythonDot "\." display containedin=pythonDottedName
  194. "
  195. " Comments
  196. "
  197. syn match pythonComment "#.*$" display contains=pythonTodo,@Spell
  198. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  199. syn match pythonRun "\%^#!.*$"
  200. syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$"
  201. endif
  202. syn keyword pythonTodo TODO FIXME XXX contained
  203. "
  204. " Errors
  205. "
  206. syn match pythonError "\<\d\+\D\+\>" display
  207. syn match pythonError "[$?]" display
  208. syn match pythonError "[&|]\{2,}" display
  209. syn match pythonError "[=]\{3,}" display
  210. " Mixing spaces and tabs also may be used for pretty formatting multiline
  211. " statements
  212. if s:Enabled("g:python_highlight_indent_errors")
  213. syn match pythonIndentError "^\s*\%( \t\|\t \)\s*\S"me=e-1 display
  214. endif
  215. " Trailing space errors
  216. if s:Enabled("g:python_highlight_space_errors")
  217. syn match pythonSpaceError "\s\+$" display
  218. endif
  219. "
  220. " Strings
  221. "
  222. if s:Python2Syntax()
  223. " Python 2 strings
  224. syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  225. syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  226. syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  227. syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  228. else
  229. " Python 3 byte strings
  230. syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  231. syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell
  232. syn region pythonBytes start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell
  233. syn region pythonBytes start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell
  234. syn match pythonBytesError ".\+" display contained
  235. syn match pythonBytesContent "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError
  236. endif
  237. syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained
  238. syn match pythonBytesEscape "\\\o\o\=\o\=" display contained
  239. syn match pythonBytesEscapeError "\\\o\{,2}[89]" display contained
  240. syn match pythonBytesEscape "\\x\x\{2}" display contained
  241. syn match pythonBytesEscapeError "\\x\x\=\X" display contained
  242. syn match pythonBytesEscape "\\$"
  243. syn match pythonUniEscape "\\u\x\{4}" display contained
  244. syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained
  245. syn match pythonUniEscape "\\U\x\{8}" display contained
  246. syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained
  247. syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained
  248. syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained
  249. if s:Python2Syntax()
  250. " Python 2 Unicode strings
  251. syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  252. syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  253. syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  254. syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  255. else
  256. " Python 3 strings
  257. syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  258. syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell
  259. syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  260. syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell
  261. endif
  262. if s:Python2Syntax()
  263. " Python 2 Unicode raw strings
  264. syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  265. syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell
  266. syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell
  267. syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell
  268. syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained
  269. syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained
  270. endif
  271. " Python 2/3 raw strings
  272. if s:Python2Syntax()
  273. syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  274. syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  275. syn region pythonRawString start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  276. syn region pythonRawString start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  277. else
  278. syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  279. syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  280. syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  281. syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  282. syn region pythonRawBytes start=+[bB][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell
  283. syn region pythonRawBytes start=+[bB][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell
  284. syn region pythonRawBytes start=+[bB][rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell
  285. syn region pythonRawBytes start=+[bB][rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell
  286. endif
  287. syn match pythonRawEscape +\\['"]+ display transparent contained
  288. if s:Enabled("g:python_highlight_string_formatting")
  289. " % operator string formatting
  290. if s:Python2Syntax()
  291. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  292. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  293. else
  294. syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  295. syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString
  296. endif
  297. endif
  298. if s:Enabled("g:python_highlight_string_format")
  299. " str.format syntax
  300. if s:Python2Syntax()
  301. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  302. 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
  303. else
  304. syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString
  305. 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
  306. endif
  307. endif
  308. if s:Enabled("g:python_highlight_string_templates")
  309. " string.Template format
  310. if s:Python2Syntax()
  311. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  312. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  313. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
  314. else
  315. syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString
  316. syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString
  317. syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString
  318. endif
  319. endif
  320. if s:Enabled("g:python_highlight_doctests")
  321. " DocTests
  322. syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained
  323. syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained
  324. endif
  325. "
  326. " Numbers (ints, longs, floats, complex)
  327. "
  328. if s:Python2Syntax()
  329. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display
  330. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*[lL]\=\>" display
  331. syn match pythonBinError "\<0[bB][01]*\D\+\d*[lL]\=\>" display
  332. syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display
  333. syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display
  334. syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display
  335. syn match pythonNumberError "\<\d\+\D[lL]\=\>" display
  336. syn match pythonNumber "\<\d[lL]\=\>" display
  337. syn match pythonNumber "\<[0-9]\d\+[lL]\=\>" display
  338. syn match pythonNumber "\<\d\+[lLjJ]\>" display
  339. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display
  340. syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display
  341. else
  342. syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*\>" display
  343. syn match pythonOctError "\<0[oO]\=\o*\D\+\d*\>" display
  344. syn match pythonBinError "\<0[bB][01]*\D\+\d*\>" display
  345. syn match pythonHexNumber "\<0[xX]\x\+\>" display
  346. syn match pythonOctNumber "\<0[oO]\o\+\>" display
  347. syn match pythonBinNumber "\<0[bB][01]\+\>" display
  348. syn match pythonNumberError "\<\d\+\D\>" display
  349. syn match pythonNumberError "\<0\d\+\>" display
  350. syn match pythonNumber "\<\d\>" display
  351. syn match pythonNumber "\<[1-9]\d\+\>" display
  352. syn match pythonNumber "\<\d\+[jJ]\>" display
  353. syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*\>" display
  354. syn match pythonBinError "\<0[bB][01]*[2-9]\d*\>" display
  355. endif
  356. syn match pythonFloat "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display
  357. syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display
  358. syn match pythonFloat "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display
  359. "
  360. " Builtin objects and types
  361. "
  362. if s:Enabled("g:python_highlight_builtin_objs")
  363. if s:Python2Syntax()
  364. syn keyword pythonBuiltinObj None
  365. syn keyword pythonBoolean True False
  366. endif
  367. syn keyword pythonBuiltinObj Ellipsis NotImplemented
  368. syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__
  369. endif
  370. "
  371. " Builtin functions
  372. "
  373. if s:Enabled("g:python_highlight_builtin_funcs")
  374. if s:Python2Syntax()
  375. syn keyword pythonBuiltinFunc apply basestring buffer callable coerce
  376. syn keyword pythonBuiltinFunc execfile file help intern long raw_input
  377. syn keyword pythonBuiltinFunc reduce reload unichr unicode xrange
  378. if s:Enabled("g:python_print_as_function")
  379. syn keyword pythonBuiltinFunc print
  380. endif
  381. else
  382. syn keyword pythonBuiltinFunc ascii exec memoryview print
  383. endif
  384. syn keyword pythonBuiltinFunc __import__ abs all any
  385. syn keyword pythonBuiltinFunc bin bool bytearray bytes
  386. syn keyword pythonBuiltinFunc chr classmethod cmp compile complex
  387. syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval
  388. syn keyword pythonBuiltinFunc filter float format frozenset getattr
  389. syn keyword pythonBuiltinFunc globals hasattr hash hex id
  390. syn keyword pythonBuiltinFunc input int isinstance
  391. syn keyword pythonBuiltinFunc issubclass iter len list locals map max
  392. syn keyword pythonBuiltinFunc min next object oct open ord
  393. syn keyword pythonBuiltinFunc pow property range
  394. syn keyword pythonBuiltinFunc repr reversed round set setattr
  395. syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple
  396. syn keyword pythonBuiltinFunc type vars zip
  397. endif
  398. "
  399. " Builtin exceptions and warnings
  400. "
  401. if s:Enabled("g:python_highlight_exceptions")
  402. if s:Python2Syntax()
  403. syn keyword pythonExClass StandardError
  404. else
  405. syn keyword pythonExClass BlockingIOError ChildProcessError
  406. syn keyword pythonExClass ConnectionError BrokenPipeError
  407. syn keyword pythonExClass ConnectionAbortedError ConnectionRefusedError
  408. syn keyword pythonExClass ConnectionResetError FileExistsError
  409. syn keyword pythonExClass FileNotFoundError InterruptedError
  410. syn keyword pythonExClass IsADirectoryError NotADirectoryError
  411. syn keyword pythonExClass PermissionError ProcessLookupError TimeoutError
  412. syn keyword pythonExClass ResourceWarning
  413. endif
  414. syn keyword pythonExClass BaseException
  415. syn keyword pythonExClass Exception ArithmeticError
  416. syn keyword pythonExClass LookupError EnvironmentError
  417. syn keyword pythonExClass AssertionError AttributeError BufferError EOFError
  418. syn keyword pythonExClass FloatingPointError GeneratorExit IOError
  419. syn keyword pythonExClass ImportError IndexError KeyError
  420. syn keyword pythonExClass KeyboardInterrupt MemoryError NameError
  421. syn keyword pythonExClass NotImplementedError OSError OverflowError
  422. syn keyword pythonExClass ReferenceError RuntimeError StopIteration
  423. syn keyword pythonExClass SyntaxError IndentationError TabError
  424. syn keyword pythonExClass SystemError SystemExit TypeError
  425. syn keyword pythonExClass UnboundLocalError UnicodeError
  426. syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError
  427. syn keyword pythonExClass UnicodeTranslateError ValueError VMSError
  428. syn keyword pythonExClass WindowsError ZeroDivisionError
  429. syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning
  430. syn keyword pythonExClass PendingDepricationWarning SyntaxWarning
  431. syn keyword pythonExClass RuntimeWarning FutureWarning
  432. syn keyword pythonExClass ImportWarning UnicodeWarning
  433. endif
  434. if s:Enabled("g:python_slow_sync")
  435. syn sync minlines=2000
  436. else
  437. " This is fast but code inside triple quoted strings screws it up. It
  438. " is impossible to fix because the only way to know if you are inside a
  439. " triple quoted string is to start from the beginning of the file.
  440. syn sync match pythonSync grouphere NONE "):$"
  441. syn sync maxlines=200
  442. endif
  443. if version >= 508 || !exists("did_python_syn_inits")
  444. if version <= 508
  445. let did_python_syn_inits = 1
  446. command -nargs=+ HiLink hi link <args>
  447. else
  448. command -nargs=+ HiLink hi def link <args>
  449. endif
  450. HiLink pythonPunctuation Special
  451. HiLink pythonStatement Statement
  452. HiLink pythonImport Include
  453. HiLink pythonFunction Function
  454. HiLink pythonConditional Conditional
  455. HiLink pythonRepeat Repeat
  456. HiLink pythonException Exception
  457. HiLink pythonOperator Operator
  458. HiLink pythonDecorator Define
  459. HiLink pythonDottedName Function
  460. HiLink pythonDot Normal
  461. HiLink pythonComment Comment
  462. if !s:Enabled("g:python_highlight_file_headers_as_comments")
  463. HiLink pythonCoding Special
  464. HiLink pythonRun Special
  465. endif
  466. HiLink pythonTodo Todo
  467. HiLink pythonError Error
  468. HiLink pythonIndentError Error
  469. HiLink pythonSpaceError Error
  470. HiLink pythonString String
  471. HiLink pythonRawString String
  472. HiLink pythonUniEscape Special
  473. HiLink pythonUniEscapeError Error
  474. if s:Python2Syntax()
  475. HiLink pythonUniString String
  476. HiLink pythonUniRawString String
  477. HiLink pythonUniRawEscape Special
  478. HiLink pythonUniRawEscapeError Error
  479. else
  480. HiLink pythonBytes String
  481. HiLink pythonRawBytes String
  482. HiLink pythonBytesContent String
  483. HiLink pythonBytesError Error
  484. HiLink pythonBytesEscape Special
  485. HiLink pythonBytesEscapeError Error
  486. endif
  487. HiLink pythonStrFormatting Special
  488. HiLink pythonStrFormat Special
  489. HiLink pythonStrTemplate Special
  490. HiLink pythonDocTest Special
  491. HiLink pythonDocTest2 Special
  492. HiLink pythonNumber Number
  493. HiLink pythonHexNumber Number
  494. HiLink pythonOctNumber Number
  495. HiLink pythonBinNumber Number
  496. HiLink pythonFloat Float
  497. HiLink pythonNumberError Error
  498. HiLink pythonOctError Error
  499. HiLink pythonHexError Error
  500. HiLink pythonBinError Error
  501. HiLink pythonBoolean Boolean
  502. HiLink pythonBuiltinObj Structure
  503. HiLink pythonBuiltinFunc Function
  504. HiLink pythonExClass Structure
  505. delcommand HiLink
  506. endif
  507. let b:current_syntax = "python"