diff options
Diffstat (limited to 'server-beta/src/searcher')
-rw-r--r-- | server-beta/src/searcher/eachRef.lua | 33 | ||||
-rw-r--r-- | server-beta/src/searcher/getField.lua | 3 | ||||
-rw-r--r-- | server-beta/src/searcher/getValue.lua | 2 | ||||
-rw-r--r-- | server-beta/src/searcher/init.lua | 65 |
4 files changed, 83 insertions, 20 deletions
diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua new file mode 100644 index 00000000..47cb8f27 --- /dev/null +++ b/server-beta/src/searcher/eachRef.lua @@ -0,0 +1,33 @@ +local function ofLocal(searcher, loc, callback) + callback { + searcher = searcher, + source = loc, + mode = 'declare', + } + if loc.ref then + for _, ref in ipairs(loc.ref) do + if ref.type == 'getlocal' then + callback { + searcher = searcher, + source = ref, + mode = 'get', + } + elseif ref.type == 'setlocal' then + callback { + searcher = searcher, + source = ref, + mode = 'set', + } + end + end + end +end + +return function (searcher, source, callback) + if source.type == 'local' then + ofLocal(searcher, source, callback) + elseif source.type == 'getlocal' + or source.type == 'setlocal' then + ofLocal(searcher, source.node, callback) + end +end diff --git a/server-beta/src/searcher/getField.lua b/server-beta/src/searcher/getField.lua new file mode 100644 index 00000000..0957a029 --- /dev/null +++ b/server-beta/src/searcher/getField.lua @@ -0,0 +1,3 @@ +return function (searcher, source) + +end diff --git a/server-beta/src/searcher/getValue.lua b/server-beta/src/searcher/getValue.lua index 5f716b65..70e10bef 100644 --- a/server-beta/src/searcher/getValue.lua +++ b/server-beta/src/searcher/getValue.lua @@ -1,2 +1,2 @@ -return function () +return function (searcher, source) end diff --git a/server-beta/src/searcher/init.lua b/server-beta/src/searcher/init.lua index 5a6bc014..b146ed82 100644 --- a/server-beta/src/searcher/init.lua +++ b/server-beta/src/searcher/init.lua @@ -1,6 +1,9 @@ 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 setmetatable = setmetatable local assert = assert @@ -21,29 +24,56 @@ local specials = { local mt = {} mt.__index = mt mt.__name = 'searcher' -mt._step = 0 -function mt:step() - self._step = self._step + 1 - assert(self.step <= 100, 'Stack overflow!') - if not self._stepClose then - self._stepClose = setmetatable({}, { - __close = function () - self._step = self._step - 1 - end - }) +function mt:lock(tp, source) + if self.locked[tp][source] then + return nil end - return self._stepClose + self.locked[tp][source] = true + return util.defer(function () + self.locked[tp][source] = nil + end) end --- 获取关联的值 ---@param source table ---@return value table function mt:getValue(source) - local _ <close> = self:step() + 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 +function mt:getField(source) + return getField(self, source) +end + +--- 获取所有的定义(不递归) +function mt:eachRef(source, callback) + local lock <close> = self:lock('eachRef', source) + if not lock then + return + end + local cache = self.cache.eachRef[source] + if cache then + for i = 1, #cache do + callback(cache[i]) + end + return + end + cache = {} + self.cache.eachRef[source] = cache + eachRef(self, source, function (info) + cache[#cache+1] = info + callback(info) + end) +end + ---@class engineer local m = {} @@ -56,14 +86,11 @@ function m.create(uri) ast = ast.ast, uri = uri, cache = { - def = {}, - ref = {}, - field = {}, - value = {}, - specialName = {}, + eachRef = {}, }, - lock = { - value = {}, + locked = { + getValue = {}, + eachRef = {}, } }, mt) return searcher |