blob: 18e97ff534dc9c7ceec2654044e01a054d94909f [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 Moolenaarb6b046b2011-12-30 13:11:27 +01005" Last Change: 2011 Dec 20
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
18if !exists("lua_version")
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010019 " Default is lua 5.2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000020 let lua_version = 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010021 let lua_subversion = 2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000022elseif !exists("lua_subversion")
23 " lua_version exists, but lua_subversion doesn't. So, set it to 0
24 let lua_subversion = 0
25endif
26
27syn case match
28
29" syncing method
30syn sync minlines=100
31
32" Comments
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010033syn keyword luaTodo contained TODO FIXME XXX
34syn match luaComment "--.*$" contains=luaTodo,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000035if lua_version == 5 && lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010036 syn region luaComment matchgroup=luaComment start="--\[\[" end="\]\]" contains=luaTodo,luaInnerComment,@Spell
37 syn region luaInnerComment contained transparent start="\[\[" end="\]\]"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000038elseif lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
39 " Comments in Lua 5.1: --[[ ... ]], [=[ ... ]=], [===[ ... ]===], etc.
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010040 syn region luaComment matchgroup=luaComment start="--\[\z(=*\)\[" end="\]\z1\]" contains=luaTodo,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000041endif
42
43" First line may start with #!
44syn match luaComment "\%^#!.*"
45
46" catch errors caused by wrong parenthesis and wrong curly brackets or
47" keywords placed outside their respective blocks
48
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010049syn region luaParen transparent start='(' end=')' contains=TOP,luaParenError
50syn match luaParenError ")"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000051syn match luaError "}"
52syn match luaError "\<\%(end\|else\|elseif\|then\|until\|in\)\>"
53
54" Function declaration
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010055syn region luaFunctionBlock transparent matchgroup=luaFunction start="\<function\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000056
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010057" else
58syn keyword luaCondElse matchgroup=luaCond contained containedin=luaCondEnd else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000059
60" then ... end
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010061syn region luaCondEnd contained transparent matchgroup=luaCond start="\<then\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000062
63" elseif ... then
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010064syn region luaCondElseif contained containedin=luaCondEnd transparent matchgroup=luaCond start="\<elseif\>" end="\<then\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000065
66" if ... then
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010067syn 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 +000068
69" do ... end
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010070syn region luaBlock transparent matchgroup=luaStatement start="\<do\>" end="\<end\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000071" repeat ... until
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010072syn region luaRepeatBlock transparent matchgroup=luaRepeat start="\<repeat\>" end="\<until\>" contains=TOP
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000073
74" while ... do
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010075syn 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 +000076
77" for ... do and for ... in ... do
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010078syn 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 +000079
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010080syn keyword luaFor contained containedin=luaFor in
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000081
82" other keywords
83syn keyword luaStatement return local break
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010084if lua_version > 5 || (lua_version == 5 && lua_subversion >= 2)
85 syn keyword luaStatement goto
86 syn match luaLabel "::\I\i*::"
87endif
88syn keyword luaOperator and or not
89syn keyword luaConstant nil
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000090if lua_version > 4
91 syn keyword luaConstant true false
92endif
93
94" Strings
95if lua_version < 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010096 syn match luaSpecial contained "\\[\\abfnrtv\'\"]\|\\[[:digit:]]\{,3}"
97elseif lua_version == 5
98 if lua_subversion == 0
99 syn match luaSpecial contained #\\[\\abfnrtv'"[\]]\|\\[[:digit:]]\{,3}#
100 syn region luaString2 matchgroup=luaString start=+\[\[+ end=+\]\]+ contains=luaString2,@Spell
101 else
102 if lua_subversion == 1
103 syn match luaSpecial contained #\\[\\abfnrtv'"]\|\\[[:digit:]]\{,3}#
104 else " Lua 5.2
105 syn match luaSpecial contained #\\[\\abfnrtvz'"]\|\\x[[:xdigit:]]\{2}\|\\[[:digit:]]\{,3}#
106 endif
107 syn region luaString2 matchgroup=luaString start="\[\z(=*\)\[" end="\]\z1\]" contains=@Spell
108 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000109endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000110syn region luaString start=+'+ end=+'+ skip=+\\\\\|\\'+ contains=luaSpecial,@Spell
111syn region luaString start=+"+ end=+"+ skip=+\\\\\|\\"+ contains=luaSpecial,@Spell
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000112
113" integer number
Bram Moolenaar9964e462007-05-05 17:54:07 +0000114syn match luaNumber "\<\d\+\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000115" floating point number, with dot, optional exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100116syn match luaNumber "\<\d\+\.\d*\%([eE][-+]\=\d\+\)\=\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000117" floating point number, starting with a dot, optional exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100118syn match luaNumber "\.\d\+\%([eE][-+]\=\d\+\)\=\>"
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000119" floating point number, without dot, with exponent
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100120syn match luaNumber "\<\d\+[eE][-+]\=\d\+\>"
Bram Moolenaar9964e462007-05-05 17:54:07 +0000121
122" hex numbers
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100123if lua_version >= 5
124 if lua_subversion == 1
125 syn match luaNumber "\<0[xX]\x\+\>"
126 elseif lua_subversion >= 2
127 syn match luaNumber "\<0[xX][[:xdigit:].]\+\%([pP][-+]\=\d\+\)\=\>"
128 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000129endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000130
131" tables
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100132syn region luaTableBlock transparent matchgroup=luaTable start="{" end="}" contains=TOP,luaStatement
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000133
134syn keyword luaFunc assert collectgarbage dofile error next
135syn keyword luaFunc print rawget rawset tonumber tostring type _VERSION
136
137if lua_version == 4
138 syn keyword luaFunc _ALERT _ERRORMESSAGE gcinfo
139 syn keyword luaFunc call copytagmethods dostring
140 syn keyword luaFunc foreach foreachi getglobal getn
141 syn keyword luaFunc gettagmethod globals newtag
142 syn keyword luaFunc setglobal settag settagmethod sort
143 syn keyword luaFunc tag tinsert tremove
144 syn keyword luaFunc _INPUT _OUTPUT _STDIN _STDOUT _STDERR
145 syn keyword luaFunc openfile closefile flush seek
146 syn keyword luaFunc setlocale execute remove rename tmpname
147 syn keyword luaFunc getenv date clock exit
148 syn keyword luaFunc readfrom writeto appendto read write
149 syn keyword luaFunc PI abs sin cos tan asin
150 syn keyword luaFunc acos atan atan2 ceil floor
151 syn keyword luaFunc mod frexp ldexp sqrt min max log
152 syn keyword luaFunc log10 exp deg rad random
153 syn keyword luaFunc randomseed strlen strsub strlower strupper
154 syn keyword luaFunc strchar strrep ascii strbyte
155 syn keyword luaFunc format strfind gsub
156 syn keyword luaFunc getinfo getlocal setlocal setcallhook setlinehook
157elseif lua_version == 5
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100158 syn keyword luaFunc getmetatable setmetatable
159 syn keyword luaFunc ipairs pairs
160 syn keyword luaFunc pcall xpcall
161 syn keyword luaFunc _G loadfile rawequal require
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000162 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100163 syn keyword luaFunc getfenv setfenv
164 syn keyword luaFunc loadstring unpack
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000165 syn keyword luaFunc gcinfo loadlib LUA_PATH _LOADED _REQUIREDNAME
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100166 else
167 syn keyword luaFunc load select
168 syn match luaFunc /\<package\.cpath\>/
169 syn match luaFunc /\<package\.loaded\>/
170 syn match luaFunc /\<package\.loadlib\>/
171 syn match luaFunc /\<package\.path\>/
172 if lua_subversion == 1
173 syn keyword luaFunc getfenv setfenv
174 syn keyword luaFunc loadstring module unpack
175 syn match luaFunc /\<package\.loaders\>/
176 syn match luaFunc /\<package\.preload\>/
177 syn match luaFunc /\<package\.seeall\>/
178 elseif lua_subversion == 2
179 syn keyword luaFunc _ENV rawlen
180 syn match luaFunc /\<package\.config\>/
181 syn match luaFunc /\<package\.preload\>/
182 syn match luaFunc /\<package\.searchers\>/
183 syn match luaFunc /\<package\.searchpath\>/
184 syn match luaFunc /\<bit32\.arshift\>/
185 syn match luaFunc /\<bit32\.band\>/
186 syn match luaFunc /\<bit32\.bnot\>/
187 syn match luaFunc /\<bit32\.bor\>/
188 syn match luaFunc /\<bit32\.btest\>/
189 syn match luaFunc /\<bit32\.bxor\>/
190 syn match luaFunc /\<bit32\.extract\>/
191 syn match luaFunc /\<bit32\.lrotate\>/
192 syn match luaFunc /\<bit32\.lshift\>/
193 syn match luaFunc /\<bit32\.replace\>/
194 syn match luaFunc /\<bit32\.rrotate\>/
195 syn match luaFunc /\<bit32\.rshift\>/
196 endif
197 syn match luaFunc /\<coroutine\.running\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000198 endif
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100199 syn match luaFunc /\<coroutine\.create\>/
200 syn match luaFunc /\<coroutine\.resume\>/
201 syn match luaFunc /\<coroutine\.status\>/
202 syn match luaFunc /\<coroutine\.wrap\>/
203 syn match luaFunc /\<coroutine\.yield\>/
204 syn match luaFunc /\<string\.byte\>/
205 syn match luaFunc /\<string\.char\>/
206 syn match luaFunc /\<string\.dump\>/
207 syn match luaFunc /\<string\.find\>/
208 syn match luaFunc /\<string\.format\>/
209 syn match luaFunc /\<string\.gsub\>/
210 syn match luaFunc /\<string\.len\>/
211 syn match luaFunc /\<string\.lower\>/
212 syn match luaFunc /\<string\.rep\>/
213 syn match luaFunc /\<string\.sub\>/
214 syn match luaFunc /\<string\.upper\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000215 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100216 syn match luaFunc /\<string\.gfind\>/
217 else
218 syn match luaFunc /\<string\.gmatch\>/
219 syn match luaFunc /\<string\.match\>/
220 syn match luaFunc /\<string\.reverse\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000221 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000222 if lua_subversion == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100223 syn match luaFunc /\<table\.getn\>/
224 syn match luaFunc /\<table\.setn\>/
225 syn match luaFunc /\<table\.foreach\>/
226 syn match luaFunc /\<table\.foreachi\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000227 elseif lua_subversion == 1
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100228 syn match luaFunc /\<table\.maxn\>/
229 elseif lua_subversion == 2
230 syn match luaFunc /\<table\.pack\>/
231 syn match luaFunc /\<table\.unpack\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000232 endif
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100233 syn match luaFunc /\<table\.concat\>/
234 syn match luaFunc /\<table\.sort\>/
235 syn match luaFunc /\<table\.insert\>/
236 syn match luaFunc /\<table\.remove\>/
237 syn match luaFunc /\<math\.abs\>/
238 syn match luaFunc /\<math\.acos\>/
239 syn match luaFunc /\<math\.asin\>/
240 syn match luaFunc /\<math\.atan\>/
241 syn match luaFunc /\<math\.atan2\>/
242 syn match luaFunc /\<math\.ceil\>/
243 syn match luaFunc /\<math\.sin\>/
244 syn match luaFunc /\<math\.cos\>/
245 syn match luaFunc /\<math\.tan\>/
246 syn match luaFunc /\<math\.deg\>/
247 syn match luaFunc /\<math\.exp\>/
248 syn match luaFunc /\<math\.floor\>/
249 syn match luaFunc /\<math\.log\>/
250 syn match luaFunc /\<math\.max\>/
251 syn match luaFunc /\<math\.min\>/
252 if lua_subversion == 0
253 syn match luaFunc /\<math\.mod\>/
254 syn match luaFunc /\<math\.log10\>/
255 else
256 if lua_subversion == 1
257 syn match luaFunc /\<math\.log10\>/
258 endif
259 syn match luaFunc /\<math\.huge\>/
260 syn match luaFunc /\<math\.fmod\>/
261 syn match luaFunc /\<math\.modf\>/
262 syn match luaFunc /\<math\.cosh\>/
263 syn match luaFunc /\<math\.sinh\>/
264 syn match luaFunc /\<math\.tanh\>/
265 endif
266 syn match luaFunc /\<math\.pow\>/
267 syn match luaFunc /\<math\.rad\>/
268 syn match luaFunc /\<math\.sqrt\>/
269 syn match luaFunc /\<math\.frexp\>/
270 syn match luaFunc /\<math\.ldexp\>/
271 syn match luaFunc /\<math\.random\>/
272 syn match luaFunc /\<math\.randomseed\>/
273 syn match luaFunc /\<math\.pi\>/
274 syn match luaFunc /\<io\.close\>/
275 syn match luaFunc /\<io\.flush\>/
276 syn match luaFunc /\<io\.input\>/
277 syn match luaFunc /\<io\.lines\>/
278 syn match luaFunc /\<io\.open\>/
279 syn match luaFunc /\<io\.output\>/
280 syn match luaFunc /\<io\.popen\>/
281 syn match luaFunc /\<io\.read\>/
282 syn match luaFunc /\<io\.stderr\>/
283 syn match luaFunc /\<io\.stdin\>/
284 syn match luaFunc /\<io\.stdout\>/
285 syn match luaFunc /\<io\.tmpfile\>/
286 syn match luaFunc /\<io\.type\>/
287 syn match luaFunc /\<io\.write\>/
288 syn match luaFunc /\<os\.clock\>/
289 syn match luaFunc /\<os\.date\>/
290 syn match luaFunc /\<os\.difftime\>/
291 syn match luaFunc /\<os\.execute\>/
292 syn match luaFunc /\<os\.exit\>/
293 syn match luaFunc /\<os\.getenv\>/
294 syn match luaFunc /\<os\.remove\>/
295 syn match luaFunc /\<os\.rename\>/
296 syn match luaFunc /\<os\.setlocale\>/
297 syn match luaFunc /\<os\.time\>/
298 syn match luaFunc /\<os\.tmpname\>/
299 syn match luaFunc /\<debug\.debug\>/
300 syn match luaFunc /\<debug\.gethook\>/
301 syn match luaFunc /\<debug\.getinfo\>/
302 syn match luaFunc /\<debug\.getlocal\>/
303 syn match luaFunc /\<debug\.getupvalue\>/
304 syn match luaFunc /\<debug\.setlocal\>/
305 syn match luaFunc /\<debug\.setupvalue\>/
306 syn match luaFunc /\<debug\.sethook\>/
307 syn match luaFunc /\<debug\.traceback\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000308 if lua_subversion == 1
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100309 syn match luaFunc /\<debug\.getfenv\>/
310 syn match luaFunc /\<debug\.setfenv\>/
311 syn match luaFunc /\<debug\.getmetatable\>/
312 syn match luaFunc /\<debug\.setmetatable\>/
313 syn match luaFunc /\<debug\.getregistry\>/
314 elseif lua_subversion == 2
315 syn match luaFunc /\<debug\.getmetatable\>/
316 syn match luaFunc /\<debug\.setmetatable\>/
317 syn match luaFunc /\<debug\.getregistry\>/
318 syn match luaFunc /\<debug\.getuservalue\>/
319 syn match luaFunc /\<debug\.setuservalue\>/
320 syn match luaFunc /\<debug\.upvalueid\>/
321 syn match luaFunc /\<debug\.upvaluejoin\>/
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000322 endif
323endif
324
325" Define the default highlighting.
326" For version 5.7 and earlier: only when not done already
327" For version 5.8 and later: only when an item doesn't have highlighting yet
328if version >= 508 || !exists("did_lua_syntax_inits")
329 if version < 508
330 let did_lua_syntax_inits = 1
331 command -nargs=+ HiLink hi link <args>
332 else
333 command -nargs=+ HiLink hi def link <args>
334 endif
335
336 HiLink luaStatement Statement
337 HiLink luaRepeat Repeat
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100338 HiLink luaFor Repeat
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000339 HiLink luaString String
340 HiLink luaString2 String
341 HiLink luaNumber Number
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000342 HiLink luaOperator Operator
343 HiLink luaConstant Constant
344 HiLink luaCond Conditional
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100345 HiLink luaCondElse Conditional
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000346 HiLink luaFunction Function
347 HiLink luaComment Comment
348 HiLink luaTodo Todo
349 HiLink luaTable Structure
350 HiLink luaError Error
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100351 HiLink luaParenError Error
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000352 HiLink luaSpecial SpecialChar
353 HiLink luaFunc Identifier
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100354 HiLink luaLabel Label
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000355
356 delcommand HiLink
357endif
358
359let b:current_syntax = "lua"
360
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100361" vim: et ts=8 sw=2