diff options
-rw-r--r-- | server-beta/src/files.lua | 3 | ||||
-rw-r--r-- | server-beta/src/parser/guide.lua | 84 | ||||
-rw-r--r-- | server-beta/src/searcher/boolean.lua | 7 | ||||
-rw-r--r-- | server-beta/src/searcher/field.lua | 4 | ||||
-rw-r--r-- | server-beta/src/searcher/getfield.lua | 8 | ||||
-rw-r--r-- | server-beta/src/searcher/getglobal.lua | 16 | ||||
-rw-r--r-- | server-beta/src/searcher/getlocal.lua | 4 | ||||
-rw-r--r-- | server-beta/src/searcher/index.lua | 10 | ||||
-rw-r--r-- | server-beta/src/searcher/init.lua | 39 | ||||
-rw-r--r-- | server-beta/src/searcher/local.lua | 10 | ||||
-rw-r--r-- | server-beta/src/searcher/method.lua | 4 | ||||
-rw-r--r-- | server-beta/src/searcher/number.lua | 7 | ||||
-rw-r--r-- | server-beta/src/searcher/select.lua | 15 | ||||
-rw-r--r-- | server-beta/src/searcher/string.lua | 7 | ||||
-rw-r--r-- | server-beta/src/searcher/table.lua | 7 | ||||
-rw-r--r-- | server-beta/src/searcher/tablefield.lua | 4 | ||||
-rw-r--r-- | server-beta/test/definition/call.lua | 8 | ||||
-rw-r--r-- | server-beta/test/definition/init.lua | 2 | ||||
-rw-r--r-- | server-beta/test/definition/set.lua | 15 | ||||
-rw-r--r-- | server-beta/test/definition/special.lua | 14 |
20 files changed, 188 insertions, 80 deletions
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua index 4a6c920f..1a1cf347 100644 --- a/server-beta/src/files.lua +++ b/server-beta/src/files.lua @@ -49,6 +49,9 @@ function m.setText(uri, text) return end file.text = text + file.searcher = nil + file.lines = nil + file.ast = nil end --- 监听编译完成 diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index af160abb..a561e49c 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -63,6 +63,21 @@ m.childMap = { ['unary'] = {1} } +m.actionMap = { + ['main'] = {'#'}, + ['repeat'] = {'#'}, + ['while'] = {'#'}, + ['in'] = {'#'}, + ['loop'] = {'#'}, + ['if'] = {'#'}, + ['ifblock'] = {'#'}, + ['elseifblock'] = {'#'}, + ['elseblock'] = {'#'}, + ['do'] = {'#'}, + ['function'] = {'#'}, + ['funcargs'] = {'#'}, +} + --- 是否是字面量 function m.isLiteral(obj) local tp = obj.type @@ -232,6 +247,28 @@ function m.getLabel(block, name) error('guide.getLocal overstack') end +--- 获取指定区块中的 return +function m.getReturns(block) + local list = { block } + local returns = {} + for i = 1, 1000 do + local len = #list + if len == 0 then + return + end + local obj = list[len] + list[len] = nil + if obj.type == 'return' then + returns[#returns+1] = obj + end + if i > 1 and obj.type == 'function' then + else + m.addChilds(list, obj, m.actionMap) + end + end + return returns +end + --- 判断source是否包含offset function m.isContain(source, offset) return source.start <= offset and source.finish >= offset - 1 @@ -244,9 +281,25 @@ function m.isInRange(source, offset) return source.start <= offset and (source.range or source.finish) >= offset - 1 end +--- 添加child +function m.addChilds(list, obj, map) + local keys = map[obj.type] + if keys then + for i = 1, #keys do + local key = keys[i] + if key == '#' then + for i = 1, #obj do + list[#list+1] = obj[i] + end + else + list[#list+1] = obj[key] + end + end + end +end + --- 遍历所有包含offset的source function m.eachSourceContain(ast, offset, callback) - local map = m.childMap local list = { ast } while true do local len = #list @@ -259,26 +312,13 @@ function m.eachSourceContain(ast, offset, callback) if m.isContain(obj, offset) then callback(obj) end - local keys = map[obj.type] - if keys then - for i = 1, #keys do - local key = keys[i] - if key == '#' then - for i = 1, #obj do - list[#list+1] = obj[i] - end - else - list[#list+1] = obj[key] - end - end - end + m.addChilds(list, obj, m.childMap) end end end --- 遍历所有的source function m.eachSource(ast, callback) - local map = m.childMap local list = { ast } while true do local len = #list @@ -288,19 +328,7 @@ function m.eachSource(ast, callback) local obj = list[len] list[len] = nil callback(obj) - local keys = map[obj.type] - if keys then - for i = 1, #keys do - local key = keys[i] - if key == '#' then - for i = 1, #obj do - list[#list+1] = obj[i] - end - else - list[#list+1] = obj[key] - end - end - end + m.addChilds(list, obj, m.childMap) end end diff --git a/server-beta/src/searcher/boolean.lua b/server-beta/src/searcher/boolean.lua index 782b37ff..f92670e1 100644 --- a/server-beta/src/searcher/boolean.lua +++ b/server-beta/src/searcher/boolean.lua @@ -36,11 +36,8 @@ function m:eachRef(source, callback) end) end -function m:eachValue(source, callback) - callback { - uri = self.uri, - source = source, - } +function m:getValue(source) + return source end return m diff --git a/server-beta/src/searcher/field.lua b/server-beta/src/searcher/field.lua index a9b01d25..c5667b04 100644 --- a/server-beta/src/searcher/field.lua +++ b/server-beta/src/searcher/field.lua @@ -31,4 +31,8 @@ function m:eachValue(source, callback) self:eachValue(source.parent, callback) end +function m:getValue(source) + return self:getValue(source.parent) +end + return m diff --git a/server-beta/src/searcher/getfield.lua b/server-beta/src/searcher/getfield.lua index f3c3df16..2fcd2d4b 100644 --- a/server-beta/src/searcher/getfield.lua +++ b/server-beta/src/searcher/getfield.lua @@ -66,9 +66,11 @@ function m:eachField(source, key, callback) checkSMT(self, key, used, found, callback) end -function m:eachValue(source, callback) - if source.value then - self:eachValue(source.value, callback) +function m:getValue(source) + if source.type == 'setfield' + or source.type == 'setmethod' + or source.type == 'setindex' then + return source.value and self:getValue(source.value) end end diff --git a/server-beta/src/searcher/getglobal.lua b/server-beta/src/searcher/getglobal.lua index c4fd10f1..1295d769 100644 --- a/server-beta/src/searcher/getglobal.lua +++ b/server-beta/src/searcher/getglobal.lua @@ -137,8 +137,20 @@ function m:eachValue(source, callback) source = source, uri = self.uri, } - if source.value then - self:eachValue(source.value, callback) + self:eachDef(source, function (info) + local src = info.source + if src.value then + callback { + source = src.value, + uri = info.uri, + } + end + end) +end + +function m:getValue(source) + if source.type == 'setglobal' then + return source.value and self:getValue(source.value) end end diff --git a/server-beta/src/searcher/getlocal.lua b/server-beta/src/searcher/getlocal.lua index ca4e58c3..4ffc871f 100644 --- a/server-beta/src/searcher/getlocal.lua +++ b/server-beta/src/searcher/getlocal.lua @@ -16,4 +16,8 @@ function m:eachValue(source, callback) self:eachValue(source.loc, callback) end +function m:getValue(source) + return self:getValue(source.loc) +end + return m diff --git a/server-beta/src/searcher/index.lua b/server-beta/src/searcher/index.lua index f96c2dcf..f05a57e6 100644 --- a/server-beta/src/searcher/index.lua +++ b/server-beta/src/searcher/index.lua @@ -1,13 +1,3 @@ local m = {} -function m:eachValue(source, callback) - local parent = source.parent - if parent.type == 'setindex' - or parent.type == 'tableindex' then - if parent.value then - self:eachValue(parent.value, callback) - end - end -end - return m diff --git a/server-beta/src/searcher/init.lua b/server-beta/src/searcher/init.lua index a56d9a22..ae831c58 100644 --- a/server-beta/src/searcher/init.lua +++ b/server-beta/src/searcher/init.lua @@ -275,6 +275,29 @@ function mt:eachValue(source, callback) self.step = self.step - 1 end +--- 获取value +---@param source table +---@return value table +function mt:getValue(source) + local tp = source.type + local d = methods[tp] + if not d then + return + end + local f = d.getValue + if not f then + return + end + if self.step >= 100 then + error('Stack overflow!') + return + end + self.step = self.step + 1 + local value = f(self, source) + self.step = self.step - 1 + return value +end + --- 获取函数的参数 ---@param source table ---@return table arg1 @@ -290,7 +313,7 @@ function mt:callArgOf(source) return tableUnpack(args) end ---- 获取函数的返回值 +--- 获取调用函数的返回值 ---@param source table ---@return table return1 ---@return table return2 @@ -311,6 +334,20 @@ function mt:callReturnOf(source) end end +--- 获取函数定义的返回值 +---@param source table +---@param callback fun(source:table) +function mt:functionReturnOf(source, callback) + if not source or source.type ~= 'function' then + return + end + local returns = guide.getReturns(source) + for i = 1, #returns do + local src = returns[i] + callback() + end +end + --- 获取source的索引,模式与值 ---@param source table ---@return table field diff --git a/server-beta/src/searcher/local.lua b/server-beta/src/searcher/local.lua index 398a9387..0213ee58 100644 --- a/server-beta/src/searcher/local.lua +++ b/server-beta/src/searcher/local.lua @@ -27,6 +27,9 @@ function m:eachDef(source, callback) local node = method.node self:eachDef(node, callback) end + if source.value then + self:eachDef(source.value, callback) + end end function m:eachRef(source, callback) @@ -166,4 +169,11 @@ function m:eachValue(source, callback) end end +function m:getValue(source) + if source.type == 'local' + or source.type == 'setlocal' then + return source.value or source + end +end + return m diff --git a/server-beta/src/searcher/method.lua b/server-beta/src/searcher/method.lua index cc7736a3..c05fba76 100644 --- a/server-beta/src/searcher/method.lua +++ b/server-beta/src/searcher/method.lua @@ -30,4 +30,8 @@ function m:eachValue(source, callback) self:eachValue(source.parent, callback) end +function m:getValue(source) + return self:getValue(source.parent) +end + return m diff --git a/server-beta/src/searcher/number.lua b/server-beta/src/searcher/number.lua index 43f838f6..f92670e1 100644 --- a/server-beta/src/searcher/number.lua +++ b/server-beta/src/searcher/number.lua @@ -36,11 +36,8 @@ function m:eachRef(source, callback) end) end -function m:eachValue(source, callback) - callback { - source = source, - uri = self.uri, - } +function m:getValue(source) + return source end return m diff --git a/server-beta/src/searcher/select.lua b/server-beta/src/searcher/select.lua index 77480dc7..f7da7bfc 100644 --- a/server-beta/src/searcher/select.lua +++ b/server-beta/src/searcher/select.lua @@ -2,11 +2,24 @@ local guide = require 'parser.guide' local m = {} +function m:eachDef(source, callback) + local vararg = source.vararg + if vararg.type == 'call' then + local func = vararg.node + self:eachValue(func, function (info) + self:functionReturnOf(info.source, function () + + end) + end) + end +end + function m:eachValue(source, callback) local vararg = source.vararg if vararg.type == 'call' then local func = vararg.node - if self:getSpecialName(func) == 'setmetatable' then + if self:getSpecialName(func) == 'setmetatable' + and source.index == 1 then local t, mt = self:callArgOf(vararg) self:eachValue(t, callback) self:eachField(mt, 's|__index', function (info) diff --git a/server-beta/src/searcher/string.lua b/server-beta/src/searcher/string.lua index 43f838f6..94907116 100644 --- a/server-beta/src/searcher/string.lua +++ b/server-beta/src/searcher/string.lua @@ -36,11 +36,8 @@ function m:eachRef(source, callback) end) end -function m:eachValue(source, callback) - callback { - source = source, - uri = self.uri, - } +function m:getValue(source, callback) + return source end return m diff --git a/server-beta/src/searcher/table.lua b/server-beta/src/searcher/table.lua index 26c499ed..fa9a064a 100644 --- a/server-beta/src/searcher/table.lua +++ b/server-beta/src/searcher/table.lua @@ -23,11 +23,8 @@ function m:eachField(source, key, callback) end end -function m:eachValue(source, callback) - callback { - source = source, - uri = self.uri, - } +function m:getValue(source) + return source end return m diff --git a/server-beta/src/searcher/tablefield.lua b/server-beta/src/searcher/tablefield.lua index 219828d4..09456029 100644 --- a/server-beta/src/searcher/tablefield.lua +++ b/server-beta/src/searcher/tablefield.lua @@ -12,4 +12,8 @@ function m:eachValue(source, callback) end end +function m:getValue(source) + return source.value and self:getValue(source.value) or source +end + return m diff --git a/server-beta/test/definition/call.lua b/server-beta/test/definition/call.lua new file mode 100644 index 00000000..42502f40 --- /dev/null +++ b/server-beta/test/definition/call.lua @@ -0,0 +1,8 @@ +TEST [[ +function f() + local <!x!> + return <!x!> +end +local <!y!> = f() +print(<?y?>) +]] diff --git a/server-beta/test/definition/init.lua b/server-beta/test/definition/init.lua index 4e2d9de5..645765a2 100644 --- a/server-beta/test/definition/init.lua +++ b/server-beta/test/definition/init.lua @@ -61,5 +61,7 @@ require 'definition.function' require 'definition.table' require 'definition.method' require 'definition.label' +require 'definition.call' require 'definition.bug' +require 'definition.special' --require 'definition.emmy' diff --git a/server-beta/test/definition/set.lua b/server-beta/test/definition/set.lua index b1d9dc70..f36be078 100644 --- a/server-beta/test/definition/set.lua +++ b/server-beta/test/definition/set.lua @@ -38,18 +38,3 @@ else end print(<?x?>) ]] - -TEST [[ -_ENV.<!x!> = 1 -print(<?x?>) -]] - -TEST [[ -_G.<!x!> = 1 -print(<?x?>) -]] - -TEST [[ -<!rawset(_G, 'x', 1)!> -print(<?x?>) -]] diff --git a/server-beta/test/definition/special.lua b/server-beta/test/definition/special.lua new file mode 100644 index 00000000..823e3a46 --- /dev/null +++ b/server-beta/test/definition/special.lua @@ -0,0 +1,14 @@ +TEST [[ + _ENV.<!x!> = 1 + print(<?x?>) + ]] + +TEST [[ +_G.<!x!> = 1 +print(<?x?>) +]] + +TEST [[ +<!rawset(_G, 'x', 1)!> +print(<?x?>) +]] |