1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
local vm = require 'vm'
local util = require 'utility'
local getClass = require 'core.hover.class'
local function getKey(info)
if not info.key or #info.key <= 2 then
local source = info.source
if not source.index then
return '[any]'
end
local class = getClass(source.index)
if class then
return ('[%s]'):format(class)
end
local tp = vm.getType(source.index)
if tp then
return ('[%s]'):format(tp)
end
return '[any]'
end
local ktype = info.key:sub(1, 2)
local key = info.key:sub(3)
if ktype == 's|' then
if key:match '^[%a_][%w_]*$' then
return key
else
return ('[%s]'):format(util.viewLiteral(key))
end
end
return ('[%s]'):format(key)
end
local function getField(info)
local tp = vm.getType(info.source)
local class = getClass(info.source)
local literal = vm.getLiteral(info.source)
local key = getKey(info)
if type(literal) == 'string' and #literal >= 50 then
literal = literal:sub(1, 47) .. '...'
end
return key, class or tp, literal
end
local function mergeLiteral(a, b)
if not a then
return b
end
local view = util.viewLiteral(a)
if not view then
return b
end
if not b then
return { view, [view] = true }
end
if b[view] then
return b
end
b[view] = true
b[#b+1] = view
return b
end
local function buildAsHash(classes, literals)
local keys = {}
for k in pairs(classes) do
keys[#keys+1] = k
end
table.sort(keys)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
local class = classes[key]
local literal = literals[key]
if literal then
table.sort(literal)
local view = table.concat(literal, '|')
lines[#lines+1] = (' %s: %s = %s,'):format(key, class, view)
else
lines[#lines+1] = (' %s: %s,'):format(key, class)
end
end
lines[#lines+1] = '}'
return table.concat(lines, '\n')
end
local function buildAsConst(classes, literals)
local keys = {}
for k in pairs(classes) do
keys[#keys+1] = k
end
table.sort(keys, function (a, b)
return tonumber(literals[a][1]) < tonumber(literals[b][1])
end)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
local class = classes[key]
local literal = literals[key]
if literal then
table.sort(literal)
local view = table.concat(literal, '|')
lines[#lines+1] = (' %s: %s = %s,'):format(key, class, view)
else
lines[#lines+1] = (' %s: %s,'):format(key, class)
end
end
lines[#lines+1] = '}'
return table.concat(lines, '\n')
end
return function (source)
local literals = {}
local classes = {}
local intValue = true
vm.eachField(source, function (info)
local key, class, literal = getField(info)
classes[key] = vm.mergeTypeViews(class, classes[key])
literals[key] = mergeLiteral(literal, literals[key])
if class ~= 'integer'
or not literals[key]
or #literals[key] ~= 1 then
intValue = false
end
end)
if classes['[any]'] == 'any' then
classes['[any]'] = nil
end
if not next(classes) then
return '{}'
end
if intValue then
return buildAsConst(classes, literals)
else
return buildAsHash(classes, literals)
end
end
|