From 4cfd0304f87e29806646bf2b24cf1e1c83ae69d1 Mon Sep 17 00:00:00 2001 From: Shararvev Date: Sun, 29 Dec 2024 20:17:08 +0500 Subject: [PATCH 1/3] #3014. Parser now considers . and * as Symbol. Improve parseCodePattern to support "`T`.*-", prevent crash with "`T``T`" and when tokens >= 8, fix wrong getStart of result. --- script/parser/luadoc.lua | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index d108cebc2..a51865889 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -54,7 +54,7 @@ EChar <- 'a' -> ea / ([0-9] [0-9]? [0-9]?) -> Char10 / ('u{' {X16*} '}') -> CharUtf8 Symbol <- ({} { - [:|,;<>()?+#{}] + [:|,;<>()?+#{}.*] / '[]' / '...' / '[' @@ -744,30 +744,42 @@ local function parseCodePattern(parent) local codeOffset local finishOffset local content - for i = 2, 8 do + local i = 2 + while true do local next, nextContent = peekToken(i) if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then if codeOffset then - finishOffset = i + finishOffset = i-1 break end ---不连续的name,无效的 return nil end - if next == 'code' then - if codeOffset and content ~= nextContent then + if next == 'name' then + pattern = pattern .. nextContent + elseif next == 'code' then + if codeOffset then -- 暂时不支持多generic return nil end codeOffset = i pattern = pattern .. "%s" content = nextContent - elseif next ~= 'name' then - return nil + elseif codeOffset then + -- should be match with Parser "name" mask + if next == 'integer' then + pattern = pattern .. nextContent + elseif next == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then + pattern = pattern .. nextContent + else + return nil + end else - pattern = pattern .. nextContent + return nil end + i = i + 1 end + nextToken() local start = getStart() for _ = 2, finishOffset do nextToken() From f4a69bcfaa32bf7f51332f97ed84fd0b3b045fa0 Mon Sep 17 00:00:00 2001 From: Shararvev Date: Sun, 29 Dec 2024 21:07:08 +0500 Subject: [PATCH 2/3] #3014. Replace parseCode with parseCodePattern to support "`T`.*-" without name token before code. Added tests. --- script/parser/luadoc.lua | 39 ++++++++++------------- test/definition/luadoc.lua | 64 +++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 28 deletions(-) diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index a51865889..250cf304a 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -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 @@ -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 @@ -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', @@ -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) diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua index 7a460593a..704c9f788 100644 --- a/test/definition/luadoc.lua +++ b/test/definition/luadoc.lua @@ -239,7 +239,7 @@ TEST [[ AAAA = {}; function AAAA:() - + end AAAA.a. @@ -304,6 +304,19 @@ local v1 = Generic(Foo) print(v1.) ]] +TEST [[ +---@class Foo +local Foo = {} +function Foo:() end + +---@generic T +---@param arg1 `T` +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic("Foo") +print(v1.) +]] TEST [[ ---@class n.Foo @@ -320,12 +333,12 @@ print(v1.) ]] TEST [[ ----@class Foo +---@class n.Foo local Foo = {} function Foo:() end ---@generic T ----@param arg1 `T` +---@param arg1 n.`T` ---@return T function Generic(arg1) print(arg1) end @@ -333,14 +346,55 @@ local v1 = Generic("Foo") print(v1.) ]] +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.) +]] TEST [[ ----@class n.Foo +---@class Foo* local Foo = {} function Foo:() end ---@generic T ----@param arg1 n.`T` +---@param arg1 `T`* +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic("Foo") +print(v1.) +]] + +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.) +]] + +TEST [[ +---@class n.Foo* +local Foo = {} +function Foo:() end + +---@generic T +---@param arg1 n.`T`* ---@return T function Generic(arg1) print(arg1) end From e66a8abad4509cef8cdd955c6ca312b535a5568f Mon Sep 17 00:00:00 2001 From: Shararvev Date: Sun, 29 Dec 2024 22:32:20 +0500 Subject: [PATCH 3/3] #3014. Fixed parsing of `...` Symbol but still separated 2 dots `..`, added changelog. --- changelog.md | 9 +++++++++ script/parser/luadoc.lua | 3 ++- test/definition/luadoc.lua | 8 ++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index bdea8e280..2e4e7801b 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,15 @@ ## Unreleased +* `CHG` [#3014] Generic pattern now supports definition after capture + ```lua + ---@generic T + ---@param t `T`.Cat + ---@return T + local function f(t) end + + local t = f('Smile') --> t is `Smile.Cat` + ``` ## 3.13.5 `2024-12-20` diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 250cf304a..3fec5c182 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -54,12 +54,13 @@ EChar <- 'a' -> ea / ([0-9] [0-9]? [0-9]?) -> Char10 / ('u{' {X16*} '}') -> CharUtf8 Symbol <- ({} { - [:|,;<>()?+#{}.*] + [:|,;<>()?+#{}*] / '[]' / '...' / '[' / ']' / '-' !'-' + / '.' !'..' } {}) -> Symbol ]], { diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua index 704c9f788..d6e0d467c 100644 --- a/test/definition/luadoc.lua +++ b/test/definition/luadoc.lua @@ -375,12 +375,12 @@ print(v1.) ]] TEST [[ ----@class n.Foo* +---@class n.Foo.2 local Foo = {} function Foo:bar1() end ---@generic T ----@param arg1 n.`T`* +---@param arg1 n.`T`.2 ---@return T function Generic(arg1) print(arg1) end @@ -389,12 +389,12 @@ print(v1.) ]] TEST [[ ----@class n.Foo* +---@class n.Foo.2 local Foo = {} function Foo:() end ---@generic T ----@param arg1 n.`T`* +---@param arg1 n.`T`.2 ---@return T function Generic(arg1) print(arg1) end