Skip to content

Commit

Permalink
#3014. Replace parseCode with parseCodePattern to support "T.*-" wi…
Browse files Browse the repository at this point in the history
…thout name token before code. Added tests.
  • Loading branch information
TIMONz1535 committed Dec 29, 2024
1 parent 4cfd030 commit f4a69bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
39 changes: 16 additions & 23 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -720,32 +720,22 @@ local function parseString(parent)
return str
end

local function parseCode(parent)
local tp, content = peekToken()
if not tp or tp ~= 'code' then
return nil
end
nextToken()
local code = {
type = 'doc.type.code',
start = getStart(),
finish = getFinish(),
parent = parent,
[1] = content,
}
return code
end

local function parseCodePattern(parent)
local tp, pattern = peekToken()
if not tp or tp ~= 'name' then
if not tp or (tp ~= 'name' and tp ~= 'code') then
return nil
end
local codeOffset
local finishOffset
local content
local i = 2
local i = 1
if tp == 'code' then
codeOffset = i
content = pattern
pattern = '%s'
end
while true do
i = i + 1
local next, nextContent = peekToken(i)
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
if codeOffset then
Expand All @@ -763,7 +753,7 @@ local function parseCodePattern(parent)
return nil
end
codeOffset = i
pattern = pattern .. "%s"
pattern = pattern .. '%s'
content = nextContent
elseif codeOffset then
-- should be match with Parser "name" mask
Expand All @@ -777,12 +767,16 @@ local function parseCodePattern(parent)
else
return nil
end
i = i + 1
end
nextToken()
local start = getStart()
for _ = 2, finishOffset do
nextToken()
if finishOffset == 1 then
-- code only, no pattern
pattern = nil
else
for _ = 2, finishOffset do
nextToken()
end
end
local code = {
type = 'doc.type.code',
Expand Down Expand Up @@ -846,7 +840,6 @@ function parseTypeUnit(parent)
or parseTable(parent)
or parseTuple(parent)
or parseString(parent)
or parseCode(parent)
or parseInteger(parent)
or parseBoolean(parent)
or parseParen(parent)
Expand Down
64 changes: 59 additions & 5 deletions test/definition/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ TEST [[
AAAA = {};
function AAAA:<!SSDF!>()
end
AAAA.a.<?SSDF?>
Expand Down Expand Up @@ -304,6 +304,19 @@ local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]

TEST [[
---@class Foo
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 `T`
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo
Expand All @@ -320,27 +333,68 @@ print(v1.<?bar1?>)
]]

TEST [[
---@class Foo
---@class n.Foo
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 `T`
---@param arg1 n.`T`
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

TEST [[
---@class Foo*
local Foo = {}
function Foo:bar1() end
---@generic T
---@param arg1 `T`*
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo
---@class Foo*
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 n.`T`
---@param arg1 `T`*
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo*
local Foo = {}
function Foo:bar1() end
---@generic T
---@param arg1 n.`T`*
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo*
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 n.`T`*
---@return T
function Generic(arg1) print(arg1) end
Expand Down

0 comments on commit f4a69bc

Please sign in to comment.