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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
local guide = require 'parser.guide'
local files = require 'files'
local methods = require 'searcher.methods'
local tableUnpack = table.unpack
local error = error
local setmetatable = setmetatable
local assert = assert
_ENV = nil
local specials = {
['_G'] = true,
['rawset'] = true,
['rawget'] = true,
['setmetatable'] = true,
['require'] = true,
['dofile'] = true,
['loadfile'] = true,
}
---@class searcher_old
local mt = {}
mt.__index = mt
mt.__name = 'searcher'
--- 获取特殊对象的名字
function mt:getSpecialName(source)
local spName = self.cache.specialName[source]
if spName ~= nil then
if spName then
return spName
end
return nil
end
local function getName(info)
local src = info.source
if src.type == 'getglobal' then
local node = src.node
if node.tag ~= '_ENV' then
return nil
end
local name = guide.getKeyName(src)
if name:sub(1, 2) ~= 's|' then
return nil
end
spName = name:sub(3)
if not specials[spName] then
spName = nil
end
elseif src.type == 'local' then
if src.tag == '_ENV' then
spName = '_G'
end
elseif src.type == 'getlocal' then
local loc = src.loc
if loc.tag == '_ENV' then
spName = '_G'
end
end
end
self:eachValue(source, getName)
self.cache.specialName[source] = spName or false
return spName
end
--- 遍历特殊对象
---@param callback fun(name:string, source:table)
function mt:eachSpecial(callback)
local cache = self.cache.special
if cache then
for i = 1, #cache do
callback(cache[i][1], cache[i][2])
end
return
end
cache = {}
self.cache.special = cache
guide.eachSource(self.ast, function (source)
if source.type == 'getlocal'
or source.type == 'getglobal'
or source.type == 'local'
or source.type == 'field'
or source.type == 'string' then
local name = self:getSpecialName(source)
if name then
cache[#cache+1] = { name, source }
end
end
end)
for i = 1, #cache do
callback(cache[i][1], cache[i][2])
end
end
--- 遍历元素
---@param source table
---@param key string
---@param callback fun(info:table)
function mt:eachField(source, key, callback)
local cache = self.cache.field[source]
if cache and cache[key] then
for i = 1, #cache[key] do
callback(cache[key][i])
end
return
end
local tp = source.type
local d = methods[tp]
if not d then
return
end
local f = d.eachField
if not f then
return
end
if self.step >= 100 then
error('Stack overflow!')
return
end
self.step = self.step + 1
local mark = {}
if not cache then
cache = {}
self.cache.field[source] = cache
end
cache[key] = {}
f(self, source, key, function (info)
local src = info.source
local uri = info.uri
assert(src and uri)
if mark[src] then
return
end
mark[src] = true
info.searcher = files.getSearcher(uri)
cache[key][#cache[key]+1] = info
end)
for i = 1, #cache[key] do
callback(cache[key][i])
end
self.step = self.step - 1
end
--- 遍历引用
---@param source table
---@param callback fun(info:table)
function mt:eachRef(source, callback)
local cache = self.cache.ref[source]
if cache then
for i = 1, #cache do
callback(cache[i])
end
return
end
local tp = source.type
local d = methods[tp]
if not d then
return
end
local f = d.eachRef
if not f then
return
end
if self.step >= 100 then
error('Stack overflow!')
return
end
self.step = self.step + 1
cache = {}
self.cache.ref[source] = cache
local mark = {}
f(self, source, function (info)
local src = info.source
local uri = info.uri
assert(src and uri)
if mark[src] then
return
end
mark[src] = true
info.searcher = files.getSearcher(uri)
cache[#cache+1] = info
end)
for i = 1, #cache do
callback(cache[i])
end
self.step = self.step - 1
end
--- 遍历定义
---@param source table
---@param callback fun(info:table)
function mt:eachDef(source, callback)
local cache = self.cache.def[source]
if cache then
for i = 1, #cache do
callback(cache[i])
end
return
end
local tp = source.type
local d = methods[tp]
if not d then
return
end
local f = d.eachDef
if not f then
return
end
if self.step >= 100 then
error('Stack overflow!')
return
end
self.step = self.step + 1
cache = {}
self.cache.def[source] = cache
local mark = {}
f(self, source, function (info)
local src = info.source
local uri = info.uri
assert(src and uri)
if mark[src] then
return
end
mark[src] = true
info.searcher = files.getSearcher(uri)
cache[#cache+1] = info
end)
for i = 1, #cache do
callback(cache[i])
end
self.step = self.step - 1
end
--- 遍历value
---@param source table
---@param callback fun(info:table)
function mt:eachValue(source, callback)
local cache = self.cache.value[source]
if cache then
for i = 1, #cache do
callback(cache[i])
end
return
end
local tp = source.type
local d = methods[tp]
if not d then
return
end
local f = d.eachValue
if not f then
return
end
if self.step >= 100 then
error('Stack overflow!')
return
end
self.step = self.step + 1
cache = {}
self.cache.value[source] = cache
local mark = {}
f(self, source, function (info)
local src = info.source
local uri = info.uri
assert(src and uri)
if mark[src] then
return
end
mark[src] = true
info.searcher = files.getSearcher(uri)
cache[#cache+1] = info
end)
for i = 1, #cache do
callback(cache[i])
end
self.step = self.step - 1
end
--- 获取value
---@param source table
---@return value table
function mt:getValue(source)
local tp = source.type
local d = methods[tp]
if not d then
return
end
local f = d.getValue
if not f then
return
end
if self.step >= 100 then
error('Stack overflow!')
return
end
self.step = self.step + 1
local value = f(self, source)
self.step = self.step - 1
return value
end
--- 获取函数的参数
---@param source table
---@return table arg1
---@return table arg2
function mt:callArgOf(source)
if not source or source.type ~= 'call' then
return
end
local args = source.args
if not args then
return
end
return tableUnpack(args)
end
--- 获取调用函数的返回值
---@param source table
---@return table return1
---@return table return2
function mt:callReturnOf(source)
if not source or source.type ~= 'call' then
return
end
local parent = source.parent
local extParent = source.extParent
if extParent then
local returns = {parent.parent}
for i = 1, #extParent do
returns[i+1] = extParent[i].parent
end
return tableUnpack(returns)
elseif parent then
return parent.parent
end
end
--- 获取函数定义的返回值
---@param source table
---@param callback fun(source:table)
function mt:functionReturnOf(source, callback)
if not source or source.type ~= 'function' then
return
end
local returns = guide.getReturns(source)
for i = 1, #returns do
local src = returns[i]
callback()
end
end
--- 获取source的索引,模式与值
---@param source table
---@return table field
---@return string mode
---@return table value
function mt:childMode(source)
if source.type == 'getfield' then
return source.field, 'get'
elseif source.type == 'setfield' then
return source.field, 'set', source.value
elseif source.type == 'getmethod' then
return source.method, 'get'
elseif source.type == 'setmethod' then
return source.method, 'set', source.value
elseif source.type == 'getindex' then
return source.index, 'get'
elseif source.type == 'setindex' then
return source.index, 'set', source.value
elseif source.type == 'field' then
return self:childMode(source.parent)
elseif source.type == 'method' then
return self:childMode(source.parent)
end
return nil, nil
end
---@class engineer_old
local m = {}
--- 新建搜索器
---@param uri string
---@return searcher_old
function m.create(uri)
local ast = files.getAst(uri)
local searcher = setmetatable({
step = 0,
ast = ast.ast,
uri = uri,
cache = {
def = {},
ref = {},
field = {},
value = {},
specialName = {},
},
}, mt)
return searcher
end
return m
|