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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
local util = require 'utility'
local guide = require 'parser.guide'
local Linkers
local LastIDCache = {}
local FirstIDCache = {}
local SPLIT_CHAR = '\x1F'
local LAST_REGEX = SPLIT_CHAR .. '[^' .. SPLIT_CHAR .. ']*$'
local FIRST_REGEX = '^[^' .. SPLIT_CHAR .. ']*'
local ANY_FIELD_CHAR = '*'
local RETURN_INDEX = SPLIT_CHAR .. '#'
local PARAM_INDEX = SPLIT_CHAR .. '@'
local TABLE_KEY = SPLIT_CHAR .. '<'
local ANY_FIELD = SPLIT_CHAR .. ANY_FIELD_CHAR
---创建source的链接信息
---@param id string
---@return link
local function getLink(id)
if not Linkers[id] then
Linkers[id] = {
id = id,
}
end
return Linkers[id]
end
---是否是全局变量(包括 _G.XXX 形式)
---@param source parser.guide.object
---@return boolean
local function isGlobal(source)
if source.type == 'setglobal'
or source.type == 'getglobal' then
if source.node and source.node.tag == '_ENV' then
return true
end
end
if source.type == 'field' then
source = source.parent
end
if source.special == '_G' then
return true
end
return false
end
---获取语法树单元的key
---@param source parser.guide.object
---@return string? key
---@return parser.guide.object? node
local function getKey(source)
if source.type == 'local' then
return tostring(source.start), nil
elseif source.type == 'setlocal'
or source.type == 'getlocal' then
return tostring(source.node.start), nil
elseif source.type == 'setglobal'
or source.type == 'getglobal' then
return ('%q'):format(source[1] or ''), nil
elseif source.type == 'getfield'
or source.type == 'setfield' then
return ('%q'):format(source.field and source.field[1] or ''), source.node
elseif source.type == 'tablefield' then
return ('%q'):format(source.field and source.field[1] or ''), source.parent
elseif source.type == 'getmethod'
or source.type == 'setmethod' then
return ('%q'):format(source.method and source.method[1] or ''), source.node
elseif source.type == 'setindex'
or source.type == 'getindex' then
local index = source.index
if not index then
return ANY_FIELD_CHAR, source.node
end
if index.type == 'string' then
return ('%q'):format(index[1] or ''), source.node
else
return ANY_FIELD_CHAR, source.node
end
elseif source.type == 'tableindex' then
local index = source.index
if not index then
return ANY_FIELD_CHAR, source.parent
end
if index.type == 'string' then
return ('%q'):format(index[1] or ''), source.parent
else
return ANY_FIELD_CHAR, source.parent
end
elseif source.type == 'table' then
return source.start, nil
elseif source.type == 'label' then
return source.start, nil
elseif source.type == 'goto' then
if source.node then
return source.node.start, nil
end
return nil, nil
elseif source.type == 'function' then
return source.start, nil
elseif source.type == 'string' then
return '', nil
elseif source.type == 'integer'
or source.type == 'number'
or source.type == 'boolean'
or source.type == 'nil' then
return source.start, nil
elseif source.type == '...' then
return source.start, nil
elseif source.type == 'select' then
return ('%d%s%d'):format(source.start, RETURN_INDEX, source.sindex)
elseif source.type == 'call' then
local node = source.node
if node.special == 'rawget'
or node.special == 'rawset' then
if not source.args then
return nil, nil
end
local tbl, key = source.args[1], source.args[2]
if not tbl or not key then
return nil, nil
end
if key.type == 'string' then
return ('%q'):format(key[1] or ''), tbl
else
return '', tbl
end
end
return source.start, nil
elseif source.type == 'doc.class.name'
or source.type == 'doc.alias.name'
or source.type == 'doc.extends.name'
or source.type == 'doc.see.name' then
local name = source[1]
return name, nil
elseif source.type == 'doc.type.name' then
if source.typeGeneric then
return source.start, nil
else
local name = source[1]
return name, nil
end
elseif source.type == 'doc.class'
or source.type == 'doc.type'
or source.type == 'doc.alias'
or source.type == 'doc.param'
or source.type == 'doc.vararg'
or source.type == 'doc.field.name'
or source.type == 'doc.type.enum'
or source.type == 'doc.type.table'
or source.type == 'doc.type.array'
or source.type == 'doc.type.function' then
return source.start, nil
elseif source.type == 'doc.see.field' then
return ('%q'):format(source[1]), source.parent.name
elseif source.type == 'generic.closure' then
return source.call.start, nil
elseif source.type == 'generic.value' then
return ('%s|%s'):format(
source.closure.call.start,
getKey(source.proto)
)
end
return nil, nil
end
local function checkMode(source)
if source.type == 'table' then
return 't:'
end
if source.type == 'select' then
return 's:'
end
if source.type == 'function' then
return 'f:'
end
if source.type == 'string' then
return 'str:'
end
if source.type == 'number'
or source.type == 'integer'
or source.type == 'boolean'
or source.type == 'nil' then
return 'l:'
end
if source.type == 'call' then
return 'c:'
end
if source.type == 'doc.class.name'
or source.type == 'doc.type.name'
or source.type == 'doc.alias.name'
or source.type == 'doc.extends.name' then
return 'dn:'
end
if source.type == 'doc.field.name' then
return 'dfn:'
end
if source.type == 'doc.see.name' then
return 'dsn:'
end
if source.type == 'doc.class' then
return 'dc:'
end
if source.type == 'doc.type' then
return 'dt:'
end
if source.type == 'doc.param' then
return 'dp:'
end
if source.type == 'doc.alias' then
return 'da:'
end
if source.type == 'doc.type.function' then
return 'dfun:'
end
if source.type == 'doc.type.table' then
return 'dtable:'
end
if source.type == 'doc.type.array' then
return 'darray:'
end
if source.type == 'doc.vararg' then
return 'dv:'
end
if source.type == 'doc.type.enum' then
return 'de:'
end
if source.type == 'generic.closure' then
return 'gc:'
end
if source.type == 'generic.value' then
return 'gv:'
end
if isGlobal(source) then
return 'g:'
end
return 'l:'
end
local IDList = {}
---获取语法树单元的字符串ID
---@param source parser.guide.object
---@return string? id
local function getID(source)
if not source then
return nil
end
if source._id ~= nil then
return source._id or nil
end
if source.type == 'field'
or source.type == 'method' then
source._id = false
return nil
end
local current = source
local index = 0
while true do
if current.type == 'paren' then
current = current.exp
goto CONTINUE
end
local id, node = getKey(current)
if not id then
break
end
index = index + 1
IDList[index] = id
if not node then
break
end
current = node
if current.special == '_G' then
break
end
::CONTINUE::
end
if index == 0 then
source._id = false
return nil
end
for i = index + 1, #IDList do
IDList[i] = nil
end
local mode = checkMode(current)
if not mode then
source._id = false
return nil
end
util.revertTable(IDList)
local id = mode .. table.concat(IDList, SPLIT_CHAR)
source._id = id
return id
end
---添加关联的前进ID
---@param id string
---@param forwardID string
local function pushForward(id, forwardID)
if not id
or not forwardID
or forwardID == ''
or id == forwardID then
return
end
local link = getLink(id)
if not link.forward then
link.forward = {}
end
link.forward[#link.forward+1] = forwardID
end
---添加关联的后退ID
---@param id string
---@param backwardID string
local function pushBackward(id, backwardID)
if not id
or not backwardID
or backwardID == ''
or id == backwardID then
return
end
local link = getLink(id)
if not link.backward then
link.backward = {}
end
link.backward[#link.backward+1] = backwardID
end
---@class link
-- 当前节点的id
---@field id string
-- 使用该ID的单元
---@field sources parser.guide.object[]
-- 前进的关联ID
---@field forward string[]
-- 后退的关联ID
---@field backward string[]
-- 函数调用参数信息(用于泛型)
---@field call parser.guide.object
local m = {}
m.SPLIT_CHAR = SPLIT_CHAR
m.RETURN_INDEX = RETURN_INDEX
m.PARAM_INDEX = PARAM_INDEX
m.TABLE_KEY = TABLE_KEY
m.ANY_FIELD = ANY_FIELD
--- 寻找doc的主体
---@param obj parser.guide.object
---@return parser.guide.object
local function getDocStateWithoutCrossFunction(obj)
for _ = 1, 1000 do
local parent = obj.parent
if not parent then
return obj
end
if parent.type == 'doc' then
return obj
end
if parent.type == 'doc.type.function' then
return nil
end
obj = parent
end
error('guide.getDocState overstack')
end
---添加关联单元
---@param source parser.guide.object
function m.pushSource(source)
local id = m.getID(source)
if not id then
return
end
local link = getLink(id)
if not link.sources then
link.sources = {}
end
link.sources[#link.sources+1] = source
end
---@param source parser.guide.object
---@return parser.guide.object[]
function m.compileLink(source)
local id = getID(source)
local parent = source.parent
if not parent then
return
end
if source.value then
-- x = y : x -> y
pushForward(id, getID(source.value))
pushBackward(getID(source.value), id)
end
-- self -> mt:xx
if source.type == 'local' and source[1] == 'self' then
local func = guide.getParentFunction(source)
local setmethod = func.parent
-- guess `self`
if setmethod and ( setmethod.type == 'setmethod'
or setmethod.type == 'setfield'
or setmethod.type == 'setindex') then
pushForward(id, getID(setmethod.node))
pushBackward(getID(setmethod.node), id)
end
end
-- 分解 @type
if source.type == 'doc.type' then
if source.bindSources then
for _, src in ipairs(source.bindSources) do
pushForward(getID(src), id)
pushForward(id, getID(src))
end
end
for _, typeUnit in ipairs(source.types) do
pushForward(id, getID(typeUnit))
pushBackward(getID(typeUnit), id)
end
for _, enumUnit in ipairs(source.enums) do
pushForward(id, getID(enumUnit))
end
end
-- 分解 @class
if source.type == 'doc.class' then
pushForward(id, getID(source.class))
pushForward(getID(source.class), id)
if source.extends then
for _, ext in ipairs(source.extends) do
pushBackward(id, getID(ext))
pushBackward(getID(ext), id)
end
end
if source.bindSources then
for _, src in ipairs(source.bindSources) do
pushForward(getID(src), id)
pushForward(id, getID(src))
end
end
do
local start
for _, doc in ipairs(source.bindGroup) do
if doc.type == 'doc.class' then
start = doc == source
end
if start and doc.type == 'doc.field' then
local key = doc.field[1]
if key then
local keyID = ('%s%s%q'):format(
id,
SPLIT_CHAR,
key
)
pushForward(keyID, getID(doc.field))
pushBackward(getID(doc.field), keyID)
pushForward(keyID, getID(doc.extends))
pushBackward(getID(doc.extends), keyID)
end
end
end
end
end
if source.type == 'doc.param' then
pushForward(getID(source), getID(source.extends))
end
if source.type == 'doc.vararg' then
pushForward(getID(source), getID(source.vararg))
end
if source.type == 'doc.see' then
local nameID = getID(source.name)
local classID = nameID:gsub('^dsn:', 'dn:')
pushForward(nameID, classID)
if source.field then
local fieldID = getID(source.field)
local fieldClassID = fieldID:gsub('^dsn:', 'dn:')
pushForward(fieldID, fieldClassID)
end
end
if source.type == 'call' then
local node = source.node
local nodeID = getID(node)
if not nodeID then
return
end
getLink(id).call = source
-- 将 call 映射到 node#1 上
local callID = ('%s%s%s'):format(
nodeID,
RETURN_INDEX,
1
)
pushForward(id, callID)
-- 将setmetatable映射到 param1 以及 param2.__index 上
if node.special == 'setmetatable' then
local tblID = getID(source.args and source.args[1])
local metaID = getID(source.args and source.args[2])
local indexID
if metaID then
indexID = ('%s%s%q'):format(
metaID,
SPLIT_CHAR,
'__index'
)
end
pushForward(id, callID)
pushBackward(callID, id)
pushForward(callID, tblID)
pushForward(callID, indexID)
pushBackward(tblID, callID)
--pushBackward(indexID, callID)
end
end
if source.type == 'select' then
if source.vararg.type == 'call' then
local call = source.vararg
local node = call.node
local nodeID = getID(node)
if not nodeID then
return
end
-- 将call的返回值接收映射到函数返回值上
local callXID = ('%s%s%s'):format(
nodeID,
RETURN_INDEX,
source.sindex
)
pushForward(id, callXID)
pushBackward(callXID, id)
getLink(id).call = call
if node.special == 'pcall'
or node.special == 'xpcall' then
local index = source.sindex - 1
if index <= 0 then
return
end
local funcID = call.args and getID(call.args[1])
if not funcID then
return
end
local funcXID = ('%s%s%s'):format(
funcID,
RETURN_INDEX,
index
)
pushForward(id, funcXID)
pushBackward(funcXID, id)
end
end
end
if source.type == 'doc.type.function' then
if source.returns then
for index, rtn in ipairs(source.returns) do
local returnID = ('%s%s%s'):format(
id,
RETURN_INDEX,
index
)
pushForward(returnID, getID(rtn))
end
end
-- @type fun(x: T):T 的情况
local docType = getDocStateWithoutCrossFunction(source)
if docType and docType.type == 'doc.type' then
guide.eachSourceType(source, 'doc.type.name', function (typeName)
if typeName.typeGeneric then
source.isGeneric = true
return false
end
end)
end
end
if source.type == 'doc.type.table' then
if source.tkey then
local keyID = ('%s%s'):format(
id,
TABLE_KEY
)
pushForward(keyID, getID(source.tkey))
end
if source.tvalue then
local valueID = ('%s%s'):format(
id,
ANY_FIELD
)
pushForward(valueID, getID(source.tvalue))
end
end
if source.type == 'doc.type.array' then
if source.node then
local nodeID = ('%s%s'):format(
id,
ANY_FIELD
)
pushForward(nodeID, getID(source.node))
end
local keyID = ('%s%s'):format(
id,
TABLE_KEY
)
pushForward(keyID, 'dn:integer')
end
-- 将函数的返回值映射到具体的返回值上
if source.type == 'function' then
-- 检查实体返回值
if source.returns then
local returns = {}
for _, rtn in ipairs(source.returns) do
for index, rtnObj in ipairs(rtn) do
if not returns[index] then
returns[index] = {}
end
returns[index][#returns[index]+1] = rtnObj
end
end
for index, rtnObjs in ipairs(returns) do
local returnID = ('%s%s%s'):format(
id,
RETURN_INDEX,
index
)
for _, rtnObj in ipairs(rtnObjs) do
pushForward(returnID, getID(rtnObj))
if rtnObj.type == 'function'
or rtnObj.type == 'call' then
pushBackward(getID(rtnObj), returnID)
end
end
end
end
-- 检查 luadoc
if source.bindDocs then
for _, doc in ipairs(source.bindDocs) do
if doc.type == 'doc.return' then
for _, rtn in ipairs(doc.returns) do
local fullID = ('%s%s%s'):format(
id,
RETURN_INDEX,
rtn.returnIndex
)
pushForward(fullID, getID(rtn))
pushBackward(getID(rtn), fullID)
end
end
if doc.type == 'doc.param' then
local paramName = doc.param[1]
if source.docParamMap then
local paramIndex = source.docParamMap[paramName]
local param = source.args[paramIndex]
if param then
pushForward(getID(param), getID(doc))
param.docParam = doc
end
end
end
if doc.type == 'doc.vararg' then
for _, param in ipairs(source.args) do
if param.type == '...' then
pushForward(getID(param), getID(doc))
end
end
end
if doc.type == 'doc.generic' then
source.isGeneric = true
end
end
end
end
if source.type == 'generic.closure' then
for i, rtn in ipairs(source.returns) do
local closureID = ('%s%s%s'):format(
id,
RETURN_INDEX,
i
)
local returnID = getID(rtn)
pushForward(closureID, returnID)
pushBackward(returnID, closureID)
end
end
if source.type == 'generic.value' then
local proto = source.proto
local closure = source.closure
local upvalues = closure.upvalues
if proto.type == 'doc.type.name' then
local key = proto[1]
if upvalues[key] then
for _, paramID in ipairs(upvalues[key]) do
pushForward(id, paramID)
pushBackward(paramID, id)
end
end
end
if proto.type == 'doc.type' then
for _, tp in ipairs(source.types) do
pushForward(id, getID(tp))
pushBackward(getID(tp), id)
end
end
if proto.type == 'doc.type.array' then
local nodeID = ('%s%s'):format(
id,
ANY_FIELD
)
pushForward(nodeID, getID(source.node))
local keyID = ('%s%s'):format(
id,
TABLE_KEY
)
pushForward(keyID, 'dn:integer')
end
if proto.type == 'doc.type.table' then
if source.tkey then
local keyID = ('%s%s'):format(
id,
TABLE_KEY
)
pushForward(keyID, getID(source.tkey))
end
if source.tvalue then
local valueID = ('%s%s'):format(
id,
ANY_FIELD
)
pushForward(valueID, getID(source.tvalue))
end
end
end
end
---根据ID来获取所有的link
---@param root parser.guide.object
---@param id string
---@return link?
function m.getLinkByID(root, id)
root = guide.getRoot(root)
local linkers = root._linkers
if not linkers then
return nil
end
return linkers[id]
end
---根据ID来获取第一个节点的ID
---@param id string
---@return string
function m.getFirstID(id)
if FirstIDCache[id] then
return FirstIDCache[id] or nil
end
local firstID, count = id:match(FIRST_REGEX)
if count == 0 then
FirstIDCache[id] = false
return nil
end
FirstIDCache[id] = firstID
return firstID
end
---根据ID来获取上个节点的ID
---@param id string
---@return string
function m.getLastID(id)
if LastIDCache[id] then
return LastIDCache[id] or nil
end
local lastID, count = id:gsub(LAST_REGEX, '')
if count == 0 then
LastIDCache[id] = false
return nil
end
LastIDCache[id] = lastID
return lastID
end
---获取source的ID
---@param source parser.guide.object
---@return string
function m.getID(source)
return getID(source)
end
---编译整个文件的link
---@param source parser.guide.object
---@return table
function m.compileLinks(source)
local root = guide.getRoot(source)
if root._linkers then
return root._linkers
end
Linkers = {}
root._linkers = Linkers
guide.eachSource(root, function (src)
m.pushSource(src)
m.compileLink(src)
end)
-- Special rule: ('').XX -> stringlib.XX
pushForward('str:', 'dn:stringlib')
return Linkers
end
return m
|