diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-05-19 16:07:05 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-05-19 16:07:12 +0800 |
commit | 98f234842eca12334c5668d7a3511a068ed528d2 (patch) | |
tree | 0bcce2e75842cf16263f96886494915f29eabba7 /script/parser | |
parent | da6cee5ab4a06cbdd4d4d3b23ac7a43726459033 (diff) | |
download | lua-language-server-98f234842eca12334c5668d7a3511a068ed528d2.zip |
update
Diffstat (limited to 'script/parser')
-rw-r--r-- | script/parser/guide.lua | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index f87bbbaa..f5eb39f3 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -97,6 +97,32 @@ m.actionMap = { ['funcargs'] = {'#'}, } +local inf = 1 / 0 +local nan = 0 / 0 + +local function isInteger(n) + if math.type then + return math.type(n) == 'integer' + else + return type(n) == 'number' and n % 1 == 0 + end +end + +local function formatNumber(n) + if n == inf + or n == -inf + or n == nan + or n ~= n then -- IEEE 标准中,NAN 不等于自己。但是某些实现中没有遵守这个规则 + return ('%q'):format(n) + end + if isInteger(n) then + return tostring(n) + end + local str = ('%.10f'):format(n) + str = str:gsub('%.?0*$', '') + return str +end + --- 是否是字面量 ---@param obj parser.guide.object ---@return boolean @@ -739,7 +765,7 @@ function m.getKeyNameOfLiteral(obj) elseif tp == 'number' then local n = obj[1] if n then - return ('%s'):format(util.viewLiteral(obj[1])) + return ('%s'):format(formatNumber(obj[1])) end elseif tp == 'boolean' then local b = obj[1] |