@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,218 @@
1
+ -- lang/lua.lua — index-level Lua parser: functions in every definition form
2
+ -- (`function a.b:c()`, `local function f()`, `x = function()`, table fields
3
+ -- `{ call = function() }`), top-level locals, require/dofile imports, and
4
+ -- calls (including `f "str"` / `f {...}` and `obj:method()`).
5
+ --
6
+ -- Lua blocks close with keywords, not brackets, so the parser pairs
7
+ -- function/if/do/repeat with end/until itself; brackets come from xscan.
8
+
9
+ local xscan = require('codeoutline.xscan')
10
+ local common = require('codeoutline.common')
11
+
12
+ local M = {}
13
+
14
+ M.config = {
15
+ keywords = {
16
+ 'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto',
17
+ 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true',
18
+ 'until', 'while',
19
+ },
20
+ ops = { '...', '..', '==', '~=', '<=', '>=', '<<', '>>', '//', '::' },
21
+ line_comment = { '--' },
22
+ strings = { { '"', '"', escape = '\\' }, { "'", "'", escape = '\\' } },
23
+ long_brackets = true,
24
+ }
25
+
26
+ M.lang = xscan.lang(M.config)
27
+
28
+ local OPENS = { ['function'] = true, ['if'] = true, ['do'] = true, ['repeat'] = true }
29
+ local IMPORTS = { require = true, dofile = true, loadfile = true }
30
+
31
+ local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
32
+ local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
33
+
34
+ -- Pair block keywords: function/if/do/repeat -> end/until. while/for need
35
+ -- no entry of their own; their `do` opens the block.
36
+ local function block_matches(T)
37
+ local bm, stack = {}, {}
38
+ for i = 1, T.n do
39
+ if T.k[i] == 'kw' then
40
+ local t = T:text(i)
41
+ if OPENS[t] then
42
+ stack[#stack + 1] = i
43
+ elseif t == 'end' or t == 'until' then
44
+ local top = stack[#stack]
45
+ if top then
46
+ stack[#stack] = nil
47
+ bm[top], bm[i] = i, top
48
+ end
49
+ end
50
+ end
51
+ end
52
+ return bm
53
+ end
54
+
55
+ -- Dotted/colon name ending at token x (walking left): "a.b:c" -> parts, start.
56
+ local function name_chain_left(T, x)
57
+ local parts, y = { T:text(x) }, x
58
+ while (is(T, y - 1, '.') or is(T, y - 1, ':')) and T.k[y - 2] == 'id' do
59
+ table.insert(parts, 1, T:text(y - 1))
60
+ table.insert(parts, 1, T:text(y - 2))
61
+ y = y - 2
62
+ end
63
+ return table.concat(parts), y
64
+ end
65
+
66
+ -- The unmatched '{' enclosing token i, if any (table constructor).
67
+ local function enclosing_brace(T, i, floor)
68
+ local y = i - 1
69
+ while y >= floor do
70
+ local m = T.m[y]
71
+ if is(T, y, '}') and m then
72
+ y = m - 1
73
+ elseif is(T, y, ')') and m then
74
+ y = m - 1
75
+ elseif is(T, y, ']') and m then
76
+ y = m - 1
77
+ elseif is(T, y, '{') then
78
+ return y
79
+ elseif is(T, y, '(') or is(T, y, '[') then
80
+ return nil -- inside a call/index, not a table
81
+ else
82
+ y = y - 1
83
+ end
84
+ end
85
+ return nil
86
+ end
87
+
88
+ -- What a table constructor at '{' b is assigned to: `NAME = {`,
89
+ -- `local NAME = {`, `a.b = {`, or `return {` (the module table).
90
+ local function table_owner(T, b, stem)
91
+ if is(T, b - 1, '=') and T.k[b - 2] == 'id' then
92
+ local name = name_chain_left(T, b - 2)
93
+ return name
94
+ end
95
+ if kw(T, b - 1, 'return') then return stem end
96
+ -- nested field: key = { ... }
97
+ return nil
98
+ end
99
+
100
+ local function string_value(T, i)
101
+ local s = T:text(i)
102
+ return s:match('^"(.*)"$') or s:match("^'(.*)'$")
103
+ end
104
+
105
+ function M.parse(path, src)
106
+ local T = M.lang:tokenize(src)
107
+ local r = common.new(path, 'lua')
108
+ r.member_ops = { ['.'] = true, [':'] = true }
109
+ local stem = path:match('([^/\\]+)%.lua$') or path
110
+ local bm = block_matches(T)
111
+ local defs = {} -- name tokens of definitions (not calls)
112
+ -- innermost function node containing a token, for nesting/qualification
113
+ local fn_stack = {}
114
+
115
+ local function parent_at(i)
116
+ while #fn_stack > 0 and r.nodes[fn_stack[#fn_stack]].tj < i do fn_stack[#fn_stack] = nil end
117
+ return fn_stack[#fn_stack]
118
+ end
119
+
120
+ for i = 1, T.n do
121
+ local k = T.k[i]
122
+ if k == 'kw' and T:text(i) == 'function' then
123
+ local close = bm[i] or T.n
124
+ local parent = parent_at(i)
125
+ local name, qualified, kind, start
126
+ if T.k[i + 1] == 'id' then
127
+ -- function a.b:c(...) / local function f(...)
128
+ local x = i + 1
129
+ while (is(T, x + 1, '.') or is(T, x + 1, ':')) and T.k[x + 2] == 'id' do x = x + 2 end
130
+ name = T:text(x)
131
+ qualified = common.sig(T, i + 1, x):gsub('%s', '')
132
+ kind = is(T, x - 1, ':') and 'method' or 'function'
133
+ start = kw(T, i - 1, 'local') and i - 1 or i
134
+ defs[x] = true
135
+ elseif is(T, i - 1, '=') and T.k[i - 2] == 'id' then
136
+ -- x = function / a.b = function / { key = function }
137
+ local lhs, y = name_chain_left(T, i - 2)
138
+ name = T:text(i - 2)
139
+ qualified = lhs
140
+ kind = 'function'
141
+ start = kw(T, y - 1, 'local') and y - 1 or y
142
+ defs[i - 2] = true
143
+ local b = enclosing_brace(T, i - 2, 1)
144
+ if b and (is(T, y - 1, '{') or is(T, y - 1, ',') or is(T, y - 1, ';')) then
145
+ local owner = table_owner(T, b, stem)
146
+ if owner then qualified = owner .. '.' .. lhs end
147
+ end
148
+ elseif is(T, i - 1, '=') and is(T, i - 2, ']') and T.m[i - 2] == i - 4 and T.k[i - 3] == 'str' then
149
+ -- t['key'] = function / M.stubs["@run"] = function
150
+ local key = string_value(T, i - 3)
151
+ if key and T.k[i - 5] == 'id' then
152
+ local lhs, y = name_chain_left(T, i - 5)
153
+ name = key
154
+ qualified = lhs .. '.' .. key
155
+ kind = 'function'
156
+ start = y
157
+ end
158
+ end
159
+ if name then
160
+ -- nested definitions qualify under their enclosing function
161
+ local sep_q = qualified
162
+ if parent and not qualified:find('[%.:]') then
163
+ sep_q = r.nodes[parent].qualified .. '.' .. qualified
164
+ end
165
+ -- signature runs to the parameter list's ')'
166
+ local p = i + 1
167
+ while p < close and not is(T, p, '(') do p = p + 1 end
168
+ local node = common.add_node(r, T, {
169
+ kind = kind, name = name, ti = start, tj = close,
170
+ sig = common.sig(T, start, T.m[p] or p),
171
+ ['local'] = kw(T, start, 'local') or nil,
172
+ })
173
+ r.nodes[node].qualified = sep_q
174
+ r.nodes[node].parent = parent
175
+ fn_stack[#fn_stack + 1] = node
176
+ end
177
+ elseif k == 'kw' and T:text(i) == 'local' and not parent_at(i) and T.k[i + 1] == 'id' then
178
+ -- top-level `local a, b = ...` (not `local function`)
179
+ local x = i + 1
180
+ while T.k[x] == 'id' do
181
+ if not kw(T, x + 2, 'function') or not is(T, x + 1, '=') then
182
+ common.add_node(r, T, { kind = 'variable', name = T:text(x), ti = x, tj = x, ['local'] = true })
183
+ end
184
+ if is(T, x + 1, ',') then x = x + 2 else break end
185
+ end
186
+ elseif k == 'id' and IMPORTS[T:text(i)] then
187
+ local arg = is(T, i + 1, '(') and i + 2 or i + 1
188
+ if T.k[arg] == 'str' then
189
+ local v = string_value(T, arg)
190
+ if v then r.imports[#r.imports + 1] = { path = v, line = T:line(i), kind = T:text(i) } end
191
+ end
192
+ end
193
+ end
194
+
195
+ -- Calls: `f(...)`, plus Lua's paren-less `f "s"` / `f {...}` forms.
196
+ common.add_calls(r, T, 1, T.n)
197
+ for i = 1, T.n - 1 do
198
+ if T.k[i] == 'id' and not defs[i] and (T.k[i + 1] == 'str' or is(T, i + 1, '{'))
199
+ and not (T.k[i - 1] == 'kw' and T:text(i - 1) == 'local') then
200
+ local kind, recv = 'call', nil
201
+ if is(T, i - 1, '.') or is(T, i - 1, ':') then
202
+ kind = 'member_call'
203
+ if T.k[i - 2] == 'id' then recv = T:text(i - 2) end
204
+ end
205
+ r.refs[#r.refs + 1] = { tok = i, name = T:text(i), kind = kind, line = T:line(i), recv = recv }
206
+ end
207
+ end
208
+ -- A definition's own name token is never a call.
209
+ local kept = {}
210
+ for _, ref in ipairs(r.refs) do
211
+ if not defs[ref.tok] then kept[#kept + 1] = ref end
212
+ end
213
+ r.refs = kept
214
+ r.member_ops = nil
215
+ return common.finish(r), T
216
+ end
217
+
218
+ return M
@@ -0,0 +1,184 @@
1
+ -- lang/python.lua — index-level Python parser: classes (+ bases), functions,
2
+ -- methods, decorators, module/class-level assignments, imports, and calls.
3
+ --
4
+ -- xscan's indent mode turns blocks into indent..dedent pairs in the match
5
+ -- table, so a block's extent is one lookup and nesting falls out of recursion.
6
+
7
+ local xscan = require('codeoutline.xscan')
8
+ local common = require('codeoutline.common')
9
+
10
+ local M = {}
11
+
12
+ M.config = {
13
+ keywords = {
14
+ 'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
15
+ 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
16
+ 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
17
+ 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield',
18
+ },
19
+ ops = {
20
+ '**=', '//=', '>>=', '<<=', '->', ':=', '**', '//', '<<', '>>', '<=', '>=', '==',
21
+ '!=', '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '@=', '...',
22
+ },
23
+ line_comment = { '#' },
24
+ strings = {
25
+ { '"""', '"""', escape = '\\', multiline = true },
26
+ { "'''", "'''", escape = '\\', multiline = true },
27
+ { '"', '"', escape = '\\' },
28
+ { "'", "'", escape = '\\' },
29
+ },
30
+ string_prefixes = 'rbfu',
31
+ indent = true,
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
+ -- End of the logical line starting at i: the 'nl' token at bracket depth 0.
40
+ local function line_end(T, i, b)
41
+ local j = i
42
+ while j <= b and T.k[j] ~= 'nl' do
43
+ if T.m[j] and T.m[j] > j and T.k[j] == 'op' then j = T.m[j] + 1 else j = j + 1 end
44
+ end
45
+ return math.min(j, b)
46
+ end
47
+
48
+ -- Dotted name starting at i ("a.b.c"); returns text and next index.
49
+ local function dotted(T, i)
50
+ local parts, j = {}, i
51
+ while T.k[j] == 'id' or (T.k[j] == 'op' and T:text(j) == '.') do
52
+ parts[#parts + 1] = T:text(j)
53
+ j = j + 1
54
+ end
55
+ return table.concat(parts), j
56
+ end
57
+
58
+ local function parse_imports(T, r, i, e)
59
+ if kw(T, i, 'import') then
60
+ local j = i + 1
61
+ while j < e do
62
+ local name, nj = dotted(T, j)
63
+ if name ~= '' then r.imports[#r.imports + 1] = { path = name, line = T:line(i) } end
64
+ j = nj
65
+ if kw(T, j, 'as') then j = j + 2 end
66
+ if is(T, j, ',') then j = j + 1 elseif name == '' then j = j + 1 end
67
+ end
68
+ else -- from X import a, b
69
+ local j = i + 1
70
+ local mod = ''
71
+ while is(T, j, '.') or is(T, j, '...') do mod = mod .. T:text(j); j = j + 1 end
72
+ local name
73
+ name, j = dotted(T, j)
74
+ mod = mod .. name
75
+ local names = {}
76
+ for x = j, e do
77
+ if T.k[x] == 'id' and not kw(T, x - 1, 'as') then names[#names + 1] = T:text(x) end
78
+ if T.k[x] == 'op' and T:text(x) == '*' then names[#names + 1] = '*' end
79
+ end
80
+ r.imports[#r.imports + 1] = { path = mod, line = T:line(i), names = names }
81
+ end
82
+ end
83
+
84
+ local parse_block
85
+
86
+ -- Statement header [i, e] (e is 'nl'); if followed by an indented block,
87
+ -- returns the block's indent and dedent indices.
88
+ local function block_after(T, e)
89
+ local o = e + 1
90
+ if T.k[o] == 'indent' and T.m[o] then return o, T.m[o] end
91
+ return nil
92
+ end
93
+
94
+ parse_block = function(T, r, a, b, parent, in_class)
95
+ local i = a
96
+ local decorators, deco_start = nil, nil
97
+ while i <= b do
98
+ local k = T.k[i]
99
+ if k == 'nl' or k == 'indent' or k == 'dedent' then
100
+ i = i + 1
101
+ else
102
+ local e = line_end(T, i, b)
103
+ local bo, bc = block_after(T, e)
104
+ local head = i
105
+ if kw(T, head, 'async') then head = head + 1 end
106
+
107
+ if is(T, i, '@') then
108
+ decorators = decorators or {}
109
+ deco_start = deco_start or i
110
+ decorators[#decorators + 1] = dotted(T, i + 1)
111
+ common.add_calls(r, T, i, e)
112
+ i = e + 1
113
+ elseif kw(T, head, 'def') or kw(T, head, 'class') then
114
+ local is_class = kw(T, head, 'class')
115
+ local name_tok = head + 1
116
+ local stop = bc or e
117
+ -- A block's dedent sits at the next statement's first token,
118
+ -- so the block ends on the line of the last token before it.
119
+ local last = stop
120
+ while last > i and (T.k[last] == 'dedent' or T.k[last] == 'nl') do last = last - 1 end
121
+ local node = common.add_node(r, T, {
122
+ kind = is_class and 'class' or (in_class and 'method' or 'function'),
123
+ name = T:text(name_tok), parent = parent,
124
+ ti = deco_start or i, tj = stop, end_line = T:eline(last),
125
+ sig = common.sig(T, i, e - 1),
126
+ decorators = decorators,
127
+ async = head ~= i or nil,
128
+ })
129
+ if is_class and is(T, name_tok + 1, '(') and T.m[name_tok + 1] then
130
+ local bases = {}
131
+ local x = name_tok + 2
132
+ while x < T.m[name_tok + 1] do
133
+ local nm, nx = dotted(T, x)
134
+ if nm ~= '' and not is(T, nx, '=') then bases[#bases + 1] = nm end
135
+ -- skip to next top-level ','
136
+ x = nx
137
+ while x < T.m[name_tok + 1] and not is(T, x, ',') do
138
+ x = (T.m[x] and T.m[x] > x) and T.m[x] + 1 or x + 1
139
+ end
140
+ x = x + 1
141
+ end
142
+ r.nodes[node].bases = bases
143
+ end
144
+ -- Calls in defaults, base-class args, and a one-line body;
145
+ -- start past `def name` so the definition isn't a call to itself.
146
+ common.add_calls(r, T, name_tok + 1, e)
147
+ if bo then
148
+ parse_block(T, r, bo + 1, bc - 1, node, is_class)
149
+ end
150
+ decorators, deco_start = nil, nil
151
+ i = (bc or e) + 1
152
+ else
153
+ decorators, deco_start = nil, nil
154
+ if kw(T, i, 'import') or kw(T, i, 'from') then
155
+ parse_imports(T, r, i, e)
156
+ elseif T.k[i] == 'id' and (is(T, i + 1, '=') or (is(T, i + 1, ':') and not bo))
157
+ and (parent == nil or in_class) then
158
+ -- NAME = ... / NAME: type = ... at module or class level
159
+ common.add_node(r, T, {
160
+ kind = in_class and 'field' or 'variable', name = T:text(i),
161
+ parent = parent, ti = i, tj = e, sig = common.sig(T, i, e - 1, 120),
162
+ })
163
+ end
164
+ common.add_calls(r, T, i, e)
165
+ if bo then
166
+ -- if/for/while/try/with blocks: defs inside keep the same owner
167
+ parse_block(T, r, bo + 1, bc - 1, parent, in_class)
168
+ i = bc + 1
169
+ else
170
+ i = e + 1
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+ function M.parse(path, src)
178
+ local T = M.lang:tokenize(src)
179
+ local r = common.new(path, 'python')
180
+ parse_block(T, r, 1, T.n, nil, false)
181
+ return common.finish(r), T
182
+ end
183
+
184
+ return M
@@ -0,0 +1,276 @@
1
+ -- lang/rust.lua — index-level Rust parser: modules (inline and `mod x;`),
2
+ -- `use` imports, functions, structs (+ named fields), enums (+ variants),
3
+ -- unions, traits (+ method signatures / default bodies), impl blocks (methods
4
+ -- qualified by the implementing type, `impl Trait for Type` recorded),
5
+ -- type aliases, consts / statics, macro_rules!, and calls incl. `name!(..)`.
6
+
7
+ local xscan = require('codeoutline.xscan')
8
+ local common = require('codeoutline.common')
9
+
10
+ local M = {}
11
+
12
+ M.config = {
13
+ keywords = {
14
+ 'as', 'async', 'await', 'break', 'const', 'continue', 'crate', 'dyn', 'else', 'enum',
15
+ 'extern', 'false', 'fn', 'for', 'if', 'impl', 'in', 'let', 'loop', 'match', 'mod', 'move',
16
+ 'mut', 'pub', 'ref', 'return', 'self', 'Self', 'static', 'struct', 'super', 'trait', 'true',
17
+ 'type', 'unsafe', 'use', 'where', 'while',
18
+ },
19
+ ops = {
20
+ '..=', '...', '<<=', '>>=', '->', '=>', '::', '..', '==', '!=', '<=', '>=', '&&', '||',
21
+ '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<', '>>',
22
+ },
23
+ line_comment = { '//' },
24
+ block_comment = { { '/*', '*/' } },
25
+ strings = {
26
+ { '"', '"', escape = '\\', multiline = true },
27
+ { "'", "'", escape = '\\' },
28
+ },
29
+ string_prefixes = 'bc',
30
+ lifetimes = true,
31
+ raw_strings = true,
32
+ nested_comments = true,
33
+ }
34
+
35
+ M.lang = xscan.lang(M.config)
36
+
37
+ local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
38
+ local function kw(T, i, s) return T.k[i] == 'kw' and T:text(i) == s end
39
+
40
+ local function angle(T, i)
41
+ if T.k[i] ~= 'op' then return 0 end
42
+ local t = T:text(i)
43
+ if t == '<' then return 1 elseif t == '>' then return -1 elseif t == '>>' then return -2 end
44
+ return 0
45
+ end
46
+
47
+ -- Index after a generic list at '<' i.
48
+ local function skip_angle(T, i, limit)
49
+ if not is(T, i, '<') then return i end
50
+ local d, x = 0, i
51
+ while x <= limit do
52
+ if (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
53
+ x = T.m[x]
54
+ elseif not is(T, x, '->') then
55
+ d = d + angle(T, x)
56
+ if d <= 0 then return x + 1 end
57
+ end
58
+ x = x + 1
59
+ end
60
+ return i + 1
61
+ end
62
+
63
+ -- Item end: the '{' body's match, or the terminating ';' at depth 0.
64
+ local function item_end(T, x, b)
65
+ local d = 0
66
+ while x <= b do
67
+ if is(T, x, ';') and d == 0 then return x, nil end
68
+ if is(T, x, '{') and d == 0 then return T.m[x] or b, x end
69
+ if (is(T, x, '(') or is(T, x, '[')) and T.m[x] then
70
+ x = T.m[x]
71
+ elseif not is(T, x, '->') and not is(T, x, '=>') then
72
+ d = math.max(0, d + angle(T, x))
73
+ end
74
+ x = x + 1
75
+ end
76
+ return b, nil
77
+ end
78
+
79
+ -- Type name in `impl<T> Trait<X> for Type<Y> where ...` / `impl Type`.
80
+ local function impl_target(T, x, body)
81
+ x = skip_angle(T, x, body)
82
+ local first, trait
83
+ local d, y = 0, x
84
+ local names = {}
85
+ while y < body do
86
+ if T.k[y] == 'kw' and T:text(y) == 'where' and d == 0 then break end
87
+ if kw(T, y, 'for') and d == 0 then
88
+ trait = names[#names]
89
+ names = {}
90
+ elseif (T.k[y] == 'id' or kw(T, y, 'Self')) and d == 0 and not is(T, y + 1, '::')
91
+ and T:text(y):sub(1, 1) ~= "'" then -- 'static is a lifetime, not the type
92
+ names[#names + 1] = T:text(y)
93
+ end
94
+ d = math.max(0, d + angle(T, y))
95
+ y = y + 1
96
+ end
97
+ first = names[1]
98
+ if not first then
99
+ -- `impl Trait for ()` / tuples: no identifier, use the type's text
100
+ local from = x
101
+ for z = x, body - 1 do if kw(T, z, 'for') then from = z + 1 end end
102
+ local to = body - 1
103
+ for z = from, body - 1 do if T.k[z] == 'kw' and T:text(z) == 'where' then to = z - 1; break end end
104
+ if to >= from then first = common.sig(T, from, to, 60):gsub('%s', '') end
105
+ end
106
+ return first, trait
107
+ end
108
+
109
+ -- Record calls in [a, b], including macro invocations `name!(..)` / `name![..]`.
110
+ local function calls(r, T, a, b)
111
+ common.add_calls(r, T, a, b)
112
+ for x = a, b - 2 do
113
+ if T.k[x] == 'id' and is(T, x + 1, '!') and (is(T, x + 2, '(') or is(T, x + 2, '[') or is(T, x + 2, '{')) then
114
+ r.refs[#r.refs + 1] = { tok = x, name = T:text(x), kind = 'macro', line = T:line(x) }
115
+ end
116
+ end
117
+ end
118
+
119
+ local parse_items
120
+
121
+ -- Named fields / variants inside '{' o .. '}' c (struct, enum, union).
122
+ local function body_members(T, r, o, c, parent, kind)
123
+ local x = o + 1
124
+ while x < c do
125
+ while is(T, x, '#') and is(T, x + 1, '[') and T.m[x + 1] do x = T.m[x + 1] + 1 end
126
+ if kw(T, x, 'pub') then
127
+ x = x + 1
128
+ if is(T, x, '(') and T.m[x] then x = T.m[x] + 1 end
129
+ end
130
+ if T.k[x] == 'id' and (kind == 'enum' or is(T, x + 1, ':')) then
131
+ common.add_node(r, T, { kind = kind == 'enum' and 'enum_member' or 'field', name = T:text(x),
132
+ parent = parent, ti = x, tj = x })
133
+ end
134
+ while x < c and not is(T, x, ',') do
135
+ if T.m[x] and T.m[x] > x then x = T.m[x] + 1
136
+ elseif is(T, x, '<') then x = skip_angle(T, x, c)
137
+ else x = x + 1 end
138
+ end
139
+ x = x + 1
140
+ end
141
+ end
142
+
143
+ -- Items in [a, b]; impl_type qualifies methods inside impl blocks.
144
+ parse_items = function(T, r, a, b, parent, impl_type, in_trait)
145
+ local i = a
146
+ while i <= b do
147
+ -- attributes, visibility and qualifiers before the item keyword
148
+ local start = i
149
+ local x = i
150
+ while true do
151
+ if is(T, x, '#') and (is(T, x + 1, '[') or (is(T, x + 1, '!') and is(T, x + 2, '['))) then
152
+ local o = is(T, x + 1, '[') and x + 1 or x + 2
153
+ x = (T.m[o] or o) + 1
154
+ start = x
155
+ elseif kw(T, x, 'pub') then
156
+ x = x + 1
157
+ if is(T, x, '(') and T.m[x] then x = T.m[x] + 1 end
158
+ elseif kw(T, x, 'async') or kw(T, x, 'unsafe') or (kw(T, x, 'const') and (kw(T, x + 1, 'fn')
159
+ or kw(T, x + 1, 'unsafe') or kw(T, x + 1, 'async') or kw(T, x + 1, 'extern')))
160
+ or (T.k[x] == 'id' and T:text(x) == 'default' and (kw(T, x + 1, 'fn') or kw(T, x + 1, 'type'))) then
161
+ x = x + 1
162
+ elseif kw(T, x, 'extern') and (kw(T, x + 1, 'fn') or (T.k[x + 1] == 'str' and kw(T, x + 2, 'fn'))) then
163
+ x = x + (T.k[x + 1] == 'str' and 2 or 1)
164
+ else
165
+ break
166
+ end
167
+ end
168
+ if x > b then break end
169
+ local t = T.k[x] == 'kw' and T:text(x) or (T.k[x] == 'id' and T:text(x))
170
+ local stop
171
+ if t == 'fn' and T.k[x + 1] == 'id' then
172
+ local name_tok = x + 1
173
+ local p = skip_angle(T, x + 2, b)
174
+ local e, body = item_end(T, p, b)
175
+ local node = common.add_node(r, T, {
176
+ kind = (impl_type or in_trait) and 'method' or 'function', name = T:text(name_tok),
177
+ parent = parent, ti = start, tj = e,
178
+ sig = common.sig(T, start, body and body - 1 or e, 200), abstract = (not body) or nil,
179
+ })
180
+ if impl_type then r.nodes[node].qualified = impl_type .. '::' .. T:text(name_tok) end
181
+ if body then
182
+ calls(r, T, body, e)
183
+ -- nested items (fn inside fn) are rare; bodies are not re-parsed
184
+ end
185
+ stop = e
186
+ elseif (t == 'struct' or t == 'enum' or t == 'union') and T.k[x + 1] == 'id' then
187
+ local e, body = item_end(T, x + 2, b)
188
+ local node = common.add_node(r, T, { kind = t == 'enum' and 'enum' or 'struct', name = T:text(x + 1),
189
+ parent = parent, ti = start, tj = e, sig = common.sig(T, start, body and body - 1 or e, 160) })
190
+ if body then body_members(T, r, body, e, node, t) end
191
+ stop = e
192
+ elseif t == 'trait' and T.k[x + 1] == 'id' then
193
+ local e, body = item_end(T, x + 2, b)
194
+ local node = common.add_node(r, T, { kind = 'interface', name = T:text(x + 1), parent = parent,
195
+ ti = start, tj = e, sig = common.sig(T, start, body and body - 1 or e, 160) })
196
+ if body then parse_items(T, r, body + 1, e - 1, node, nil, true) end
197
+ stop = e
198
+ elseif t == 'impl' then
199
+ local e, body = item_end(T, x + 1, b)
200
+ if body then
201
+ local ty, trait = impl_target(T, x + 1, body)
202
+ local node = common.add_node(r, T, { kind = 'impl', name = ty or 'impl', parent = parent,
203
+ ti = start, tj = e, sig = common.sig(T, start, body - 1, 160), trait = trait })
204
+ r.nodes[node].qualified = ty or 'impl'
205
+ parse_items(T, r, body + 1, e - 1, node, ty, false)
206
+ end
207
+ stop = e
208
+ elseif t == 'mod' and T.k[x + 1] == 'id' then
209
+ local e, body = item_end(T, x + 2, b)
210
+ if body then
211
+ local node = common.add_node(r, T, { kind = 'namespace', name = T:text(x + 1), parent = parent,
212
+ ti = start, tj = e })
213
+ parse_items(T, r, body + 1, e - 1, node, nil, false)
214
+ else
215
+ -- `mod x;` declares a file module: a node and an import of x.rs / x/mod.rs
216
+ common.add_node(r, T, { kind = 'namespace', name = T:text(x + 1), parent = parent, ti = start, tj = e })
217
+ r.imports[#r.imports + 1] = { path = T:text(x + 1), line = T:line(x), kind = 'mod' }
218
+ end
219
+ stop = e
220
+ elseif t == 'use' then
221
+ local e = item_end(T, x + 1, b)
222
+ r.imports[#r.imports + 1] = { path = common.sig(T, x + 1, e - 1, 400):gsub('%s', ''), line = T:line(x) }
223
+ stop = e
224
+ elseif (t == 'type') and T.k[x + 1] == 'id' then
225
+ local e = item_end(T, x + 2, b)
226
+ common.add_node(r, T, { kind = 'typedef', name = T:text(x + 1), parent = parent, ti = start, tj = e,
227
+ sig = common.sig(T, start, e, 160) })
228
+ stop = e
229
+ elseif (t == 'const' or t == 'static') then
230
+ local y = x + 1
231
+ if kw(T, y, 'mut') then y = y + 1 end
232
+ local e = item_end(T, y, b)
233
+ if T.k[y] == 'id' then
234
+ local node = common.add_node(r, T, { kind = t == 'const' and 'constant' or 'variable', name = T:text(y),
235
+ parent = parent, ti = start, tj = e, sig = common.sig(T, start, math.min(e, y + 2), 120) })
236
+ if impl_type then r.nodes[node].qualified = impl_type .. '::' .. T:text(y) end
237
+ calls(r, T, y, e)
238
+ end
239
+ stop = e
240
+ elseif t == 'macro_rules' and is(T, x + 1, '!') and T.k[x + 2] == 'id' then
241
+ local o = x + 3
242
+ local e = (T.m[o] or o)
243
+ if is(T, e + 1, ';') then e = e + 1 end
244
+ common.add_node(r, T, { kind = 'macro', name = T:text(x + 2), parent = parent, ti = start, tj = e })
245
+ stop = e
246
+ elseif kw(T, x, 'extern') and kw(T, x + 1, 'crate') then
247
+ local e = item_end(T, x, b)
248
+ if T.k[x + 2] == 'id' then r.imports[#r.imports + 1] = { path = T:text(x + 2), line = T:line(x), kind = 'crate' } end
249
+ stop = e
250
+ elseif kw(T, x, 'extern') and T.k[x + 1] == 'str' and is(T, x + 2, '{') and T.m[x + 2] then
251
+ parse_items(T, r, x + 3, T.m[x + 2] - 1, parent, nil, false) -- extern "C" { fn ...; }
252
+ stop = T.m[x + 2]
253
+ else
254
+ -- top-level macro invocation or anything else: skip one token/group
255
+ if T.k[x] == 'id' and is(T, x + 1, '!') then
256
+ local e = item_end(T, x + 2, b)
257
+ calls(r, T, x, e)
258
+ stop = e
259
+ else
260
+ stop = x
261
+ end
262
+ end
263
+ i = math.max(stop, i) + 1
264
+ end
265
+ end
266
+
267
+ function M.parse(path, src)
268
+ local T = M.lang:tokenize(src)
269
+ local r = common.new(path, 'rust')
270
+ parse_items(T, r, 1, T.n, nil, nil, false)
271
+ -- '::' is the path separator: fix qualified names built with '.'
272
+ for _, n in ipairs(r.nodes) do n.qualified = n.qualified:gsub('%.', '::') end
273
+ return common.finish(r), T
274
+ end
275
+
276
+ return M