@@ -89,6 +89,7 @@ local KeyWord = {
8989 [' false' ] = true ,
9090 [' for' ] = true ,
9191 [' function' ] = true ,
92+ [' global' ] = true ,
9293 [' if' ] = true ,
9394 [' in' ] = true ,
9495 [' local' ] = true ,
@@ -208,6 +209,7 @@ local ChunkStartMap = {
208209 [' elseif' ] = true ,
209210 [' for' ] = true ,
210211 [' function' ] = true ,
212+ [' global' ] = true ,
211213 [' if' ] = true ,
212214 [' local' ] = true ,
213215 [' repeat' ] = true ,
@@ -752,6 +754,19 @@ local function createLocal(obj, attrs)
752754 return obj
753755end
754756
757+ --- @param obj table
758+ local function createGlobal (obj , attrs )
759+ obj .type = ' setglobal'
760+ obj .effect = obj .finish
761+
762+ if attrs then
763+ obj .attrs = attrs
764+ attrs .parent = obj
765+ end
766+
767+ return obj
768+ end
769+
755770local function pushChunk (chunk )
756771 Chunk [# Chunk + 1 ] = chunk
757772end
@@ -3121,6 +3136,65 @@ local function parseLocal()
31213136 return loc
31223137end
31233138
3139+ local function parseGlobal ()
3140+ local globalPos = getPosition (Tokens [Index ], ' left' )
3141+
3142+ -- Global declarations are only supported in Lua 5.5
3143+ if State .version ~= ' Lua 5.5' then
3144+ pushError {
3145+ type = ' UNSUPPORT_SYMBOL' ,
3146+ start = globalPos ,
3147+ finish = getPosition (Tokens [Index ] + 5 , ' right' ),
3148+ version = ' Lua 5.5' ,
3149+ info = {
3150+ version = State .version ,
3151+ }
3152+ }
3153+ end
3154+
3155+ Index = Index + 2
3156+ skipSpace ()
3157+ local word = peekWord ()
3158+ if not word then
3159+ missName ()
3160+ return nil
3161+ end
3162+
3163+ if word == ' function' then
3164+ local func = parseFunction (false , true )
3165+ local name = func .name
3166+ if name then
3167+ func .name = nil
3168+ name .type = GetToSetMap [name .type ]
3169+ name .value = func
3170+ name .vstart = func .start
3171+ name .range = func .finish
3172+ name .globPos = globalPos
3173+ func .parent = name
3174+ pushActionIntoCurrentChunk (name )
3175+ return name
3176+ else
3177+ missName (func .keyword [2 ])
3178+ pushActionIntoCurrentChunk (func )
3179+ return func
3180+ end
3181+ end
3182+
3183+ local name = parseName (true )
3184+ if not name then
3185+ missName ()
3186+ return nil
3187+ end
3188+ local glob = createGlobal (name )
3189+ glob .globPos = globalPos
3190+ glob .effect = maxinteger
3191+ pushActionIntoCurrentChunk (glob )
3192+ skipSpace ()
3193+ parseMultiVars (glob , parseName , false )
3194+
3195+ return glob
3196+ end
3197+
31243198local function parseDo ()
31253199 local doLeft = getPosition (Tokens [Index ], ' left' )
31263200 local doRight = getPosition (Tokens [Index ] + 1 , ' right' )
@@ -3904,6 +3978,10 @@ function parseAction()
39043978 return parseLocal ()
39053979 end
39063980
3981+ if token == ' global' then
3982+ return parseGlobal ()
3983+ end
3984+
39073985 if token == ' if'
39083986 or token == ' elseif'
39093987 or token == ' else' then
0 commit comments