From 6a83c3aabf00300ca9742a1f207fe249e443fa6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 21 Apr 2021 17:22:24 +0800 Subject: cleanup --- script/core/linker.lua | 145 ++++++++++++++++++---------------------------- script/core/searcher.lua | 10 ++-- script/parser/compile.lua | 4 +- 3 files changed, 65 insertions(+), 94 deletions(-) (limited to 'script') diff --git a/script/core/linker.lua b/script/core/linker.lua index c4f48835..0674ca73 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -2,6 +2,12 @@ local util = require 'utility' local guide = require 'parser.guide' local vm = require 'vm.vm' +local Linkers + +local function pushLastID(id, lastID) + Linkers.lastIDMap[id] = lastID +end + ---是否是全局变量(包括 _G.XXX 形式) ---@param source parser.guide.object ---@return boolean @@ -84,6 +90,10 @@ local function getKey(source) or source.type == 'doc.type.name' or source.type == 'doc.alias.name' then return source[1], nil + elseif source.type == 'doc.class' + or source.type == 'doc.type' + or source.type == 'doc.alias' then + return source.start, nil end return nil, nil end @@ -102,7 +112,12 @@ local function checkMode(source) or source.type == 'doc.type.name' or source.type == 'doc.alias.name' or source.type == 'doc.extends.name' then - return 'd' + return 'dn' + end + if source.type == 'doc.class' + or source.type == 'doc.type' + or source.type == 'doc.alias' then + return 'ds' end if isGlobal(source) then return 'g' @@ -153,17 +168,23 @@ local function checkForward(source) local bindDocs = source.bindDocs if bindDocs then for _, doc in ipairs(bindDocs) do - if doc.type == 'doc.class' then - list[#list+1] = doc.class - elseif doc.type == 'doc.type' then - if doc.types then - for _, typeUnit in ipairs(doc.types) do - list[#list+1] = typeUnit - end - end + if doc.type == 'doc.class' + or doc.type == 'doc.type' then + list[#list+1] = doc end end end + -- 分解 @type + if source.type == 'doc.type' then + for _, typeUnit in ipairs(source.types) do + list[#list+1] = typeUnit + end + end + -- 分解 @class + if source.type == 'doc.class' then + list[#list+1] = source.class + list[#list+1] = source.extends + end if #list == 0 then return nil else @@ -189,69 +210,21 @@ local function checkBackward(source) list[#list+1] = setmethod.node end end - if parent.parent and parent.parent.type == 'doc' then - -- @class 绑定的 source - if source.type == 'doc.class.name' then - if parent.type == 'doc.class' then - for _, src in ipairs(parent.bindSources) do - list[#list+1] = src - end - end - end - -- @type 绑定的 source - if source.type == 'doc.type.name' then - if parent.type == 'doc.type' and parent.parent.type == 'doc' then - for _, src in ipairs(parent.bindSources) do - list[#list+1] = src - end - end - end - end - if #list == 0 then - return nil - else - TempList = {} - return list - end -end - -local function checkSpecial(source) - local list = TempList - -- 接收 call 的返回值 - if source.type == 'select' then - local index = source.index - local call = source.vararg - if call.type == 'call' then - list.call = { - node = call.node, - index = index, - } - end + -- name 映射回 class 与 type + if source.type == 'doc.class.name' + or source.type == 'doc.type.name' then + list[#list+1] = parent end - -- func 的返回值 - if source.type == 'function' then - local returns = {} - list.returns = returns - if source.bindDocs then - local index = 0 - for _, doc in ipairs(source.bindDocs) do - if doc.type == 'doc.return' then - for _, rtn in ipairs(doc.returns) do - index = index + 1 - if not returns[index] then - returns[index] = {} - end - if rtn.types then - for _, typeUnit in ipairs(rtn.types) do - returns[index][#returns[index]+1] = typeUnit - end - end - end - end + -- class 与 type 绑定的 source + if source.type == 'doc.class' + or source.type == 'doc.type' then + if source.bindSources then + for _, src in ipairs(source.bindSources) do + list[#list+1] = src end end end - if not next(list) then + if #list == 0 then return nil else TempList = {} @@ -299,18 +272,19 @@ local function getID(source) end util.revertTable(IDList) local id = table.concat(IDList, '|') - local lastID if index > 1 then - lastID = table.concat(IDList, '|', 1, index) + local lastID = table.concat(IDList, '|', 1, index) + pushLastID(id, lastID) end - return id, current, lastID + if mode == 's' then + pushLastID(id, id:gsub(':%d+$', '')) + end + return id, current end ---@class link -- 当前节点的id ---@field id string --- 上个节点的id ----@field lastID string -- 语法树单元 ---@field source parser.guide.object -- 返回值,文件返回值总是0,函数返回值为第几个返回值 @@ -326,32 +300,27 @@ end ---@param source parser.guide.object ---@return link local function createLink(source) - local id, node, lastID = getID(source) + local id, node = getID(source) if not id then return nil end return { id = id, source = source, - lastID = lastID, freturn = checkFunctionReturn(node), forward = checkForward(source), backward = checkBackward(source), - special = checkSpecial(source), } end ---@param link link -local function insertLinker(linkers, link) - local idMap = linkers.idMap +local function insertLinker(link) + local idMap = Linkers.idMap local id = link.id if not idMap[id] then idMap[id] = {} end idMap[id][#idMap[id]+1] = link - if link.lastID then - linkers.lastIDMap[id] = link.lastID - end end local m = {} @@ -386,10 +355,10 @@ end ---@param source parser.guide.object ---@return link function m.getLink(source) - if not source._link then - source._link = createLink(source) + if source._link == nil then + source._link = createLink(source) or false end - return source._link + return source._link or nil end ---获取source的ID @@ -432,19 +401,19 @@ function m.compileLinks(source) if root._linkers then return root._linkers end - local linkers = { + Linkers = { idMap = {}, lastIDMap = {}, } + root._linkers = Linkers guide.eachSource(root, function (src) local link = m.getLink(src) if not link then return end - insertLinker(linkers, link) + insertLinker(link) end) - root._linkers = linkers - return linkers + return Linkers end return m diff --git a/script/core/searcher.lua b/script/core/searcher.lua index 2d1ddaa4..2f8c6dac 100644 --- a/script/core/searcher.lua +++ b/script/core/searcher.lua @@ -200,7 +200,7 @@ function m.searchRefsByID(status, uri, expect, mode) end if special.returns then local newStatus = m.status(status) - m.searchRefs(newStatus, special.call.node, 'def') + --m.searchRefs(newStatus, special.call.node, 'def') end end @@ -219,7 +219,7 @@ function m.searchRefsByID(status, uri, expect, mode) return end stackCount = stackCount + 1 - if stackCount >= 10 then + if stackCount >= 100 then error('stack overflow') end for _, eachLink in ipairs(links) do @@ -247,8 +247,10 @@ function m.searchRefs(status, source, mode) or source.type == 'method' then source = source.parent end - local uri = guide.getUri(source) - local id = linker.getID(source) + local root = guide.getRoot(source) + linker.compileLinks(root) + local uri = guide.getUri(source) + local id = linker.getID(source) if not id then return end diff --git a/script/parser/compile.lua b/script/parser/compile.lua index a7e0dc1f..ae4808df 100644 --- a/script/parser/compile.lua +++ b/script/parser/compile.lua @@ -150,8 +150,8 @@ local vmMap = { local value = obj.value local localself = { type = 'local', - start = 0, - finish = 0, + start = value.start, + finish = value.finish, method = obj, effect = obj.finish, tag = 'self', -- cgit v1.2.3