summaryrefslogtreecommitdiff
path: root/script/parser
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-02-07 21:28:21 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-02-07 21:28:21 +0800
commit54985b6ba69478dde5e7fa107ccdd74735c3811b (patch)
tree5a0eded7742055baa0600a57c92773eccc953044 /script/parser
parent1c10b0129373e52bdf56b787b4c4230c0628a259 (diff)
downloadlua-language-server-54985b6ba69478dde5e7fa107ccdd74735c3811b.zip
fix count of local variables
fix #1886
Diffstat (limited to 'script/parser')
-rw-r--r--script/parser/compile.lua26
1 files changed, 22 insertions, 4 deletions
diff --git a/script/parser/compile.lua b/script/parser/compile.lua
index 2a95e6a9..f563c6d0 100644
--- a/script/parser/compile.lua
+++ b/script/parser/compile.lua
@@ -232,7 +232,7 @@ local ListFinishMap = {
['while'] = true,
}
-local State, Lua, Line, LineOffset, Chunk, Tokens, Index, LastTokenFinish, Mode, LocalCount
+local State, Lua, Line, LineOffset, Chunk, Tokens, Index, LastTokenFinish, Mode, LocalCount, LocalLimited
local LocalLimit = 200
@@ -728,7 +728,8 @@ local function createLocal(obj, attrs)
end
locals[#locals+1] = obj
LocalCount = LocalCount + 1
- if LocalCount > LocalLimit then
+ if not LocalLimited and LocalCount > LocalLimit then
+ LocalLimited = true
pushError {
type = 'LOCAL_LIMIT',
start = obj.start,
@@ -2252,8 +2253,6 @@ local function parseFunction(isLocal, isAction)
},
}
Index = Index + 2
- local LastLocalCount = LocalCount
- LocalCount = 0
skipSpace(true)
local hasLeftParen = Tokens[Index + 1] == '('
if not hasLeftParen then
@@ -2289,6 +2288,8 @@ local function parseFunction(isLocal, isAction)
hasLeftParen = Tokens[Index + 1] == '('
end
end
+ local LastLocalCount = LocalCount
+ LocalCount = 0
pushChunk(func)
local params
if func.name and func.name.type == 'getmethod' then
@@ -2870,6 +2871,8 @@ local function compileExpAsAction(exp)
local isLocal
if exp.type == 'getlocal' and exp[1] == State.ENVMode then
exp.special = nil
+ -- TODO: need + 1 at the end
+ LocalCount = LocalCount - 1
local loc = createLocal(exp, parseLocalAttrs())
loc.locPos = exp.start
loc.effect = maxinteger
@@ -3375,6 +3378,7 @@ local function parseFor()
missName()
end
skipSpace()
+ local forStateVars
-- for i =
if expectAssign() then
action.type = 'loop'
@@ -3389,6 +3393,9 @@ local function parseFor()
name = nameOrList[1]
end
end
+ -- for x in ... uses 4 variables
+ forStateVars = 3
+ LocalCount = LocalCount + forStateVars
if name then
local loc = createLocal(name)
loc.parent = action
@@ -3480,6 +3487,13 @@ local function parseFor()
missExp()
end
+ if State.version == 'Lua 5.4' then
+ forStateVars = 4
+ else
+ forStateVars = 3
+ end
+ LocalCount = LocalCount + forStateVars
+
if list then
local lastName = list[#list]
list.range = lastName and lastName.range or inRight
@@ -3541,6 +3555,9 @@ local function parseFor()
if action.locals then
LocalCount = LocalCount - #action.locals
end
+ if forStateVars then
+ LocalCount = LocalCount - forStateVars
+ end
return action
end
@@ -3868,6 +3885,7 @@ local function initState(lua, version, options)
LineOffset = 1
LastTokenFinish = 0
LocalCount = 0
+ LocalLimited = false
Chunk = {}
Tokens = tokens(lua)
Index = 1