blob: 2952ad962a9e5473804aa7fc0534affaf0a8428a [file] [log] [blame]
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001" Vim syntax file
Bram Moolenaarb6b046b2011-12-30 13:11:27 +01002" Language: Lua 4.0, Lua 5.0, Lua 5.1 and Lua 5.2
3" Maintainer: Marcus Aurelius Farias <masserahguard-lua 'at' yahoo com>
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004" First Author: Carlos Augusto Teixeira Mendes <cmendes 'at' inf puc-rio br>
Bram Moolenaar5dc62522012-02-13 00:05:22 +01005" Last Change: 2012 Feb 07
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006" Options: lua_version = 4 or 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +01007" lua_subversion = 0 (4.0, 5.0) or 1 (5.1) or 2 (5.2)
8" default 5.2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00009
10" For version 5.x: Clear all syntax items
11" For version 6.x: Quit when a syntax file was already loaded
12if version < 600
13 syntax clear
14elseif exists("b:current_syntax")
15 finish
16endif
17
Bram Moolenaar5dc62522012-02-13 00:05:22 +010018let s:cpo_save = &cpo
19set cpo&vim
20
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000021if !exists("lua_version")
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010022 " Default is lua 5.2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000023 let lua_version = 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010024 let lua_subversion = 2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000025elseif !exists("lua_subversion")
26 " lua_version exists, but lua_subversion doesn't. So, set it to 0
27 let lua_subversion = 0
28endif
29
30syn case match
31
32" syncing method
33syn sync minlines=100
34
35" Comments
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010036syn keyword luaTodo contained TODO FIXME XXX
37syn match luaComment "--.*$" contains=luaTodo,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000038if lua_version == 5 && lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010039 syn region luaComment matchgroup=luaComment start="--\[\[" end="\]\]" contains=luaTodo,luaInnerComment,@Spell
40 syn region luaInnerComment contained transparent start="\[\[" end="\]\]"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000041elseif lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
42 " Comments in Lua 5.1: --[[ ... ]], [=[ ... ]=], [===[ ... ]===], etc.
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010043 syn region luaComment matchgroup=luaComment start="--\[\z(=*\)\[" end="\]\z1\]" contains=luaTodo,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000044endif
45
46" First line may start with #!
47syn match luaComment "\%^#!.*"
48
49" catch errors caused by wrong parenthesis and wrong curly brackets or
50" keywords placed outside their respective blocks
51
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010052syn region luaParen transparent start='(' end=')' contains=TOP,luaParenError
53syn match luaParenError ")"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000054syn match luaError "}"
55syn match luaError "\<\%(end\|else\|elseif\|then\|until\|in\)\>"
56
57" Function declaration
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010058syn region luaFunctionBlock transparent matchgroup=luaFunction start="\<function\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000059
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010060" else
61syn keyword luaCondElse matchgroup=luaCond contained containedin=luaCondEnd else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000062
63" then ... end
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010064syn region luaCondEnd contained transparent matchgroup=luaCond start="\<then\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000065
66" elseif ... then
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010067syn region luaCondElseif contained containedin=luaCondEnd transparent matchgroup=luaCond start="\<elseif\>" end="\<then\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000068
69" if ... then
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010070syn region luaCondStart transparent matchgroup=luaCond start="\<if\>" end="\<then\>"me=e-4 contains=TOP nextgroup=luaCondEnd skipwhite skipempty
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000071
72" do ... end
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010073syn region luaBlock transparent matchgroup=luaStatement start="\<do\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000074" repeat ... until
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010075syn region luaRepeatBlock transparent matchgroup=luaRepeat start="\<repeat\>" end="\<until\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000076
77" while ... do
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010078syn region luaWhile transparent matchgroup=luaRepeat start="\<while\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000079
80" for ... do and for ... in ... do
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010081syn region luaFor transparent matchgroup=luaRepeat start="\<for\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000082
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010083syn keyword luaFor contained containedin=luaFor in
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000084
85" other keywords
86syn keyword luaStatement return local break
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010087if lua_version > 5 || (lua_version == 5 && lua_subversion >= 2)
88 syn keyword luaStatement goto
89 syn match luaLabel "::\I\i*::"
90endif
91syn keyword luaOperator and or not
92syn keyword luaConstant nil
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000093if lua_version > 4
94 syn keyword luaConstant true false
95endif
96
97" Strings
98if lua_version < 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010099 syn match luaSpecial contained "\\[\\abfnrtv\'\"]\|\\[[:digit:]]\{,3}"
100elseif lua_version == 5
101 if lua_subversion == 0
102 syn match luaSpecial contained #\\[\\abfnrtv'"[\]]\|\\[[:digit:]]\{,3}#
103 syn region luaString2 matchgroup=luaString start=+\[\[+ end=+\]\]+ contains=luaString2,@Spell
104 else
105 if lua_subversion == 1
106 syn match luaSpecial contained #\\[\\abfnrtv'"]\|\\[[:digit:]]\{,3}#
107 else " Lua 5.2
108 syn match luaSpecial contained #\\[\\abfnrtvz'"]\|\\x[[:xdigit:]]\{2}\|\\[[:digit:]]\{,3}#
109 endif
110 syn region luaString2 matchgroup=luaString start="\[\z(=*\)\[" end="\]\z1\]" contains=@Spell
111 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000112endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000113syn region luaString start=+'+ end=+'+ skip=+\\\\\|\\'+ contains=luaSpecial,@Spell
114syn region luaString start=+"+ end=+"+ skip=+\\\\\|\\"+ contains=luaSpecial,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000115
116" integer number
Bram Moolenaar9964e462007-05-05 17:54:07 +0000117syn match luaNumber "\<\d\+\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000118" floating point number, with dot, optional exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100119syn match luaNumber "\<\d\+\.\d*\%([eE][-+]\=\d\+\)\=\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000120" floating point number, starting with a dot, optional exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100121syn match luaNumber "\.\d\+\%([eE][-+]\=\d\+\)\=\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000122" floating point number, without dot, with exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100123syn match luaNumber "\<\d\+[eE][-+]\=\d\+\>"
Bram Moolenaar9964e462007-05-05 17:54:07 +0000124
125" hex numbers
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100126if lua_version >= 5
127 if lua_subversion == 1
128 syn match luaNumber "\<0[xX]\x\+\>"
129 elseif lua_subversion >= 2
130 syn match luaNumber "\<0[xX][[:xdigit:].]\+\%([pP][-+]\=\d\+\)\=\>"
131 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000132endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000133
134" tables
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100135syn region luaTableBlock transparent matchgroup=luaTable start="{" end="}" contains=TOP,luaStatement
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000136
137syn keyword luaFunc assert collectgarbage dofile error next
138syn keyword luaFunc print rawget rawset tonumber tostring type _VERSION
139
140if lua_version == 4
141 syn keyword luaFunc _ALERT _ERRORMESSAGE gcinfo
142 syn keyword luaFunc call copytagmethods dostring
143 syn keyword luaFunc foreach foreachi getglobal getn
144 syn keyword luaFunc gettagmethod globals newtag
145 syn keyword luaFunc setglobal settag settagmethod sort
146 syn keyword luaFunc tag tinsert tremove
147 syn keyword luaFunc _INPUT _OUTPUT _STDIN _STDOUT _STDERR
148 syn keyword luaFunc openfile closefile flush seek
149 syn keyword luaFunc setlocale execute remove rename tmpname
150 syn keyword luaFunc getenv date clock exit
151 syn keyword luaFunc readfrom writeto appendto read write
152 syn keyword luaFunc PI abs sin cos tan asin
153 syn keyword luaFunc acos atan atan2 ceil floor
154 syn keyword luaFunc mod frexp ldexp sqrt min max log
155 syn keyword luaFunc log10 exp deg rad random
156 syn keyword luaFunc randomseed strlen strsub strlower strupper
157 syn keyword luaFunc strchar strrep ascii strbyte
158 syn keyword luaFunc format strfind gsub
159 syn keyword luaFunc getinfo getlocal setlocal setcallhook setlinehook
160elseif lua_version == 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100161 syn keyword luaFunc getmetatable setmetatable
162 syn keyword luaFunc ipairs pairs
163 syn keyword luaFunc pcall xpcall
164 syn keyword luaFunc _G loadfile rawequal require
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000165 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100166 syn keyword luaFunc getfenv setfenv
167 syn keyword luaFunc loadstring unpack
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000168 syn keyword luaFunc gcinfo loadlib LUA_PATH _LOADED _REQUIREDNAME
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100169 else
170 syn keyword luaFunc load select
171 syn match luaFunc /\<package\.cpath\>/
172 syn match luaFunc /\<package\.loaded\>/
173 syn match luaFunc /\<package\.loadlib\>/
174 syn match luaFunc /\<package\.path\>/
175 if lua_subversion == 1
176 syn keyword luaFunc getfenv setfenv
177 syn keyword luaFunc loadstring module unpack
178 syn match luaFunc /\<package\.loaders\>/
179 syn match luaFunc /\<package\.preload\>/
180 syn match luaFunc /\<package\.seeall\>/
181 elseif lua_subversion == 2
182 syn keyword luaFunc _ENV rawlen
183 syn match luaFunc /\<package\.config\>/
184 syn match luaFunc /\<package\.preload\>/
185 syn match luaFunc /\<package\.searchers\>/
186 syn match luaFunc /\<package\.searchpath\>/
187 syn match luaFunc /\<bit32\.arshift\>/
188 syn match luaFunc /\<bit32\.band\>/
189 syn match luaFunc /\<bit32\.bnot\>/
190 syn match luaFunc /\<bit32\.bor\>/
191 syn match luaFunc /\<bit32\.btest\>/
192 syn match luaFunc /\<bit32\.bxor\>/
193 syn match luaFunc /\<bit32\.extract\>/
194 syn match luaFunc /\<bit32\.lrotate\>/
195 syn match luaFunc /\<bit32\.lshift\>/
196 syn match luaFunc /\<bit32\.replace\>/
197 syn match luaFunc /\<bit32\.rrotate\>/
198 syn match luaFunc /\<bit32\.rshift\>/
199 endif
200 syn match luaFunc /\<coroutine\.running\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000201 endif
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100202 syn match luaFunc /\<coroutine\.create\>/
203 syn match luaFunc /\<coroutine\.resume\>/
204 syn match luaFunc /\<coroutine\.status\>/
205 syn match luaFunc /\<coroutine\.wrap\>/
206 syn match luaFunc /\<coroutine\.yield\>/
207 syn match luaFunc /\<string\.byte\>/
208 syn match luaFunc /\<string\.char\>/
209 syn match luaFunc /\<string\.dump\>/
210 syn match luaFunc /\<string\.find\>/
211 syn match luaFunc /\<string\.format\>/
212 syn match luaFunc /\<string\.gsub\>/
213 syn match luaFunc /\<string\.len\>/
214 syn match luaFunc /\<string\.lower\>/
215 syn match luaFunc /\<string\.rep\>/
216 syn match luaFunc /\<string\.sub\>/
217 syn match luaFunc /\<string\.upper\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000218 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100219 syn match luaFunc /\<string\.gfind\>/
220 else
221 syn match luaFunc /\<string\.gmatch\>/
222 syn match luaFunc /\<string\.match\>/
223 syn match luaFunc /\<string\.reverse\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000224 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100226 syn match luaFunc /\<table\.getn\>/
227 syn match luaFunc /\<table\.setn\>/
228 syn match luaFunc /\<table\.foreach\>/
229 syn match luaFunc /\<table\.foreachi\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 elseif lua_subversion == 1
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100231 syn match luaFunc /\<table\.maxn\>/
232 elseif lua_subversion == 2
233 syn match luaFunc /\<table\.pack\>/
234 syn match luaFunc /\<table\.unpack\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000235 endif
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100236 syn match luaFunc /\<table\.concat\>/
237 syn match luaFunc /\<table\.sort\>/
238 syn match luaFunc /\<table\.insert\>/
239 syn match luaFunc /\<table\.remove\>/
240 syn match luaFunc /\<math\.abs\>/
241 syn match luaFunc /\<math\.acos\>/
242 syn match luaFunc /\<math\.asin\>/
243 syn match luaFunc /\<math\.atan\>/
244 syn match luaFunc /\<math\.atan2\>/
245 syn match luaFunc /\<math\.ceil\>/
246 syn match luaFunc /\<math\.sin\>/
247 syn match luaFunc /\<math\.cos\>/
248 syn match luaFunc /\<math\.tan\>/
249 syn match luaFunc /\<math\.deg\>/
250 syn match luaFunc /\<math\.exp\>/
251 syn match luaFunc /\<math\.floor\>/
252 syn match luaFunc /\<math\.log\>/
253 syn match luaFunc /\<math\.max\>/
254 syn match luaFunc /\<math\.min\>/
255 if lua_subversion == 0
256 syn match luaFunc /\<math\.mod\>/
257 syn match luaFunc /\<math\.log10\>/
258 else
259 if lua_subversion == 1
260 syn match luaFunc /\<math\.log10\>/
261 endif
262 syn match luaFunc /\<math\.huge\>/
263 syn match luaFunc /\<math\.fmod\>/
264 syn match luaFunc /\<math\.modf\>/
265 syn match luaFunc /\<math\.cosh\>/
266 syn match luaFunc /\<math\.sinh\>/
267 syn match luaFunc /\<math\.tanh\>/
268 endif
269 syn match luaFunc /\<math\.pow\>/
270 syn match luaFunc /\<math\.rad\>/
271 syn match luaFunc /\<math\.sqrt\>/
272 syn match luaFunc /\<math\.frexp\>/
273 syn match luaFunc /\<math\.ldexp\>/
274 syn match luaFunc /\<math\.random\>/
275 syn match luaFunc /\<math\.randomseed\>/
276 syn match luaFunc /\<math\.pi\>/
277 syn match luaFunc /\<io\.close\>/
278 syn match luaFunc /\<io\.flush\>/
279 syn match luaFunc /\<io\.input\>/
280 syn match luaFunc /\<io\.lines\>/
281 syn match luaFunc /\<io\.open\>/
282 syn match luaFunc /\<io\.output\>/
283 syn match luaFunc /\<io\.popen\>/
284 syn match luaFunc /\<io\.read\>/
285 syn match luaFunc /\<io\.stderr\>/
286 syn match luaFunc /\<io\.stdin\>/
287 syn match luaFunc /\<io\.stdout\>/
288 syn match luaFunc /\<io\.tmpfile\>/
289 syn match luaFunc /\<io\.type\>/
290 syn match luaFunc /\<io\.write\>/
291 syn match luaFunc /\<os\.clock\>/
292 syn match luaFunc /\<os\.date\>/
293 syn match luaFunc /\<os\.difftime\>/
294 syn match luaFunc /\<os\.execute\>/
295 syn match luaFunc /\<os\.exit\>/
296 syn match luaFunc /\<os\.getenv\>/
297 syn match luaFunc /\<os\.remove\>/
298 syn match luaFunc /\<os\.rename\>/
299 syn match luaFunc /\<os\.setlocale\>/
300 syn match luaFunc /\<os\.time\>/
301 syn match luaFunc /\<os\.tmpname\>/
302 syn match luaFunc /\<debug\.debug\>/
303 syn match luaFunc /\<debug\.gethook\>/
304 syn match luaFunc /\<debug\.getinfo\>/
305 syn match luaFunc /\<debug\.getlocal\>/
306 syn match luaFunc /\<debug\.getupvalue\>/
307 syn match luaFunc /\<debug\.setlocal\>/
308 syn match luaFunc /\<debug\.setupvalue\>/
309 syn match luaFunc /\<debug\.sethook\>/
310 syn match luaFunc /\<debug\.traceback\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000311 if lua_subversion == 1
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100312 syn match luaFunc /\<debug\.getfenv\>/
313 syn match luaFunc /\<debug\.setfenv\>/
314 syn match luaFunc /\<debug\.getmetatable\>/
315 syn match luaFunc /\<debug\.setmetatable\>/
316 syn match luaFunc /\<debug\.getregistry\>/
317 elseif lua_subversion == 2
318 syn match luaFunc /\<debug\.getmetatable\>/
319 syn match luaFunc /\<debug\.setmetatable\>/
320 syn match luaFunc /\<debug\.getregistry\>/
321 syn match luaFunc /\<debug\.getuservalue\>/
322 syn match luaFunc /\<debug\.setuservalue\>/
323 syn match luaFunc /\<debug\.upvalueid\>/
324 syn match luaFunc /\<debug\.upvaluejoin\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000325 endif
326endif
327
328" Define the default highlighting.
329" For version 5.7 and earlier: only when not done already
330" For version 5.8 and later: only when an item doesn't have highlighting yet
331if version >= 508 || !exists("did_lua_syntax_inits")
332 if version < 508
333 let did_lua_syntax_inits = 1
334 command -nargs=+ HiLink hi link <args>
335 else
336 command -nargs=+ HiLink hi def link <args>
337 endif
338
339 HiLink luaStatement Statement
340 HiLink luaRepeat Repeat
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100341 HiLink luaFor Repeat
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000342 HiLink luaString String
343 HiLink luaString2 String
344 HiLink luaNumber Number
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000345 HiLink luaOperator Operator
346 HiLink luaConstant Constant
347 HiLink luaCond Conditional
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100348 HiLink luaCondElse Conditional
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000349 HiLink luaFunction Function
350 HiLink luaComment Comment
351 HiLink luaTodo Todo
352 HiLink luaTable Structure
353 HiLink luaError Error
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100354 HiLink luaParenError Error
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000355 HiLink luaSpecial SpecialChar
356 HiLink luaFunc Identifier
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100357 HiLink luaLabel Label
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000358
359 delcommand HiLink
360endif
361
362let b:current_syntax = "lua"
363
Bram Moolenaar5dc62522012-02-13 00:05:22 +0100364let &cpo = s:cpo_save
365unlet s:cpo_save
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100366" vim: et ts=8 sw=2