diff options
-rw-r--r-- | script/core/generic.lua | 18 | ||||
-rw-r--r-- | script/core/infer.lua | 10 | ||||
-rw-r--r-- | script/core/noder.lua (renamed from script/core/linker.lua) | 72 | ||||
-rw-r--r-- | script/core/searcher.lua | 78 | ||||
-rw-r--r-- | script/vm/getLinks.lua | 23 | ||||
-rw-r--r-- | test/basic/linker.lua | 6 |
6 files changed, 103 insertions, 104 deletions
diff --git a/script/core/generic.lua b/script/core/generic.lua index a7baf090..7e22aaa8 100644 --- a/script/core/generic.lua +++ b/script/core/generic.lua @@ -1,4 +1,4 @@ -local linker = require "core.linker" +local noder = require "core.noder" ---@class generic.value ---@field type string ---@field closure generic.closure @@ -51,7 +51,7 @@ local function createValue(closure, proto, callback, road) end local value = instantValue(closure, proto) value.types = types - linker.compileLink(value) + noder.compileNode(value) return value end if proto.type == 'doc.type.name' then @@ -63,7 +63,7 @@ local function createValue(closure, proto, callback, road) if callback then callback(road, key, proto) end - linker.compileLink(value) + noder.compileNode(value) return value end if proto.type == 'doc.type.function' then @@ -91,12 +91,12 @@ local function createValue(closure, proto, callback, road) value.args = args value.returns = returns value.isGeneric = true - linker.pushSource(value) + noder.pushSource(value) return value end if proto.type == 'doc.type.array' then if road then - road[#road+1] = linker.ANY_FIELD + road[#road+1] = noder.ANY_FIELD end local node = createValue(closure, proto.node, callback, road) if road then @@ -110,11 +110,11 @@ local function createValue(closure, proto, callback, road) return value end if proto.type == 'doc.type.table' then - road[#road+1] = linker.TABLE_KEY + road[#road+1] = noder.TABLE_KEY local tkey = createValue(closure, proto.tkey, callback, road) road[#road] = nil - road[#road+1] = linker.ANY_FIELD + road[#road+1] = noder.ANY_FIELD local tvalue = createValue(closure, proto.tvalue, callback, road) road[#road] = nil @@ -137,7 +137,7 @@ local function buildValue(road, key, proto, param, upvalues) end paramID = 'dn:' .. str else - paramID = linker.getID(param) + paramID = noder.getID(param) end if not paramID then return @@ -219,7 +219,7 @@ function m.createClosure(proto, call) return nil end - linker.compileLink(closure) + noder.compileNode(closure) return closure end diff --git a/script/core/infer.lua b/script/core/infer.lua index c46f26cf..a4156263 100644 --- a/script/core/infer.lua +++ b/script/core/infer.lua @@ -1,6 +1,6 @@ local searcher = require 'core.searcher' local config = require 'config' -local linker = require 'core.linker' +local noder = require 'core.noder' local STRING_OR_TABLE = {'STRING_OR_TABLE'} local BE_RETURN = {'BE_RETURN'} @@ -418,11 +418,11 @@ function m.searchInfers(source) searchInfer(def, infers) end end - local id = linker.getID(source) + local id = noder.getID(source) if id then - local link = linker.getLinkByID(source, id) - if link and link.sources then - for _, src in ipairs(link.sources) do + local node = noder.getNodeByID(source, id) + if node and node.sources then + for _, src in ipairs(node.sources) do if not mark[src] then mark[src] = true searchInfer(src, infers) diff --git a/script/core/linker.lua b/script/core/noder.lua index f11855e0..cfad7129 100644 --- a/script/core/linker.lua +++ b/script/core/noder.lua @@ -1,7 +1,7 @@ local util = require 'utility' local guide = require 'parser.guide' -local Linkers +local noders local LastIDCache = {} local FirstIDCache = {} local SPLIT_CHAR = '\x1F' @@ -15,14 +15,14 @@ local ANY_FIELD = SPLIT_CHAR .. ANY_FIELD_CHAR ---创建source的链接信息 ---@param id string ----@return link -local function getLink(id) - if not Linkers[id] then - Linkers[id] = { +---@return node +local function getNode(id) + if not noders[id] then + noders[id] = { id = id, } end - return Linkers[id] + return noders[id] end ---是否是全局变量(包括 _G.XXX 形式) @@ -302,11 +302,11 @@ local function pushForward(id, forwardID) or id == forwardID then return end - local link = getLink(id) - if not link.forward then - link.forward = {} + local node = getNode(id) + if not node.forward then + node.forward = {} end - link.forward[#link.forward+1] = forwardID + node.forward[#node.forward+1] = forwardID end ---添加关联的后退ID @@ -319,14 +319,14 @@ local function pushBackward(id, backwardID) or id == backwardID then return end - local link = getLink(id) - if not link.backward then - link.backward = {} + local node = getNode(id) + if not node.backward then + node.backward = {} end - link.backward[#link.backward+1] = backwardID + node.backward[#node.backward+1] = backwardID end ----@class link +---@class node -- 当前节点的id ---@field id string -- 使用该ID的单元 @@ -373,16 +373,16 @@ function m.pushSource(source) if not id then return end - local link = getLink(id) - if not link.sources then - link.sources = {} + local node = getNode(id) + if not node.sources then + node.sources = {} end - link.sources[#link.sources+1] = source + node.sources[#node.sources+1] = source end ---@param source parser.guide.object ---@return parser.guide.object[] -function m.compileLink(source) +function m.compileNode(source) local id = getID(source) local parent = source.parent if not parent then @@ -485,7 +485,7 @@ function m.compileLink(source) if not nodeID then return end - getLink(id).call = source + getNode(id).call = source -- 将 call 映射到 node#1 上 local callID = ('%s%s%s'):format( nodeID, @@ -529,7 +529,7 @@ function m.compileLink(source) ) pushForward(id, callXID) pushBackward(callXID, id) - getLink(id).call = call + getNode(id).call = call if node.special == 'pcall' or node.special == 'xpcall' then local index = source.sindex - 1 @@ -730,17 +730,17 @@ function m.compileLink(source) end end ----根据ID来获取所有的link +---根据ID来获取所有的node ---@param root parser.guide.object ---@param id string ----@return link? -function m.getLinkByID(root, id) +---@return node? +function m.getNodeByID(root, id) root = guide.getRoot(root) - local linkers = root._linkers - if not linkers then + local noders = root._noders + if not noders then return nil end - return linkers[id] + return noders[id] end ---根据ID来获取第一个节点的ID @@ -789,23 +789,23 @@ function m.getKey(source) return getKey(source) end ----编译整个文件的link +---编译整个文件的node ---@param source parser.guide.object ---@return table -function m.compileLinks(source) +function m.compileNodes(source) local root = guide.getRoot(source) - if root._linkers then - return root._linkers + if root._noders then + return root._noders end - Linkers = {} - root._linkers = Linkers + noders = {} + root._noders = noders guide.eachSource(root, function (src) m.pushSource(src) - m.compileLink(src) + m.compileNode(src) end) -- Special rule: ('').XX -> stringlib.XX pushForward('str:', 'dn:stringlib') - return Linkers + return noders end return m diff --git a/script/core/searcher.lua b/script/core/searcher.lua index 5590a0ba..eb9e624f 100644 --- a/script/core/searcher.lua +++ b/script/core/searcher.lua @@ -1,4 +1,4 @@ -local linker = require 'core.linker' +local noder = require 'core.noder' local guide = require 'parser.guide' local files = require 'files' local generic = require 'core.generic' @@ -65,7 +65,7 @@ function m.pushResult(status, mode, source) end if parent.type == 'return' or parent.type == 'callargs' then - if linker.getID(source) ~= status.id then + if noder.getID(source) ~= status.id then results[#results+1] = source end end @@ -106,7 +106,7 @@ function m.pushResult(status, mode, source) end end if parent.type == 'return' then - if linker.getID(source) ~= status.id then + if noder.getID(source) ~= status.id then results[#results+1] = source end end @@ -130,7 +130,7 @@ end -- TODO function m.findGlobals(root) - linker.compileLinks(root) + noder.compileNode(root) -- TODO return {} end @@ -185,7 +185,7 @@ function m.searchRefsByID(status, uri, expect, mode) end local root = ast.ast local searchStep - linker.compileLinks(root) + noder.compileNode(root) status.id = expect @@ -225,7 +225,7 @@ function m.searchRefsByID(status, uri, expect, mode) if cmark[LAST] then return end - local lastID = linker.getLastID(id) + local lastID = noder.getLastID(id) if not lastID then return end @@ -250,11 +250,11 @@ function m.searchRefsByID(status, uri, expect, mode) end local function searchFunction(id) - local link = linker.getLinkByID(root, id) - if not link or not link.sources then + local node = noder.getNodeByID(root, id) + if not node or not node.sources then return end - local obj = link.sources[1] + local obj = node.sources[1] if not obj or obj.type ~= 'function' then return end @@ -266,18 +266,18 @@ function m.searchRefsByID(status, uri, expect, mode) if not func or func.type ~= 'function' then return end - local parentID = linker.getID(func) + local parentID = noder.getID(func) if not parentID then return end - search(parentID, linker.RETURN_INDEX .. returnIndex) + search(parentID, noder.RETURN_INDEX .. returnIndex) end local function isCallID(field) if not field then return false end - if field:sub(1, 2) == linker.RETURN_INDEX then + if field:sub(1, 2) == noder.RETURN_INDEX then return true end return false @@ -308,7 +308,7 @@ function m.searchRefsByID(status, uri, expect, mode) return end - local cacheID = linker.getID(source) .. linker.getID(call) + local cacheID = noder.getID(source) .. noder.getID(call) local closure = closureCache[cacheID] if closure == false then return @@ -320,46 +320,46 @@ function m.searchRefsByID(status, uri, expect, mode) return end end - local id = linker.getID(closure) + local id = noder.getID(closure) searchID(id, field) end - local function checkForward(id, link, field) - for _, forwardID in ipairs(link.forward) do + local function checkForward(id, node, field) + for _, forwardID in ipairs(node.forward) do searchID(forwardID, field) end end - local function checkBackward(id, link, field) + local function checkBackward(id, node, field) if mode ~= 'ref' and not field then return end - for _, backwardID in ipairs(link.backward) do + for _, backwardID in ipairs(node.backward) do searchID(backwardID, field) end end - local function searchLink(id, link, field) - if link.call then - callStack[#callStack+1] = link.call + local function searchNode(id, node, field) + if node.call then + callStack[#callStack+1] = node.call end - if field == nil and link.sources then - for _, source in ipairs(link.sources) do + if field == nil and node.sources then + for _, source in ipairs(node.sources) do m.pushResult(status, mode, source) end end - if link.forward then - checkForward(id, link, field) + if node.forward then + checkForward(id, node, field) end - if link.backward then - checkBackward(id, link, field) + if node.backward then + checkBackward(id, node, field) end - if link.sources then - checkGeneric(link.sources[1], field) + if node.sources then + checkGeneric(node.sources[1], field) end - if link.call then + if node.call then callStack[#callStack] = nil end end @@ -370,16 +370,16 @@ function m.searchRefsByID(status, uri, expect, mode) if stepCount > 1000 then error('too large') end - local link = linker.getLinkByID(root, id) - if link then - searchLink(id, link, field) + local node = noder.getNodeByID(root, id) + if node then + searchNode(id, node, field) end local lastID = checkLastID(id, field) if lastID then - local anyFieldID = lastID .. linker.ANY_FIELD - local anyFieldLink = linker.getLinkByID(root, anyFieldID) - if anyFieldLink then - searchLink(anyFieldID, anyFieldLink, field) + local anyFieldID = lastID .. noder.ANY_FIELD + local anyFieldNode = noder.getNodeByID(root, anyFieldID) + if anyFieldNode then + searchNode(anyFieldID, anyFieldNode, field) end end end @@ -398,9 +398,9 @@ function m.searchRefs(status, source, mode) source = source.parent end local root = guide.getRoot(source) - linker.compileLinks(root) + noder.compileNodes(root) local uri = guide.getUri(source) - local id = linker.getID(source) + local id = noder.getID(source) if not id then return end diff --git a/script/vm/getLinks.lua b/script/vm/getLinks.lua index 86a38cfc..51a18d58 100644 --- a/script/vm/getLinks.lua +++ b/script/vm/getLinks.lua @@ -1,7 +1,6 @@ -local searcher = require 'core.searcher' ----@type vm -local vm = require 'vm.vm' -local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm.vm' +local files = require 'files' local function getFileLinks(uri) local ws = require 'workspace' @@ -11,7 +10,7 @@ local function getFileLinks(uri) return links end tracy.ZoneBeginN('getFileLinks') - searcher.eachSpecialOf(ast.ast, 'require', function (source) + guide.eachSpecialOf(ast.ast, 'require', function (source) local call = source.parent if not call or call.type ~= 'call' then return @@ -33,11 +32,17 @@ local function getFileLinks(uri) return links end +local function getFileLinksOrCache(uri) + local cache = files.getCache(uri) + cache.links = cache.links or getFileLinks(uri) + return cache.links +end + local function getLinksTo(uri) uri = files.asKey(uri) local links = {} for u in files.eachFile() do - local ls = vm.getFileLinks(u) + local ls = getFileLinksOrCache(u) if ls[uri] then for _, l in ipairs(ls[uri]) do links[#links+1] = l @@ -56,9 +61,3 @@ function vm.getLinksTo(uri) vm.getCache('getLinksTo')[uri] = cache return cache end - -function vm.getFileLinks(uri) - local cache = files.getCache(uri) - cache.links = cache.links or getFileLinks(uri) - return cache.links -end diff --git a/test/basic/linker.lua b/test/basic/linker.lua index d5525c40..3e5e9f25 100644 --- a/test/basic/linker.lua +++ b/test/basic/linker.lua @@ -1,4 +1,4 @@ -local linker = require 'core.linker' +local noder = require 'core.noder' local files = require 'files' local util = require 'utility' local guide = require 'parser.guide' @@ -37,9 +37,9 @@ local function TEST(script) files.setText('', newScript) local source = getSource(pos) assert(source) - linker.compileLinks(source) + noder.compileNodes(source) local result = { - id = linker.getID(source), + id = noder.getID(source), } expect['id'] = expect['id']:gsub('|', '\x1F') |