xref: /vim-8.2.3635/runtime/syntax/lua.vim (revision 5dc6252d)
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 Feb 07
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
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
18let s:cpo_save = &cpo
19set cpo&vim
20
21if !exists("lua_version")
22  " Default is lua 5.2
23  let lua_version = 5
24  let lua_subversion = 2
25elseif !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
36syn keyword luaTodo            contained TODO FIXME XXX
37syn match   luaComment         "--.*$" contains=luaTodo,@Spell
38if lua_version == 5 && lua_subversion == 0
39  syn region luaComment        matchgroup=luaComment start="--\[\[" end="\]\]" contains=luaTodo,luaInnerComment,@Spell
40  syn region luaInnerComment   contained transparent start="\[\[" end="\]\]"
41elseif lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
42  " Comments in Lua 5.1: --[[ ... ]], [=[ ... ]=], [===[ ... ]===], etc.
43  syn region luaComment        matchgroup=luaComment start="--\[\z(=*\)\[" end="\]\z1\]" contains=luaTodo,@Spell
44endif
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
52syn region luaParen transparent start='(' end=')' contains=TOP,luaParenError
53syn match  luaParenError ")"
54syn match  luaError "}"
55syn match  luaError "\<\%(end\|else\|elseif\|then\|until\|in\)\>"
56
57" Function declaration
58syn region luaFunctionBlock transparent matchgroup=luaFunction start="\<function\>" end="\<end\>" contains=TOP
59
60" else
61syn keyword luaCondElse matchgroup=luaCond contained containedin=luaCondEnd else
62
63" then ... end
64syn region luaCondEnd contained transparent matchgroup=luaCond start="\<then\>" end="\<end\>" contains=TOP
65
66" elseif ... then
67syn region luaCondElseif contained containedin=luaCondEnd transparent matchgroup=luaCond start="\<elseif\>" end="\<then\>" contains=TOP
68
69" if ... then
70syn region luaCondStart transparent matchgroup=luaCond start="\<if\>" end="\<then\>"me=e-4 contains=TOP nextgroup=luaCondEnd skipwhite skipempty
71
72" do ... end
73syn region luaBlock transparent matchgroup=luaStatement start="\<do\>" end="\<end\>" contains=TOP
74" repeat ... until
75syn region luaRepeatBlock transparent matchgroup=luaRepeat start="\<repeat\>" end="\<until\>" contains=TOP
76
77" while ... do
78syn region luaWhile transparent matchgroup=luaRepeat start="\<while\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
79
80" for ... do and for ... in ... do
81syn region luaFor transparent matchgroup=luaRepeat start="\<for\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
82
83syn keyword luaFor contained containedin=luaFor in
84
85" other keywords
86syn keyword luaStatement return local break
87if 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
93if lua_version > 4
94  syn keyword luaConstant true false
95endif
96
97" Strings
98if lua_version < 5
99  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
112endif
113syn region luaString  start=+'+ end=+'+ skip=+\\\\\|\\'+ contains=luaSpecial,@Spell
114syn region luaString  start=+"+ end=+"+ skip=+\\\\\|\\"+ contains=luaSpecial,@Spell
115
116" integer number
117syn match luaNumber "\<\d\+\>"
118" floating point number, with dot, optional exponent
119syn match luaNumber  "\<\d\+\.\d*\%([eE][-+]\=\d\+\)\=\>"
120" floating point number, starting with a dot, optional exponent
121syn match luaNumber  "\.\d\+\%([eE][-+]\=\d\+\)\=\>"
122" floating point number, without dot, with exponent
123syn match luaNumber  "\<\d\+[eE][-+]\=\d\+\>"
124
125" hex numbers
126if 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
132endif
133
134" tables
135syn region luaTableBlock transparent matchgroup=luaTable start="{" end="}" contains=TOP,luaStatement
136
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
161  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
165  if lua_subversion == 0
166    syn keyword luaFunc getfenv setfenv
167    syn keyword luaFunc loadstring unpack
168    syn keyword luaFunc gcinfo loadlib LUA_PATH _LOADED _REQUIREDNAME
169  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\>/
201  endif
202  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\>/
218  if lua_subversion == 0
219    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\>/
224  endif
225  if lua_subversion == 0
226    syn match luaFunc /\<table\.getn\>/
227    syn match luaFunc /\<table\.setn\>/
228    syn match luaFunc /\<table\.foreach\>/
229    syn match luaFunc /\<table\.foreachi\>/
230  elseif lua_subversion == 1
231    syn match luaFunc /\<table\.maxn\>/
232  elseif lua_subversion == 2
233    syn match luaFunc /\<table\.pack\>/
234    syn match luaFunc /\<table\.unpack\>/
235  endif
236  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\>/
311  if lua_subversion == 1
312    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\>/
325  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
341  HiLink luaFor			Repeat
342  HiLink luaString		String
343  HiLink luaString2		String
344  HiLink luaNumber		Number
345  HiLink luaOperator		Operator
346  HiLink luaConstant		Constant
347  HiLink luaCond		Conditional
348  HiLink luaCondElse		Conditional
349  HiLink luaFunction		Function
350  HiLink luaComment		Comment
351  HiLink luaTodo		Todo
352  HiLink luaTable		Structure
353  HiLink luaError		Error
354  HiLink luaParenError		Error
355  HiLink luaSpecial		SpecialChar
356  HiLink luaFunc		Identifier
357  HiLink luaLabel		Label
358
359  delcommand HiLink
360endif
361
362let b:current_syntax = "lua"
363
364let &cpo = s:cpo_save
365unlet s:cpo_save
366" vim: et ts=8 sw=2
367