diff options
Diffstat (limited to 'server-beta/src/searcher/init.lua')
-rw-r--r-- | server-beta/src/searcher/init.lua | 98 |
1 files changed, 78 insertions, 20 deletions
diff --git a/server-beta/src/searcher/init.lua b/server-beta/src/searcher/init.lua index 054067d7..4796be16 100644 --- a/server-beta/src/searcher/init.lua +++ b/server-beta/src/searcher/init.lua @@ -1,9 +1,8 @@ -local guide = require 'parser.guide' -local files = require 'files' -local util = require 'utility' -local getValue = require 'searcher.getValue' -local getField = require 'searcher.getField' -local eachRef = require 'searcher.eachRef' +local guide = require 'parser.guide' +local files = require 'files' +local util = require 'utility' +local getField = require 'searcher.getField' +local eachRef = require 'searcher.eachRef' local eachField = require 'searcher.eachField' local setmetatable = setmetatable @@ -36,17 +35,6 @@ function mt:lock(tp, source) end) end ---- 获取关联的值 ----@param source table ----@return value table -function mt:getValue(source) - local lock <close> = self:lock('getValue', source) - if not lock then - return nil - end - return getValue(self, source) -end - --- 获取关联的field ---@param source table ---@return table field @@ -108,6 +96,75 @@ function mt:eachField(source, callback) end) end +--- 获取特殊对象的名字 +function mt:getSpecialName(source) + local spName = self.cache.specialName[source] + if spName ~= nil then + if spName then + return spName + end + return nil + end + local function getName(info) + local src = info.source + if src.type == 'getglobal' then + local node = src.node + if node.tag ~= '_ENV' then + return nil + end + local name = guide.getKeyName(src) + if name:sub(1, 2) ~= 's|' then + return nil + end + spName = name:sub(3) + if not specials[spName] then + spName = nil + end + elseif src.type == 'local' then + if src.tag == '_ENV' then + spName = '_G' + end + elseif src.type == 'getlocal' then + local loc = src.loc + if loc.tag == '_ENV' then + spName = '_G' + end + end + end + self:eachRef(source, getName) + self.cache.specialName[source] = spName or false + return spName +end + +--- 遍历特殊对象 +---@param callback fun(name:string, source:table) +function mt:eachSpecial(callback) + local cache = self.cache.specials + if cache then + for i = 1, #cache do + callback(cache[i][1], cache[i][2]) + end + return + end + cache = {} + self.cache.specials = cache + guide.eachSource(self.ast, function (source) + if source.type == 'getlocal' + or source.type == 'getglobal' + or source.type == 'local' + or source.type == 'field' + or source.type == 'string' then + local name = self:getSpecialName(source) + if name then + cache[#cache+1] = { name, source } + end + end + end) + for i = 1, #cache do + callback(cache[i][1], cache[i][2]) + end +end + ---@class engineer local m = {} @@ -120,11 +177,12 @@ function m.create(uri) ast = ast.ast, uri = uri, cache = { - eachRef = {}, - eachField = {}, + eachRef = {}, + eachField = {}, + specialName = {}, + specials = nil, }, locked = { - getValue = {}, eachRef = {}, eachField = {}, } |