summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-07-15 17:56:23 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-07-15 17:56:23 +0800
commitd80a3db90e27f1c4598bb2bf316891857fe71bb9 (patch)
tree5ede35d02896ab41d6353095d032cd8c66006129
parentbe19b8b2e6246bd53e08df3fabaa8442b3422dc6 (diff)
downloadlua-language-server-d80a3db90e27f1c4598bb2bf316891857fe71bb9.zip
#589 first supports integer field
-rw-r--r--script/parser/luadoc.lua39
-rw-r--r--test/definition/luadoc.lua15
2 files changed, 53 insertions, 1 deletions
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 3a3a0984..10a0b4d7 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -12,7 +12,7 @@ Main <- (Token / Sp)*
Sp <- %s+
X16 <- [a-fA-F0-9]
Word <- [a-zA-Z0-9_]
-Token <- Name / String / Symbol
+Token <- Name / String / Symbol / Index
Name <- ({} {[a-zA-Z0-9_] [a-zA-Z0-9_.*-]*} {})
-> Name
String <- ({} StringDef {})
@@ -50,6 +50,8 @@ Symbol <- ({} {
/ '...'
} {})
-> Symbol
+Index <- ({} '[' Sp? {[0-9]+} Sp? ']' {})
+ -> Index
]], {
s = m.S' \t',
ea = '\a',
@@ -103,6 +105,13 @@ Symbol <- ({} {
TokenFinishs[Ci] = finish - 1
TokenContents[Ci] = content
end,
+ Index = function (start, content, finish)
+ Ci = Ci + 1
+ TokenTypes[Ci] = 'index'
+ TokenStarts[Ci] = start
+ TokenFinishs[Ci] = finish - 1
+ TokenContents[Ci] = content
+ end,
})
local function trim(str)
@@ -180,6 +189,32 @@ local function parseName(tp, parent)
return class
end
+local function parseIndexField(tp, parent)
+ local indexTp, indexText = peekToken()
+ if indexTp ~= 'index' then
+ return nil
+ end
+ nextToken()
+ local int = math.tointeger(indexText)
+ if not int then
+ int = 1
+ -- TODO: translate
+ pushError {
+ type = 'LUADOC_INDEX_MUST_INT',
+ start = getStart(),
+ finish = getFinish(),
+ }
+ end
+ local class = {
+ type = tp,
+ start = getStart(),
+ finish = getFinish(),
+ parent = parent,
+ [1] = int,
+ }
+ return class
+end
+
local function parseClass(parent)
local result = {
type = 'doc.class',
@@ -407,6 +442,7 @@ local function parseTypeUnitLiteralTable()
do
field.name = parseName('doc.field.name', field)
+ or parseIndexField('doc.field.name', field)
if not field.name then
pushError {
type = 'LUADOC_MISS_FIELD_NAME',
@@ -777,6 +813,7 @@ local function parseField()
return false
end)
result.field = parseName('doc.field.name', result)
+ or parseIndexField('doc.field.name', result)
if not result.field then
pushError {
type = 'LUADOC_MISS_FIELD_NAME',
diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua
index 34d24cf2..5380f4ca 100644
--- a/test/definition/luadoc.lua
+++ b/test/definition/luadoc.lua
@@ -652,3 +652,18 @@ local t
print(t.<?x?>)
]]
+
+TEST [[
+---@class A
+---@field <![1]!>? boolean
+local t
+
+print(t[<?1?>])
+]]
+
+TEST [[
+---@type { <![1]?: boolean!> }
+local t
+
+print(t[<?1?>])
+]]