@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.
Files changed (44) hide show
  1. package/DISTRIBUTION.md +98 -0
  2. package/LICENSE +24 -0
  3. package/README.md +132 -0
  4. package/README.zh-CN.md +132 -0
  5. package/THIRD_PARTY.md +32 -0
  6. package/build-info.json +1 -0
  7. package/codeoutline +4 -0
  8. package/codeoutline.cmd +3 -0
  9. package/licenses/libdeflate.txt +22 -0
  10. package/licenses/lua-cmsgpack.txt +22 -0
  11. package/licenses/luajit.txt +56 -0
  12. package/licenses/mbedtls.txt +553 -0
  13. package/licenses/rpmalloc.txt +12 -0
  14. package/licenses/xnet2lua.txt +24 -0
  15. package/licenses/yyjson.txt +21 -0
  16. package/package.json +1 -0
  17. package/scripts/codeoutline/cli.lua +32 -0
  18. package/scripts/codeoutline/command.lua +155 -0
  19. package/scripts/codeoutline/common.lua +124 -0
  20. package/scripts/codeoutline/control.lua +4 -0
  21. package/scripts/codeoutline/explore.lua +500 -0
  22. package/scripts/codeoutline/graph.lua +380 -0
  23. package/scripts/codeoutline/http.lua +156 -0
  24. package/scripts/codeoutline/index.lua +404 -0
  25. package/scripts/codeoutline/index_worker.lua +43 -0
  26. package/scripts/codeoutline/lang/c.lua +362 -0
  27. package/scripts/codeoutline/lang/cpp.lua +539 -0
  28. package/scripts/codeoutline/lang/csharp.lua +334 -0
  29. package/scripts/codeoutline/lang/go.lua +233 -0
  30. package/scripts/codeoutline/lang/java.lua +274 -0
  31. package/scripts/codeoutline/lang/js.lua +466 -0
  32. package/scripts/codeoutline/lang/lua.lua +218 -0
  33. package/scripts/codeoutline/lang/python.lua +184 -0
  34. package/scripts/codeoutline/lang/rust.lua +276 -0
  35. package/scripts/codeoutline/main.lua +104 -0
  36. package/scripts/codeoutline/mcp.lua +188 -0
  37. package/scripts/codeoutline/parse.lua +49 -0
  38. package/scripts/codeoutline/path.lua +36 -0
  39. package/scripts/codeoutline/service.lua +135 -0
  40. package/scripts/codeoutline/text.lua +43 -0
  41. package/scripts/codeoutline/version.lua +1 -0
  42. package/scripts/codeoutline/xscan.lua +625 -0
  43. package/xnet2lua/bin/xnet.exe +0 -0
  44. package/xnet2lua/scripts/core/share/xhttp_codec.lua +835 -0
