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
|
---@class vm
local vm = require 'vm.vm'
local util = require 'utility'
local guide = require 'parser.guide'
local simpleSwitch
simpleSwitch = util.switch()
: case 'goto'
: call(function (source, pushResult)
if source.node then
pushResult(source.node)
end
end)
: case 'doc.cast.name'
: call(function (source, pushResult)
local loc = guide.getLocal(source, source[1], source.start)
if loc then
pushResult(loc)
end
end)
local searchFieldSwitch = util.switch()
: case 'table'
: call(function (suri, obj, key, pushResult)
for _, field in ipairs(obj) do
if field.type == 'tablefield'
or field.type == 'tableindex' then
if guide.getKeyName(field) == key then
pushResult(field)
end
end
end
end)
: case 'global'
---@param obj vm.object
---@param key string
: call(function (suri, obj, key, pushResult)
if obj.cate == 'variable' then
local newGlobal = vm.getGlobal('variable', obj.name, key)
if newGlobal then
for _, set in ipairs(newGlobal:getSets(suri)) do
pushResult(set)
end
end
end
if obj.cate == 'type' then
vm.getClassFields(suri, obj, key, false, pushResult)
end
end)
: case 'local'
: call(function (suri, obj, key, pushResult)
local sources = vm.getLocalSourcesSets(obj, key)
if sources then
for _, src in ipairs(sources) do
pushResult(src)
end
end
end)
: case 'doc.type.table'
: call(function (suri, obj, key, pushResult)
for _, field in ipairs(obj.fields) do
local fieldKey = field.name
if fieldKey.type == 'doc.field.name' then
if fieldKey[1] == key then
pushResult(field)
end
end
end
end)
local nodeSwitch;nodeSwitch = util.switch()
: case 'field'
: case 'method'
: call(function (source, lastKey, pushResult)
return nodeSwitch(source.parent.type, source.parent, lastKey, pushResult)
end)
: case 'getfield'
: case 'setfield'
: case 'getmethod'
: case 'setmethod'
: case 'getindex'
: case 'setindex'
: call(function (source, lastKey, pushResult)
local parentNode = vm.compileNode(source.node)
local uri = guide.getUri(source)
local key = guide.getKeyName(source)
if not key then
return
end
if lastKey then
key = key .. vm.ID_SPLITE .. lastKey
end
for pn in parentNode:eachObject() do
searchFieldSwitch(pn.type, uri, pn, key, pushResult)
end
return key, source.node
end)
: case 'tableindex'
: case 'tablefield'
: call(function (source, lastKey, pushResult)
if lastKey then
return
end
local tbl = source.parent
local uri = guide.getUri(source)
searchFieldSwitch(tbl.type, uri, tbl, guide.getKeyName(source), pushResult)
end)
: case 'doc.see.field'
: call(function (source, lastKey, pushResult)
if lastKey then
return
end
local parentNode = vm.compileNode(source.parent.name)
local uri = guide.getUri(source)
for pn in parentNode:eachObject() do
searchFieldSwitch(pn.type, uri, pn, source[1], pushResult)
end
end)
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchBySimple(source, pushResult)
simpleSwitch(source.type, source, pushResult)
end
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchByLocalID(source, pushResult)
local idSources = vm.getLocalSourcesSets(source)
if not idSources then
return
end
for _, src in ipairs(idSources) do
pushResult(src)
end
end
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchByParentNode(source, pushResult)
local lastKey
local src = source
while true do
local key, node = nodeSwitch(src.type, src, lastKey, pushResult)
if not key then
break
end
src = node
if lastKey then
lastKey = key .. vm.ID_SPLITE .. lastKey
else
lastKey = key
end
end
end
local function searchByNode(source, pushResult)
local node = vm.compileNode(source)
local suri = guide.getUri(source)
for n in node:eachObject() do
if n.type == 'global' then
for _, set in ipairs(n:getSets(suri)) do
pushResult(set)
end
else
pushResult(n)
end
end
end
---@param source parser.object
---@return parser.object[]
function vm.getDefs(source)
local results = {}
local mark = {}
local hasLocal
local function pushResult(src)
if src.type == 'local' then
if hasLocal then
return
end
hasLocal = true
end
if not mark[src] then
mark[src] = true
if guide.isSet(src)
or guide.isLiteral(src) then
results[#results+1] = src
end
end
end
searchBySimple(source, pushResult)
searchByLocalID(source, pushResult)
searchByParentNode(source, pushResult)
searchByNode(source, pushResult)
return results
end
|