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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
local guide = require 'parser.guide'
local config = require 'config'
local type = type
local setmetatable = setmetatable
local ipairs = ipairs
_ENV = nil
---@class engineer
local mt = {}
mt.__index = mt
mt.type = 'engineer'
mt['local'] = function (self, source, mode, callback)
if mode == 'def' then
if source.tag ~= 'self' then
callback(source, 'local')
end
if source.ref then
for _, ref in ipairs(source.ref) do
if ref.type == 'setlocal' then
callback(ref, 'set')
end
end
end
if source.tag == 'self' then
local method = source.method
local node = method.node
self:search(node, node.type, mode, callback)
end
elseif mode == 'ref' then
if source.tag ~= 'self' then
callback(source, 'local')
end
if source.ref then
for _, ref in ipairs(source.ref) do
if ref.type == 'setlocal' then
callback(ref, 'set')
elseif ref.type == 'getlocal' then
callback(ref, 'get')
end
end
end
if source.tag == 'self' then
local method = source.method
local node = method.node
self:search(node, node.type, mode, callback)
end
elseif mode == 'field' then
if source.ref then
for _, ref in ipairs(source.ref) do
if ref.type == 'getlocal' then
local parent = ref.parent
local tp = parent.type
if tp == 'setfield'
or tp == 'getfield'
or tp == 'setindex'
or tp == 'getindex' then
callback(parent)
end
end
end
end
end
end
mt['getlocal'] = function (self, source, mode, callback)
self:search(source.loc, 'local', mode, callback)
end
mt['setlocal'] = mt['getlocal']
mt['_G'] = function (self, source, mode, callback)
if mode == 'def' then
local parent = source.parent
if parent.type == 'setfield'
or parent.type == 'setindex' then
callback(parent, 'set')
elseif parent.type == 'getfield'
or parent.type == 'getindex' then
self:search(parent, 'special', mode, callback)
elseif parent.type == 'callargs' then
self:search(parent.parent, 'special', mode, callback)
end
elseif mode == 'ref' then
local parent = source.parent
if parent.type == 'setfield'
or parent.type == 'setindex' then
callback(parent, 'set')
elseif parent.type == 'getfield'
or parent.type == 'getindex' then
callback(parent, 'get')
elseif parent.type == 'getfield' then
self:search(parent, 'special', mode, callback)
elseif parent.type == 'callargs' then
self:search(parent.parent, 'special', mode, callback)
end
end
end
mt['getglobal'] = function (self, source, mode, callback)
local env = source.node
if mode == 'def' then
if env.ref then
for _, ref in ipairs(env.ref) do
if ref.type == 'setglobal' then
callback(ref, 'set')
elseif ref.type == 'getglobal' then
self:search(ref, 'special', mode, callback)
elseif ref.type == 'getlocal' then
self:search(ref, '_G', mode, callback)
end
end
end
elseif mode == 'ref' then
if env.ref then
for _, ref in ipairs(env.ref) do
if ref.type == 'setglobal' then
callback(ref, 'set')
elseif ref.type == 'getglobal' then
callback(ref, 'get')
self:search(ref, 'special', mode, callback)
elseif ref.type == 'getlocal' then
self:search(ref, '_G', mode, callback)
end
end
end
elseif mode == 'field' then
self:search(source, 'getglobal', 'ref', function (src)
local parent = src.parent
local tp = parent.type
if tp == 'setfield'
or tp == 'getfield'
or tp == 'setindex'
or tp == 'getindex' then
callback(parent)
end
end)
end
end
mt['setglobal'] = mt['getglobal']
mt['field'] = function (self, source, mode, callback)
local node = source.parent.node
local key = guide.getKeyName(source)
self:eachRef(node, 'field', function (src)
if key == guide.getKeyName(src) then
if mode == 'def' then
if src.type == 'setfield' then
callback(src.field, 'set')
end
end
end
end)
end
mt['special'] = function (self, source, mode, callback)
local name = self:getSpecial(source)
if not name then
return
end
if mode == 'def' then
if name == 's|_G' then
self:search(source, '_G', mode, callback)
elseif name == 's|rawset' then
callback(source.parent, 'set')
end
end
end
mt['asindex'] = function (self, source, mode, callback)
local parent = source.parent
if not parent then
return
end
if parent.type ~= 'setindex' and parent.type ~= 'getindex' then
return
end
local node = parent.node
local key = guide.getKeyName(source)
self:eachRef(node, 'field', function (src)
if key == guide.getKeyName(src) then
if mode == 'def' then
if src.type == 'setfield' then
callback(src.field, 'set')
elseif src.type == 'setindex' then
callback(src.index, 'set')
end
end
end
end)
end
mt['number'] = mt['asindex']
mt['boolean'] = mt['asindex']
mt['string'] = function (self, source, mode, callback)
mt['asindex'](self, source, mode, callback)
end
function mt:getSpecial(source)
local node = source.node
if node.tag ~= '_ENV' then
return nil
end
local name = guide.getKeyName(source)
return name
end
function mt:search(source, method, mode, callback)
local f = mt[method]
if not f then
return
end
f(self, source, mode, callback)
end
function mt:eachRef(source, mode, callback)
local tp = source.type
self:search(source, tp, mode, callback)
end
return function (ast)
local self = setmetatable({
step = 0,
ast = ast.ast,
}, mt)
if not ast.vm then
ast.vm = {}
end
return self
end
|