@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,362 @@
|
|
|
1
|
+
-- lang/c.lua — index-level C parser: functions, prototypes, structs/unions/
|
|
2
|
+
-- enums (+ members), typedefs, globals, macros, #includes, and the calls and
|
|
3
|
+
-- callback references inside bodies and initializers.
|
|
4
|
+
--
|
|
5
|
+
-- Declarations are parsed; bodies are skipped with the bracket match table
|
|
6
|
+
-- and only scanned for call sites. The preprocessor is handled the way ctags
|
|
7
|
+
-- does it: keep the first live #if branch (xscan pp_first_branch), so both
|
|
8
|
+
-- arms of an #ifdef never meet in one token stream.
|
|
9
|
+
|
|
10
|
+
local xscan = require('codeoutline.xscan')
|
|
11
|
+
local common = require('codeoutline.common')
|
|
12
|
+
|
|
13
|
+
local M = {}
|
|
14
|
+
|
|
15
|
+
M.keywords = {
|
|
16
|
+
'auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do', 'double',
|
|
17
|
+
'else', 'enum', 'extern', 'float', 'for', 'goto', 'if', 'inline', 'int', 'long',
|
|
18
|
+
'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'struct',
|
|
19
|
+
'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while',
|
|
20
|
+
'_Bool', '_Complex', '_Atomic', '_Static_assert', '_Noreturn', '_Thread_local',
|
|
21
|
+
'_Alignas', '_Alignof', '__inline', '__inline__', '__restrict', '__volatile__',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
M.ops = {
|
|
25
|
+
'->', '++', '--', '<<=', '>>=', '<<', '>>', '<=', '>=', '==', '!=', '&&', '||',
|
|
26
|
+
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '...', '::', '##',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
M.config = {
|
|
30
|
+
keywords = M.keywords,
|
|
31
|
+
ops = M.ops,
|
|
32
|
+
line_comment = { '//' },
|
|
33
|
+
block_comment = { { '/*', '*/' } },
|
|
34
|
+
strings = { { '"', '"', escape = '\\' }, { "'", "'", escape = '\\' } },
|
|
35
|
+
string_prefixes = 'LuU8',
|
|
36
|
+
directive = '#',
|
|
37
|
+
pp_first_branch = true,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
M.lang = xscan.lang(M.config)
|
|
41
|
+
-- Macro bodies are re-tokenized without directive handling to find calls.
|
|
42
|
+
local body_cfg = {}
|
|
43
|
+
for k, v in pairs(M.config) do body_cfg[k] = v end
|
|
44
|
+
body_cfg.directive, body_cfg.pp_first_branch = nil, nil
|
|
45
|
+
local body_lang = xscan.lang(body_cfg)
|
|
46
|
+
|
|
47
|
+
-- Names that look like a call but decorate a declaration.
|
|
48
|
+
local ATTR = {
|
|
49
|
+
__attribute__ = true, __attribute = true, __declspec = true, __asm__ = true,
|
|
50
|
+
__asm = true, asm = true, alignas = true, _Alignas = true,
|
|
51
|
+
}
|
|
52
|
+
local AGGREGATE = { struct = true, union = true, enum = true }
|
|
53
|
+
|
|
54
|
+
local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
|
|
55
|
+
|
|
56
|
+
-- `int (name)(args)` — Lua's headers wrap API names in parens to block
|
|
57
|
+
-- macro expansion. Return the inner identifier's index for a `(id)` group
|
|
58
|
+
-- ending at p, otherwise p itself.
|
|
59
|
+
local function paren_name(T, p)
|
|
60
|
+
if is(T, p, ')') and T.m[p] == p - 2 and T.k[p - 1] == 'id' then return p - 1 end
|
|
61
|
+
return p
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
-- First token of a declaration whose name is at `x`: walk back over type
|
|
65
|
+
-- words and pointer stars, stopping at anything that ends a prior construct
|
|
66
|
+
-- (so a preceding `MACRO(args)` without ';' is not swallowed).
|
|
67
|
+
local function decl_start(T, a, x)
|
|
68
|
+
local y = x - 1
|
|
69
|
+
while y >= a do
|
|
70
|
+
local k = T.k[y]
|
|
71
|
+
if k == 'id' or k == 'kw' or (k == 'op' and (T:text(y) == '*' or T:text(y) == '&')) then
|
|
72
|
+
y = y - 1
|
|
73
|
+
elseif is(T, y, ')') and T.m[y] and T.m[y] >= a and ATTR[T:text(T.m[y] - 1)] then
|
|
74
|
+
y = T.m[y] - 2 -- __attribute__((...)) before the type
|
|
75
|
+
else
|
|
76
|
+
break
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
return y + 1
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
-- If the '{' at j opens a function body, return name index, '(' and ')'.
|
|
83
|
+
local function function_head(T, a, j)
|
|
84
|
+
local x = j - 1
|
|
85
|
+
while x >= a do
|
|
86
|
+
if is(T, x, ')') then
|
|
87
|
+
local o = T.m[x]
|
|
88
|
+
if not o or o <= a then return nil end
|
|
89
|
+
local p = paren_name(T, o - 1)
|
|
90
|
+
if T.k[p] == 'id' and ATTR[T:text(p)] then
|
|
91
|
+
x = p - 1 -- trailing __attribute__((...))
|
|
92
|
+
elseif T.k[p] == 'id' then
|
|
93
|
+
return p, o, x
|
|
94
|
+
else
|
|
95
|
+
return nil
|
|
96
|
+
end
|
|
97
|
+
elseif T.k[x] == 'id' or (T.k[x] == 'kw' and T:text(x) == 'const') then
|
|
98
|
+
x = x - 1 -- trailing macro, e.g. NOEXCEPT
|
|
99
|
+
else
|
|
100
|
+
return nil
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
return nil
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
-- When a body's '{' has no match (unbalanced preprocessor arms), end the
|
|
107
|
+
-- body at the next '}' in column 1 — the conventional function end.
|
|
108
|
+
local function fallback_close(T, j, b)
|
|
109
|
+
for y = j + 1, b do
|
|
110
|
+
if is(T, y, '}') and (T.s[y] == 1 or T.src:byte(T.s[y] - 1) == 10) then return y end
|
|
111
|
+
end
|
|
112
|
+
return b
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
local function handle_directive(T, r, i)
|
|
116
|
+
local text = T:text(i)
|
|
117
|
+
local inc = text:match('^#%s*include%s*[<"]([^>"]+)[>"]')
|
|
118
|
+
if inc then
|
|
119
|
+
r.imports[#r.imports + 1] = { path = inc, line = T:line(i), system = text:match('^#%s*include%s*<') ~= nil }
|
|
120
|
+
return
|
|
121
|
+
end
|
|
122
|
+
local name, rest = text:match('^#%s*define%s+([%a_][%w_]*)(.*)$')
|
|
123
|
+
if not name then return end
|
|
124
|
+
common.add_node(r, T, {
|
|
125
|
+
kind = 'macro', name = name, ti = i, tj = i,
|
|
126
|
+
sig = common.sig(T, i, i, 160),
|
|
127
|
+
})
|
|
128
|
+
-- Calls inside a function-like macro body belong to the macro.
|
|
129
|
+
if rest:sub(1, 1) == '(' then
|
|
130
|
+
local body = rest:gsub('\\\r?\n', ' \n')
|
|
131
|
+
local BT = body_lang:tokenize(body)
|
|
132
|
+
for _, x in ipairs(BT:calls(1, BT.n)) do
|
|
133
|
+
r.refs[#r.refs + 1] = {
|
|
134
|
+
tok = i, name = BT:text(x), kind = 'call', line = T:line(i) + BT:line(x) - 1,
|
|
135
|
+
}
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
-- Enum members and struct/union fields directly inside '{' o .. '}' c.
|
|
141
|
+
local function members(T, r, o, c, parent, is_enum)
|
|
142
|
+
local x = o + 1
|
|
143
|
+
while x < c do
|
|
144
|
+
-- one member runs to ',' (enum) or ';' (struct) at this depth
|
|
145
|
+
local y = x
|
|
146
|
+
while y < c and not is(T, y, is_enum and ',' or ';') do
|
|
147
|
+
y = (T.m[y] and T.m[y] > y) and T.m[y] + 1 or y + 1
|
|
148
|
+
end
|
|
149
|
+
if is_enum then
|
|
150
|
+
if T.k[x] == 'id' then
|
|
151
|
+
common.add_node(r, T, { kind = 'enum_member', name = T:text(x), parent = parent, ti = x, tj = math.max(x, y - 1) })
|
|
152
|
+
end
|
|
153
|
+
else
|
|
154
|
+
-- field names: identifiers followed by ';' ',' '[' ':' at this depth
|
|
155
|
+
for z = x, y - 1 do
|
|
156
|
+
if T.k[z] == 'id' and T.k[z + 1] == 'op' then
|
|
157
|
+
local nx = T:text(z + 1)
|
|
158
|
+
if (nx == ';' or nx == ',' or nx == '[' or nx == ':') or z + 1 == y then
|
|
159
|
+
common.add_node(r, T, { kind = 'field', name = T:text(z), parent = parent, ti = z, tj = z })
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
if is(T, z, '(') and is(T, z + 1, '*') and T.k[z + 2] == 'id' then
|
|
163
|
+
common.add_node(r, T, { kind = 'field', name = T:text(z + 2), parent = parent, ti = z + 2, tj = z + 2 })
|
|
164
|
+
end
|
|
165
|
+
if T.m[z] and T.m[z] > z then break end -- nested struct / fn-pointer: skip
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
x = y + 1
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
local parse_range
|
|
173
|
+
|
|
174
|
+
-- Handle one statement in [i, j] where j is ';' (or the last token).
|
|
175
|
+
local function declaration(T, r, i, j, parent)
|
|
176
|
+
local first = T:text(i)
|
|
177
|
+
if first == 'typedef' then
|
|
178
|
+
-- alias = last top-level identifier; function-pointer typedefs keep
|
|
179
|
+
-- it inside the first paren group: typedef int (*name)(...);
|
|
180
|
+
local name_tok
|
|
181
|
+
local y = i + 1
|
|
182
|
+
while y < j do
|
|
183
|
+
if T.k[y] == 'id' then name_tok = y end
|
|
184
|
+
if T.m[y] and T.m[y] > y then
|
|
185
|
+
if not name_tok or is(T, y, '(') then
|
|
186
|
+
for z = y + 1, T.m[y] - 1 do
|
|
187
|
+
if T.k[z] == 'id' then name_tok = z; break end
|
|
188
|
+
end
|
|
189
|
+
if is(T, y, '(') then break end
|
|
190
|
+
end
|
|
191
|
+
y = T.m[y] + 1
|
|
192
|
+
else
|
|
193
|
+
y = y + 1
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
if name_tok then
|
|
197
|
+
common.add_node(r, T, { kind = 'typedef', name = T:text(name_tok), parent = parent, ti = i, tj = j, sig = common.sig(T, i, j) })
|
|
198
|
+
end
|
|
199
|
+
return
|
|
200
|
+
end
|
|
201
|
+
local is_extern = first == 'extern' and T.k[i + 1] ~= 'str'
|
|
202
|
+
-- Walk the top level of the statement.
|
|
203
|
+
local y, eq = i, nil
|
|
204
|
+
local names = {}
|
|
205
|
+
while y < j do
|
|
206
|
+
if is(T, y, '=') and not eq then eq = y end
|
|
207
|
+
if is(T, y, '(') and T.m[y] then
|
|
208
|
+
local p = paren_name(T, y - 1)
|
|
209
|
+
if not eq and p >= i and T.k[p] == 'id' and not ATTR[T:text(p)] then
|
|
210
|
+
if p == i then
|
|
211
|
+
-- MACRO(args); at file scope: a call, not a declaration
|
|
212
|
+
common.add_calls(r, T, i, j)
|
|
213
|
+
return
|
|
214
|
+
end
|
|
215
|
+
common.add_node(r, T, {
|
|
216
|
+
kind = 'prototype', name = T:text(p), parent = parent,
|
|
217
|
+
ti = decl_start(T, i, p), tj = j, sig = common.sig(T, decl_start(T, i, p), T.m[y]),
|
|
218
|
+
static = first == 'static' or nil,
|
|
219
|
+
})
|
|
220
|
+
return
|
|
221
|
+
end
|
|
222
|
+
y = T.m[y] + 1
|
|
223
|
+
elseif (is(T, y, '[') or is(T, y, '{')) and T.m[y] then
|
|
224
|
+
y = T.m[y] + 1
|
|
225
|
+
else
|
|
226
|
+
if T.k[y] == 'id' and not eq and T.k[y + 1] == 'op' then
|
|
227
|
+
local nx = T:text(y + 1)
|
|
228
|
+
if nx == '=' or nx == ';' or nx == ',' or nx == '[' then names[#names + 1] = y end
|
|
229
|
+
end
|
|
230
|
+
if is(T, y, ',') then eq = nil end
|
|
231
|
+
y = y + 1
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
if is_extern then names = {} end -- extern int x; is a declaration of a def elsewhere
|
|
235
|
+
for _, x in ipairs(names) do
|
|
236
|
+
common.add_node(r, T, {
|
|
237
|
+
kind = 'variable', name = T:text(x), parent = parent, ti = x, tj = j,
|
|
238
|
+
static = first == 'static' or nil, sig = common.sig(T, i, math.min(j, x + 1)),
|
|
239
|
+
})
|
|
240
|
+
end
|
|
241
|
+
common.add_calls(r, T, i, j)
|
|
242
|
+
common.add_value_refs(r, T, i, j)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
-- Parse the statements in token range [a, b].
|
|
246
|
+
parse_range = function(T, r, a, b, parent)
|
|
247
|
+
local i = a
|
|
248
|
+
while i <= b do
|
|
249
|
+
local k = T.k[i]
|
|
250
|
+
if k == 'dir' then
|
|
251
|
+
handle_directive(T, r, i)
|
|
252
|
+
i = i + 1
|
|
253
|
+
elseif k == 'op' and (T:text(i) == ';' or T:text(i) == '}') then
|
|
254
|
+
i = i + 1
|
|
255
|
+
else
|
|
256
|
+
local j, done = i, false
|
|
257
|
+
while j <= b and not done do
|
|
258
|
+
local kj = T.k[j]
|
|
259
|
+
if kj == 'dir' then
|
|
260
|
+
declaration(T, r, i, j - 1, parent)
|
|
261
|
+
j = j - 1
|
|
262
|
+
done = true
|
|
263
|
+
elseif is(T, j, ';') then
|
|
264
|
+
declaration(T, r, i, j, parent)
|
|
265
|
+
done = true
|
|
266
|
+
elseif is(T, j, '{') then
|
|
267
|
+
local name, o, c = function_head(T, i, j)
|
|
268
|
+
local close = T.m[j] or fallback_close(T, j, b)
|
|
269
|
+
if name then
|
|
270
|
+
local start = decl_start(T, i, name)
|
|
271
|
+
if start > i then common.add_calls(r, T, i, start - 1) end -- MACRO(...) before it
|
|
272
|
+
common.add_node(r, T, {
|
|
273
|
+
kind = 'function', name = T:text(name), parent = parent,
|
|
274
|
+
ti = start, tj = close, sig = common.sig(T, start, c),
|
|
275
|
+
static = T:text(start) == 'static' or nil,
|
|
276
|
+
})
|
|
277
|
+
common.add_calls(r, T, j, close)
|
|
278
|
+
common.add_value_refs(r, T, j, close)
|
|
279
|
+
j = close
|
|
280
|
+
done = true
|
|
281
|
+
elseif T.k[j - 1] == 'str' and T:text(j - 2) == 'extern' then
|
|
282
|
+
-- extern "C" { ... }: transparent
|
|
283
|
+
parse_range(T, r, j + 1, close - 1, parent)
|
|
284
|
+
j = close
|
|
285
|
+
done = true
|
|
286
|
+
else
|
|
287
|
+
-- aggregate definition or brace initializer
|
|
288
|
+
local agg
|
|
289
|
+
for y = i, j - 1 do
|
|
290
|
+
if T.k[y] == 'kw' and AGGREGATE[T:text(y)] then agg = y; break end
|
|
291
|
+
end
|
|
292
|
+
if agg and not (function() for y = i, agg do if is(T, y, '=') then return true end end end)() then
|
|
293
|
+
local kind = T:text(agg)
|
|
294
|
+
local name_tok = (T.k[agg + 1] == 'id' and agg + 1 < j) and agg + 1 or nil
|
|
295
|
+
-- typedef struct {..} Alias; names the struct after its alias
|
|
296
|
+
local stop = close + 1
|
|
297
|
+
while stop <= b and not is(T, stop, ';') and T.k[stop] ~= 'dir' do
|
|
298
|
+
stop = (T.m[stop] and T.m[stop] > stop) and T.m[stop] + 1 or stop + 1
|
|
299
|
+
end
|
|
300
|
+
local alias
|
|
301
|
+
if T:text(i) == 'typedef' then
|
|
302
|
+
for y = close + 1, stop - 1 do
|
|
303
|
+
if T.k[y] == 'id' then alias = y; break end
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
local nm = name_tok or alias
|
|
307
|
+
local node
|
|
308
|
+
if nm then
|
|
309
|
+
node = common.add_node(r, T, {
|
|
310
|
+
kind = kind, name = T:text(nm), parent = parent,
|
|
311
|
+
ti = i, tj = close, sig = common.sig(T, i, j - 1),
|
|
312
|
+
})
|
|
313
|
+
members(T, r, j, close, node, kind == 'enum')
|
|
314
|
+
elseif kind == 'enum' then
|
|
315
|
+
members(T, r, j, close, parent, true) -- anonymous enum constants
|
|
316
|
+
end
|
|
317
|
+
if alias and alias ~= nm then
|
|
318
|
+
common.add_node(r, T, { kind = 'typedef', name = T:text(alias), parent = parent, ti = alias, tj = alias })
|
|
319
|
+
elseif not alias and stop > close + 1 then
|
|
320
|
+
-- struct X {..} var; → globals after the body
|
|
321
|
+
declaration(T, r, close + 1, math.min(stop, b), parent)
|
|
322
|
+
end
|
|
323
|
+
j = math.min(stop, b)
|
|
324
|
+
done = true
|
|
325
|
+
else
|
|
326
|
+
j = close + 1 -- initializer braces: keep scanning to ';'
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
elseif (is(T, j, '(') or is(T, j, '[')) and T.m[j] then
|
|
330
|
+
j = T.m[j] + 1
|
|
331
|
+
else
|
|
332
|
+
j = j + 1
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
if not done then
|
|
336
|
+
declaration(T, r, i, math.min(j, b), parent)
|
|
337
|
+
end
|
|
338
|
+
i = math.max(j, i) + 1
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
function M.parse(path, src)
|
|
344
|
+
local T = M.lang:tokenize(src)
|
|
345
|
+
local r = common.new(path, 'c')
|
|
346
|
+
r.skip_calls = ATTR
|
|
347
|
+
parse_range(T, r, 1, T.n, nil)
|
|
348
|
+
return common.finish(r), T
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
-- Exposed for the C++ parser (lang/cpp.lua), which reuses these pieces.
|
|
352
|
+
M.parse_range = parse_range
|
|
353
|
+
M.function_head = function_head
|
|
354
|
+
M.decl_start = decl_start
|
|
355
|
+
M.declaration = declaration
|
|
356
|
+
M.members = members
|
|
357
|
+
M.handle_directive = handle_directive
|
|
358
|
+
M.fallback_close = fallback_close
|
|
359
|
+
M.paren_name = paren_name
|
|
360
|
+
M.ATTR = ATTR
|
|
361
|
+
|
|
362
|
+
return M
|