vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

lua.vim (16050B)


      1 " Vim syntax file
      2 " Language:     Lua 4.0, Lua 5.0, Lua 5.1, Lua 5.2 and Lua 5.3
      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:  2025 Feb 25
      6 " Options:      lua_version = 4 or 5
      7 "               lua_subversion = 0 (for 4.0 or 5.0)
      8 "                               or 1, 2, 3 (for 5.1, 5.2 or 5.3)
      9 "               the default is 5.3
     10 
     11 " quit when a syntax file was already loaded
     12 if exists("b:current_syntax")
     13   finish
     14 endif
     15 
     16 let s:cpo_save = &cpo
     17 set cpo&vim
     18 
     19 " keep in sync with ftplugin/lua.vim
     20 if !exists("lua_version")
     21   " Default is lua 5.3
     22   let lua_version = 5
     23   let lua_subversion = 3
     24 elseif !exists("lua_subversion")
     25   " lua_version exists, but lua_subversion doesn't. In this case set it to 0
     26   let lua_subversion = 0
     27 endif
     28 
     29 syn case match
     30 
     31 " syncing method
     32 syn sync minlines=1000
     33 
     34 if lua_version >= 5
     35   syn keyword luaMetaMethod __add __sub __mul __div __pow __unm __concat
     36   syn keyword luaMetaMethod __eq __lt __le
     37   syn keyword luaMetaMethod __index __newindex __call
     38   syn keyword luaMetaMethod __metatable __mode __gc __tostring
     39 endif
     40 
     41 if lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
     42   syn keyword luaMetaMethod __mod __len
     43 endif
     44 
     45 if lua_version > 5 || (lua_version == 5 && lua_subversion >= 2)
     46   syn keyword luaMetaMethod __pairs
     47 endif
     48 
     49 if lua_version > 5 || (lua_version == 5 && lua_subversion >= 3)
     50   syn keyword luaMetaMethod __idiv __name
     51   syn keyword luaMetaMethod __band __bor __bxor __bnot __shl __shr
     52 endif
     53 
     54 if lua_version > 5 || (lua_version == 5 && lua_subversion >= 4)
     55   syn keyword luaMetaMethod __close
     56 endif
     57 
     58 " catch errors caused by wrong parenthesis and wrong curly brackets or
     59 " keywords placed outside their respective blocks
     60 
     61 syn region luaParen transparent start='(' end=')' contains=TOP,luaParenError
     62 syn match  luaParenError ")"
     63 syn match  luaError "}"
     64 syn match  luaError "\<\%(end\|else\|elseif\|then\|until\|in\)\>"
     65 
     66 " Function declaration
     67 syn region luaFunctionBlock transparent matchgroup=luaFunction start="\<function\>" end="\<end\>" contains=TOP
     68 
     69 " else
     70 syn keyword luaCondElse matchgroup=luaCond contained containedin=luaCondEnd else
     71 
     72 " then ... end
     73 syn region luaCondEnd contained transparent matchgroup=luaCond start="\<then\>" end="\<end\>" contains=TOP
     74 
     75 " elseif ... then
     76 syn region luaCondElseif contained containedin=luaCondEnd transparent matchgroup=luaCond start="\<elseif\>" end="\<then\>" contains=TOP
     77 
     78 " if ... then
     79 syn region luaCondStart transparent matchgroup=luaCond start="\<if\>" end="\<then\>"me=e-4 contains=TOP nextgroup=luaCondEnd skipwhite skipempty
     80 
     81 " do ... end
     82 syn region luaBlock transparent matchgroup=luaStatement start="\<do\>" end="\<end\>" contains=TOP
     83 " repeat ... until
     84 syn region luaRepeatBlock transparent matchgroup=luaRepeat start="\<repeat\>" end="\<until\>" contains=TOP
     85 
     86 " while ... do
     87 syn region luaWhile transparent matchgroup=luaRepeat start="\<while\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
     88 
     89 " for ... do and for ... in ... do
     90 syn region luaFor transparent matchgroup=luaRepeat start="\<for\>" end="\<do\>"me=e-2 contains=TOP nextgroup=luaBlock skipwhite skipempty
     91 
     92 syn keyword luaFor contained containedin=luaFor in
     93 
     94 " other keywords
     95 syn keyword luaStatement return local break
     96 if lua_version > 5 || (lua_version == 5 && lua_subversion >= 2)
     97   syn keyword luaStatement goto
     98   syn match luaLabel "::\I\i*::"
     99 endif
    100 
    101 " operators
    102 syn keyword luaOperator and or not
    103 
    104 if (lua_version == 5 && lua_subversion >= 3) || lua_version > 5
    105   syn match luaSymbolOperator "[#<>=~^&|*/%+-]\|\.\{2,3}"
    106 elseif lua_version == 5 && (lua_subversion == 1 || lua_subversion == 2)
    107   syn match luaSymbolOperator "[#<>=~^*/%+-]\|\.\{2,3}"
    108 else
    109   syn match luaSymbolOperator "[<>=~^*/+-]\|\.\{2,3}"
    110 endif
    111 
    112 " comments
    113 syn keyword luaTodo            contained TODO FIXME XXX
    114 syn match   luaComment         "--.*$" contains=luaTodo,@Spell
    115 if lua_version == 5 && lua_subversion == 0
    116   syn region luaComment        matchgroup=luaCommentDelimiter start="--\[\[" end="\]\]" contains=luaTodo,luaInnerComment,@Spell
    117   syn region luaInnerComment   contained transparent start="\[\[" end="\]\]"
    118 elseif lua_version > 5 || (lua_version == 5 && lua_subversion >= 1)
    119   " Comments in Lua 5.1: --[[ ... ]], [=[ ... ]=], [===[ ... ]===], etc.
    120   syn region luaComment        matchgroup=luaCommentDelimiter start="--\[\z(=*\)\[" end="\]\z1\]" contains=luaTodo,@Spell
    121 endif
    122 
    123 " first line may start with #!
    124 syn match luaComment "\%^#!.*"
    125 
    126 syn keyword luaConstant nil
    127 if lua_version > 4
    128   syn keyword luaConstant true false
    129 endif
    130 
    131 " strings
    132 syn match  luaSpecial contained #\\[\\abfnrtv'"[\]]\|\\[[:digit:]]\{,3}#
    133 if lua_version == 5
    134   if lua_subversion == 0
    135     syn region luaString2 matchgroup=luaStringDelimiter start=+\[\[+ end=+\]\]+ contains=luaString2,@Spell
    136   else
    137     if lua_subversion >= 2
    138       syn match  luaSpecial contained #\\z\|\\x[[:xdigit:]]\{2}#
    139     endif
    140     if lua_subversion >= 3
    141       syn match  luaSpecial contained #\\u{[[:xdigit:]]\+}#
    142     endif
    143     syn region luaString2 matchgroup=luaStringDelimiter start="\[\z(=*\)\[" end="\]\z1\]" contains=@Spell
    144   endif
    145 endif
    146 syn region luaString matchgroup=luaStringDelimiter start=+'+ end=+'+ skip=+\\\\\|\\'+ contains=luaSpecial,@Spell
    147 syn region luaString matchgroup=luaStringDelimiter start=+"+ end=+"+ skip=+\\\\\|\\"+ contains=luaSpecial,@Spell
    148 
    149 " integer number
    150 syn match luaNumber "\<\d\+\>"
    151 " floating point number, with dot, optional exponent
    152 syn match luaNumber  "\<\d\+\.\d*\%([eE][-+]\=\d\+\)\="
    153 " floating point number, starting with a dot, optional exponent
    154 syn match luaNumber  "\.\d\+\%([eE][-+]\=\d\+\)\=\>"
    155 " floating point number, without dot, with exponent
    156 syn match luaNumber  "\<\d\+[eE][-+]\=\d\+\>"
    157 
    158 " hex numbers
    159 if lua_version >= 5
    160   if lua_subversion == 1
    161     syn match luaNumber "\<0[xX]\x\+\>"
    162   elseif lua_subversion >= 2
    163     syn match luaNumber "\<0[xX][[:xdigit:].]\+\%([pP][-+]\=\d\+\)\=\>"
    164   endif
    165 endif
    166 
    167 " tables
    168 syn region luaTableBlock transparent matchgroup=luaTable start="{" end="}" contains=TOP,luaStatement
    169 
    170 " methods
    171 syntax match luaFunc ":\@<=\k\+"
    172 
    173 " built-in functions
    174 syn keyword luaFunc assert collectgarbage dofile error next
    175 syn keyword luaFunc print rawget rawset self tonumber tostring type _VERSION
    176 
    177 if lua_version == 4
    178   syn keyword luaFunc _ALERT _ERRORMESSAGE gcinfo
    179   syn keyword luaFunc call copytagmethods dostring
    180   syn keyword luaFunc foreach foreachi getglobal getn
    181   syn keyword luaFunc gettagmethod globals newtag
    182   syn keyword luaFunc setglobal settag settagmethod sort
    183   syn keyword luaFunc tag tinsert tremove
    184   syn keyword luaFunc _INPUT _OUTPUT _STDIN _STDOUT _STDERR
    185   syn keyword luaFunc openfile closefile flush seek
    186   syn keyword luaFunc setlocale execute remove rename tmpname
    187   syn keyword luaFunc getenv date clock exit
    188   syn keyword luaFunc readfrom writeto appendto read write
    189   syn keyword luaFunc PI abs sin cos tan asin
    190   syn keyword luaFunc acos atan atan2 ceil floor
    191   syn keyword luaFunc mod frexp ldexp sqrt min max log
    192   syn keyword luaFunc log10 exp deg rad random
    193   syn keyword luaFunc randomseed strlen strsub strlower strupper
    194   syn keyword luaFunc strchar strrep ascii strbyte
    195   syn keyword luaFunc format strfind gsub
    196   syn keyword luaFunc getinfo getlocal setlocal setcallhook setlinehook
    197 elseif lua_version == 5
    198   syn keyword luaFunc getmetatable setmetatable
    199   syn keyword luaFunc ipairs pairs
    200   syn keyword luaFunc pcall xpcall
    201   syn keyword luaFunc _G loadfile rawequal require
    202   if lua_subversion == 0
    203     syn keyword luaFunc getfenv setfenv
    204     syn keyword luaFunc loadstring unpack
    205     syn keyword luaFunc gcinfo loadlib LUA_PATH _LOADED _REQUIREDNAME
    206   else
    207     syn keyword luaFunc load select
    208     syn match   luaFunc /\<package\.cpath\>/
    209     syn match   luaFunc /\<package\.loaded\>/
    210     syn match   luaFunc /\<package\.loadlib\>/
    211     syn match   luaFunc /\<package\.path\>/
    212     syn match   luaFunc /\<package\.preload\>/
    213     if lua_subversion == 1
    214       syn keyword luaFunc getfenv setfenv
    215       syn keyword luaFunc loadstring module unpack
    216       syn match   luaFunc /\<package\.loaders\>/
    217       syn match   luaFunc /\<package\.seeall\>/
    218     elseif lua_subversion >= 2
    219       syn keyword luaFunc _ENV rawlen
    220       syn match   luaFunc /\<package\.config\>/
    221       syn match   luaFunc /\<package\.preload\>/
    222       syn match   luaFunc /\<package\.searchers\>/
    223       syn match   luaFunc /\<package\.searchpath\>/
    224     endif
    225 
    226     if lua_subversion >= 3
    227       syn match luaFunc /\<coroutine\.isyieldable\>/
    228     endif
    229     if lua_subversion >= 4
    230       syn keyword luaFunc warn
    231       syn match luaFunc /\<coroutine\.close\>/
    232     endif
    233     syn match luaFunc /\<coroutine\.running\>/
    234   endif
    235   syn match   luaFunc /\<coroutine\.create\>/
    236   syn match   luaFunc /\<coroutine\.resume\>/
    237   syn match   luaFunc /\<coroutine\.status\>/
    238   syn match   luaFunc /\<coroutine\.wrap\>/
    239   syn match   luaFunc /\<coroutine\.yield\>/
    240 
    241   syn match   luaFunc /\<string\.byte\>/
    242   syn match   luaFunc /\<string\.char\>/
    243   syn match   luaFunc /\<string\.dump\>/
    244   syn match   luaFunc /\<string\.find\>/
    245   syn match   luaFunc /\<string\.format\>/
    246   syn match   luaFunc /\<string\.gsub\>/
    247   syn match   luaFunc /\<string\.len\>/
    248   syn match   luaFunc /\<string\.lower\>/
    249   syn match   luaFunc /\<string\.rep\>/
    250   syn match   luaFunc /\<string\.sub\>/
    251   syn match   luaFunc /\<string\.upper\>/
    252   if lua_subversion == 0
    253     syn match luaFunc /\<string\.gfind\>/
    254   else
    255     syn match luaFunc /\<string\.gmatch\>/
    256     syn match luaFunc /\<string\.match\>/
    257     syn match luaFunc /\<string\.reverse\>/
    258   endif
    259   if lua_subversion >= 3
    260     syn match luaFunc /\<string\.pack\>/
    261     syn match luaFunc /\<string\.packsize\>/
    262     syn match luaFunc /\<string\.unpack\>/
    263     syn match luaFunc /\<utf8\.char\>/
    264     syn match luaFunc /\<utf8\.charpattern\>/
    265     syn match luaFunc /\<utf8\.codes\>/
    266     syn match luaFunc /\<utf8\.codepoint\>/
    267     syn match luaFunc /\<utf8\.len\>/
    268     syn match luaFunc /\<utf8\.offset\>/
    269   endif
    270 
    271   if lua_subversion == 0
    272     syn match luaFunc /\<table\.getn\>/
    273     syn match luaFunc /\<table\.setn\>/
    274     syn match luaFunc /\<table\.foreach\>/
    275     syn match luaFunc /\<table\.foreachi\>/
    276   elseif lua_subversion == 1
    277     syn match luaFunc /\<table\.maxn\>/
    278   elseif lua_subversion >= 2
    279     syn match luaFunc /\<table\.pack\>/
    280     syn match luaFunc /\<table\.unpack\>/
    281     if lua_subversion >= 3
    282       syn match luaFunc /\<table\.move\>/
    283     endif
    284   endif
    285   syn match   luaFunc /\<table\.concat\>/
    286   syn match   luaFunc /\<table\.insert\>/
    287   syn match   luaFunc /\<table\.sort\>/
    288   syn match   luaFunc /\<table\.remove\>/
    289 
    290   if lua_subversion == 2
    291     syn match   luaFunc /\<bit32\.arshift\>/
    292     syn match   luaFunc /\<bit32\.band\>/
    293     syn match   luaFunc /\<bit32\.bnot\>/
    294     syn match   luaFunc /\<bit32\.bor\>/
    295     syn match   luaFunc /\<bit32\.btest\>/
    296     syn match   luaFunc /\<bit32\.bxor\>/
    297     syn match   luaFunc /\<bit32\.extract\>/
    298     syn match   luaFunc /\<bit32\.lrotate\>/
    299     syn match   luaFunc /\<bit32\.lshift\>/
    300     syn match   luaFunc /\<bit32\.replace\>/
    301     syn match   luaFunc /\<bit32\.rrotate\>/
    302     syn match   luaFunc /\<bit32\.rshift\>/
    303   endif
    304 
    305   syn match   luaFunc /\<math\.abs\>/
    306   syn match   luaFunc /\<math\.acos\>/
    307   syn match   luaFunc /\<math\.asin\>/
    308   syn match   luaFunc /\<math\.atan\>/
    309   if lua_subversion < 3
    310     syn match   luaFunc /\<math\.atan2\>/
    311   endif
    312   syn match   luaFunc /\<math\.ceil\>/
    313   syn match   luaFunc /\<math\.sin\>/
    314   syn match   luaFunc /\<math\.cos\>/
    315   syn match   luaFunc /\<math\.tan\>/
    316   syn match   luaFunc /\<math\.deg\>/
    317   syn match   luaFunc /\<math\.exp\>/
    318   syn match   luaFunc /\<math\.floor\>/
    319   syn match   luaFunc /\<math\.log\>/
    320   syn match   luaFunc /\<math\.max\>/
    321   syn match   luaFunc /\<math\.min\>/
    322   if lua_subversion == 0
    323     syn match luaFunc /\<math\.mod\>/
    324     syn match luaFunc /\<math\.log10\>/
    325   elseif lua_subversion == 1
    326     syn match luaFunc /\<math\.log10\>/
    327   endif
    328   if lua_subversion >= 1
    329     syn match luaFunc /\<math\.huge\>/
    330     syn match luaFunc /\<math\.fmod\>/
    331     syn match luaFunc /\<math\.modf\>/
    332     if lua_subversion == 1 || lua_subversion == 2
    333       syn match luaFunc /\<math\.cosh\>/
    334       syn match luaFunc /\<math\.sinh\>/
    335       syn match luaFunc /\<math\.tanh\>/
    336     endif
    337   endif
    338   syn match   luaFunc /\<math\.rad\>/
    339   syn match   luaFunc /\<math\.sqrt\>/
    340   if lua_subversion < 3
    341     syn match   luaFunc /\<math\.pow\>/
    342     syn match   luaFunc /\<math\.frexp\>/
    343     syn match   luaFunc /\<math\.ldexp\>/
    344   else
    345     syn match   luaFunc /\<math\.maxinteger\>/
    346     syn match   luaFunc /\<math\.mininteger\>/
    347     syn match   luaFunc /\<math\.tointeger\>/
    348     syn match   luaFunc /\<math\.type\>/
    349     syn match   luaFunc /\<math\.ult\>/
    350   endif
    351   syn match   luaFunc /\<math\.random\>/
    352   syn match   luaFunc /\<math\.randomseed\>/
    353   syn match   luaFunc /\<math\.pi\>/
    354 
    355   syn match   luaFunc /\<io\.close\>/
    356   syn match   luaFunc /\<io\.flush\>/
    357   syn match   luaFunc /\<io\.input\>/
    358   syn match   luaFunc /\<io\.lines\>/
    359   syn match   luaFunc /\<io\.open\>/
    360   syn match   luaFunc /\<io\.output\>/
    361   syn match   luaFunc /\<io\.popen\>/
    362   syn match   luaFunc /\<io\.read\>/
    363   syn match   luaFunc /\<io\.stderr\>/
    364   syn match   luaFunc /\<io\.stdin\>/
    365   syn match   luaFunc /\<io\.stdout\>/
    366   syn match   luaFunc /\<io\.tmpfile\>/
    367   syn match   luaFunc /\<io\.type\>/
    368   syn match   luaFunc /\<io\.write\>/
    369 
    370   syn match   luaFunc /\<os\.clock\>/
    371   syn match   luaFunc /\<os\.date\>/
    372   syn match   luaFunc /\<os\.difftime\>/
    373   syn match   luaFunc /\<os\.execute\>/
    374   syn match   luaFunc /\<os\.exit\>/
    375   syn match   luaFunc /\<os\.getenv\>/
    376   syn match   luaFunc /\<os\.remove\>/
    377   syn match   luaFunc /\<os\.rename\>/
    378   syn match   luaFunc /\<os\.setlocale\>/
    379   syn match   luaFunc /\<os\.time\>/
    380   syn match   luaFunc /\<os\.tmpname\>/
    381 
    382   syn match   luaFunc /\<debug\.debug\>/
    383   syn match   luaFunc /\<debug\.gethook\>/
    384   syn match   luaFunc /\<debug\.getinfo\>/
    385   syn match   luaFunc /\<debug\.getlocal\>/
    386   syn match   luaFunc /\<debug\.getupvalue\>/
    387   syn match   luaFunc /\<debug\.setlocal\>/
    388   syn match   luaFunc /\<debug\.setupvalue\>/
    389   syn match   luaFunc /\<debug\.sethook\>/
    390   syn match   luaFunc /\<debug\.traceback\>/
    391   if lua_subversion == 1
    392     syn match luaFunc /\<debug\.getfenv\>/
    393     syn match luaFunc /\<debug\.setfenv\>/
    394   endif
    395   if lua_subversion >= 1
    396     syn match luaFunc /\<debug\.getmetatable\>/
    397     syn match luaFunc /\<debug\.setmetatable\>/
    398     syn match luaFunc /\<debug\.getregistry\>/
    399     if lua_subversion >= 2
    400       syn match luaFunc /\<debug\.getuservalue\>/
    401       syn match luaFunc /\<debug\.setuservalue\>/
    402       syn match luaFunc /\<debug\.upvalueid\>/
    403       syn match luaFunc /\<debug\.upvaluejoin\>/
    404     endif
    405     if lua_subversion >= 4
    406       syn match luaFunc /\<debug.setcstacklimit\>/
    407     endif
    408   endif
    409 endif
    410 
    411 " Define the default highlighting.
    412 " Only when an item doesn't have highlighting yet
    413 
    414 hi def link luaStatement        Statement
    415 hi def link luaRepeat           Repeat
    416 hi def link luaFor              Repeat
    417 hi def link luaString           String
    418 hi def link luaString2          String
    419 hi def link luaStringDelimiter  luaString
    420 hi def link luaNumber           Number
    421 hi def link luaOperator         Operator
    422 hi def link luaSymbolOperator   luaOperator
    423 hi def link luaConstant         Constant
    424 hi def link luaCond             Conditional
    425 hi def link luaCondElse         Conditional
    426 hi def link luaFunction         Function
    427 hi def link luaMetaMethod       Function
    428 hi def link luaComment          Comment
    429 hi def link luaCommentDelimiter luaComment
    430 hi def link luaTodo             Todo
    431 hi def link luaTable            Structure
    432 hi def link luaError            Error
    433 hi def link luaParenError       Error
    434 hi def link luaSpecial          SpecialChar
    435 hi def link luaFunc             Identifier
    436 hi def link luaLabel            Label
    437 
    438 
    439 let b:current_syntax = "lua"
    440 
    441 let &cpo = s:cpo_save
    442 unlet s:cpo_save
    443 " vim: et ts=8 sw=2