@codua/codeoutline-win32-x64 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DISTRIBUTION.md +98 -0
- package/LICENSE +24 -0
- package/README.md +132 -0
- package/README.zh-CN.md +132 -0
- package/THIRD_PARTY.md +32 -0
- package/build-info.json +1 -0
- package/codeoutline +4 -0
- package/codeoutline.cmd +3 -0
- package/licenses/libdeflate.txt +22 -0
- package/licenses/lua-cmsgpack.txt +22 -0
- package/licenses/luajit.txt +56 -0
- package/licenses/mbedtls.txt +553 -0
- package/licenses/rpmalloc.txt +12 -0
- package/licenses/xnet2lua.txt +24 -0
- package/licenses/yyjson.txt +21 -0
- package/package.json +1 -0
- package/scripts/codeoutline/cli.lua +32 -0
- package/scripts/codeoutline/command.lua +155 -0
- package/scripts/codeoutline/common.lua +124 -0
- package/scripts/codeoutline/control.lua +4 -0
- package/scripts/codeoutline/explore.lua +500 -0
- package/scripts/codeoutline/graph.lua +380 -0
- package/scripts/codeoutline/http.lua +156 -0
- package/scripts/codeoutline/index.lua +404 -0
- package/scripts/codeoutline/index_worker.lua +43 -0
- package/scripts/codeoutline/lang/c.lua +362 -0
- package/scripts/codeoutline/lang/cpp.lua +539 -0
- package/scripts/codeoutline/lang/csharp.lua +334 -0
- package/scripts/codeoutline/lang/go.lua +233 -0
- package/scripts/codeoutline/lang/java.lua +274 -0
- package/scripts/codeoutline/lang/js.lua +466 -0
- package/scripts/codeoutline/lang/lua.lua +218 -0
- package/scripts/codeoutline/lang/python.lua +184 -0
- package/scripts/codeoutline/lang/rust.lua +276 -0
- package/scripts/codeoutline/main.lua +104 -0
- package/scripts/codeoutline/mcp.lua +188 -0
- package/scripts/codeoutline/parse.lua +49 -0
- package/scripts/codeoutline/path.lua +36 -0
- package/scripts/codeoutline/service.lua +135 -0
- package/scripts/codeoutline/text.lua +43 -0
- package/scripts/codeoutline/version.lua +1 -0
- package/scripts/codeoutline/xscan.lua +625 -0
- package/xnet2lua/bin/xnet.exe +0 -0
- package/xnet2lua/scripts/core/share/xhttp_codec.lua +835 -0
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
-- lang/js.lua — index-level JavaScript / TypeScript parser: functions,
|
|
2
|
+
-- arrow functions bound to names, classes (+ members: methods, accessors,
|
|
3
|
+
-- fields, constructors), TS interfaces, type aliases, enums, namespaces,
|
|
4
|
+
-- `a.b = function` assignments, module-level constants, ES imports /
|
|
5
|
+
-- require(), and calls.
|
|
6
|
+
--
|
|
7
|
+
-- JS has automatic semicolon insertion, so instead of statement parsing the
|
|
8
|
+
-- file is scanned for definition patterns at any depth; ranges come from
|
|
9
|
+
-- the bracket match table and nesting from containing ranges.
|
|
10
|
+
|
|
11
|
+
local xscan = require('codeoutline.xscan')
|
|
12
|
+
local common = require('codeoutline.common')
|
|
13
|
+
|
|
14
|
+
local M = {}
|
|
15
|
+
|
|
16
|
+
-- Reserved words only: contextual ones (type, get, set, async, of, as,
|
|
17
|
+
-- static, declare, readonly...) stay identifiers because code uses them as
|
|
18
|
+
-- names (`node.type`, `map.get(k)`); the parser checks their text instead.
|
|
19
|
+
M.config = {
|
|
20
|
+
keywords = {
|
|
21
|
+
'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete',
|
|
22
|
+
'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'function', 'if',
|
|
23
|
+
'import', 'in', 'instanceof', 'let', 'new', 'null', 'return', 'super', 'switch', 'this',
|
|
24
|
+
'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield',
|
|
25
|
+
},
|
|
26
|
+
ops = {
|
|
27
|
+
'>>>=', '...', '===', '!==', '**=', '&&=', '||=', '??=', '<<=', '>>=', '>>>', '=>', '?.',
|
|
28
|
+
'??', '**', '==', '!=', '<=', '>=', '&&', '||', '++', '--', '+=', '-=', '*=', '/=', '%=',
|
|
29
|
+
'&=', '|=', '^=', '<<', '>>',
|
|
30
|
+
},
|
|
31
|
+
ident_start = 'A-Za-z_$',
|
|
32
|
+
ident_char = 'A-Za-z0-9_$',
|
|
33
|
+
line_comment = { '//' },
|
|
34
|
+
block_comment = { { '/*', '*/' } },
|
|
35
|
+
strings = { { '"', '"', escape = '\\' }, { "'", "'", escape = '\\' } },
|
|
36
|
+
template_literals = true,
|
|
37
|
+
regex_literals = true,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
M.lang = xscan.lang(M.config)
|
|
41
|
+
|
|
42
|
+
local MODIFIERS = {
|
|
43
|
+
static = true, async = true, get = true, set = true, public = true, private = true,
|
|
44
|
+
protected = true, readonly = true, abstract = true, override = true, declare = true,
|
|
45
|
+
accessor = true, export = true, default = true,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
|
|
49
|
+
local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
|
|
50
|
+
local function word(T, i, s) return (T.k[i] == 'id' or T.k[i] == 'kw') and T:text(i) == s end
|
|
51
|
+
|
|
52
|
+
-- Skip a TS generic parameter list at '<' i; returns the next index.
|
|
53
|
+
local function skip_angle(T, i, limit)
|
|
54
|
+
if not is(T, i, '<') then return i end
|
|
55
|
+
local d, x = 0, i
|
|
56
|
+
while x <= limit do
|
|
57
|
+
local t = T.k[x] == 'op' and T:text(x)
|
|
58
|
+
if t == '<' then d = d + 1
|
|
59
|
+
elseif t == '>' then d = d - 1
|
|
60
|
+
elseif t == '>>' then d = d - 2
|
|
61
|
+
elseif t == '>>>' then d = d - 3
|
|
62
|
+
elseif t == '=>' then -- arrow inside a generic default: keep going
|
|
63
|
+
elseif (t == '(' or t == '[' or t == '{') and T.m[x] then x = T.m[x] end
|
|
64
|
+
if d <= 0 then return x + 1 end
|
|
65
|
+
x = x + 1
|
|
66
|
+
end
|
|
67
|
+
return i + 1
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
-- The body '{' after a parameter list closing at p (skipping a TS return
|
|
71
|
+
-- type annotation), or nil when there is none (overload / abstract / sig).
|
|
72
|
+
-- A '{' directly after ':' '|' '&' '<' ',' is an object type, not a body.
|
|
73
|
+
local TYPE_BEFORE = { [':'] = true, ['|'] = true, ['&'] = true, ['<'] = true, [','] = true, ['=>'] = false }
|
|
74
|
+
local function body_after(T, p, limit)
|
|
75
|
+
local x = p + 1
|
|
76
|
+
while x <= limit do
|
|
77
|
+
if is(T, x, '{') then
|
|
78
|
+
local prev = T.k[x - 1] == 'op' and T:text(x - 1)
|
|
79
|
+
if prev and TYPE_BEFORE[prev] then
|
|
80
|
+
x = (T.m[x] or x) + 1
|
|
81
|
+
else
|
|
82
|
+
return x
|
|
83
|
+
end
|
|
84
|
+
elseif is(T, x, ';') or is(T, x, '}') or is(T, x, ',') and x > p + 1 then
|
|
85
|
+
return nil
|
|
86
|
+
elseif (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
|
|
87
|
+
x = T.m[x] + 1
|
|
88
|
+
elseif is(T, x, '<') then
|
|
89
|
+
x = skip_angle(T, x, limit)
|
|
90
|
+
elseif is(T, x, '=>') then
|
|
91
|
+
return nil -- `): Type => ...` is not a declaration head
|
|
92
|
+
elseif T.l[x] ~= T.el[x - 1] and not is(T, x - 1, ':') and not is(T, x - 1, '|')
|
|
93
|
+
and not is(T, x, '|') and not is(T, x, '&') and not is(T, x, '.') then
|
|
94
|
+
return nil -- next line starts something else (ASI)
|
|
95
|
+
else
|
|
96
|
+
x = x + 1
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
return nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
-- End of an expression-bodied statement starting at x: ';' or the line end
|
|
103
|
+
-- at bracket depth 0 (ASI), extended across groups and trailing operators.
|
|
104
|
+
local CONT = { ['.'] = true, ['?.'] = true, ['+'] = true, ['-'] = true, ['*'] = true, ['/'] = true,
|
|
105
|
+
['&&'] = true, ['||'] = true, ['??'] = true, ['?'] = true, [':'] = true, [','] = true, ['='] = true,
|
|
106
|
+
['=>'] = true, ['|'] = true, ['&'] = true }
|
|
107
|
+
local function expr_end(T, x, limit)
|
|
108
|
+
local y = x
|
|
109
|
+
while y <= limit do
|
|
110
|
+
if T.m[y] and T.m[y] > y then y = T.m[y] end
|
|
111
|
+
local nx = y + 1
|
|
112
|
+
if nx > limit or is(T, nx, ';') or is(T, nx, '}') or is(T, nx, ')') or is(T, nx, ']') then return y end
|
|
113
|
+
if is(T, nx, ',') and T.l[nx] == T.el[y] then return y end
|
|
114
|
+
if T.l[nx] ~= T.el[y] then
|
|
115
|
+
local contd = (T.k[y] == 'op' and CONT[T:text(y)]) or (T.k[nx] == 'op' and CONT[T:text(nx)])
|
|
116
|
+
if not contd then return y end
|
|
117
|
+
end
|
|
118
|
+
y = nx
|
|
119
|
+
end
|
|
120
|
+
return limit
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
-- Arrow function / function expression starting at x (after '=' or ':')?
|
|
124
|
+
-- Returns the token range [x, stop] of the function, or nil.
|
|
125
|
+
local function func_expr(T, x, limit)
|
|
126
|
+
local y = x
|
|
127
|
+
if word(T, y, 'async') then y = y + 1 end
|
|
128
|
+
if kw(T, y, 'function') then
|
|
129
|
+
local p = y + 1
|
|
130
|
+
if is(T, p, '*') then p = p + 1 end
|
|
131
|
+
if T.k[p] == 'id' then p = p + 1 end
|
|
132
|
+
p = skip_angle(T, p, limit)
|
|
133
|
+
if is(T, p, '(') and T.m[p] then
|
|
134
|
+
local b = body_after(T, T.m[p], limit)
|
|
135
|
+
if b then return x, T.m[b] or limit, b end
|
|
136
|
+
end
|
|
137
|
+
return nil
|
|
138
|
+
end
|
|
139
|
+
y = skip_angle(T, y, limit)
|
|
140
|
+
local arrow
|
|
141
|
+
if is(T, y, '(') and T.m[y] then
|
|
142
|
+
arrow = T.m[y] + 1
|
|
143
|
+
if is(T, arrow, ':') then -- (x): T => ...
|
|
144
|
+
local z = arrow + 1
|
|
145
|
+
while z <= limit and not is(T, z, '=>') and not is(T, z, ';') and T.l[z] == T.l[arrow] do
|
|
146
|
+
z = (T.m[z] and T.m[z] > z) and T.m[z] + 1 or z + 1
|
|
147
|
+
end
|
|
148
|
+
arrow = z
|
|
149
|
+
end
|
|
150
|
+
elseif T.k[y] == 'id' then
|
|
151
|
+
arrow = y + 1
|
|
152
|
+
end
|
|
153
|
+
if arrow and is(T, arrow, '=>') then
|
|
154
|
+
if is(T, arrow + 1, '{') and T.m[arrow + 1] then return x, T.m[arrow + 1], arrow + 1 end
|
|
155
|
+
return x, expr_end(T, arrow + 1, limit), arrow + 1
|
|
156
|
+
end
|
|
157
|
+
return nil
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
local parse_class_body
|
|
161
|
+
|
|
162
|
+
-- Scan [a, b] for definitions; `parent` is the enclosing node (if any).
|
|
163
|
+
local function scan(T, r, a, b, parent, defs, stem)
|
|
164
|
+
local i = a
|
|
165
|
+
while i <= b do
|
|
166
|
+
local k, t = T.k[i], T:text(i)
|
|
167
|
+
local handled = false
|
|
168
|
+
if k == 'kw' and t == 'function' then
|
|
169
|
+
local p = i + 1
|
|
170
|
+
if is(T, p, '*') then p = p + 1 end
|
|
171
|
+
if T.k[p] == 'id' then
|
|
172
|
+
local name_tok = p
|
|
173
|
+
p = skip_angle(T, p + 1, b)
|
|
174
|
+
if is(T, p, '(') and T.m[p] then
|
|
175
|
+
local body = body_after(T, T.m[p], b)
|
|
176
|
+
local start = i
|
|
177
|
+
while start > a and (word(T, start - 1, 'async') or kw(T, start - 1, 'export')
|
|
178
|
+
or kw(T, start - 1, 'default') or word(T, start - 1, 'declare')) do start = start - 1 end
|
|
179
|
+
local close = body and (T.m[body] or b) or T.m[p]
|
|
180
|
+
defs[name_tok] = true
|
|
181
|
+
local node = common.add_node(r, T, { kind = 'function', name = T:text(name_tok), parent = parent,
|
|
182
|
+
ti = start, tj = close, sig = common.sig(T, start, body and body - 1 or T.m[p], 200) })
|
|
183
|
+
if body then scan(T, r, body + 1, close - 1, node, defs, stem) end
|
|
184
|
+
i = close + 1
|
|
185
|
+
handled = true
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
elseif (k == 'kw' and t == 'class' and (T.k[i + 1] == 'id' or kw(T, i + 1, 'extends') or is(T, i + 1, '{'))
|
|
189
|
+
and not is(T, i - 1, '.') and not is(T, i - 1, '<')) -- not JSX `class="..."`
|
|
190
|
+
or ((t == 'interface') and k == 'id' and T.k[i + 1] == 'id') then
|
|
191
|
+
local name_tok = T.k[i + 1] == 'id' and i + 1 or nil
|
|
192
|
+
if name_tok and T:text(name_tok) == 'extends' then name_tok = nil end
|
|
193
|
+
local x = i + 1
|
|
194
|
+
while x <= b and not is(T, x, '{') do
|
|
195
|
+
if (is(T, x, '(') or is(T, x, '[')) and T.m[x] then x = T.m[x] end
|
|
196
|
+
if is(T, x, ';') then break end
|
|
197
|
+
x = x + 1
|
|
198
|
+
end
|
|
199
|
+
if is(T, x, '{') and T.m[x] and (name_tok or not is(T, i - 1, '=')) then
|
|
200
|
+
local start = i
|
|
201
|
+
while start > a and (kw(T, start - 1, 'export') or kw(T, start - 1, 'default')
|
|
202
|
+
or word(T, start - 1, 'abstract') or word(T, start - 1, 'declare')) do start = start - 1 end
|
|
203
|
+
local extends, implements = {}, {}
|
|
204
|
+
local mode
|
|
205
|
+
for y = (name_tok or i) + 1, x - 1 do
|
|
206
|
+
if kw(T, y, 'extends') then mode = extends
|
|
207
|
+
elseif word(T, y, 'implements') then mode = implements
|
|
208
|
+
elseif mode and T.k[y] == 'id' and not is(T, y - 1, '.') and not is(T, y - 1, '<') then
|
|
209
|
+
local nm, z = T:text(y), y
|
|
210
|
+
while is(T, z + 1, '.') and T.k[z + 2] == 'id' do nm = nm .. '.' .. T:text(z + 2); z = z + 2 end
|
|
211
|
+
mode[#mode + 1] = nm
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
local kind = t == 'class' and 'class' or 'interface'
|
|
215
|
+
local node = common.add_node(r, T, {
|
|
216
|
+
kind = kind, name = name_tok and T:text(name_tok) or (is(T, i - 1, '=') and T:text(i - 2)) or 'default',
|
|
217
|
+
parent = parent, ti = start, tj = T.m[x], sig = common.sig(T, start, x - 1, 200),
|
|
218
|
+
extends = #extends > 0 and extends or nil, implements = #implements > 0 and implements or nil,
|
|
219
|
+
})
|
|
220
|
+
if name_tok then defs[name_tok] = true end
|
|
221
|
+
parse_class_body(T, r, x, T.m[x], node, defs, stem)
|
|
222
|
+
i = T.m[x] + 1
|
|
223
|
+
handled = true
|
|
224
|
+
end
|
|
225
|
+
elseif k == 'kw' and t == 'enum' and T.k[i + 1] == 'id' and is(T, i + 2, '{') and T.m[i + 2] then
|
|
226
|
+
local c = T.m[i + 2]
|
|
227
|
+
local node = common.add_node(r, T, { kind = 'enum', name = T:text(i + 1), parent = parent, ti = i, tj = c })
|
|
228
|
+
local x = i + 3
|
|
229
|
+
while x < c do
|
|
230
|
+
if T.k[x] == 'id' or T.k[x] == 'str' then
|
|
231
|
+
local nm = T.k[x] == 'str' and T:text(x):sub(2, -2) or T:text(x)
|
|
232
|
+
common.add_node(r, T, { kind = 'enum_member', name = nm, parent = node, ti = x, tj = x })
|
|
233
|
+
end
|
|
234
|
+
while x < c and not is(T, x, ',') do x = (T.m[x] and T.m[x] > x) and T.m[x] + 1 or x + 1 end
|
|
235
|
+
x = x + 1
|
|
236
|
+
end
|
|
237
|
+
i = c + 1
|
|
238
|
+
handled = true
|
|
239
|
+
elseif k == 'id' and t == 'type' and T.k[i + 1] == 'id' and (is(T, i + 2, '=') or is(T, i + 2, '<'))
|
|
240
|
+
and not is(T, i - 1, '.') then
|
|
241
|
+
local y = skip_angle(T, i + 2, b)
|
|
242
|
+
if is(T, y, '=') then
|
|
243
|
+
local stop = expr_end(T, y + 1, b)
|
|
244
|
+
common.add_node(r, T, { kind = 'typedef', name = T:text(i + 1), parent = parent, ti = i, tj = stop,
|
|
245
|
+
sig = common.sig(T, i, stop, 160) })
|
|
246
|
+
i = stop + 1
|
|
247
|
+
handled = true
|
|
248
|
+
end
|
|
249
|
+
elseif k == 'id' and (t == 'namespace' or t == 'module') and (T.k[i + 1] == 'id' or T.k[i + 1] == 'str')
|
|
250
|
+
and not is(T, i - 1, '.') then
|
|
251
|
+
local x = i + 1
|
|
252
|
+
while x <= b and (T.k[x] == 'id' or is(T, x, '.') or T.k[x] == 'str') do x = x + 1 end
|
|
253
|
+
if is(T, x, '{') and T.m[x] then
|
|
254
|
+
local nm = common.sig(T, i + 1, x - 1):gsub('%s', ''):gsub('^["\']', ''):gsub('["\']$', '')
|
|
255
|
+
local node = common.add_node(r, T, { kind = 'namespace', name = nm, parent = parent, ti = i, tj = T.m[x] })
|
|
256
|
+
scan(T, r, x + 1, T.m[x] - 1, node, defs, stem)
|
|
257
|
+
i = T.m[x] + 1
|
|
258
|
+
handled = true
|
|
259
|
+
end
|
|
260
|
+
elseif k == 'kw' and (t == 'const' or t == 'let' or t == 'var') and T.k[i + 1] == 'id' then
|
|
261
|
+
-- const name = <function|arrow|value>; destructuring is skipped
|
|
262
|
+
local name_tok = i + 1
|
|
263
|
+
local y = name_tok + 1
|
|
264
|
+
if is(T, y, ':') then -- const x: Type = ...
|
|
265
|
+
y = y + 1
|
|
266
|
+
while y <= b and not is(T, y, '=') and not is(T, y, ';') and T.l[y] == T.l[name_tok] do
|
|
267
|
+
y = (T.m[y] and T.m[y] > y) and T.m[y] + 1 or (is(T, y, '<') and skip_angle(T, y, b) or y + 1)
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
if is(T, y, '=') then
|
|
271
|
+
local fs, fe, fb = func_expr(T, y + 1, b)
|
|
272
|
+
local start = kw(T, i - 1, 'export') and i - 1 or i
|
|
273
|
+
if fs then
|
|
274
|
+
defs[name_tok] = true
|
|
275
|
+
local node = common.add_node(r, T, { kind = 'function', name = T:text(name_tok), parent = parent,
|
|
276
|
+
ti = start, tj = fe, sig = common.sig(T, start, math.max(fb - 1, name_tok), 200) })
|
|
277
|
+
if fb and is(T, fb, '{') then scan(T, r, fb + 1, fe - 1, node, defs, stem) end
|
|
278
|
+
i = fe + 1
|
|
279
|
+
handled = true
|
|
280
|
+
elseif not parent then
|
|
281
|
+
local stop = expr_end(T, y + 1, b)
|
|
282
|
+
defs[name_tok] = true
|
|
283
|
+
common.add_node(r, T, { kind = 'variable', name = T:text(name_tok), ti = start, tj = stop,
|
|
284
|
+
sig = common.sig(T, start, math.min(stop, y + 1), 120) })
|
|
285
|
+
-- keep scanning inside the initializer (object literals with functions, IIFEs)
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
elseif k == 'id' and is(T, i + 1, ':') and (is(T, i - 1, '{') or is(T, i - 1, ',')) and not defs[i]
|
|
289
|
+
and func_expr(T, i + 2, b) then
|
|
290
|
+
-- object literal property: { key: function () {}, other: (x) => {} }
|
|
291
|
+
local fs, fe, fb = func_expr(T, i + 2, b)
|
|
292
|
+
defs[i] = true
|
|
293
|
+
local node = common.add_node(r, T, { kind = 'function', name = t, parent = parent,
|
|
294
|
+
ti = i, tj = fe, sig = common.sig(T, i, math.max(fb - 1, i), 200) })
|
|
295
|
+
if is(T, fb, '{') then scan(T, r, fb + 1, fe - 1, node, defs, stem) end
|
|
296
|
+
i = fe + 1
|
|
297
|
+
handled = true
|
|
298
|
+
elseif k == 'id' and not defs[i] and (is(T, i + 1, '(') or is(T, i + 1, '<'))
|
|
299
|
+
and (is(T, i - 1, '{') or is(T, i - 1, ',')
|
|
300
|
+
or ((word(T, i - 1, 'async') or word(T, i - 1, 'get') or word(T, i - 1, 'set') or is(T, i - 1, '*'))
|
|
301
|
+
and (is(T, i - 2, '{') or is(T, i - 2, ','))))
|
|
302
|
+
and (function()
|
|
303
|
+
local p = skip_angle(T, i + 1, b)
|
|
304
|
+
if not (is(T, p, '(') and T.m[p]) then return false end
|
|
305
|
+
local body = body_after(T, T.m[p], b)
|
|
306
|
+
return body and T.l[body] == T.el[T.m[p]]
|
|
307
|
+
end)() then
|
|
308
|
+
-- object literal method shorthand: { fetch(req) { ... } }
|
|
309
|
+
local p = skip_angle(T, i + 1, b)
|
|
310
|
+
local body = body_after(T, T.m[p], b)
|
|
311
|
+
local close = T.m[body] or b
|
|
312
|
+
defs[i] = true
|
|
313
|
+
local node = common.add_node(r, T, { kind = 'method', name = t, parent = parent,
|
|
314
|
+
ti = i, tj = close, sig = common.sig(T, i, body - 1, 200) })
|
|
315
|
+
scan(T, r, body + 1, close - 1, node, defs, stem)
|
|
316
|
+
i = close + 1
|
|
317
|
+
handled = true
|
|
318
|
+
elseif k == 'id' and is(T, i + 1, '=') and is(T, i - 1, '.') and not defs[i] then
|
|
319
|
+
-- a.b.c = function / arrow (exports.x = ..., Foo.prototype.bar = ...)
|
|
320
|
+
local fs, fe, fb = func_expr(T, i + 2, b)
|
|
321
|
+
if fs then
|
|
322
|
+
local parts, y = { T:text(i) }, i
|
|
323
|
+
while is(T, y - 1, '.') and (T.k[y - 2] == 'id' or T.k[y - 2] == 'kw') do
|
|
324
|
+
table.insert(parts, 1, T:text(y - 2))
|
|
325
|
+
y = y - 2
|
|
326
|
+
end
|
|
327
|
+
defs[i] = true
|
|
328
|
+
local node = common.add_node(r, T, { kind = 'function', name = T:text(i), parent = parent,
|
|
329
|
+
ti = y, tj = fe, sig = common.sig(T, y, math.max(fb - 1, i), 200) })
|
|
330
|
+
r.nodes[node].qualified = table.concat(parts, '.')
|
|
331
|
+
if fb and is(T, fb, '{') then scan(T, r, fb + 1, fe - 1, node, defs, stem) end
|
|
332
|
+
i = fe + 1
|
|
333
|
+
handled = true
|
|
334
|
+
end
|
|
335
|
+
elseif (k == 'kw' and t == 'import') or (k == 'kw' and t == 'export') then
|
|
336
|
+
-- import x from 'm' / import 'm' / export ... from 'm'
|
|
337
|
+
local x = i + 1
|
|
338
|
+
local stop = math.min(b, i + 400)
|
|
339
|
+
if is(T, x, '(') and T.k[x + 1] == 'str' then
|
|
340
|
+
r.imports[#r.imports + 1] = { path = T:text(x + 1):sub(2, -2), line = T:line(i), dynamic = true }
|
|
341
|
+
else
|
|
342
|
+
while x <= stop do
|
|
343
|
+
if T.k[x] == 'str' and (k == 'kw' and t == 'import' and x == i + 1 or word(T, x - 1, 'from')) then
|
|
344
|
+
r.imports[#r.imports + 1] = { path = T:text(x):sub(2, -2), line = T:line(i) }
|
|
345
|
+
break
|
|
346
|
+
end
|
|
347
|
+
if is(T, x, ';') or (T.l[x] ~= T.l[i] and not (is(T, x - 1, ',') or is(T, x - 1, '{')
|
|
348
|
+
or is(T, x, '}') or word(T, x, 'from') or T.k[x] == 'id' or is(T, x, ','))) then break end
|
|
349
|
+
if kw(T, x, 'function') or kw(T, x, 'class') or kw(T, x, 'const') then break end
|
|
350
|
+
x = x + 1
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
elseif k == 'id' and t == 'require' and is(T, i + 1, '(') and T.k[i + 2] == 'str' then
|
|
354
|
+
r.imports[#r.imports + 1] = { path = T:text(i + 2):sub(2, -2), line = T:line(i), kind = 'require' }
|
|
355
|
+
end
|
|
356
|
+
if not handled then i = i + 1 end
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
-- Members of a class / interface body between '{' o and '}' c.
|
|
361
|
+
parse_class_body = function(T, r, o, c, parent, defs, stem)
|
|
362
|
+
local x = o + 1
|
|
363
|
+
while x < c do
|
|
364
|
+
if is(T, x, ';') or is(T, x, ',') then
|
|
365
|
+
x = x + 1
|
|
366
|
+
elseif is(T, x, '@') then
|
|
367
|
+
-- decorator: @name, @a.b, @name(...)
|
|
368
|
+
x = x + 1
|
|
369
|
+
while T.k[x] == 'id' or is(T, x, '.') do x = x + 1 end
|
|
370
|
+
if is(T, x, '(') and T.m[x] then x = T.m[x] + 1 end
|
|
371
|
+
else
|
|
372
|
+
local start = x
|
|
373
|
+
-- a modifier word is only a modifier when another member name follows
|
|
374
|
+
-- (`get<T>(...)` / `static()` are methods named get / static)
|
|
375
|
+
local function names_follow(y)
|
|
376
|
+
local k2 = T.k[y]
|
|
377
|
+
return k2 == 'id' or k2 == 'kw' or k2 == 'str' or k2 == 'num' or is(T, y, '#') or is(T, y, '[') or is(T, y, '*')
|
|
378
|
+
end
|
|
379
|
+
while (T.k[x] == 'id' and MODIFIERS[T:text(x)] and names_follow(x + 1) and T.l[x + 1] == T.l[x])
|
|
380
|
+
or is(T, x, '*') do
|
|
381
|
+
x = x + 1
|
|
382
|
+
end
|
|
383
|
+
local name_tok, name
|
|
384
|
+
if T.k[x] == 'id' or T.k[x] == 'kw' or T.k[x] == 'num' then
|
|
385
|
+
name_tok, name = x, T:text(x)
|
|
386
|
+
elseif T.k[x] == 'str' then
|
|
387
|
+
name_tok, name = x, T:text(x):sub(2, -2)
|
|
388
|
+
elseif is(T, x, '#') and T.k[x + 1] == 'id' then
|
|
389
|
+
name_tok, name = x + 1, '#' .. T:text(x + 1)
|
|
390
|
+
x = x + 1
|
|
391
|
+
elseif is(T, x, '[') and T.m[x] then
|
|
392
|
+
name_tok, name = x, nil -- computed key: skip below
|
|
393
|
+
x = T.m[x]
|
|
394
|
+
end
|
|
395
|
+
if not name_tok then
|
|
396
|
+
x = x + 1
|
|
397
|
+
else
|
|
398
|
+
local y = x + 1
|
|
399
|
+
if is(T, y, '?') or is(T, y, '!') then y = y + 1 end
|
|
400
|
+
y = skip_angle(T, y, c)
|
|
401
|
+
if is(T, y, '(') and T.m[y] then
|
|
402
|
+
-- method / constructor / signature
|
|
403
|
+
local body = body_after(T, T.m[y], c - 1)
|
|
404
|
+
local close = body and (T.m[body] or c - 1) or T.m[y]
|
|
405
|
+
if not body then
|
|
406
|
+
-- signature: runs to ';' / line end
|
|
407
|
+
close = expr_end(T, T.m[y], c - 1)
|
|
408
|
+
end
|
|
409
|
+
if name then
|
|
410
|
+
defs[name_tok] = true
|
|
411
|
+
local kind = (name == 'constructor') and 'constructor' or 'method'
|
|
412
|
+
local node = common.add_node(r, T, { kind = kind, name = name, parent = parent,
|
|
413
|
+
ti = start, tj = close, sig = common.sig(T, start, body and body - 1 or T.m[y], 200),
|
|
414
|
+
abstract = (not body) or nil })
|
|
415
|
+
if body then scan(T, r, body + 1, close - 1, node, defs, stem) end
|
|
416
|
+
end
|
|
417
|
+
x = close + 1
|
|
418
|
+
else
|
|
419
|
+
-- field: name?: Type = init
|
|
420
|
+
local stop = expr_end(T, x, c - 1)
|
|
421
|
+
local eq
|
|
422
|
+
local z = y
|
|
423
|
+
while z <= stop do
|
|
424
|
+
if is(T, z, '=') then eq = z; break end
|
|
425
|
+
if (is(T, z, '(') or is(T, z, '{') or is(T, z, '[')) and T.m[z] then z = T.m[z] end
|
|
426
|
+
z = z + 1
|
|
427
|
+
end
|
|
428
|
+
if name then
|
|
429
|
+
defs[name_tok] = true
|
|
430
|
+
-- (not `eq and func_expr(...)`: `and` would keep only the first result)
|
|
431
|
+
local fs, fe, fb
|
|
432
|
+
if eq then fs, fe, fb = func_expr(T, eq + 1, c - 1) end
|
|
433
|
+
if fs then
|
|
434
|
+
local node = common.add_node(r, T, { kind = 'method', name = name, parent = parent,
|
|
435
|
+
ti = start, tj = fe, sig = common.sig(T, start, math.max(fb - 1, name_tok), 200) })
|
|
436
|
+
if is(T, fb, '{') then scan(T, r, fb + 1, fe - 1, node, defs, stem) end
|
|
437
|
+
stop = math.max(stop, fe)
|
|
438
|
+
else
|
|
439
|
+
common.add_node(r, T, { kind = 'field', name = name, parent = parent,
|
|
440
|
+
ti = start, tj = stop, sig = common.sig(T, start, stop, 120) })
|
|
441
|
+
if eq then scan(T, r, eq + 1, stop, parent, defs, stem) end
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
x = stop + 1
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
end
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
function M.parse(path, src, language)
|
|
452
|
+
local T = M.lang:tokenize(src)
|
|
453
|
+
local r = common.new(path, language or 'javascript')
|
|
454
|
+
local defs = {}
|
|
455
|
+
local stem = path:match('([^/\\]+)%.[^./\\]+$') or path
|
|
456
|
+
scan(T, r, 1, T.n, nil, defs, stem)
|
|
457
|
+
common.add_calls(r, T, 1, T.n)
|
|
458
|
+
local kept = {}
|
|
459
|
+
for _, ref in ipairs(r.refs) do
|
|
460
|
+
if not defs[ref.tok] then kept[#kept + 1] = ref end
|
|
461
|
+
end
|
|
462
|
+
r.refs = kept
|
|
463
|
+
return common.finish(r), T
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
return M
|