From bc92c4dcefc0c87420c7d4b239c1eb4b9d14588c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 9 Jun 2022 19:33:28 +0800 Subject: add `doc.type.code` --- script/core/semantic-tokens.lua | 9 +++++++++ script/parser/luadoc.lua | 45 ++++++++++++++++++++++++++++++++--------- script/vm/compiler.lua | 1 + script/vm/infer.lua | 4 ++++ 4 files changed, 49 insertions(+), 10 deletions(-) (limited to 'script') diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index b16f55fd..c892aaac 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -675,6 +675,15 @@ local Care = util.switch() type = define.TokenTypes.variable, } end) + : case 'doc.type.code' + : call(function (source, options, results) + results[#results+1] = { + start = source.start, + finish = source.finish, + type = define.TokenTypes.string, + modifieres = define.TokenModifiers.abstract, + } + end) local function buildTokens(uri, results) local tokens = {} diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index d8e31950..99ef8781 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -12,11 +12,13 @@ local Parser = re.compile([[ Main <- (Token / Sp)* Sp <- %s+ X16 <- [a-fA-F0-9] -Token <- Integer / Name / String / Symbol +Token <- Integer / Name / String / Code / Symbol Name <- ({} {%name} {}) -> Name Integer <- ({} {[0-9]+} !'.' {}) -> Integer +Code <- ({} '`' { (!'`' .)*} '`' {}) + -> Code String <- ({} StringDef {}) -> String StringDef <- {'"'} @@ -48,7 +50,7 @@ EChar <- 'a' -> ea / ([0-9] [0-9]? [0-9]?) -> Char10 / ('u{' {X16*} '}') -> CharUtf8 Symbol <- ({} { - [:|,<>()?+#`{}] + [:|,<>()?+#{}] / '[]' / '...' / '[' @@ -114,6 +116,13 @@ Symbol <- ({} { TokenFinishs[Ci] = finish - 1 TokenContents[Ci] = math.tointeger(content) end, + Code = function (start, content, finish) + Ci = Ci + 1 + TokenTypes[Ci] = 'code' + TokenStarts[Ci] = start + TokenFinishs[Ci] = finish - 1 + TokenContents[Ci] = content + end, Symbol = function (start, content, finish) Ci = Ci + 1 TokenTypes[Ci] = 'symbol' @@ -534,6 +543,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 parseInteger(parent) local tp, content = peekToken() if not tp or tp ~= 'integer' then @@ -584,23 +609,16 @@ function parseTypeUnit(parent) local result = parseFunction(parent) or parseTable(parent) or parseString(parent) + or parseCode(parent) or parseInteger(parent) or parseBoolean(parent) or parseDots('doc.type.name', parent) or parseParen(parent) if not result then - local literal = checkToken('symbol', '`', 1) - if literal then - nextToken() - end result = parseName('doc.type.name', parent) if not result then return nil end - if literal then - result.literal = true - nextSymbolOrError '`' - end end while true do local newResult = parseTypeUnitSign(parent, result) @@ -1418,6 +1436,13 @@ local function bindGeneric(binded) src.type = 'doc.generic.name' end end) + guide.eachSourceType(doc, 'doc.type.code', function (src) + local name = src[1] + if generics[name] then + src.type = 'doc.generic.name' + src.literal = true + end + end) end end end diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 1c3be7c8..9b871553 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1290,6 +1290,7 @@ local compilerSwitch = util.switch() : case 'doc.type.integer' : case 'doc.type.string' : case 'doc.type.boolean' + : case 'doc.type.code' : call(function (source) vm.setNode(source, source) end) diff --git a/script/vm/infer.lua b/script/vm/infer.lua index ef8c6f29..9db69b4d 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -135,6 +135,10 @@ local viewNodeSwitch = util.switch() : call(function (source, infer) return ('%q'):format(source[1]) end) + : case 'doc.type.code' + : call(function (source, infer) + return ('`%s`'):format(source[1]) + end) : case 'doc.type.function' : call(function (source, infer, uri) infer._hasDocFunction = true -- cgit v1.2.3