PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vim73/syntax/python.vim

https://gitlab.com/shinvdu/vim_win_config
Vim Script | 295 lines | 211 code | 12 blank | 72 comment | 13 complexity | a28b23753417d7a5c39224fe37e58a3a MD5 | raw file
  1. " Vim syntax file
  2. " Language: Python
  3. " Maintainer: Neil Schemenauer <nas@python.ca>
  4. " Last Change: 2009-10-13
  5. " Credits: Zvezdan Petkovic <zpetkovic@acm.org>
  6. " Neil Schemenauer <nas@python.ca>
  7. " Dmitry Vasiliev
  8. "
  9. " This version is a major rewrite by Zvezdan Petkovic.
  10. "
  11. " - introduced highlighting of doctests
  12. " - updated keywords, built-ins, and exceptions
  13. " - corrected regular expressions for
  14. "
  15. " * functions
  16. " * decorators
  17. " * strings
  18. " * escapes
  19. " * numbers
  20. " * space error
  21. "
  22. " - corrected synchronization
  23. " - more highlighting is ON by default, except
  24. " - space error highlighting is OFF by default
  25. "
  26. " Optional highlighting can be controlled using these variables.
  27. "
  28. " let python_no_builtin_highlight = 1
  29. " let python_no_doctest_code_highlight = 1
  30. " let python_no_doctest_highlight = 1
  31. " let python_no_exception_highlight = 1
  32. " let python_no_number_highlight = 1
  33. " let python_space_error_highlight = 1
  34. "
  35. " All the options above can be switched on together.
  36. "
  37. " let python_highlight_all = 1
  38. "
  39. " For version 5.x: Clear all syntax items.
  40. " For version 6.x: Quit when a syntax file was already loaded.
  41. if version < 600
  42. syntax clear
  43. elseif exists("b:current_syntax")
  44. finish
  45. endif
  46. " Keep Python keywords in alphabetical order inside groups for easy
  47. " comparison with the table in the 'Python Language Reference'
  48. " http://docs.python.org/reference/lexical_analysis.html#keywords.
  49. " Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
  50. " Exceptions come last at the end of each group (class and def below).
  51. "
  52. " Keywords 'with' and 'as' are new in Python 2.6
  53. " (use 'from __future__ import with_statement' in Python 2.5).
  54. "
  55. " Some compromises had to be made to support both Python 3.0 and 2.6.
  56. " We include Python 3.0 features, but when a definition is duplicated,
  57. " the last definition takes precedence.
  58. "
  59. " - 'False', 'None', and 'True' are keywords in Python 3.0 but they are
  60. " built-ins in 2.6 and will be highlighted as built-ins below.
  61. " - 'exec' is a built-in in Python 3.0 and will be highlighted as
  62. " built-in below.
  63. " - 'nonlocal' is a keyword in Python 3.0 and will be highlighted.
  64. " - 'print' is a built-in in Python 3.0 and will be highlighted as
  65. " built-in below (use 'from __future__ import print_function' in 2.6)
  66. "
  67. syn keyword pythonStatement False, None, True
  68. syn keyword pythonStatement as assert break continue del exec global
  69. syn keyword pythonStatement lambda nonlocal pass print return with yield
  70. syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
  71. syn keyword pythonConditional elif else if
  72. syn keyword pythonRepeat for while
  73. syn keyword pythonOperator and in is not or
  74. syn keyword pythonException except finally raise try
  75. syn keyword pythonInclude from import
  76. " Decorators (new in Python 2.4)
  77. syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
  78. " The zero-length non-grouping match before the function name is
  79. " extremely important in pythonFunction. Without it, everything is
  80. " interpreted as a function inside the contained environment of
  81. " doctests.
  82. " A dot must be allowed because of @MyClass.myfunc decorators.
  83. syn match pythonFunction
  84. \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
  85. syn match pythonComment "#.*$" contains=pythonTodo,@Spell
  86. syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
  87. " Triple-quoted strings can contain doctests.
  88. syn region pythonString
  89. \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
  90. \ contains=pythonEscape,@Spell
  91. syn region pythonString
  92. \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
  93. \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
  94. syn region pythonRawString
  95. \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
  96. \ contains=@Spell
  97. syn region pythonRawString
  98. \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
  99. \ contains=pythonSpaceError,pythonDoctest,@Spell
  100. syn match pythonEscape +\\[abfnrtv'"\\]+ contained
  101. syn match pythonEscape "\\\o\{1,3}" contained
  102. syn match pythonEscape "\\x\x\{2}" contained
  103. syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
  104. " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
  105. syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
  106. syn match pythonEscape "\\$"
  107. if exists("python_highlight_all")
  108. if exists("python_no_builtin_highlight")
  109. unlet python_no_builtin_highlight
  110. endif
  111. if exists("python_no_doctest_code_highlight")
  112. unlet python_no_doctest_code_highlight
  113. endif
  114. if exists("python_no_doctest_highlight")
  115. unlet python_no_doctest_highlight
  116. endif
  117. if exists("python_no_exception_highlight")
  118. unlet python_no_exception_highlight
  119. endif
  120. if exists("python_no_number_highlight")
  121. unlet python_no_number_highlight
  122. endif
  123. let python_space_error_highlight = 1
  124. endif
  125. " It is very important to understand all details before changing the
  126. " regular expressions below or their order.
  127. " The word boundaries are *not* the floating-point number boundaries
  128. " because of a possible leading or trailing decimal point.
  129. " The expressions below ensure that all valid number literals are
  130. " highlighted, and invalid number literals are not. For example,
  131. "
  132. " - a decimal point in '4.' at the end of a line is highlighted,
  133. " - a second dot in 1.0.0 is not highlighted,
  134. " - 08 is not highlighted,
  135. " - 08e0 or 08j are highlighted,
  136. "
  137. " and so on, as specified in the 'Python Language Reference'.
  138. " http://docs.python.org/reference/lexical_analysis.html#numeric-literals
  139. if !exists("python_no_number_highlight")
  140. " numbers (including longs and complex)
  141. syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
  142. syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
  143. syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
  144. syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
  145. syn match pythonNumber "\<\d\+[jJ]\>"
  146. syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
  147. syn match pythonNumber
  148. \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
  149. syn match pythonNumber
  150. \ "\%(^\|\W\)\@<=\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
  151. endif
  152. " Group the built-ins in the order in the 'Python Library Reference' for
  153. " easier comparison.
  154. " http://docs.python.org/library/constants.html
  155. " http://docs.python.org/library/functions.html
  156. " http://docs.python.org/library/functions.html#non-essential-built-in-functions
  157. " Python built-in functions are in alphabetical order.
  158. if !exists("python_no_builtin_highlight")
  159. " built-in constants
  160. " 'False', 'True', and 'None' are also reserved words in Python 3.0
  161. syn keyword pythonBuiltin False True None
  162. syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
  163. " built-in functions
  164. syn keyword pythonBuiltin abs all any bin bool chr classmethod
  165. syn keyword pythonBuiltin compile complex delattr dict dir divmod
  166. syn keyword pythonBuiltin enumerate eval filter float format
  167. syn keyword pythonBuiltin frozenset getattr globals hasattr hash
  168. syn keyword pythonBuiltin help hex id input int isinstance
  169. syn keyword pythonBuiltin issubclass iter len list locals map max
  170. syn keyword pythonBuiltin min next object oct open ord pow print
  171. syn keyword pythonBuiltin property range repr reversed round set
  172. syn keyword pythonBuiltin setattr slice sorted staticmethod str
  173. syn keyword pythonBuiltin sum super tuple type vars zip __import__
  174. " Python 2.6 only
  175. syn keyword pythonBuiltin basestring callable cmp execfile file
  176. syn keyword pythonBuiltin long raw_input reduce reload unichr
  177. syn keyword pythonBuiltin unicode xrange
  178. " Python 3.0 only
  179. syn keyword pythonBuiltin ascii bytearray bytes exec memoryview
  180. " non-essential built-in functions; Python 2.6 only
  181. syn keyword pythonBuiltin apply buffer coerce intern
  182. endif
  183. " From the 'Python Library Reference' class hierarchy at the bottom.
  184. " http://docs.python.org/library/exceptions.html
  185. if !exists("python_no_exception_highlight")
  186. " builtin base exceptions (only used as base classes for other exceptions)
  187. syn keyword pythonExceptions BaseException Exception
  188. syn keyword pythonExceptions ArithmeticError EnvironmentError
  189. syn keyword pythonExceptions LookupError
  190. " builtin base exception removed in Python 3.0
  191. syn keyword pythonExceptions StandardError
  192. " builtin exceptions (actually raised)
  193. syn keyword pythonExceptions AssertionError AttributeError BufferError
  194. syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
  195. syn keyword pythonExceptions IOError ImportError IndentationError
  196. syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
  197. syn keyword pythonExceptions MemoryError NameError NotImplementedError
  198. syn keyword pythonExceptions OSError OverflowError ReferenceError
  199. syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
  200. syn keyword pythonExceptions SystemError SystemExit TabError TypeError
  201. syn keyword pythonExceptions UnboundLocalError UnicodeError
  202. syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
  203. syn keyword pythonExceptions UnicodeTranslateError ValueError VMSError
  204. syn keyword pythonExceptions WindowsError ZeroDivisionError
  205. " builtin warnings
  206. syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
  207. syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
  208. syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
  209. syn keyword pythonExceptions UserWarning Warning
  210. endif
  211. if exists("python_space_error_highlight")
  212. " trailing whitespace
  213. syn match pythonSpaceError display excludenl "\s\+$"
  214. " mixed tabs and spaces
  215. syn match pythonSpaceError display " \+\t"
  216. syn match pythonSpaceError display "\t\+ "
  217. endif
  218. " Do not spell doctests inside strings.
  219. " Notice that the end of a string, either ''', or """, will end the contained
  220. " doctest too. Thus, we do *not* need to have it as an end pattern.
  221. if !exists("python_no_doctest_highlight")
  222. if !exists("python_no_doctest_code_higlight")
  223. syn region pythonDoctest
  224. \ start="^\s*>>>\s" end="^\s*$"
  225. \ contained contains=ALLBUT,pythonDoctest,@Spell
  226. syn region pythonDoctestValue
  227. \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
  228. \ contained
  229. else
  230. syn region pythonDoctest
  231. \ start="^\s*>>>" end="^\s*$"
  232. \ contained contains=@NoSpell
  233. endif
  234. endif
  235. " Sync at the beginning of class, function, or method definition.
  236. syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
  237. if version >= 508 || !exists("did_python_syn_inits")
  238. if version <= 508
  239. let did_python_syn_inits = 1
  240. command -nargs=+ HiLink hi link <args>
  241. else
  242. command -nargs=+ HiLink hi def link <args>
  243. endif
  244. " The default highlight links. Can be overridden later.
  245. HiLink pythonStatement Statement
  246. HiLink pythonConditional Conditional
  247. HiLink pythonRepeat Repeat
  248. HiLink pythonOperator Operator
  249. HiLink pythonException Exception
  250. HiLink pythonInclude Include
  251. HiLink pythonDecorator Define
  252. HiLink pythonFunction Function
  253. HiLink pythonComment Comment
  254. HiLink pythonTodo Todo
  255. HiLink pythonString String
  256. HiLink pythonRawString String
  257. HiLink pythonEscape Special
  258. if !exists("python_no_number_highlight")
  259. HiLink pythonNumber Number
  260. endif
  261. if !exists("python_no_builtin_highlight")
  262. HiLink pythonBuiltin Function
  263. endif
  264. if !exists("python_no_exception_highlight")
  265. HiLink pythonExceptions Structure
  266. endif
  267. if exists("python_space_error_highlight")
  268. HiLink pythonSpaceError Error
  269. endif
  270. if !exists("python_no_doctest_highlight")
  271. HiLink pythonDoctest Special
  272. HiLink pythonDoctestValue Define
  273. endif
  274. delcommand HiLink
  275. endif
  276. let b:current_syntax = "python"
  277. " vim:set sw=2 sts=2 ts=8 noet: