@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,334 @@
|
|
|
1
|
+
-- lang/csharp.lua — index-level C# parser: namespaces (block and
|
|
2
|
+
-- file-scoped), usings, classes / structs / interfaces / records / enums
|
|
3
|
+
-- (+ bases), methods, constructors (with `: base(...)`), destructors,
|
|
4
|
+
-- operators, properties (accessor and expression-bodied), fields, events,
|
|
5
|
+
-- delegates, enum members, attributes, and calls.
|
|
6
|
+
|
|
7
|
+
local xscan = require('codeoutline.xscan')
|
|
8
|
+
local common = require('codeoutline.common')
|
|
9
|
+
|
|
10
|
+
local M = {}
|
|
11
|
+
|
|
12
|
+
-- Reserved keywords only; contextual ones (get, set, value, var, record,
|
|
13
|
+
-- partial, async, await, where, init, global, nameof...) stay identifiers.
|
|
14
|
+
M.config = {
|
|
15
|
+
keywords = {
|
|
16
|
+
'abstract', 'as', 'base', 'bool', 'break', 'byte', 'case', 'catch', 'char', 'checked',
|
|
17
|
+
'class', 'const', 'continue', 'decimal', 'default', 'delegate', 'do', 'double', 'else',
|
|
18
|
+
'enum', 'event', 'explicit', 'extern', 'false', 'finally', 'fixed', 'float', 'for',
|
|
19
|
+
'foreach', 'goto', 'if', 'implicit', 'in', 'int', 'interface', 'internal', 'is', 'lock',
|
|
20
|
+
'long', 'namespace', 'new', 'null', 'object', 'operator', 'out', 'override', 'params',
|
|
21
|
+
'private', 'protected', 'public', 'readonly', 'ref', 'return', 'sbyte', 'sealed', 'short',
|
|
22
|
+
'sizeof', 'stackalloc', 'static', 'string', 'struct', 'switch', 'this', 'throw', 'true',
|
|
23
|
+
'try', 'typeof', 'uint', 'ulong', 'unchecked', 'unsafe', 'ushort', 'using', 'virtual',
|
|
24
|
+
'void', 'volatile', 'while',
|
|
25
|
+
},
|
|
26
|
+
ops = {
|
|
27
|
+
'??=', '<<=', '>>=', '>>>', '=>', '::', '?.', '??', '++', '--', '&&', '||', '==', '!=',
|
|
28
|
+
'<=', '>=', '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<', '>>',
|
|
29
|
+
},
|
|
30
|
+
ident_start = 'A-Za-z_@',
|
|
31
|
+
ident_char = 'A-Za-z0-9_',
|
|
32
|
+
line_comment = { '//' },
|
|
33
|
+
block_comment = { { '/*', '*/' } },
|
|
34
|
+
strings = {
|
|
35
|
+
{ '"""', '"""', multiline = true }, -- raw string literal
|
|
36
|
+
{ '$"""', '"""', multiline = true },
|
|
37
|
+
{ '$@"', '"', multiline = true }, -- verbatim: "" escapes a quote
|
|
38
|
+
{ '@$"', '"', multiline = true },
|
|
39
|
+
{ '@"', '"', multiline = true },
|
|
40
|
+
{ '$"', '"', escape = '\\' },
|
|
41
|
+
{ '"', '"', escape = '\\' },
|
|
42
|
+
{ "'", "'", escape = '\\' },
|
|
43
|
+
},
|
|
44
|
+
directive = '#',
|
|
45
|
+
pp_first_branch = true,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
M.lang = xscan.lang(M.config)
|
|
49
|
+
|
|
50
|
+
local TYPE_KW = { class = 'class', struct = 'struct', interface = 'interface', enum = 'enum' }
|
|
51
|
+
|
|
52
|
+
local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
|
|
53
|
+
local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
|
|
54
|
+
|
|
55
|
+
local function angle(T, i)
|
|
56
|
+
if T.k[i] ~= 'op' then return 0 end
|
|
57
|
+
local t = T:text(i)
|
|
58
|
+
if t == '<' then return 1 elseif t == '>' then return -1 elseif t == '>>' then return -2
|
|
59
|
+
elseif t == '>>>' then return -3 end
|
|
60
|
+
return 0
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
-- Dotted name at x: "A.B.C", next index.
|
|
64
|
+
local function dotted(T, x)
|
|
65
|
+
local parts = {}
|
|
66
|
+
while T.k[x] == 'id' do
|
|
67
|
+
parts[#parts + 1] = T:text(x)
|
|
68
|
+
if is(T, x + 1, '.') and T.k[x + 2] == 'id' then x = x + 2 else x = x + 1; break end
|
|
69
|
+
end
|
|
70
|
+
return table.concat(parts, '.'), x
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
-- First parameter list of the member [i, j): the '(' at angle depth 0 whose
|
|
74
|
+
-- previous token is the member name (`Name(`, `Name<T>(`, `operator +(`).
|
|
75
|
+
-- Returns name, name token, '(' index — or nil.
|
|
76
|
+
local function member_head(T, i, j)
|
|
77
|
+
local d = 0
|
|
78
|
+
local x = i
|
|
79
|
+
while x < j do
|
|
80
|
+
if is(T, x, '(') and d == 0 then
|
|
81
|
+
local p = x - 1
|
|
82
|
+
if T.k[p] == 'op' and kw(T, p - 1, 'operator') then return 'operator' .. T:text(p), p, x end
|
|
83
|
+
if is(T, p, '>') or is(T, p, '>>') then
|
|
84
|
+
local dd = 0
|
|
85
|
+
while p > i do
|
|
86
|
+
dd = dd - angle(T, p)
|
|
87
|
+
if dd <= 0 then break end
|
|
88
|
+
p = p - 1
|
|
89
|
+
end
|
|
90
|
+
p = p - 1
|
|
91
|
+
end
|
|
92
|
+
if T.k[p] == 'id' then
|
|
93
|
+
if is(T, p - 1, '~') then return '~' .. T:text(p), p, x end
|
|
94
|
+
if kw(T, p - 1, 'operator') then return 'operator ' .. T:text(p), p, x end -- conversion
|
|
95
|
+
return T:text(p), p, x
|
|
96
|
+
end
|
|
97
|
+
if T.k[p] == 'op' and kw(T, p - 1, 'operator') then return 'operator' .. T:text(p), p, x end
|
|
98
|
+
if T.k[p] == 'kw' and kw(T, p - 1, 'operator') then return 'operator ' .. T:text(p), p, x end
|
|
99
|
+
-- not after a name: a tuple type such as `(int, string) Name(...)`
|
|
100
|
+
if T.m[x] then x = T.m[x] end
|
|
101
|
+
elseif (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
|
|
102
|
+
x = T.m[x]
|
|
103
|
+
elseif is(T, x, '=') or is(T, x, '=>') then
|
|
104
|
+
return nil
|
|
105
|
+
elseif not kw(T, x - 1, 'operator') then
|
|
106
|
+
d = math.max(0, d + angle(T, x)) -- `operator <(...)` is not a generic list
|
|
107
|
+
end
|
|
108
|
+
x = x + 1
|
|
109
|
+
end
|
|
110
|
+
return nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
local parse_members
|
|
114
|
+
|
|
115
|
+
-- Base list between ':' and '{' (generic arguments and `where` dropped).
|
|
116
|
+
local function bases(T, a, b)
|
|
117
|
+
local out, d, x = {}, 0, a
|
|
118
|
+
while x < b do
|
|
119
|
+
if T.k[x] == 'id' and T:text(x) == 'where' and d == 0 then break end
|
|
120
|
+
d = d + angle(T, x)
|
|
121
|
+
if d == 0 and T.k[x] == 'id' then
|
|
122
|
+
local nm, nx = dotted(T, x)
|
|
123
|
+
out[#out + 1] = nm
|
|
124
|
+
x = nx
|
|
125
|
+
else
|
|
126
|
+
x = x + 1
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
return out
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
local function type_decl(T, r, i, kwi, o, parent, kind)
|
|
133
|
+
local name_tok = kwi + 1
|
|
134
|
+
if kind == 'record' and (kw(T, name_tok, 'class') or kw(T, name_tok, 'struct')) then name_tok = name_tok + 1 end
|
|
135
|
+
local bodyless = is(T, o, ';')
|
|
136
|
+
local close = bodyless and o or (T.m[o] or T.n)
|
|
137
|
+
local node = common.add_node(r, T, { kind = kind, name = T:text(name_tok), parent = parent,
|
|
138
|
+
ti = i, tj = close, sig = common.sig(T, i, o - 1, 200) })
|
|
139
|
+
-- `: Base, IFace` after the name, generic parameters and record params
|
|
140
|
+
local x = name_tok + 1
|
|
141
|
+
local d = 0
|
|
142
|
+
while x < o do
|
|
143
|
+
d = d + angle(T, x)
|
|
144
|
+
if d == 0 and is(T, x, ':') then
|
|
145
|
+
r.nodes[node].bases = bases(T, x + 1, o)
|
|
146
|
+
break
|
|
147
|
+
end
|
|
148
|
+
if is(T, x, '(') and T.m[x] then
|
|
149
|
+
if kind == 'record' then -- positional record parameters -> properties
|
|
150
|
+
for z = x + 1, T.m[x] - 1 do
|
|
151
|
+
if T.k[z] == 'id' and (is(T, z + 1, ',') or z + 1 == T.m[x] or is(T, z + 1, '=')) then
|
|
152
|
+
common.add_node(r, T, { kind = 'property', name = T:text(z), parent = node, ti = z, tj = z })
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
x = T.m[x]
|
|
157
|
+
end
|
|
158
|
+
x = x + 1
|
|
159
|
+
end
|
|
160
|
+
if bodyless then
|
|
161
|
+
return close
|
|
162
|
+
elseif kind == 'enum' then
|
|
163
|
+
local y = o + 1
|
|
164
|
+
while y < close do
|
|
165
|
+
while is(T, y, '[') and T.m[y] do y = T.m[y] + 1 end
|
|
166
|
+
if T.k[y] == 'id' then
|
|
167
|
+
common.add_node(r, T, { kind = 'enum_member', name = T:text(y), parent = node, ti = y, tj = y })
|
|
168
|
+
end
|
|
169
|
+
while y < close and not is(T, y, ',') do y = (T.m[y] and T.m[y] > y) and T.m[y] + 1 or y + 1 end
|
|
170
|
+
y = y + 1
|
|
171
|
+
end
|
|
172
|
+
else
|
|
173
|
+
parse_members(T, r, o + 1, close - 1, node, T:text(name_tok))
|
|
174
|
+
end
|
|
175
|
+
return close
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
-- Declarations in token range [a, b].
|
|
179
|
+
parse_members = function(T, r, a, b, parent, class_name)
|
|
180
|
+
local i = a
|
|
181
|
+
while i <= b do
|
|
182
|
+
local k, t = T.k[i], T:text(i)
|
|
183
|
+
if k == 'dir' then
|
|
184
|
+
i = i + 1
|
|
185
|
+
elseif is(T, i, ';') then
|
|
186
|
+
i = i + 1
|
|
187
|
+
elseif is(T, i, '[') and T.m[i] then
|
|
188
|
+
i = T.m[i] + 1 -- [Attribute]
|
|
189
|
+
elseif (k == 'kw' and t == 'using') or (k == 'id' and t == 'global' and kw(T, i + 1, 'using')) then
|
|
190
|
+
local j = i
|
|
191
|
+
while j <= b and not is(T, j, ';') do j = j + 1 end
|
|
192
|
+
local x = kw(T, i, 'using') and i + 1 or i + 2
|
|
193
|
+
if kw(T, x, 'static') then x = x + 1 end
|
|
194
|
+
if T.k[x] == 'id' and is(T, x + 1, '=') then x = x + 2 end -- alias
|
|
195
|
+
local path = dotted(T, x)
|
|
196
|
+
if path ~= '' then r.imports[#r.imports + 1] = { path = path, line = T:line(i) } end
|
|
197
|
+
i = j + 1
|
|
198
|
+
elseif k == 'kw' and t == 'namespace' then
|
|
199
|
+
local name, x = dotted(T, i + 1)
|
|
200
|
+
if is(T, x, '{') and T.m[x] then
|
|
201
|
+
local node = common.add_node(r, T, { kind = 'namespace', name = name, parent = parent, ti = i, tj = T.m[x] })
|
|
202
|
+
parse_members(T, r, x + 1, T.m[x] - 1, node, nil)
|
|
203
|
+
i = T.m[x] + 1
|
|
204
|
+
else
|
|
205
|
+
-- file-scoped: namespace A.B; applies to the rest of the file
|
|
206
|
+
local node = common.add_node(r, T, { kind = 'namespace', name = name, parent = parent, ti = i, tj = b })
|
|
207
|
+
parse_members(T, r, x + 1, b, node, nil)
|
|
208
|
+
i = b + 1
|
|
209
|
+
end
|
|
210
|
+
else
|
|
211
|
+
-- one member: to ';' or its '{'-body
|
|
212
|
+
local j, eq, done = i, nil, false
|
|
213
|
+
local d = 0
|
|
214
|
+
while j <= b and not done do
|
|
215
|
+
if (is(T, j, '=') or is(T, j, '=>')) and not eq and d == 0 and not kw(T, j - 1, 'operator') then eq = j end
|
|
216
|
+
local rec_kw
|
|
217
|
+
if is(T, j, ';') then
|
|
218
|
+
for y = i, j - 1 do
|
|
219
|
+
if T.k[y] == 'id' and T:text(y) == 'record' and (T.k[y + 1] == 'id'
|
|
220
|
+
or kw(T, y + 1, 'class') or kw(T, y + 1, 'struct')) then rec_kw = y; break end
|
|
221
|
+
if is(T, y, '(') or is(T, y, '=') then break end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
if is(T, j, ';') and rec_kw then
|
|
225
|
+
-- positional record without a body: record Point(int X, int Y);
|
|
226
|
+
type_decl(T, r, i, rec_kw, j, parent, 'record')
|
|
227
|
+
done = true
|
|
228
|
+
elseif is(T, j, ';') then
|
|
229
|
+
-- declaration without a '{' body
|
|
230
|
+
local is_delegate = false
|
|
231
|
+
for y = i, j do if kw(T, y, 'delegate') then is_delegate = true; break end end
|
|
232
|
+
local name, nt, o = member_head(T, i, j)
|
|
233
|
+
if name and (not eq or eq > o) then
|
|
234
|
+
local kind = is_delegate and 'typedef' or (name == class_name and 'constructor')
|
|
235
|
+
or (name:sub(1, 1) == '~' and 'destructor') or 'method'
|
|
236
|
+
common.add_node(r, T, { kind = kind, name = name, parent = parent, ti = i, tj = j,
|
|
237
|
+
sig = common.sig(T, i, T.m[o] or o, 200), abstract = (not eq) or nil })
|
|
238
|
+
common.add_calls(r, T, (T.m[o] or o) + 1, j)
|
|
239
|
+
elseif eq and is(T, eq, '=>') and T.k[eq - 1] == 'id' then
|
|
240
|
+
-- expression-bodied property: int X => expr;
|
|
241
|
+
common.add_node(r, T, { kind = 'property', name = T:text(eq - 1), parent = parent,
|
|
242
|
+
ti = i, tj = j, sig = common.sig(T, i, eq - 1, 160) })
|
|
243
|
+
common.add_calls(r, T, eq, j)
|
|
244
|
+
else
|
|
245
|
+
-- fields / events: names at angle depth 0 followed by = , ;
|
|
246
|
+
local dd, init, y = 0, false, i
|
|
247
|
+
while y < j do
|
|
248
|
+
dd = dd + angle(T, y)
|
|
249
|
+
if is(T, y, '=') then init = true
|
|
250
|
+
elseif is(T, y, ',') and dd == 0 then init = false
|
|
251
|
+
elseif not init and dd == 0 and T.k[y] == 'id' and T.k[y + 1] == 'op' then
|
|
252
|
+
local nx = T:text(y + 1)
|
|
253
|
+
if nx == '=' or nx == ';' or nx == ',' then
|
|
254
|
+
common.add_node(r, T, { kind = 'field', name = T:text(y), parent = parent,
|
|
255
|
+
ti = y, tj = j, sig = common.sig(T, i, math.min(j - 1, y + 1), 160) })
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
if init and T.m[y] and T.m[y] > y and T.k[y] == 'op' then y = T.m[y] end
|
|
259
|
+
y = y + 1
|
|
260
|
+
end
|
|
261
|
+
common.add_calls(r, T, i, j)
|
|
262
|
+
end
|
|
263
|
+
done = true
|
|
264
|
+
elseif is(T, j, '{') then
|
|
265
|
+
local close = T.m[j] or b
|
|
266
|
+
local kwi, kind
|
|
267
|
+
if not eq then
|
|
268
|
+
for y = i, j - 1 do
|
|
269
|
+
local ty = T:text(y)
|
|
270
|
+
if T.k[y] == 'kw' and TYPE_KW[ty] and not kw(T, y - 1, 'record') then
|
|
271
|
+
kwi, kind = y, TYPE_KW[ty]; break
|
|
272
|
+
elseif T.k[y] == 'id' and ty == 'record' and (T.k[y + 1] == 'id' or kw(T, y + 1, 'class')
|
|
273
|
+
or kw(T, y + 1, 'struct')) then
|
|
274
|
+
kwi, kind = y, 'record'; break
|
|
275
|
+
elseif is(T, y, '(') and T.m[y] then
|
|
276
|
+
break -- a parameter list comes first: a method
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
if kwi then
|
|
281
|
+
type_decl(T, r, i, kwi, j, parent, kind)
|
|
282
|
+
j = close
|
|
283
|
+
done = true
|
|
284
|
+
elseif eq then
|
|
285
|
+
j = close -- initializer { ... }: continue to ';'
|
|
286
|
+
else
|
|
287
|
+
local name, nt, o = member_head(T, i, j)
|
|
288
|
+
if name and o < j then
|
|
289
|
+
local kind = (name == class_name and 'constructor')
|
|
290
|
+
or (name:sub(1, 1) == '~' and 'destructor') or 'method'
|
|
291
|
+
common.add_node(r, T, { kind = kind, name = name, parent = parent, ti = i, tj = close,
|
|
292
|
+
sig = common.sig(T, i, T.m[o] or o, 200) })
|
|
293
|
+
common.add_calls(r, T, (T.m[o] or o) + 1, close)
|
|
294
|
+
j = close
|
|
295
|
+
done = true
|
|
296
|
+
elseif T.k[j - 1] == 'id' then
|
|
297
|
+
-- property with accessors: Type Name { get; set; } [= init;]
|
|
298
|
+
common.add_node(r, T, { kind = 'property', name = T:text(j - 1), parent = parent,
|
|
299
|
+
ti = i, tj = close, sig = common.sig(T, i, j - 1, 160) })
|
|
300
|
+
common.add_calls(r, T, j, close)
|
|
301
|
+
if is(T, close + 1, '=') then
|
|
302
|
+
local y = close + 1
|
|
303
|
+
while y <= b and not is(T, y, ';') do y = (T.m[y] and T.m[y] > y) and T.m[y] + 1 or y + 1 end
|
|
304
|
+
common.add_calls(r, T, close + 1, y)
|
|
305
|
+
close = y
|
|
306
|
+
end
|
|
307
|
+
j = close
|
|
308
|
+
done = true
|
|
309
|
+
else
|
|
310
|
+
common.add_calls(r, T, i, close) -- indexer / static ctor-like blocks
|
|
311
|
+
j = close
|
|
312
|
+
done = true
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
elseif (is(T, j, '(') or is(T, j, '[')) and T.m[j] then
|
|
316
|
+
j = T.m[j]
|
|
317
|
+
else
|
|
318
|
+
d = math.max(0, d + angle(T, j))
|
|
319
|
+
end
|
|
320
|
+
if not done then j = j + 1 end
|
|
321
|
+
end
|
|
322
|
+
i = j + 1
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
function M.parse(path, src)
|
|
328
|
+
local T = M.lang:tokenize(src)
|
|
329
|
+
local r = common.new(path, 'csharp')
|
|
330
|
+
parse_members(T, r, 1, T.n, nil, nil)
|
|
331
|
+
return common.finish(r), T
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
return M
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
-- lang/go.lua — index-level Go parser: package, imports, functions, methods
|
|
2
|
+
-- (receiver type as the qualifier), generics, struct types (+ fields,
|
|
3
|
+
-- embedded types), interfaces (+ method sets), type aliases/definitions,
|
|
4
|
+
-- top-level var/const, and calls.
|
|
5
|
+
--
|
|
6
|
+
-- Go statements end at newlines, so specs and members are split with
|
|
7
|
+
-- stmt_end(): a statement runs to the end of its line, extended across any
|
|
8
|
+
-- bracket group that spans lines.
|
|
9
|
+
|
|
10
|
+
local xscan = require('codeoutline.xscan')
|
|
11
|
+
local common = require('codeoutline.common')
|
|
12
|
+
|
|
13
|
+
local M = {}
|
|
14
|
+
|
|
15
|
+
M.config = {
|
|
16
|
+
keywords = {
|
|
17
|
+
'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', 'fallthrough',
|
|
18
|
+
'for', 'func', 'go', 'goto', 'if', 'import', 'interface', 'map', 'package', 'range',
|
|
19
|
+
'return', 'select', 'struct', 'switch', 'type', 'var',
|
|
20
|
+
},
|
|
21
|
+
ops = {
|
|
22
|
+
'<-', ':=', '...', '&&', '||', '==', '!=', '<=', '>=', '<<', '>>', '&^', '++', '--',
|
|
23
|
+
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=', '&^=',
|
|
24
|
+
},
|
|
25
|
+
line_comment = { '//' },
|
|
26
|
+
block_comment = { { '/*', '*/' } },
|
|
27
|
+
strings = {
|
|
28
|
+
{ '"', '"', escape = '\\' },
|
|
29
|
+
{ "'", "'", escape = '\\' },
|
|
30
|
+
{ '`', '`', multiline = true },
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
M.lang = xscan.lang(M.config)
|
|
35
|
+
|
|
36
|
+
local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
|
|
37
|
+
local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
|
|
38
|
+
|
|
39
|
+
-- Last token of the statement starting at x (bounded by b).
|
|
40
|
+
local function stmt_end(T, x, b)
|
|
41
|
+
local y = x
|
|
42
|
+
while true do
|
|
43
|
+
if T.m[y] and T.m[y] > y and T.m[y] <= b then y = T.m[y] end
|
|
44
|
+
if y + 1 <= b and T.l[y + 1] == T.el[y] and not is(T, y + 1, ';') then
|
|
45
|
+
y = y + 1
|
|
46
|
+
else
|
|
47
|
+
return y
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
-- Body '{' of a func whose parameter list closes at p, or nil when the
|
|
53
|
+
-- declaration has none. `struct{..}` / `interface{..}` in the result type
|
|
54
|
+
-- are type literals, not the body.
|
|
55
|
+
local function func_body(T, p, b)
|
|
56
|
+
local x = p + 1
|
|
57
|
+
while x <= b do
|
|
58
|
+
if is(T, x, '{') then
|
|
59
|
+
if not (kw(T, x - 1, 'struct') or kw(T, x - 1, 'interface')) then return x end
|
|
60
|
+
x = (T.m[x] or x) + 1
|
|
61
|
+
elseif (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
|
|
62
|
+
x = T.m[x] + 1
|
|
63
|
+
elseif T.l[x] ~= T.el[x - 1] or is(T, x, ';') then
|
|
64
|
+
return nil -- result type ended at the line break
|
|
65
|
+
else
|
|
66
|
+
x = x + 1
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
return nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
-- Receiver `(r *T)` / `(T[K])` -> type name.
|
|
73
|
+
local function receiver_type(T, o, c)
|
|
74
|
+
local name
|
|
75
|
+
for x = o + 1, c - 1 do
|
|
76
|
+
if is(T, x, '[') then break end
|
|
77
|
+
if T.k[x] == 'id' then name = T:text(x) end
|
|
78
|
+
end
|
|
79
|
+
return name
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
-- Struct fields / interface methods between '{' o and '}' c.
|
|
83
|
+
local function type_members(T, r, o, c, parent, is_interface)
|
|
84
|
+
local x = o + 1
|
|
85
|
+
while x < c do
|
|
86
|
+
if is(T, x, ';') then
|
|
87
|
+
x = x + 1
|
|
88
|
+
else
|
|
89
|
+
local first, last = x, stmt_end(T, x, c - 1)
|
|
90
|
+
if T.k[first] == 'id' then
|
|
91
|
+
if is_interface then
|
|
92
|
+
if is(T, first + 1, '(') then
|
|
93
|
+
common.add_node(r, T, { kind = 'method', name = T:text(first), parent = parent,
|
|
94
|
+
ti = first, tj = last, sig = common.sig(T, first, last), abstract = true })
|
|
95
|
+
end
|
|
96
|
+
else
|
|
97
|
+
-- `A, B Type`; a lone `Type` / `pkg.Type` (+ tag) is embedded
|
|
98
|
+
local names, z = {}, first
|
|
99
|
+
while T.k[z] == 'id' do
|
|
100
|
+
names[#names + 1] = z
|
|
101
|
+
if is(T, z + 1, ',') then z = z + 2 else break end
|
|
102
|
+
end
|
|
103
|
+
local embedded = #names == 1 and (last == first or is(T, first + 1, '.')
|
|
104
|
+
or (last == first + 1 and T.k[last] == 'str'))
|
|
105
|
+
if embedded then
|
|
106
|
+
local e = is(T, first + 1, '.') and first + 2 or first
|
|
107
|
+
common.add_node(r, T, { kind = 'field', name = T:text(e), parent = parent,
|
|
108
|
+
ti = first, tj = last, sig = common.sig(T, first, last), embedded = true })
|
|
109
|
+
else
|
|
110
|
+
for _, nz in ipairs(names) do
|
|
111
|
+
common.add_node(r, T, { kind = 'field', name = T:text(nz), parent = parent,
|
|
112
|
+
ti = nz, tj = last, sig = common.sig(T, first, last) })
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
elseif is(T, first, '*') and T.k[first + 1] == 'id' and not is_interface then
|
|
117
|
+
local e = is(T, first + 2, '.') and first + 3 or first + 1
|
|
118
|
+
common.add_node(r, T, { kind = 'field', name = T:text(e), parent = parent,
|
|
119
|
+
ti = first, tj = last, embedded = true })
|
|
120
|
+
end
|
|
121
|
+
x = last + 1
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
-- One type spec at name token x; start is the `type` keyword for the
|
|
127
|
+
-- single form. Returns the next index.
|
|
128
|
+
local function type_spec(T, r, x, b, start)
|
|
129
|
+
local y = x + 1
|
|
130
|
+
if is(T, y, '[') and T.m[y] then y = T.m[y] + 1 end -- type parameters
|
|
131
|
+
if is(T, y, '=') then y = y + 1 end -- alias
|
|
132
|
+
local kind, body = 'typedef', nil
|
|
133
|
+
if kw(T, y, 'struct') and is(T, y + 1, '{') then
|
|
134
|
+
kind, body = 'struct', y + 1
|
|
135
|
+
elseif kw(T, y, 'interface') and is(T, y + 1, '{') then
|
|
136
|
+
kind, body = 'interface', y + 1
|
|
137
|
+
end
|
|
138
|
+
local stop = body and (T.m[body] or b) or stmt_end(T, x, b)
|
|
139
|
+
local node = common.add_node(r, T, {
|
|
140
|
+
kind = kind, name = T:text(x), ti = start or x, tj = stop,
|
|
141
|
+
sig = common.sig(T, start or x, body and body - 1 or stop, 160),
|
|
142
|
+
})
|
|
143
|
+
if body then type_members(T, r, body, stop, node, kind == 'interface') end
|
|
144
|
+
return stop + 1
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
-- var/const specs in [a, b]: one per statement, `a, b = ...` names each.
|
|
148
|
+
local function value_specs(T, r, a, b, kind)
|
|
149
|
+
local x = a
|
|
150
|
+
while x <= b do
|
|
151
|
+
local last = stmt_end(T, x, b)
|
|
152
|
+
local z = x
|
|
153
|
+
while T.k[z] == 'id' and z <= last do
|
|
154
|
+
common.add_node(r, T, { kind = kind, name = T:text(z), ti = z, tj = last,
|
|
155
|
+
sig = common.sig(T, x, last, 120) })
|
|
156
|
+
if is(T, z + 1, ',') then z = z + 2 else break end
|
|
157
|
+
end
|
|
158
|
+
common.add_calls(r, T, x, last)
|
|
159
|
+
x = last + 1
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
function M.parse(path, src)
|
|
164
|
+
local T = M.lang:tokenize(src)
|
|
165
|
+
local r = common.new(path, 'go')
|
|
166
|
+
local i, n = 1, T.n
|
|
167
|
+
while i <= n do
|
|
168
|
+
if kw(T, i, 'package') and T.k[i + 1] == 'id' then
|
|
169
|
+
r.package = T:text(i + 1)
|
|
170
|
+
i = i + 2
|
|
171
|
+
elseif kw(T, i, 'import') then
|
|
172
|
+
local stop = (is(T, i + 1, '(') and T.m[i + 1]) or stmt_end(T, i, n)
|
|
173
|
+
for x = i + 1, stop do
|
|
174
|
+
if T.k[x] == 'str' then
|
|
175
|
+
r.imports[#r.imports + 1] = { path = T:text(x):sub(2, -2), line = T:line(x),
|
|
176
|
+
alias = (T.k[x - 1] == 'id' or is(T, x - 1, '.')) and T:text(x - 1) or nil }
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
i = stop + 1
|
|
180
|
+
elseif kw(T, i, 'func') then
|
|
181
|
+
local x = i + 1
|
|
182
|
+
local recv
|
|
183
|
+
if is(T, x, '(') and T.m[x] then
|
|
184
|
+
recv = receiver_type(T, x, T.m[x])
|
|
185
|
+
x = T.m[x] + 1
|
|
186
|
+
end
|
|
187
|
+
local p = x + 1
|
|
188
|
+
if is(T, p, '[') and T.m[p] then p = T.m[p] + 1 end -- type parameters
|
|
189
|
+
if T.k[x] == 'id' and is(T, p, '(') and T.m[p] then
|
|
190
|
+
local pc = T.m[p]
|
|
191
|
+
local body = func_body(T, pc, n)
|
|
192
|
+
local close = body and (T.m[body] or n) or pc
|
|
193
|
+
local node = common.add_node(r, T, {
|
|
194
|
+
kind = recv and 'method' or 'function', name = T:text(x), ti = i, tj = close,
|
|
195
|
+
sig = common.sig(T, i, body and body - 1 or pc, 200), recv = recv,
|
|
196
|
+
})
|
|
197
|
+
if recv then r.nodes[node].qualified = recv .. '.' .. T:text(x) end
|
|
198
|
+
if body then common.add_calls(r, T, body, close) end
|
|
199
|
+
i = close + 1
|
|
200
|
+
else
|
|
201
|
+
i = x -- func literal / type: not a declaration
|
|
202
|
+
end
|
|
203
|
+
elseif kw(T, i, 'type') then
|
|
204
|
+
if is(T, i + 1, '(') and T.m[i + 1] then
|
|
205
|
+
local c = T.m[i + 1]
|
|
206
|
+
local x = i + 2
|
|
207
|
+
while x < c do
|
|
208
|
+
if T.k[x] == 'id' then x = type_spec(T, r, x, c - 1) else x = x + 1 end
|
|
209
|
+
end
|
|
210
|
+
i = c + 1
|
|
211
|
+
elseif T.k[i + 1] == 'id' then
|
|
212
|
+
i = type_spec(T, r, i + 1, n, i)
|
|
213
|
+
else
|
|
214
|
+
i = i + 1
|
|
215
|
+
end
|
|
216
|
+
elseif kw(T, i, 'var') or kw(T, i, 'const') then
|
|
217
|
+
local kind = kw(T, i, 'const') and 'constant' or 'variable'
|
|
218
|
+
if is(T, i + 1, '(') and T.m[i + 1] then
|
|
219
|
+
value_specs(T, r, i + 2, T.m[i + 1] - 1, kind)
|
|
220
|
+
i = T.m[i + 1] + 1
|
|
221
|
+
else
|
|
222
|
+
local last = stmt_end(T, i, n)
|
|
223
|
+
value_specs(T, r, i + 1, last, kind)
|
|
224
|
+
i = last + 1
|
|
225
|
+
end
|
|
226
|
+
else
|
|
227
|
+
i = i + 1
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
return common.finish(r), T
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
return M
|