From d19514f1904557a464467bfc9540882b4a214840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 10 Dec 2018 10:45:39 +0800 Subject: =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=8E=A8=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/matcher/compile.lua | 14 ++++++++++++ server/src/matcher/init.lua | 3 +++ server/src/matcher/type_inference.lua | 43 +++++++++++++++++++++++++++++++++++ server/test/definition/init.lua | 1 + server/test/find_lib/init.lua | 6 ++--- server/test/main.lua | 1 + server/test/type_inference/init.lua | 28 +++++++++++++++++++++++ 7 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 server/src/matcher/type_inference.lua create mode 100644 server/test/type_inference/init.lua (limited to 'server') diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua index 557f4f59..bcf2ca91 100644 --- a/server/src/matcher/compile.lua +++ b/server/src/matcher/compile.lua @@ -187,7 +187,9 @@ function mt:searchCall(call, func, lastobj) if lastobj then table.insert(self.results.calls, { call = call, + func = func, lastobj = lastobj, + results = results, }) end @@ -312,6 +314,18 @@ function mt:setValue(var, value) return end var.value = value.value or value + local group = var.group or value.group + if not group then + group = {} + end + if not group[var] then + var.group = group + group[var] = group + end + if not group[value] then + value.group = group + group[value] = group + end if value.childs then var.childs = value.childs for _, child in pairs(value.childs) do diff --git a/server/src/matcher/init.lua b/server/src/matcher/init.lua index cacef3e5..eda4ac7b 100644 --- a/server/src/matcher/init.lua +++ b/server/src/matcher/init.lua @@ -6,6 +6,9 @@ local api = { hover = require 'matcher.hover', diagnostics = require 'matcher.diagnostics', compile = require 'matcher.compile', + typeInference = require 'matcher.type_inference', + findResult = require 'matcher.find_result', + findLib = require 'matcher.find_lib', } return api diff --git a/server/src/matcher/type_inference.lua b/server/src/matcher/type_inference.lua new file mode 100644 index 00000000..59874a7e --- /dev/null +++ b/server/src/matcher/type_inference.lua @@ -0,0 +1,43 @@ +local mt = {} +mt.__index = mt + +function mt:searchGroup(group) + if not group then + return + end + if group.type ~= nil then + return + end + for obj in pairs(group) do + if obj.valuetype then + group.type = obj.valuetype + return + end + end + group.type = false +end + +function mt:searchVar(var) + if var.valuetype then + return + end + if self.lock[var] then + return + end + self.lock[var] = true + self:searchGroup(var.group) + self.lock[var] = nil +end + +function mt:searchVars(vars) + for _, var in ipairs(vars) do + self:searchVar(var) + end +end + +return function (results) + local session = setmetatable({ + lock = {}, + }, mt) + session:searchVars(results.vars) +end diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua index 11a79391..8bbaf65f 100644 --- a/server/test/definition/init.lua +++ b/server/test/definition/init.lua @@ -49,6 +49,7 @@ function TEST(script) assert(ast) local results = matcher.compile(ast) assert(results) + matcher.typeInference(results) local result = matcher.definition(results, pos) if result then diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua index f8d7f115..18b88399 100644 --- a/server/test/find_lib/init.lua +++ b/server/test/find_lib/init.lua @@ -1,7 +1,5 @@ local matcher = require 'matcher' local parser = require 'parser' -local findResult = require 'matcher.find_result' -local findLib = require 'matcher.find_lib' rawset(_G, 'TEST', true) @@ -15,12 +13,12 @@ function TEST(fullkey) assert(ast) local results = matcher.compile(ast) assert(results) - local result = findResult(results, pos) + local result = matcher.findResult(results, pos) assert(result) assert(result.type == 'var') local var = result.var assert(var) - local _, name = findLib(var) + local _, name = matcher.findLib(var) assert(name == fullkey) end end diff --git a/server/test/main.lua b/server/test/main.lua index 05cc7ee6..3167089e 100644 --- a/server/test/main.lua +++ b/server/test/main.lua @@ -24,6 +24,7 @@ local function main() end test 'compile' + test 'type_inference' test 'definition' test 'find_lib' diff --git a/server/test/type_inference/init.lua b/server/test/type_inference/init.lua new file mode 100644 index 00000000..ee47b223 --- /dev/null +++ b/server/test/type_inference/init.lua @@ -0,0 +1,28 @@ +local parser = require 'parser' +local matcher = require 'matcher' + +rawset(_G, 'TEST', true) + +function TEST(res) + return function (script) + local start = script:find('', 1, true) + local pos = (start + finish) // 2 + 1 + local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + local ast = parser:ast(new_script) + assert(ast) + local results = matcher.compile(ast) + assert(results) + matcher.typeInference(results) + local result = matcher.findResult(results, pos) + assert(result) + local var = result.var + assert(var) + assert(var.group) + assert(var.group.type == res) + end +end + +TEST 'string' [[ +local = '111' +]] -- cgit v1.2.3