Skip to content

Commit 174a333

Browse files
committed
#3014, #3015 Generic pattern supports optional, union, array and comment without a space. Fixed regression.
1 parent cb964c6 commit 174a333

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

changelog.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5-
* `CHG` [#3014] Generic pattern now supports definition after capture
5+
* `CHG` [#3014] Generic pattern now supports definition after capture and optional, union, array
66
```lua
77
---@generic T
8-
---@param t `T`.Cat
9-
---@return T
8+
---@param t `T`.Cat?
9+
---@return T?
1010
local function f(t) end
1111

12-
local t = f('Smile') --> t is `Smile.Cat`
12+
local t = f('Smile') --> t is `(Smile.Cat)?`
1313
```
1414
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
1515
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.

script/parser/luadoc.lua

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Symbol <- ({} {
7272
er = '\r',
7373
et = '\t',
7474
ev = '\v',
75-
name = (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_')) * (m.R('az', 'AZ', '__', '09', '\x80\xff') + m.S('_.*-'))^0,
75+
name = (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_')) * (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_.*-'))^0,
7676
Char10 = function (char)
7777
---@type integer?
7878
char = tonumber(char)
@@ -727,7 +727,6 @@ local function parseCodePattern(parent)
727727
return nil
728728
end
729729
local codeOffset
730-
local finishOffset
731730
local content
732731
local i = 1
733732
if tp == 'code' then
@@ -736,41 +735,41 @@ local function parseCodePattern(parent)
736735
pattern = '%s'
737736
end
738737
while true do
739-
i = i + 1
740-
local next, nextContent = peekToken(i)
741-
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
742-
if codeOffset then
743-
finishOffset = i-1
744-
break
745-
end
738+
i = i+1
739+
local nextTp, nextContent = peekToken(i)
740+
if not nextTp or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
746741
---不连续的name,无效的
747-
return nil
742+
break
748743
end
749-
if next == 'name' then
744+
if nextTp == 'name' then
750745
pattern = pattern .. nextContent
751-
elseif next == 'code' then
746+
elseif nextTp == 'code' then
752747
if codeOffset then
753748
-- 暂时不支持多generic
754-
return nil
749+
break
755750
end
756751
codeOffset = i
757752
pattern = pattern .. '%s'
758753
content = nextContent
759754
elseif codeOffset then
760755
-- should be match with Parser "name" mask
761-
if next == 'integer' then
756+
if nextTp == 'integer' then
762757
pattern = pattern .. nextContent
763-
elseif next == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then
758+
elseif nextTp == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then
764759
pattern = pattern .. nextContent
765760
else
766-
return nil
761+
break
767762
end
768763
else
769-
return nil
764+
break
770765
end
771766
end
767+
if not codeOffset then
768+
return nil
769+
end
772770
nextToken()
773771
local start = getStart()
772+
local finishOffset = i-1
774773
if finishOffset == 1 then
775774
-- code only, no pattern
776775
pattern = nil
@@ -932,7 +931,7 @@ function parseType(parent)
932931
local function pushResume()
933932
local comments
934933
for i = 0, 100 do
935-
local nextComm = NextComment(i,'peek')
934+
local nextComm = NextComment(i, true)
936935
if not nextComm then
937936
return false
938937
end

test/definition/luadoc.lua

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ TEST [[
239239
AAAA = {};
240240
241241
function AAAA:<!SSDF!>()
242-
242+
243243
end
244244
245245
AAAA.a.<?SSDF?>
@@ -352,7 +352,7 @@ local Foo = {}
352352
function Foo:bar1() end
353353
354354
---@generic T
355-
---@param arg1 `T`*
355+
---@param arg1 `T`*?
356356
---@return T
357357
function Generic(arg1) print(arg1) end
358358
@@ -366,7 +366,7 @@ local Foo = {}
366366
function Foo:<!bar1!>() end
367367
368368
---@generic T
369-
---@param arg1 `T`*
369+
---@param arg1 `T`*?
370370
---@return T
371371
function Generic(arg1) print(arg1) end
372372
@@ -402,6 +402,34 @@ local v1 = Generic("Foo")
402402
print(v1.<?bar1?>)
403403
]]
404404

405+
TEST [[
406+
---@class n-Foo-2
407+
local Foo = {}
408+
function Foo:bar1() end
409+
410+
---@generic T
411+
---@param arg1 n-`T`-2[]
412+
---@return T
413+
function Generic(arg1) print(arg1) end
414+
415+
local v1 = Generic({Foo})
416+
print(v1.<?bar1?>)
417+
]]
418+
419+
TEST [[
420+
---@class n-Foo-2
421+
local Foo = {}
422+
function Foo:<!bar1!>() end
423+
424+
---@generic T
425+
---@param arg1 n-`T`-2[]
426+
---@return T
427+
function Generic(arg1) print(arg1) end
428+
429+
local v1 = Generic({"Foo"})
430+
print(v1.<?bar1?>)
431+
]]
432+
405433
TEST [[
406434
---@class A
407435
local t

0 commit comments

Comments
 (0)