diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/definition.lua | 106 | ||||
-rw-r--r-- | server/test/definition/arg.lua | 8 | ||||
-rw-r--r-- | server/test/definition/bug.lua | 4 | ||||
-rw-r--r-- | server/test/definition/function.lua | 8 | ||||
-rw-r--r-- | server/test/definition/init.lua | 55 | ||||
-rw-r--r-- | server/test/definition/local.lua | 42 | ||||
-rw-r--r-- | server/test/definition/method.lua | 23 | ||||
-rw-r--r-- | server/test/definition/set.lua | 8 | ||||
-rw-r--r-- | server/test/definition/table.lua | 4 |
9 files changed, 165 insertions, 93 deletions
diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua index 12139e62..6c2f64b4 100644 --- a/server/src/matcher/definition.lua +++ b/server/src/matcher/definition.lua @@ -6,13 +6,16 @@ function mt:isContainPos(obj) return obj.start <= self.pos and obj.finish + 1 >= self.pos end -function mt:getVar(key) +function mt:getVar(key, source) if key == nil then return nil end - return - self.env.var[key] or - self:getField(self.env.var._ENV, key) -- 这里不需要用getVar来递归获取_ENV + local var = self.env.var[key] + or self:getField(self.env.var._ENV, key, source) -- 这里不需要用getVar来递归获取_ENV + if not var then + var = self:addField(self:getVar '_ENV', key, source) + end + return var end function mt:addVarInfo(var, info) @@ -24,51 +27,49 @@ function mt:createLocal(key, source) if key == nil then return nil end - local var = self:addVarInfo({}, { - type = 'local', + local var = { + type = 'local', source = source, - }) + } self.env.var[key] = var return var end -function mt:createGlobal(key, source) - if key == nil then - return nil - end - local info = { - type = 'global', - source = source, - } - local var = self:addField(self:getVar '_ENV', key, info) - return var +function mt:bindLocal(key, other_key, source) + self.env.var[key] = self:getVar(other_key, source) end -function mt:addField(parent, key, info) +function mt:addField(parent, key, source) if parent == nil or key == nil then return nil end - assert(info) + assert(source) if not parent.childs then parent.childs = {} end local var = parent.childs[key] if not var then - var = {} + var = { + type = 'field', + source = source, + } parent.childs[key] = var end - self:addVarInfo(var, info) return var end -function mt:getField(parent, key) +function mt:getField(parent, key, source) if parent == nil or key == nil then return nil end if not parent.childs then return nil end - return parent.childs[key] + local var = parent.childs[key] + if not var then + var = self:addField(parent, key, source) + end + return var end function mt:checkVar(var, source) @@ -84,7 +85,7 @@ function mt:checkVar(var, source) end function mt:checkName(name) - local var = self:getVar(name[1]) + local var = self:getVar(name[1], name) self:checkVar(var, name) end @@ -110,24 +111,26 @@ end function mt:searchSimple(simple) local name = simple[1] - local var = self:getVar(name[1]) or self:createGlobal(name[1], name) + local var = self:getVar(name[1], name) self:checkVar(var, name) for i = 2, #simple do local obj = simple[i] local tp = obj.type if tp == 'call' then + var = nil self:searchCall(obj) + elseif tp == ':' then elseif tp == 'name' then if obj.index then var = nil self:searchExp(obj) else - var = self:getField(var, obj[1]) + var = self:getField(var, obj[1], obj) self:checkVar(var, obj) end else if obj.index then - var = self:getField(var, obj[1]) + var = self:getField(var, obj[1], obj) self:checkVar(var, obj) else var = nil @@ -185,27 +188,21 @@ end function mt:markSimple(simple) local name = simple[1] - local var = self:getVar(name[1]) or self:createGlobal(name[1], name) + local var = self:getVar(name[1], name) for i = 2, #simple do local obj = simple[i] local tp = obj.type if tp == ':' then - self:createLocal('self', obj) + self:bindLocal('self', simple[i-1][1], simple[i-1]) elseif tp == 'name' then if not obj.index then - var = self:addField(var, obj[1], { - type = 'field', - source = obj, - }) + var = self:addField(var, obj[1], obj) else var = nil end else if obj.index then - var = self:addField(var, obj[1], { - type = 'field', - source = obj, - }) + var = self:addField(var, obj[1], obj) else var = nil end @@ -215,7 +212,7 @@ end function mt:markSet(simple) if simple.type == 'name' then - local var = self:getVar(simple[1]) or self:createGlobal(simple[1], simple) + local var = self:getVar(simple[1], simple) self:addVarInfo(var, { type = 'set', source = simple, @@ -399,15 +396,40 @@ function mt:searchActions(actions) end local function parseResult(result) + local results = {} local tp = result.type if tp == 'var' then - local first = result.var[1] - local source = first.source - return true, source.start, source.finish + local var = result.var + if var.type == 'local' then + local source = var.source + if not source then + return false + end + results[1] = {source.start, source.finish} + elseif var.type == 'field' then + local source = var.source + if not source then + return false + end + if #var == 0 then + results[1] = {source.start, source.finish} + else + for i, info in ipairs(var) do + if info.type == 'set' then + results[i] = {info.source.start, info.source.finish} + end + end + end + else + error('unknow var.type:' .. var.type) + end elseif tp == 'dots' then local dots = result.dots - return true, dots.start, dots.finish + results[1] = {dots.start, dots.finish} + else + error('unknow result.type:' .. result.type) end + return true, results end return function (ast, pos) diff --git a/server/test/definition/arg.lua b/server/test/definition/arg.lua index 2004d666..83afb04f 100644 --- a/server/test/definition/arg.lua +++ b/server/test/definition/arg.lua @@ -1,6 +1,6 @@ TEST [[ local function xx (<!xx!>) - <?xx?> = 1 + <?xx?>() end ]] @@ -11,13 +11,13 @@ end ]] TEST [[ -function mt<!:!>x() - <?self?> = 1 +function <!mt!>:x() + <?self?>() end ]] TEST [[ function mt:x(<!self!>) - <?self?> = 1 + <?self?>() end ]] diff --git a/server/test/definition/bug.lua b/server/test/definition/bug.lua index 4ad2a05a..7019e079 100644 --- a/server/test/definition/bug.lua +++ b/server/test/definition/bug.lua @@ -10,12 +10,12 @@ end TEST [[ function _(<!x!>) do return end - <?x?> = 1 + <?x?>() end ]] TEST [[ -function a<!:!>b() +function <!a!>:b() a:b() <?self?>() end diff --git a/server/test/definition/function.lua b/server/test/definition/function.lua index 1ef6a463..5deda51c 100644 --- a/server/test/definition/function.lua +++ b/server/test/definition/function.lua @@ -1,18 +1,18 @@ TEST [[ function <!x!> () end -<?x?> = 1 +<?x?>() ]] TEST [[ local function <!x!> () end -<?x?> = 1 +<?x?>() ]] TEST [[ local x local function <!x!> () - <?x?> = 1 + <?x?>() end ]] @@ -20,5 +20,5 @@ TEST [[ local <!x!> function x() end -<?x?> = 1 +<?x?>() ]] diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua index 1de41c57..de503c81 100644 --- a/server/test/definition/init.lua +++ b/server/test/definition/init.lua @@ -3,26 +3,52 @@ local parser = require 'parser' rawset(_G, 'TEST', true) -function TEST(script) - local start = script:find('<!', 1, true) - local finish = script:find('!>', 1, true) - if start and finish then - start = start + 2 - finish = finish - 1 +local function catch_target(script) + local list = {} + local cur = 1 + while true do + local start, finish = script:find('<!.-!>', cur) + if not start then + break + end + list[#list+1] = { start + 2, finish - 2 } + cur = finish + 1 + end + return list +end + +local function founded(targets, results) + while true do + local target = table.remove(targets) + if not target then + break + end + for i, result in ipairs(results) do + if target[1] == result[1] and target[2] == result[2] then + table.remove(results, i) + goto CONTINUE + end + end + do return false end + ::CONTINUE:: + end + if #results == 0 then + return true + else + return false end +end + +function TEST(script) + local target = catch_target(script) local pos = script:find('<?', 1, true) + 2 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') local ast, err = parser:ast(new_script) assert(ast) - local suc, a, b = matcher.definition(ast, pos) - if start and finish then - assert(suc == true) - assert(a == start) - assert(b == finish) - else - assert(suc == false) - end + local suc, result = matcher.definition(ast, pos) + assert(suc) + assert(founded(target, result)) end require 'definition.set' @@ -30,4 +56,5 @@ require 'definition.local' require 'definition.arg' require 'definition.function' require 'definition.table' +require 'definition.method' require 'definition.bug' diff --git a/server/test/definition/local.lua b/server/test/definition/local.lua index 0737443d..f4744799 100644 --- a/server/test/definition/local.lua +++ b/server/test/definition/local.lua @@ -1,33 +1,33 @@ TEST [[ local <!x!> -<?x?> = 1 +<?x?>() ]] TEST [[ local z, y, <!x!> -<?x?> = 1 +<?x?>() ]] TEST [[ local <!x!> = 1 -<?x?> = 1 +<?x?>() ]] TEST [[ local z, y, <!x!> = 1 -<?x?> = 1 +<?x?>() ]] TEST [[ local x local <!x!> -<?x?> = 1 +<?x?>() ]] TEST [[ local <!x!> do - <?x?> = 1 + <?x?>() end ]] @@ -36,7 +36,7 @@ local <!x!> do local x end -<?x?> = 1 +<?x?>() ]] TEST [[ @@ -64,13 +64,13 @@ elseif x then else local x end -<?x?> = 1 +<?x?>() ]] TEST [[ local <!x!> if x then - <?x?> = 1 + <?x?>() elseif x then local x else @@ -82,13 +82,13 @@ TEST [[ local <!x!> for x = 1, 10 do end -<?x?> = 1 +<?x?>() ]] TEST [[ local x for <!x!> = 1, 10 do - <?x?> = 1 + <?x?>() end ]] @@ -96,7 +96,7 @@ TEST [[ local <!x!> for x in x do end -<?x?> = 1 +<?x?>() ]] TEST [[ @@ -108,14 +108,14 @@ end TEST [[ local x for <!x!> in x do - <?x?> = 1 + <?x?>() end ]] TEST [[ local x for z, y, <!x!> in x do - <?x?> = 1 + <?x?>() end ]] @@ -128,7 +128,7 @@ end TEST [[ local <!x!> while x do - <?x?> = 1 + <?x?>() end ]] @@ -137,13 +137,13 @@ local <!x!> while x do local x end -<?x?> = 1 +<?x?>() ]] TEST [[ local <!x!> repeat - <?x?> = 1 + <?x?>() until true ]] @@ -152,7 +152,7 @@ local <!x!> repeat local x until true -<?x?> = 1 +<?x?>() ]] TEST [[ @@ -173,19 +173,19 @@ local <!x!> function _() local x end -<?x?> = 1 +<?x?>() ]] TEST [[ local <!x!> return function () - <?x?> = 1 + <?x?>() end ]] TEST [[ local <!x!> local x = function () - <?x?> = 1 + <?x?>() end ]] diff --git a/server/test/definition/method.lua b/server/test/definition/method.lua new file mode 100644 index 00000000..96b77a60 --- /dev/null +++ b/server/test/definition/method.lua @@ -0,0 +1,23 @@ +TEST [[ +function mt:<!a!>() +end +function mt:b() + mt:<?a?>() +end +]] + +TEST [[ +function mt:<!m1!>() +end +function mt:m2() + self:<?m1?>() +end +]] + +TEST [[ +function mt:m3() + mt:<?m4?>() +end +function mt:<!m4!>() +end +]] diff --git a/server/test/definition/set.lua b/server/test/definition/set.lua index 2e48e490..294a92ca 100644 --- a/server/test/definition/set.lua +++ b/server/test/definition/set.lua @@ -1,13 +1,13 @@ TEST [[ <!x!> = 1 -<?x?> = 1 +<?x?>() ]] TEST [[ do <!global!> = 1 end -<?global?> = 1 +<?global?>() ]] TEST [[ @@ -15,7 +15,7 @@ TEST [[ do local x = 1 end -<?x?> = 1 +<?x?>() ]] TEST [[ @@ -25,6 +25,6 @@ do do x = 2 end - <?x?> = 1 + <?x?>() end ]] diff --git a/server/test/definition/table.lua b/server/test/definition/table.lua index 47248508..398abc80 100644 --- a/server/test/definition/table.lua +++ b/server/test/definition/table.lua @@ -8,12 +8,12 @@ a = { TEST [[ local t t.<!x!> = 1 -t.<?x?> = 1 +t.<?x?>() ]] TEST [[ t.<!x!> = 1 -t.<?x?> = 1 +t.<?x?>() ]] TEST [[ |