local core = require 'core.reference' local files = require 'files' local function catch_target(script) local list = {} local cur = 1 while true do local start, finish = script:find('<[!?].-[!?]>', cur) if not start then break end list[#list+1] = { start + 2, finish - 2 } cur = finish + 1 end return list end local function founded(targets, results) if #targets ~= #results then return false end for _, target in ipairs(targets) do for _, result in ipairs(results) do if target[1] == result[1] and target[2] == result[2] then goto NEXT end end do return false end ::NEXT:: end return true end function TEST(script) files.removeAll() local expect = catch_target(script) local start = script:find('<[?~]') local finish = script:find('[?~]>') local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?~]', ' '):gsub('[!?~]>', ' ') files.setText('', new_script) local results = core('', pos) if results then local positions = {} for i, result in ipairs(results) do positions[i] = { result.target.start, result.target.finish } end assert(founded(expect, positions)) else assert(#expect == 0) end end TEST [[ local = 1 = ]] TEST [[ = 1 = ]] TEST [[ local t t. = 1 t. = t. ]] TEST [[ t. = 1 t. = t. ]] TEST [[ :: :: goto if true then goto end ]] TEST [[ :: :: goto if true then goto end ]] TEST [[ local a = 1 local = 1 = ]] TEST [[ local local = ]] TEST [[ local local = ]] TEST [[ local t = { = 1 } print(t.) ]] TEST [[ local t = { = 1 } print(t.) ]] TEST [[ t[] = 1 print(t.) ]] TEST [[ local t = { [] = 1 } print(t.) ]] TEST [[ table.() function table.() end ]] TEST [[ local function f() return <~ () end!> end local = f() ]] TEST [[ local function f() return nil, <~ () end!> end local _, = f() ]] TEST [[ local local function f() return end local y = f() ]] TEST [[ local local function f() return function () return end end local y = f()() ]] TEST [[ local t = {} t. = 1 t[] = 1 ]] TEST [[ local t = {} t.x = 1 t[a.b.] = 1 ]] TEST [[ local t local = t. () return { = , } ]] TEST [[ self = { results = { = {}, } } self[self.results.] = lbl ]] TEST [[ a.b. = 1 print(a.b.) ]] TEST [[ local = {} function :x() :x() end ]] TEST [[ local = {} function :x() :x() end ]] TEST [[ local mt = {} function mt:() self:() end ]] TEST [[ local mt = {} function mt:() self:() end ]] TEST [[ a..c = 1 print(a..c) ]] TEST [[ local local t = { = } print(t.) ]] TEST [[ local local = ]] TEST [[ local a. = ]] TEST [[ . = ]] TEST [[ local local = ]] TEST [[ local . = ]] TEST [[ _G. = 1 print() ]] TEST [[ ---@class ---@type ---@type ]] TEST [[ ---@class ---@type ---@type ]] TEST [[ ---@class Class local ---@type Class local ]] TEST [[ ---@class Class local ---@type Class local ]] -- BUG TEST [[ ---@return function f() end ]] TEST [[ ---@class Dog local mt = {} function mt:() end ---@class Master local mt2 = {} function mt2:init() ---@type Dog local foo = self:doSomething() ---@type Dog self.dog = getDog() end function mt2:feed() self.dog:() end function mt2:doSomething() end ]] TEST [[ ---@class A local a = {} a. = 1 ---@return A local function f() end local b = f() return b. ]] TEST [[ ---@class A local a = {} a. = 1 ---@return table ---@return A local function f() end local a, b = f() return a.x, b. ]] -- TODO 支持泛型 do return end TEST [[ ---@class Dog local = {} ---@generic T ---@param type1 T ---@return T function foobar(type1) end local = foobar() ]] TEST [[ ---@class Dog local Dog = {} function Dog:() end ---@generic T ---@param type1 T ---@return T function foobar(type1) return {} end local v1 = foobar(Dog) v1:() ]] TEST [[ ---@class Dog local Dog = {} function Dog:() end ---@class Master local Master = {} ---@generic T ---@param type1 string ---@param type2 T ---@return T function Master:foobar(type1, type2) return {} end local v1 = Master:foobar("", Dog) v1.() ]] TEST [[ ---@class A local ---@generic T ---@param self T ---@return T function m.f(self) end local = m.f() ]] TEST [[ ---@class A local ---@generic T ---@param self T ---@return T function m:f() end local = m.f() ]] TEST [[ ---@class A local ---@generic T ---@param self T ---@return T function .f(self) end local = :f() ]] TEST [[ ---@class A local ---@generic T ---@param self T ---@return T function :f() end local = :f() ]]