lua.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. " Vim syntax file
  2. " Language: Lua 4.0, Lua 5.0, Lua 5.1 and Lua 5.2
  3. " Maintainer: Marcus Aurelius Farias <masserahguard-lua 'at' yahoo com>
  4. " First Author: Carlos Augusto Teixeira Mendes <cmendes 'at' inf puc-rio br>
  5. " Last Change: 2012 Aug 12
  6. " Options: lua_version = 4 or 5
  7. " lua_subversion = 0 (4.0, 5.0) or 1 (5.1) or 2 (5.2)
  8. " default 5.2
  9. " quit when a syntax file was already loaded
  10. if exists("b:current_syntax")
  11. finish
  12. endif
  13. let s:cpo_save = &cpo
  14. set cpo&vim
  15. if !exists("lua_version")
  16. " Default is lua 5.2
  17. let lua_version = 5
  18. let lua_subversion = 2
  19. elseif !exists("lua_subversion")
  20. " lua_version exists, but lua_subversion doesn't. So, set it to 0
  21. let lua_subversion = 0
  22. endif
  23. syn case match
  24. " syncing method
  25. syn sync minlines=100
  26. " Comments
  27. syn keyword luaTodo contained TODO FIXME XXX
  28. syn match luaComment "--.*$" contains=luaTodo,@Spell
  29. if lua_version == 5 && lua_subversion == 0
  30. syn region luaComment matchgroup=luaComment start="--\[\[" end="\]\]" contains=luaTodo,luaInnerComment,@Spell
  31. syn region luaInnerComment contained transparent start="\[\[" end="\]\]"
  32. elseif lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
  33. " Comments in Lua 5.1: --[[ ... ]], [=[ ... ]=], [===[ ... ]===], etc.
  34. syn region luaComment matchgroup=luaComment start="--\[\z(=*\)\[" end="\]\z1\]" contains=luaTodo,@Spell
  35. endif
  36. " First line may start with #!
  37. syn match luaComment "\%^#!.*"
  38. " catch errors caused by wrong parenthesis and wrong curly brackets or
  39. " keywords placed outside their respective blocks
  40. syn region luaParen transparent start='(' end=')' contains=ALLBUT,luaParenError,luaTodo,luaSpecial,luaIfThen,luaElseifThen,luaElse,luaThenEnd,luaBlock,luaLoopBlock,luaIn,luaStatement
  41. syn region luaTableBlock transparent matchgroup=luaTable start="{" end="}" contains=ALLBUT,luaBraceError,luaTodo,luaSpecial,luaIfThen,luaElseifThen,luaElse,luaThenEnd,luaBlock,luaLoopBlock,luaIn,luaStatement
  42. syn match luaParenError ")"
  43. syn match luaBraceError "}"
  44. syn match luaError "\<\%(end\|else\|elseif\|then\|until\|in\)\>"
  45. " function ... end
  46. syn region luaFunctionBlock transparent matchgroup=luaFunction start="\<function\>" end="\<end\>" contains=ALLBUT,luaTodo,luaSpecial,luaElseifThen,luaElse,luaThenEnd,luaIn
  47. " if ... then
  48. syn region luaIfThen transparent matchgroup=luaCond start="\<if\>" end="\<then\>"me=e-4 contains=ALLBUT,luaTodo,luaSpecial,luaElseifThen,luaElse,luaIn nextgroup=luaThenEnd skipwhite skipempty
  49. " then ... end
  50. syn region luaThenEnd contained transparent matchgroup=luaCond start="\<then\>" end="\<end\>" contains=ALLBUT,luaTodo,luaSpecial,luaThenEnd,luaIn
  51. " elseif ... then
  52. syn region luaElseifThen contained transparent matchgroup=luaCond start="\<elseif\>" end="\<then\>" contains=ALLBUT,luaTodo,luaSpecial,luaElseifThen,luaElse,luaThenEnd,luaIn
  53. " else
  54. syn keyword luaElse contained else
  55. " do ... end
  56. syn region luaBlock transparent matchgroup=luaStatement start="\<do\>" end="\<end\>" contains=ALLBUT,luaTodo,luaSpecial,luaElseifThen,luaElse,luaThenEnd,luaIn
  57. " repeat ... until
  58. syn region luaLoopBlock transparent matchgroup=luaRepeat start="\<repeat\>" end="\<until\>" contains=ALLBUT,luaTodo,luaSpecial,luaElseifThen,luaElse,luaThenEnd,luaIn
  59. " while ... do
  60. syn region luaLoopBlock transparent matchgroup=luaRepeat start="\<while\>" end="\<do\>"me=e-2 contains=ALLBUT,luaTodo,luaSpecial,luaIfThen,luaElseifThen,luaElse,luaThenEnd,luaIn nextgroup=luaBlock skipwhite skipempty
  61. " for ... do and for ... in ... do
  62. syn region luaLoopBlock transparent matchgroup=luaRepeat start="\<for\>" end="\<do\>"me=e-2 contains=ALLBUT,luaTodo,luaSpecial,luaIfThen,luaElseifThen,luaElse,luaThenEnd nextgroup=luaBlock skipwhite skipempty
  63. syn keyword luaIn contained in
  64. " other keywords
  65. syn keyword luaStatement return local break
  66. if lua_version > 5 || (lua_version == 5 && lua_subversion >= 2)
  67. syn keyword luaStatement goto
  68. syn match luaLabel "::\I\i*::"
  69. endif
  70. syn keyword luaOperator and or not
  71. syn keyword luaConstant nil
  72. if lua_version > 4
  73. syn keyword luaConstant true false
  74. endif
  75. " Strings
  76. if lua_version < 5
  77. syn match luaSpecial contained "\\[\\abfnrtv\'\"]\|\\[[:digit:]]\{,3}"
  78. elseif lua_version == 5
  79. if lua_subversion == 0
  80. syn match luaSpecial contained #\\[\\abfnrtv'"[\]]\|\\[[:digit:]]\{,3}#
  81. syn region luaString2 matchgroup=luaString start=+\[\[+ end=+\]\]+ contains=luaString2,@Spell
  82. else
  83. if lua_subversion == 1
  84. syn match luaSpecial contained #\\[\\abfnrtv'"]\|\\[[:digit:]]\{,3}#
  85. else " Lua 5.2
  86. syn match luaSpecial contained #\\[\\abfnrtvz'"]\|\\x[[:xdigit:]]\{2}\|\\[[:digit:]]\{,3}#
  87. endif
  88. syn region luaString2 matchgroup=luaString start="\[\z(=*\)\[" end="\]\z1\]" contains=@Spell
  89. endif
  90. endif
  91. syn region luaString start=+'+ end=+'+ skip=+\\\\\|\\'+ contains=luaSpecial,@Spell
  92. syn region luaString start=+"+ end=+"+ skip=+\\\\\|\\"+ contains=luaSpecial,@Spell
  93. " integer number
  94. syn match luaNumber "\<\d\+\>"
  95. " floating point number, with dot, optional exponent
  96. syn match luaNumber "\<\d\+\.\d*\%([eE][-+]\=\d\+\)\=\>"
  97. " floating point number, starting with a dot, optional exponent
  98. syn match luaNumber "\.\d\+\%([eE][-+]\=\d\+\)\=\>"
  99. " floating point number, without dot, with exponent
  100. syn match luaNumber "\<\d\+[eE][-+]\=\d\+\>"
  101. " hex numbers
  102. if lua_version >= 5
  103. if lua_subversion == 1
  104. syn match luaNumber "\<0[xX]\x\+\>"
  105. elseif lua_subversion >= 2
  106. syn match luaNumber "\<0[xX][[:xdigit:].]\+\%([pP][-+]\=\d\+\)\=\>"
  107. endif
  108. endif
  109. syn keyword luaFunc assert collectgarbage dofile error next
  110. syn keyword luaFunc print rawget rawset tonumber tostring type _VERSION
  111. if lua_version == 4
  112. syn keyword luaFunc _ALERT _ERRORMESSAGE gcinfo
  113. syn keyword luaFunc call copytagmethods dostring
  114. syn keyword luaFunc foreach foreachi getglobal getn
  115. syn keyword luaFunc gettagmethod globals newtag
  116. syn keyword luaFunc setglobal settag settagmethod sort
  117. syn keyword luaFunc tag tinsert tremove
  118. syn keyword luaFunc _INPUT _OUTPUT _STDIN _STDOUT _STDERR
  119. syn keyword luaFunc openfile closefile flush seek
  120. syn keyword luaFunc setlocale execute remove rename tmpname
  121. syn keyword luaFunc getenv date clock exit
  122. syn keyword luaFunc readfrom writeto appendto read write
  123. syn keyword luaFunc PI abs sin cos tan asin
  124. syn keyword luaFunc acos atan atan2 ceil floor
  125. syn keyword luaFunc mod frexp ldexp sqrt min max log
  126. syn keyword luaFunc log10 exp deg rad random
  127. syn keyword luaFunc randomseed strlen strsub strlower strupper
  128. syn keyword luaFunc strchar strrep ascii strbyte
  129. syn keyword luaFunc format strfind gsub
  130. syn keyword luaFunc getinfo getlocal setlocal setcallhook setlinehook
  131. elseif lua_version == 5
  132. syn keyword luaFunc getmetatable setmetatable
  133. syn keyword luaFunc ipairs pairs
  134. syn keyword luaFunc pcall xpcall
  135. syn keyword luaFunc _G loadfile rawequal require
  136. if lua_subversion == 0
  137. syn keyword luaFunc getfenv setfenv
  138. syn keyword luaFunc loadstring unpack
  139. syn keyword luaFunc gcinfo loadlib LUA_PATH _LOADED _REQUIREDNAME
  140. else
  141. syn keyword luaFunc load select
  142. syn match luaFunc /\<package\.cpath\>/
  143. syn match luaFunc /\<package\.loaded\>/
  144. syn match luaFunc /\<package\.loadlib\>/
  145. syn match luaFunc /\<package\.path\>/
  146. if lua_subversion == 1
  147. syn keyword luaFunc getfenv setfenv
  148. syn keyword luaFunc loadstring module unpack
  149. syn match luaFunc /\<package\.loaders\>/
  150. syn match luaFunc /\<package\.preload\>/
  151. syn match luaFunc /\<package\.seeall\>/
  152. elseif lua_subversion == 2
  153. syn keyword luaFunc _ENV rawlen
  154. syn match luaFunc /\<package\.config\>/
  155. syn match luaFunc /\<package\.preload\>/
  156. syn match luaFunc /\<package\.searchers\>/
  157. syn match luaFunc /\<package\.searchpath\>/
  158. syn match luaFunc /\<bit32\.arshift\>/
  159. syn match luaFunc /\<bit32\.band\>/
  160. syn match luaFunc /\<bit32\.bnot\>/
  161. syn match luaFunc /\<bit32\.bor\>/
  162. syn match luaFunc /\<bit32\.btest\>/
  163. syn match luaFunc /\<bit32\.bxor\>/
  164. syn match luaFunc /\<bit32\.extract\>/
  165. syn match luaFunc /\<bit32\.lrotate\>/
  166. syn match luaFunc /\<bit32\.lshift\>/
  167. syn match luaFunc /\<bit32\.replace\>/
  168. syn match luaFunc /\<bit32\.rrotate\>/
  169. syn match luaFunc /\<bit32\.rshift\>/
  170. endif
  171. syn match luaFunc /\<coroutine\.running\>/
  172. endif
  173. syn match luaFunc /\<coroutine\.create\>/
  174. syn match luaFunc /\<coroutine\.resume\>/
  175. syn match luaFunc /\<coroutine\.status\>/
  176. syn match luaFunc /\<coroutine\.wrap\>/
  177. syn match luaFunc /\<coroutine\.yield\>/
  178. syn match luaFunc /\<string\.byte\>/
  179. syn match luaFunc /\<string\.char\>/
  180. syn match luaFunc /\<string\.dump\>/
  181. syn match luaFunc /\<string\.find\>/
  182. syn match luaFunc /\<string\.format\>/
  183. syn match luaFunc /\<string\.gsub\>/
  184. syn match luaFunc /\<string\.len\>/
  185. syn match luaFunc /\<string\.lower\>/
  186. syn match luaFunc /\<string\.rep\>/
  187. syn match luaFunc /\<string\.sub\>/
  188. syn match luaFunc /\<string\.upper\>/
  189. if lua_subversion == 0
  190. syn match luaFunc /\<string\.gfind\>/
  191. else
  192. syn match luaFunc /\<string\.gmatch\>/
  193. syn match luaFunc /\<string\.match\>/
  194. syn match luaFunc /\<string\.reverse\>/
  195. endif
  196. if lua_subversion == 0
  197. syn match luaFunc /\<table\.getn\>/
  198. syn match luaFunc /\<table\.setn\>/
  199. syn match luaFunc /\<table\.foreach\>/
  200. syn match luaFunc /\<table\.foreachi\>/
  201. elseif lua_subversion == 1
  202. syn match luaFunc /\<table\.maxn\>/
  203. elseif lua_subversion == 2
  204. syn match luaFunc /\<table\.pack\>/
  205. syn match luaFunc /\<table\.unpack\>/
  206. endif
  207. syn match luaFunc /\<table\.concat\>/
  208. syn match luaFunc /\<table\.sort\>/
  209. syn match luaFunc /\<table\.insert\>/
  210. syn match luaFunc /\<table\.remove\>/
  211. syn match luaFunc /\<math\.abs\>/
  212. syn match luaFunc /\<math\.acos\>/
  213. syn match luaFunc /\<math\.asin\>/
  214. syn match luaFunc /\<math\.atan\>/
  215. syn match luaFunc /\<math\.atan2\>/
  216. syn match luaFunc /\<math\.ceil\>/
  217. syn match luaFunc /\<math\.sin\>/
  218. syn match luaFunc /\<math\.cos\>/
  219. syn match luaFunc /\<math\.tan\>/
  220. syn match luaFunc /\<math\.deg\>/
  221. syn match luaFunc /\<math\.exp\>/
  222. syn match luaFunc /\<math\.floor\>/
  223. syn match luaFunc /\<math\.log\>/
  224. syn match luaFunc /\<math\.max\>/
  225. syn match luaFunc /\<math\.min\>/
  226. if lua_subversion == 0
  227. syn match luaFunc /\<math\.mod\>/
  228. syn match luaFunc /\<math\.log10\>/
  229. else
  230. if lua_subversion == 1
  231. syn match luaFunc /\<math\.log10\>/
  232. endif
  233. syn match luaFunc /\<math\.huge\>/
  234. syn match luaFunc /\<math\.fmod\>/
  235. syn match luaFunc /\<math\.modf\>/
  236. syn match luaFunc /\<math\.cosh\>/
  237. syn match luaFunc /\<math\.sinh\>/
  238. syn match luaFunc /\<math\.tanh\>/
  239. endif
  240. syn match luaFunc /\<math\.pow\>/
  241. syn match luaFunc /\<math\.rad\>/
  242. syn match luaFunc /\<math\.sqrt\>/
  243. syn match luaFunc /\<math\.frexp\>/
  244. syn match luaFunc /\<math\.ldexp\>/
  245. syn match luaFunc /\<math\.random\>/
  246. syn match luaFunc /\<math\.randomseed\>/
  247. syn match luaFunc /\<math\.pi\>/
  248. syn match luaFunc /\<io\.close\>/
  249. syn match luaFunc /\<io\.flush\>/
  250. syn match luaFunc /\<io\.input\>/
  251. syn match luaFunc /\<io\.lines\>/
  252. syn match luaFunc /\<io\.open\>/
  253. syn match luaFunc /\<io\.output\>/
  254. syn match luaFunc /\<io\.popen\>/
  255. syn match luaFunc /\<io\.read\>/
  256. syn match luaFunc /\<io\.stderr\>/
  257. syn match luaFunc /\<io\.stdin\>/
  258. syn match luaFunc /\<io\.stdout\>/
  259. syn match luaFunc /\<io\.tmpfile\>/
  260. syn match luaFunc /\<io\.type\>/
  261. syn match luaFunc /\<io\.write\>/
  262. syn match luaFunc /\<os\.clock\>/
  263. syn match luaFunc /\<os\.date\>/
  264. syn match luaFunc /\<os\.difftime\>/
  265. syn match luaFunc /\<os\.execute\>/
  266. syn match luaFunc /\<os\.exit\>/
  267. syn match luaFunc /\<os\.getenv\>/
  268. syn match luaFunc /\<os\.remove\>/
  269. syn match luaFunc /\<os\.rename\>/
  270. syn match luaFunc /\<os\.setlocale\>/
  271. syn match luaFunc /\<os\.time\>/
  272. syn match luaFunc /\<os\.tmpname\>/
  273. syn match luaFunc /\<debug\.debug\>/
  274. syn match luaFunc /\<debug\.gethook\>/
  275. syn match luaFunc /\<debug\.getinfo\>/
  276. syn match luaFunc /\<debug\.getlocal\>/
  277. syn match luaFunc /\<debug\.getupvalue\>/
  278. syn match luaFunc /\<debug\.setlocal\>/
  279. syn match luaFunc /\<debug\.setupvalue\>/
  280. syn match luaFunc /\<debug\.sethook\>/
  281. syn match luaFunc /\<debug\.traceback\>/
  282. if lua_subversion == 1
  283. syn match luaFunc /\<debug\.getfenv\>/
  284. syn match luaFunc /\<debug\.setfenv\>/
  285. syn match luaFunc /\<debug\.getmetatable\>/
  286. syn match luaFunc /\<debug\.setmetatable\>/
  287. syn match luaFunc /\<debug\.getregistry\>/
  288. elseif lua_subversion == 2
  289. syn match luaFunc /\<debug\.getmetatable\>/
  290. syn match luaFunc /\<debug\.setmetatable\>/
  291. syn match luaFunc /\<debug\.getregistry\>/
  292. syn match luaFunc /\<debug\.getuservalue\>/
  293. syn match luaFunc /\<debug\.setuservalue\>/
  294. syn match luaFunc /\<debug\.upvalueid\>/
  295. syn match luaFunc /\<debug\.upvaluejoin\>/
  296. endif
  297. endif
  298. " Define the default highlighting.
  299. " Only when an item doesn't have highlighting yet
  300. hi def link luaStatement Statement
  301. hi def link luaRepeat Repeat
  302. hi def link luaFor Repeat
  303. hi def link luaString String
  304. hi def link luaString2 String
  305. hi def link luaNumber Number
  306. hi def link luaOperator Operator
  307. hi def link luaIn Operator
  308. hi def link luaConstant Constant
  309. hi def link luaCond Conditional
  310. hi def link luaElse Conditional
  311. hi def link luaFunction Function
  312. hi def link luaComment Comment
  313. hi def link luaTodo Todo
  314. hi def link luaTable Structure
  315. hi def link luaError Error
  316. hi def link luaParenError Error
  317. hi def link luaBraceError Error
  318. hi def link luaSpecial SpecialChar
  319. hi def link luaFunc Identifier
  320. hi def link luaLabel Label
  321. let b:current_syntax = "lua"
  322. let &cpo = s:cpo_save
  323. unlet s:cpo_save
  324. " vim: et ts=8 sw=2