diff options
-rw-r--r-- | changelog.md | 7 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 78 | ||||
-rw-r--r-- | test/definition/luadoc.lua | 15 |
3 files changed, 62 insertions, 38 deletions
diff --git a/changelog.md b/changelog.md index 80ddc795..47365e84 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,13 @@ # changelog ## 2.3.2 +* `NEW` `LuaDoc`: supports `['string']` as field: + ```lua + ---@class keyboard + ---@field ['!'] number + ---@field ['?'] number + ---@field ['#'] number + ``` ## 2.3.1 `2021-7-19` diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index aeef1a37..eca24c57 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -12,9 +12,11 @@ Main <- (Token / Sp)* Sp <- %s+ X16 <- [a-fA-F0-9] Word <- [a-zA-Z0-9_] -Token <- Name / String / Symbol / Index -Name <- ({} {[a-zA-Z0-9_] [a-zA-Z0-9_.*-]*} {}) +Token <- Name / String / Symbol / Integer +Name <- ({} {[a-zA-Z_] [a-zA-Z0-9_.*-]*} {}) -> Name +Integer <- ({} {[0-9]+} {}) + -> Integer String <- ({} StringDef {}) -> String StringDef <- '"' @@ -48,10 +50,10 @@ Symbol <- ({} { [:|,<>()?+#`{}] / '[]' / '...' + / '[' + / ']' } {}) -> Symbol -Index <- ({} '[' Sp? {[0-9]+} Sp? ']' {}) - -> Index ]], { s = m.S' \t', ea = '\a', @@ -98,16 +100,16 @@ Index <- ({} '[' Sp? {[0-9]+} Sp? ']' {}) TokenFinishs[Ci] = finish - 1 TokenContents[Ci] = content end, - Symbol = function (start, content, finish) + Integer = function (start, content, finish) Ci = Ci + 1 - TokenTypes[Ci] = 'symbol' + TokenTypes[Ci] = 'integer' TokenStarts[Ci] = start TokenFinishs[Ci] = finish - 1 - TokenContents[Ci] = content + TokenContents[Ci] = math.tointeger(content) end, - Index = function (start, content, finish) + Symbol = function (start, content, finish) Ci = Ci + 1 - TokenTypes[Ci] = 'index' + TokenTypes[Ci] = 'symbol' TokenStarts[Ci] = start TokenFinishs[Ci] = finish - 1 TokenContents[Ci] = content @@ -189,29 +191,45 @@ local function parseName(tp, parent) return class end +local function nextSymbolOrError(symbol) + if checkToken('symbol', symbol, 1) then + nextToken() + return true + end + pushError { + type = 'LUADOC_MISS_SYMBOL', + start = getFinish(), + finish = getFinish(), + info = { + symbol = symbol, + } + } + return false +end + local function parseIndexField(tp, parent) - local indexTp, indexText = peekToken() - if indexTp ~= 'index' then + if not checkToken('symbol', '[', 1) 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, } + local indexTP, index = nextToken() + if indexTP ~= 'integer' + and indexTP ~= 'string' then + pushError { + type = 'LUADOC_INDEX_MUST_INT', + start = getStart(), + finish = getFinish(), + } + end + class[1] = index + nextSymbolOrError ']' + class.finish = getFinish() return class end @@ -267,22 +285,6 @@ local function parseClass(parent) return result end -local function nextSymbolOrError(symbol) - if checkToken('symbol', symbol, 1) then - nextToken() - return true - end - pushError { - type = 'LUADOC_MISS_SYMBOL', - start = getFinish(), - finish = getFinish(), - info = { - symbol = symbol, - } - } - return false -end - local function parseTypeUnitArray(parent, node) if not checkToken('symbol', '[]', 1) then return nil diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua index 5380f4ca..22a51845 100644 --- a/test/definition/luadoc.lua +++ b/test/definition/luadoc.lua @@ -667,3 +667,18 @@ local t print(t[<?1?>]) ]] + +TEST [[ +---@class A +---@field <!['xx']!>? boolean +local t + +print(t.<?xx?>) +]] + +TEST [[ +---@type { <!['xx']?: boolean!> } +local t + +print(t.<?xx?>) +]] |