summaryrefslogtreecommitdiff
path: root/server/src/utility/table.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-20 18:40:33 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-20 18:40:33 +0800
commit67f6a20b27c98788843883c3712c648e110d781f (patch)
tree209705e0bc3c2c44d5ffade78cc2c7041eeffdda /server/src/utility/table.lua
parenta65a1ddb13854c2dfde4c04630a0a942abdafa32 (diff)
downloadlua-language-server-67f6a20b27c98788843883c3712c648e110d781f.zip
转移目录
Diffstat (limited to 'server/src/utility/table.lua')
-rw-r--r--server/src/utility/table.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/server/src/utility/table.lua b/server/src/utility/table.lua
new file mode 100644
index 00000000..37a74632
--- /dev/null
+++ b/server/src/utility/table.lua
@@ -0,0 +1,62 @@
+local table_sort = table.sort
+local string_rep = string.rep
+local type = type
+local pairs = pairs
+local ipairs = ipairs
+local math_type = math.type
+
+local TAB = setmetatable({}, { __index = function (self, n)
+ self[n] = string_rep('\t', n)
+ return self[n]
+end})
+
+local KEY = {}
+
+function table.dump(tbl)
+ if type(tbl) ~= 'table' then
+ error('必须是表')
+ end
+ local table_mark = {}
+ local lines = {}
+ lines[#lines+1] = '{'
+ local function unpack(tbl, tab)
+ if table_mark[tbl] then
+ error('不能循环引用')
+ end
+ table_mark[tbl] = true
+ local keys = {}
+ for key in pairs(tbl) do
+ if type(key) == 'string' then
+ if key:find('[^%w_]') then
+ KEY[key] = ('[%q]'):format(key)
+ else
+ KEY[key] = key
+ end
+ elseif math_type(key) == 'integer' then
+ KEY[key] = ('[%d]'):format(key)
+ else
+ error('必须使用字符串或整数作为键')
+ end
+ keys[#keys+1] = key
+ end
+ table_sort(keys, function (a, b)
+ return KEY[a] < KEY[b]
+ end)
+ for _, key in ipairs(keys) do
+ local value = tbl[key]
+ local tp = type(value)
+ if tp == 'table' then
+ lines[#lines+1] = ('%s%s = {'):format(TAB[tab+1], KEY[key])
+ unpack(value, tab+1)
+ lines[#lines+1] = ('%s},'):format(TAB[tab+1])
+ elseif tp == 'string' or tp == 'number' or tp == 'boolean' then
+ lines[#lines+1] = ('%s%s = %q,'):format(TAB[tab+1], KEY[key], value)
+ else
+ error(('不支持的值类型[%s]'):format(tp))
+ end
+ end
+ end
+ unpack(tbl, 0)
+ lines[#lines+1] = '}'
+ return table.concat(lines, '\n')
+end