@@ -0,0 +1,539 @@
1
+ -- lang/cpp.lua — index-level C++ parser: namespaces, classes/structs/unions
2
+ -- (+ bases, members), enums (incl. `enum class`), functions and methods
3
+ -- (inline and out-of-line `A::B::f`), constructors with init lists,
4
+ -- destructors, operators, templates, `using` aliases, plus everything the C
5
+ -- parser handles (macros, #include, typedefs, globals, calls).
6
+ --
7
+ -- Built on the C parser's helpers rather than its main loop, so C results
8
+ -- stay exactly as validated. Qualified names use '::'.
9
+
10
+ local xscan = require('codeoutline.xscan')
11
+ local common = require('codeoutline.common')
12
+ local c = require('codeoutline.lang.c')
13
+
14
+ local M = {}
15
+
16
+ local KEYWORDS = {}
17
+ for _, k in ipairs(c.keywords) do KEYWORDS[#KEYWORDS + 1] = k end
18
+ for k in ([[alignas alignof asm bool catch char8_t char16_t char32_t class concept consteval
19
+ constexpr constinit const_cast co_await co_return co_yield decltype delete dynamic_cast
20
+ explicit export false friend mutable namespace new noexcept nullptr operator private
21
+ protected public reinterpret_cast requires static_assert static_cast template this
22
+ thread_local throw true try typeid typename using virtual wchar_t]]):gmatch('%S+') do
23
+ KEYWORDS[#KEYWORDS + 1] = k
24
+ end
25
+
26
+ local OPS = {}
27
+ for _, o in ipairs(c.ops) do OPS[#OPS + 1] = o end
28
+ for _, o in ipairs({ '->*', '.*', '<=>' }) do OPS[#OPS + 1] = o end
29
+
30
+ M.config = {
31
+ keywords = KEYWORDS,
32
+ ops = OPS,
33
+ line_comment = { '//' },
34
+ block_comment = { { '/*', '*/' } },
35
+ strings = { { '"', '"', escape = '\\' }, { "'", "'", escape = '\\' } },
36
+ string_prefixes = 'LuU8R',
37
+ directive = '#',
38
+ pp_first_branch = true,
39
+ }
40
+
41
+ M.lang = xscan.lang(M.config)
42
+
43
+ local ATTR = {}
44
+ for k in pairs(c.ATTR) do ATTR[k] = true end
45
+ ATTR.decltype = true
46
+
47
+ local ACCESS = { public = true, private = true, protected = true }
48
+ local QT_SECTIONS = { signals = true, slots = true, Q_SIGNALS = true, Q_SLOTS = true }
49
+ -- Tokens that may follow a parameter list before the body.
50
+ local TRAIL_KW = { const = true, noexcept = true, volatile = true, mutable = true, throw = true, try = true }
51
+ local CLASSLIKE = { class = true, struct = true, union = true }
52
+
53
+ local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
54
+ local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
55
+
56
+ -- Change in template angle depth ('>>' closes two).
57
+ local function angle(T, i)
58
+ if T.k[i] ~= 'op' then return 0 end
59
+ local t = T:text(i)
60
+ if t == '<' then return 1 elseif t == '>' then return -1 elseif t == '>>' then return -2 end
61
+ return 0
62
+ end
63
+
64
+ -- Index after a template argument list opening at '<' i, or nil.
65
+ local function skip_angle(T, i, limit)
66
+ local d = 0
67
+ local x = i
68
+ while x <= limit do
69
+ if is(T, x, ';') or is(T, x, '{') or is(T, x, '}') then return nil end
70
+ if (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
71
+ x = T.m[x]
72
+ else
73
+ d = d + angle(T, x)
74
+ if d <= 0 then return x + 1 end
75
+ end
76
+ x = x + 1
77
+ end
78
+ return nil
79
+ end
80
+
81
+ -- Skip backwards over a template argument list ending at '>' / '>>' x.
82
+ local function skip_angle_back(T, x, floor)
83
+ local d = 0
84
+ while x >= floor do
85
+ d = d - angle(T, x)
86
+ if d <= 0 then return x - 1 end
87
+ x = x - 1
88
+ end
89
+ return floor - 1
90
+ end
91
+
92
+ -- The declarator name whose parameter list opens at '(' o. Returns
93
+ -- { first = first token of the qualified name, name = text, quals = {...} }.
94
+ local function name_before_paren(T, o, floor)
95
+ local p = o - 1
96
+ local first, name
97
+ if p < floor then return nil end
98
+ if is(T, p, ')') and T.m[p] == p - 1 and kw(T, p - 2, 'operator') then
99
+ first, name = p - 2, 'operator()'
100
+ elseif is(T, p, ']') and T.m[p] == p - 1 and kw(T, p - 2, 'operator') then
101
+ first, name = p - 2, 'operator[]'
102
+ elseif T.k[p] == 'op' then
103
+ local q = p
104
+ while q > p - 3 and q >= floor and not kw(T, q, 'operator') do q = q - 1 end
105
+ if not kw(T, q, 'operator') then return nil end
106
+ local parts = {}
107
+ for z = q + 1, p do parts[#parts + 1] = T:text(z) end
108
+ first, name = q, 'operator' .. table.concat(parts)
109
+ elseif T.k[p] == 'id' or (T.k[p] == 'kw' and kw(T, p - 1, 'operator')) then
110
+ p = c.paren_name(T, p)
111
+ if kw(T, p - 1, 'operator') then
112
+ first, name = p - 1, 'operator ' .. T:text(p) -- conversion: operator bool
113
+ elseif is(T, p - 1, '~') then
114
+ first, name = p - 1, '~' .. T:text(p)
115
+ elseif T.k[p] == 'id' then
116
+ first, name = p, T:text(p)
117
+ else
118
+ return nil
119
+ end
120
+ else
121
+ return nil
122
+ end
123
+ -- qualifier chain: A::B::name, A<T>::name
124
+ local quals = {}
125
+ local q = first - 1
126
+ while q > floor and is(T, q, '::') do
127
+ local z = q - 1
128
+ if is(T, z, '>') or is(T, z, '>>') then z = skip_angle_back(T, z, floor) end
129
+ if T.k[z] ~= 'id' then break end
130
+ table.insert(quals, 1, T:text(z))
131
+ first = z
132
+ q = z - 1
133
+ end
134
+ return { first = first, name = name, quals = quals }
135
+ end
136
+
137
+ -- If the '{' at j opens a function body, describe the head; the parameter
138
+ -- list is found by walking back over trailing qualifiers, a trailing return
139
+ -- type (`-> T`), noexcept(...)/attribute groups and macros.
140
+ local function cpp_head(T, a, j)
141
+ local x = j - 1
142
+ -- trailing return type: `) -> T {`
143
+ local y, guard = x, 0
144
+ while y > a and guard < 40 do
145
+ if is(T, y, '->') then
146
+ x = y - 1
147
+ break
148
+ end
149
+ if is(T, y, ';') or is(T, y, '{') or is(T, y, '}') or is(T, y, '=') or is(T, y, ')') and not T.m[y] then break end
150
+ if (is(T, y, ')') or is(T, y, ']')) and T.m[y] then y = T.m[y] - 1 else y = y - 1 end
151
+ guard = guard + 1
152
+ end
153
+ while x >= a do
154
+ if is(T, x, ')') then
155
+ local o = T.m[x]
156
+ if not o or o <= a then return nil end
157
+ local p = o - 1
158
+ if (T.k[p] == 'kw' and (T:text(p) == 'noexcept' or T:text(p) == 'throw' or T:text(p) == 'decltype'))
159
+ or (T.k[p] == 'id' and ATTR[T:text(p)]) then
160
+ x = p - 1 -- noexcept(...), __attribute__((...))
161
+ else
162
+ local info = name_before_paren(T, o, a)
163
+ if not info then return nil end
164
+ info.open, info.close = o, x
165
+ return info
166
+ end
167
+ elseif is(T, x, ']') and T.m[x] and is(T, T.m[x] + 1, '[') then
168
+ x = T.m[x] - 1 -- [[attribute]]
169
+ elseif (T.k[x] == 'kw' and TRAIL_KW[T:text(x)]) or T.k[x] == 'id' or is(T, x, '&') or is(T, x, '&&') then
170
+ x = x - 1 -- const, override, final, &&, macros
171
+ else
172
+ return nil
173
+ end
174
+ end
175
+ return nil
176
+ end
177
+
178
+ -- Constructor init list after ') :' at colon index k: returns the body '{'.
179
+ local function init_list_body(T, k, b)
180
+ local x = k + 1
181
+ while x <= b do
182
+ if T.k[x] ~= 'id' then return nil end
183
+ x = x + 1
184
+ while is(T, x, '::') and T.k[x + 1] == 'id' do x = x + 2 end
185
+ if is(T, x, '<') then
186
+ x = skip_angle(T, x, b)
187
+ if not x then return nil end
188
+ end
189
+ if (is(T, x, '(') or is(T, x, '{')) and T.m[x] then x = T.m[x] + 1 else return nil end
190
+ if is(T, x, '...') then x = x + 1 end
191
+ if is(T, x, ',') then
192
+ x = x + 1
193
+ elseif is(T, x, '{') then
194
+ return x
195
+ else
196
+ return nil
197
+ end
198
+ end
199
+ return nil
200
+ end
201
+
202
+ -- Statement `[i, j]` ending in ';': prototypes, fields/variables, aliases.
203
+ local function cpp_decl(T, r, i, j, parent)
204
+ local first = T:text(i)
205
+ if first == 'typedef' then return c.declaration(T, r, i, j, parent) end
206
+ if first == 'using' then
207
+ if T.k[i + 1] == 'id' and is(T, i + 2, '=') then
208
+ common.add_node(r, T, { kind = 'typedef', name = T:text(i + 1), parent = parent, ti = i, tj = j, sig = common.sig(T, i, j) })
209
+ end
210
+ return
211
+ end
212
+ if first == 'friend' or first == 'static_assert' or first == 'namespace' then return end
213
+ if first == 'enum' then return end -- enum class X : int;
214
+ if T.k[i] == 'kw' and CLASSLIKE[first] and j - i <= 3 then
215
+ return -- class X; (struct X *p; still falls through)
216
+ end
217
+ local is_extern = first == 'extern' and T.k[i + 1] ~= 'str'
218
+ local y, eq, d = i, nil, 0
219
+ local names = {}
220
+ while y < j do
221
+ d = d + angle(T, y)
222
+ if d < 0 then d = 0 end
223
+ if is(T, y, '=') and not eq and d == 0 and not kw(T, y - 1, 'operator') then eq = y end
224
+ if is(T, y, '(') and T.m[y] then
225
+ if not eq and d == 0 then
226
+ local info = name_before_paren(T, y, i)
227
+ if info and info.first > i - 1 and not ATTR[info.name] then
228
+ local ctor = parent and r.nodes[parent].name == info.name
229
+ if info.first == i and #info.quals == 0 and not info.name:find('^[~o]') and not ctor then
230
+ common.add_calls(r, T, i, j) -- MACRO(args);
231
+ return
232
+ end
233
+ common.add_node(r, T, {
234
+ kind = 'prototype', name = info.name, parent = parent,
235
+ ti = i, tj = j, sig = common.sig(T, i, T.m[y]),
236
+ quals = #info.quals > 0 and info.quals or nil,
237
+ static = first == 'static' or nil,
238
+ })
239
+ common.add_calls(r, T, T.m[y] + 1, j) -- default member initializers etc.
240
+ return
241
+ end
242
+ end
243
+ y = T.m[y] + 1
244
+ elseif (is(T, y, '[') or is(T, y, '{')) and T.m[y] then
245
+ if T.k[y - 1] == 'id' and is(T, y, '{') and not eq and d == 0 then names[#names + 1] = y - 1 end
246
+ y = T.m[y] + 1
247
+ else
248
+ if T.k[y] == 'id' and not eq and d == 0 and T.k[y + 1] == 'op' then
249
+ local nx = T:text(y + 1)
250
+ if nx == '=' or nx == ';' or nx == ',' or nx == '[' or nx == ':' then names[#names + 1] = y end
251
+ end
252
+ if is(T, y, ',') and d == 0 then eq = nil end
253
+ y = y + 1
254
+ end
255
+ end
256
+ if is_extern then names = {} end
257
+ for _, x in ipairs(names) do
258
+ -- `int Shape::counter_ = 0;` defines a static member of Shape
259
+ local quals, q = {}, x - 1
260
+ while is(T, q, '::') and T.k[q - 1] == 'id' do
261
+ table.insert(quals, 1, T:text(q - 1))
262
+ q = q - 2
263
+ end
264
+ common.add_node(r, T, {
265
+ kind = 'variable', name = T:text(x), parent = parent, ti = x, tj = j,
266
+ static = first == 'static' or nil, sig = common.sig(T, i, math.min(j, x + 1)),
267
+ quals = #quals > 0 and quals or nil,
268
+ })
269
+ end
270
+ common.add_calls(r, T, i, j)
271
+ common.add_value_refs(r, T, i, j)
272
+ end
273
+
274
+ local cpp_range
275
+
276
+ -- class/struct/union/enum definition with its body '{' at j.
277
+ local function aggregate(T, r, i, head, j, close, b, parent, agg)
278
+ local kind = T:text(agg)
279
+ local x = agg + 1
280
+ if kind == 'enum' and (kw(T, x, 'class') or kw(T, x, 'struct')) then x = x + 1 end
281
+ while is(T, x, '[') and T.m[x] do x = T.m[x] + 1 end -- [[attr]]
282
+ while T.k[x] == 'id' and ATTR[T:text(x)] and is(T, x + 1, '(') and T.m[x + 1] do x = T.m[x + 1] + 1 end
283
+ -- `class MYLIB_API Widget final : Base` -> the last identifier of the
284
+ -- run before ':' / '{' / '<' is the name; earlier ones are export macros
285
+ local name_tok
286
+ while T.k[x] == 'id' and x < j do
287
+ local t = T:text(x)
288
+ if t ~= 'final' and t ~= 'sealed' then name_tok = x end
289
+ if is(T, x + 1, '::') and T.k[x + 2] == 'id' then
290
+ x = x + 2
291
+ elseif T.k[x + 1] == 'id' then
292
+ x = x + 1
293
+ else
294
+ break
295
+ end
296
+ end
297
+ -- bases: `: public A, private ns::B<T>`
298
+ local bases
299
+ if name_tok and kind ~= 'enum' then
300
+ local y = name_tok + 1
301
+ if is(T, y, '<') then y = skip_angle(T, y, j) or y end
302
+ while T.k[y] == 'id' and (T:text(y) == 'final' or T:text(y) == 'sealed') do y = y + 1 end
303
+ if is(T, y, ':') then
304
+ bases = {}
305
+ local z = y + 1
306
+ while z < j do
307
+ if T.k[z] == 'kw' and (ACCESS[T:text(z)] or T:text(z) == 'virtual') then
308
+ z = z + 1
309
+ elseif T.k[z] == 'id' then
310
+ local nm = T:text(z)
311
+ z = z + 1
312
+ while is(T, z, '::') and T.k[z + 1] == 'id' do nm = nm .. '::' .. T:text(z + 1); z = z + 2 end
313
+ bases[#bases + 1] = nm
314
+ if is(T, z, '<') then z = skip_angle(T, z, j) or j end
315
+ else
316
+ z = z + 1
317
+ end
318
+ end
319
+ end
320
+ end
321
+ -- trailing declarators: `} name;` / typedef alias
322
+ local stop = close + 1
323
+ while stop <= b and not is(T, stop, ';') and T.k[stop] ~= 'dir' do
324
+ stop = (T.m[stop] and T.m[stop] > stop) and T.m[stop] + 1 or stop + 1
325
+ end
326
+ local alias
327
+ if T:text(i) == 'typedef' then
328
+ for y = close + 1, stop - 1 do
329
+ if T.k[y] == 'id' then alias = y; break end
330
+ end
331
+ end
332
+ local nm = name_tok or alias
333
+ local node
334
+ if nm then
335
+ node = common.add_node(r, T, {
336
+ kind = kind, name = T:text(nm), parent = parent,
337
+ ti = i, tj = close, sig = common.sig(T, head, j - 1), bases = bases,
338
+ })
339
+ if kind == 'enum' then
340
+ c.members(T, r, j, close, node, true)
341
+ else
342
+ cpp_range(T, r, j + 1, close - 1, node)
343
+ end
344
+ elseif kind == 'enum' then
345
+ c.members(T, r, j, close, parent, true)
346
+ else
347
+ cpp_range(T, r, j + 1, close - 1, parent) -- anonymous struct/union members
348
+ end
349
+ if alias and alias ~= nm then
350
+ common.add_node(r, T, { kind = 'typedef', name = T:text(alias), parent = parent, ti = alias, tj = alias })
351
+ elseif not alias and stop > close + 1 then
352
+ cpp_decl(T, r, close + 1, math.min(stop, b), parent)
353
+ end
354
+ return math.min(stop, b)
355
+ end
356
+
357
+ -- Parse the declarations in token range [a, b].
358
+ cpp_range = function(T, r, a, b, parent)
359
+ local i = a
360
+ while i <= b do
361
+ local k = T.k[i]
362
+ local t = T:text(i)
363
+ if k == 'dir' then
364
+ c.handle_directive(T, r, i)
365
+ i = i + 1
366
+ elseif k == 'op' and (t == ';' or t == '}') then
367
+ i = i + 1
368
+ elseif (k == 'kw' and ACCESS[t] or k == 'id' and QT_SECTIONS[t]) and is(T, i + 1, ':') then
369
+ i = i + 2 -- public: signals:
370
+ elseif k == 'kw' and ACCESS[t] and T.k[i + 1] == 'id' and QT_SECTIONS[T:text(i + 1)] and is(T, i + 2, ':') then
371
+ i = i + 3 -- public slots:
372
+ elseif k == 'kw' and t == 'namespace' then
373
+ local j = i + 1
374
+ while j <= b and not is(T, j, '{') and not is(T, j, ';') and not is(T, j, '=') do j = j + 1 end
375
+ if is(T, j, '{') then
376
+ local close = T.m[j] or c.fallback_close(T, j, b)
377
+ local full = common.sig(T, i + 1, j - 1):gsub('%s', ''):gsub('^inline', '')
378
+ local node = parent
379
+ if full ~= '' and j > i + 1 then
380
+ -- C++17 `namespace a::b {`: name is the last part, the rest qualifies it
381
+ local quals = {}
382
+ for part in full:gmatch('[^:]+') do quals[#quals + 1] = part end
383
+ local name = table.remove(quals)
384
+ node = common.add_node(r, T, {
385
+ kind = 'namespace', name = name, parent = parent, ti = i, tj = close,
386
+ quals = #quals > 0 and quals or nil,
387
+ })
388
+ end
389
+ cpp_range(T, r, j + 1, close - 1, node)
390
+ i = close + 1
391
+ else
392
+ while j <= b and not is(T, j, ';') do j = j + 1 end -- namespace alias
393
+ i = j + 1
394
+ end
395
+ elseif k == 'kw' and t == 'extern' and T.k[i + 1] == 'str' and is(T, i + 2, '{') then
396
+ local close = T.m[i + 2] or c.fallback_close(T, i + 2, b)
397
+ cpp_range(T, r, i + 3, close - 1, parent) -- extern "C" { ... }
398
+ i = close + 1
399
+ else
400
+ -- template<...> prefix: the declaration proper starts after it
401
+ local head = i
402
+ while kw(T, head, 'template') and is(T, head + 1, '<') do
403
+ head = skip_angle(T, head + 1, b) or (head + 2)
404
+ end
405
+ local j, done, eq, d = head, false, nil, 0
406
+ while j <= b and not done do
407
+ local kj = T.k[j]
408
+ if kj == 'dir' then
409
+ if j > head then cpp_decl(T, r, head, j - 1, parent) end
410
+ j = j - 1
411
+ done = true
412
+ elseif is(T, j, ';') then
413
+ cpp_decl(T, r, head, j, parent)
414
+ done = true
415
+ elseif kj == 'kw' and ACCESS[T:text(j)] and is(T, j + 1, ':') and j > head then
416
+ -- a macro line without ';' (Q_OBJECT) ran into `public:`
417
+ j = j - 1
418
+ done = true
419
+ elseif is(T, j, ':') and is(T, j - 1, ')') and not eq then
420
+ local body = init_list_body(T, j, b)
421
+ if body then
422
+ local info = cpp_head(T, head, j)
423
+ local close = T.m[body] or c.fallback_close(T, body, b)
424
+ if info then
425
+ common.add_node(r, T, {
426
+ kind = 'function', name = info.name, parent = parent, ti = i, tj = close,
427
+ sig = common.sig(T, head, info.close),
428
+ quals = #info.quals > 0 and info.quals or nil,
429
+ })
430
+ end
431
+ common.add_calls(r, T, j, close) -- init list + body
432
+ common.add_value_refs(r, T, body, close)
433
+ j = close
434
+ done = true
435
+ else
436
+ j = j + 1
437
+ end
438
+ elseif is(T, j, '{') then
439
+ local close = T.m[j] or c.fallback_close(T, j, b)
440
+ local info = not eq and cpp_head(T, head, j)
441
+ local agg
442
+ if not info and not eq then
443
+ local y, dd = head, 0
444
+ while y < j do
445
+ dd = dd + angle(T, y)
446
+ if dd == 0 and T.k[y] == 'kw' and (CLASSLIKE[T:text(y)] or T:text(y) == 'enum') then agg = y; break end
447
+ if (is(T, y, '(') or is(T, y, '[')) and T.m[y] then y = T.m[y] end
448
+ y = y + 1
449
+ end
450
+ end
451
+ if info then
452
+ common.add_node(r, T, {
453
+ kind = 'function', name = info.name, parent = parent, ti = i, tj = close,
454
+ sig = common.sig(T, head, info.close),
455
+ quals = #info.quals > 0 and info.quals or nil,
456
+ static = T:text(head) == 'static' or nil,
457
+ })
458
+ common.add_calls(r, T, info.close + 1, close)
459
+ common.add_value_refs(r, T, j, close)
460
+ j = close
461
+ done = true
462
+ elseif agg then
463
+ j = aggregate(T, r, i, head, j, close, b, parent, agg)
464
+ done = true
465
+ else
466
+ j = close + 1 -- initializer braces / lambda
467
+ end
468
+ else
469
+ if is(T, j, '=') and d == 0 and not kw(T, j - 1, 'operator') then eq = j end
470
+ d = math.max(0, d + angle(T, j))
471
+ if (is(T, j, '(') or is(T, j, '[')) and T.m[j] then
472
+ j = T.m[j] + 1
473
+ else
474
+ j = j + 1
475
+ end
476
+ end
477
+ end
478
+ if not done and j > head then
479
+ cpp_decl(T, r, head, math.min(j, b), parent)
480
+ end
481
+ i = math.max(j, i) + 1
482
+ end
483
+ end
484
+ end
485
+
486
+ -- Kinds and '::'-qualified names, fixed up once the tree is known:
487
+ -- members of a class become methods/fields, `A::f` out-of-line
488
+ -- definitions become methods of A, and ctor/dtor names are recognized.
489
+ local CLASS_KINDS = { class = true, struct = true, union = true }
490
+ local function finalize(r)
491
+ for _, n in ipairs(r.nodes) do
492
+ local p = n.parent and r.nodes[n.parent]
493
+ local owner = p and CLASS_KINDS[p.kind] and p or nil
494
+ local quals = n.quals
495
+ if quals then
496
+ local prefix = (p and p.kind == 'namespace') and (p.qualified .. '::') or ''
497
+ n.qualified = prefix .. table.concat(quals, '::') .. '::' .. n.name
498
+ else
499
+ n.qualified = p and (p.qualified .. '::' .. n.name) or n.name
500
+ end
501
+ -- `static` on a class member is not internal linkage: only file- or
502
+ -- namespace-scope statics are invisible outside their file
503
+ if owner or quals then n.static = nil end
504
+ local cls_name = owner and owner.name or (quals and quals[#quals])
505
+ -- an in-class declaration stays marked, so resolution prefers the
506
+ -- out-of-line definition the way it prefers definitions over prototypes
507
+ if n.kind == 'prototype' and (owner or quals) then n.decl = true end
508
+ if n.kind == 'function' or n.kind == 'prototype' then
509
+ if cls_name and n.name == cls_name then
510
+ n.kind = 'constructor'
511
+ elseif cls_name and n.name == '~' .. cls_name then
512
+ n.kind = 'destructor'
513
+ elseif owner or quals then
514
+ n.kind = 'method'
515
+ end
516
+ elseif n.kind == 'variable' and (owner or quals) then
517
+ n.kind = 'field'
518
+ end
519
+ n.quals = nil
520
+ end
521
+ end
522
+
523
+ function M.parse(path, src)
524
+ local T = M.lang:tokenize(src)
525
+ local r = common.new(path, 'cpp')
526
+ r.skip_calls = ATTR
527
+ cpp_range(T, r, 1, T.n, nil)
528
+ finalize(r)
529
+ return common.finish(r), T
530
+ end
531
+
532
+ -- A .h file is C++ when it uses C++-only syntax; otherwise the C parser
533
+ -- keeps handling it (C code may use `new`/`class` as plain identifiers).
534
+ function M.looks_cpp(src)
535
+ return src:find('%f[%w_]namespace%s+[%w_]*%s*{') or src:find('%f[%w_]class%s+[%w_]+%s*[:{]')
536
+ or src:find('%f[%w_]template%s*<') or src:find('%f[%w_]public%s*:') or src:find('%f[%w_]std::')
537
+ end
538
+
539
+ return M