blob: 3b266916e177d7101fe5b070967eff5f1f5f97e5 (
plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
local util = require 'utility'
local guide = require 'parser.guide'
---@class parser.object
---@field _localID string
---@field _localIDs table<string, parser.object[]>
---@class vm.local-id
local m = {}
m.ID_SPLITE = '\x1F'
local compileMap = util.switch()
: case 'local'
: call(function (source)
source._localID = ('%d'):format(source.start)
if not source.ref then
return
end
for _, ref in ipairs(source.ref) do
m.compileLocalID(ref)
end
end)
: case 'getlocal'
: call(function (source)
source._localID = ('%d'):format(source.node.start)
m.compileLocalID(source.next)
end)
: case 'getfield'
: case 'setfield'
: call(function (source)
local parentID = source.node._localID
if not parentID then
return
end
source._localID = parentID .. m.ID_SPLITE .. guide.getKeyName(source)
source.field._localID = source._localID
if source.type == 'getfield' then
m.compileLocalID(source.next)
end
end)
: case 'getmethod'
: case 'setmethod'
: call(function (source)
local parentID = source.node._localID
if not parentID then
return
end
source._localID = parentID .. m.ID_SPLITE .. guide.getKeyName(source)
source.method._localID = source._localID
if source.type == 'getmethod' then
m.compileLocalID(source.next)
end
end)
: case 'getindex'
: case 'setindex'
: call(function (source)
local parentID = source.node._localID
if not parentID then
return
end
local key = guide.getKeyName(source)
if not type(key) ~= 'string' then
return
end
source._localID = parentID .. m.ID_SPLITE .. key
source.index._localID = source._localID
if source.type == 'setindex' then
m.compileLocalID(source.next)
end
end)
: getMap()
local leftMap = util.switch()
: case 'field'
: case 'method'
: call(function (source)
return m.getLocal(source.parent)
end)
: case 'getfield'
: case 'setfield'
: case 'getmethod'
: case 'setmethod'
: case 'getindex'
: case 'setindex'
: call(function (source)
return m.getLocal(source.node)
end)
: case 'getlocal'
: call(function (source)
return source.node
end)
: case 'local'
: call(function (source)
return source
end)
: getMap()
---@param source parser.object
---@return parser.object?
function m.getLocal(source)
local getLeft = leftMap[source.type]
if getLeft then
return getLeft(source)
end
return nil
end
function m.compileLocalID(source)
if not source then
return
end
source._localID = false
local compiler = compileMap[source.type]
if not compiler then
return
end
compiler(source)
local root = guide.getRoot(source)
if not root._localIDs then
root._localIDs = util.multiTable(2)
end
local sources = root._localIDs[source._localID]
sources[#sources+1] = source
end
---@param source parser.object
---@return string|boolean
function m.getID(source)
if source._localID ~= nil then
return source._localID
end
source._localID = false
local loc = m.getLocal(source)
if not loc then
return source._localID
end
m.compileLocalID(loc)
return source._localID
end
---@param source parser.object
---@param key? string
---@return parser.object[]?
function m.getSources(source, key)
local id = m.getID(source)
if not id then
return nil
end
local root = guide.getRoot(source)
if not root._localIDs then
return nil
end
if key then
if type(key) ~= 'string' then
return nil
end
id = id .. m.ID_SPLITE .. key
end
return root._localIDs[id]
end
return m
|