1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
package main
import "core:strings"
// Symbol extraction is per-language: the diff only shows text, so each
// language needs its own idea of what a declaration looks like.
Lang :: enum {
Odin,
Rust,
C_Family, // .c .h .cc .cpp .cxx .hpp .hh .hxx .ipp .tpp .inl
Python, // .py
Ruby, // .rb
Other,
}
// C_EXTENSIONS are the suffixes handed to ctags. `.h' is shared by C and C++,
// which is why the family is named C_Family and not Cpp: universal-ctags picks
// the dialect from the extension, and for a header that could be either it reads
// the syntax. The list lives here so that `lang_of', the commit's file list and
// symbol_name all agree on what a C-family file is.
C_EXTENSIONS :: [?]string{
".c",
".h",
".cc",
".cpp",
".cxx",
".hpp",
".hh",
".hxx",
".ipp",
".tpp",
".inl",
}
lang_of :: proc(file: string) -> Lang {
switch {
case strings.has_suffix(file, ".odin"):
return .Odin
case strings.has_suffix(file, ".rs"):
return .Rust
case strings.has_suffix(file, ".py"):
return .Python
case strings.has_suffix(file, ".rb"):
return .Ruby
}
for ext in C_EXTENSIONS {
if strings.has_suffix(file, ext) {
return .C_Family
}
}
return .Other
}
// parse_decl reports the declaration NAME and KIND introduced or removed by a
// single line of a diff, if that line declares something.
parse_decl :: proc(file, line: string) -> (name: string, kind: string, ok: bool) {
switch lang_of(file) {
case .Odin:
return parse_odin_decl(line)
case .Rust:
return parse_rust_decl(line)
case .Python:
return parse_python_decl(line)
case .Ruby:
return parse_ruby_decl(line)
case .C_Family:
// Deliberately no lexical scan. In C and C++ a diff line cannot say
// whether it declares anything: inside a body `foo(bar);' is a call and
// inside a class body the same bytes declare a constructor, and both are
// indented the same way. Declarations come from ctags over the whole
// file instead -- see blob_tags in git.odin -- and what a hunk line can
// still give us is the enclosing function, which is the hunk-header path.
case .Other:
}
return "", "", false
}
// c_family_kind maps a universal-ctags kind name to the store's vocabulary,
// rather than inventing a second one. The kinds left out are the ones that
// churn without changing what a hunk was about: members and enumerators and
// variables inside a body, namespaces (structure, not behaviour), and the
// bookkeeping tags (header, parameter) that say nothing about a declaration.
c_family_kind :: proc(tag_kind: string) -> (kind: string, ok: bool) {
switch tag_kind {
case "function", "prototype", "method":
return "fn", true
case "class", "struct", "union", "enum", "typedef":
return "type", true
case "macro":
// A macro is a named constant as far as a reader is concerned.
return "const", true
case "variable", "externvar":
return "var", true
}
return "", false
}
// -- Odin -------------------------------------------------------------------
//
// NAME :: proc ... -> proc
// NAME :: struct/enum/... -> type
// NAME :: value -> const
// NAME: Type -> var (top level only: struct fields are indented)
// NAME := value -> var (top level only)
parse_odin_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t')
s := strings.trim_left_space(line)
if len(s) == 0 || s[0] == '@' || s[0] == '/' || s[0] == '}' || s[0] == '#' {
return "", "", false
}
if sep := strings.index(s, "::"); sep >= 0 {
lhs := strings.trim_space(s[:sep])
if !is_ident(lhs) {
return "", "", false
}
rhs := strings.trim_space(s[sep + 2:])
switch {
case strings.has_prefix(rhs, "proc"):
return lhs, "proc", true
case strings.has_prefix(rhs, "struct"),
strings.has_prefix(rhs, "enum"),
strings.has_prefix(rhs, "union"),
strings.has_prefix(rhs, "bit_set"),
strings.has_prefix(rhs, "distinct"),
strings.has_prefix(rhs, "#type"),
strings.has_prefix(rhs, "map["),
strings.has_prefix(rhs, "matrix["),
strings.has_prefix(rhs, "proc{"):
return lhs, "type", true
case:
return lhs, "const", true
}
}
// Globals: only when not indented, otherwise this is a struct field.
if indented {
return "", "", false
}
if sep := strings.index(s, ":="); sep > 0 {
lhs := strings.trim_space(s[:sep])
if is_ident(lhs) {
return lhs, "var", true
}
}
if sep := strings.index_byte(s, ':'); sep > 0 {
lhs := strings.trim_space(s[:sep])
if is_ident(lhs) {
return lhs, "var", true
}
}
return "", "", false
}
// -- Rust -------------------------------------------------------------------
//
// fn NAME / pub fn NAME -> fn (methods included; they are indented)
// struct/enum/trait/type/mod -> type
// const NAME: / static NAME: -> const
parse_rust_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
s := strings.trim_left_space(line)
if len(s) == 0 || s[0] == '/' || s[0] == '#' || s[0] == '}' {
return "", "", false
}
// Drop modifiers so that `pub(crate) async unsafe fn` still matches.
for {
switch {
case strings.has_prefix(s, "pub(crate) "):
s = strings.trim_left_space(s[len("pub(crate) "):])
case strings.has_prefix(s, "pub(super) "):
s = strings.trim_left_space(s[len("pub(super) "):])
case strings.has_prefix(s, "pub "):
s = strings.trim_left_space(s[len("pub "):])
case strings.has_prefix(s, "async "):
s = strings.trim_left_space(s[len("async "):])
case strings.has_prefix(s, "unsafe "):
s = strings.trim_left_space(s[len("unsafe "):])
case strings.has_prefix(s, "extern "):
// `extern "C" fn foo'
s = strings.trim_left_space(s[len("extern "):])
if len(s) > 0 && s[0] == '"' {
if end := strings.index_byte(s[1:], '"'); end >= 0 {
s = strings.trim_left_space(s[end + 2:])
}
}
case strings.has_prefix(s, "const fn "):
// const fn is a function, not a constant
if n, nok := ident_after(s, "const fn "); nok {
return n, "fn", true
}
return "", "", false
case strings.has_prefix(s, "default "), strings.has_prefix(s, "unsafe impl "):
s = strings.trim_left_space(s[strings.index_byte(s, ' ') + 1:])
case:
}
break
}
entries := [?]struct {
prefix: string,
kind: string,
}{
{"fn ", "fn"},
{"struct ", "type"},
{"enum ", "type"},
{"trait ", "type"},
{"type ", "type"},
{"mod ", "type"},
{"union ", "type"},
{"const ", "const"},
{"static ", "const"},
}
for e in entries {
if strings.has_prefix(s, e.prefix) {
if n, nok := ident_after(s, e.prefix); nok {
return n, e.kind, true
}
return "", "", false
}
}
return "", "", false
}
// -- Python -----------------------------------------------------------------
//
// def NAME(...): -> fn (and `async def')
// class NAME: -> type
// NAME = value -> const (module level only; indented is inside a def)
// NAME: Type -> var (module level only)
//
// A line scanner is the right tool here, unlike C and C++: a Python
// declaration is the first thing on its line, so there is no statement that
// can look like one -- `foo(bar)` is a call and `def foo(bar):` is not, and the
// keyword settles it. What it cannot see is a definition built at run time
// (`f = lambda x: x`, a `def` inside an `exec`), which is why the gist of a
// symbol is a name and the file it lives in, not a promise about the AST.
parse_python_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t')
s := strings.trim_left_space(line)
if len(s) == 0 || s[0] == '#' || s[0] == '@' || s[0] == '"' || s[0] == '\'' {
return "", "", false
}
if strings.has_prefix(s, "async def ") {
if n, nok := ident_after(s, "async def "); nok {
return n, "fn", true
}
return "", "", false
}
if strings.has_prefix(s, "def ") {
if n, nok := ident_after(s, "def "); nok {
return n, "fn", true
}
return "", "", false
}
if strings.has_prefix(s, "class ") {
if n, nok := ident_after(s, "class "); nok {
return n, "type", true
}
return "", "", false
}
if indented {
// Inside a def or a class body: an assignment there is a local, not a
// declaration a reader asks "why" about.
return "", "", false
}
if n, aok := assignment_name(s); aok {
return n, "const", true
}
if sep := strings.index_byte(s, ':'); sep > 0 {
if lhs := strings.trim_space(s[:sep]); is_ident(lhs) && !strings.contains(s, "=") {
return lhs, "var", true
}
}
return "", "", false
}
// -- Ruby -------------------------------------------------------------------
//
// def NAME / def self.NAME / def NAME? -> fn
// class NAME / module NAME -> type
// NAME = value -> const (module level, capitalised)
//
// Ruby's declarations are line-anchored in the same way, with one trap the
// callers of this store care about: `def` and `class` also appear inside
// strings and heredocs, and no line scanner can tell. Anchoring on the first
// token keeps that rare.
parse_ruby_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t')
s := strings.trim_left_space(line)
if len(s) == 0 || s[0] == '#' || s[0] == '=' {
return "", "", false
}
if strings.has_prefix(s, "def ") {
rest := s[len("def "):]
if strings.has_prefix(rest, "self.") {
rest = rest[len("self."):]
}
if n, nok := ident_after(rest, ""); nok {
// `ready?', `save!', `name=' are one identifier to Ruby.
return strings.trim_right(n, "?!=:"), "fn", true
}
return "", "", false
}
if strings.has_prefix(s, "class ") || strings.has_prefix(s, "module ") {
prefix := strings.has_prefix(s, "class ") ? "class " : "module "
if n, nok := ident_after(s, prefix); nok {
return n, "type", true
}
return "", "", false
}
if indented {
return "", "", false
}
// A Ruby constant is capitalised; `x = 1` at column 0 is a local in a
// script and not worth a symbol.
if n, aok := assignment_name(s); aok && n[0] >= 'A' && n[0] <= 'Z' {
return n, "const", true
}
return "", "", false
}
// assignment_name reports NAME from `NAME = value' (not `NAME == value').
assignment_name :: proc(s: string) -> (name: string, ok: bool) {
for i in 0 ..< len(s) {
if s[i] != '=' {
continue
}
if i + 1 < len(s) && s[i + 1] == '=' {
return "", false
}
if i > 0 && (s[i - 1] == '!' || s[i - 1] == '<' || s[i - 1] == '>' || s[i - 1] == '=') {
return "", false
}
lhs := strings.trim_space(s[:i])
if is_ident(lhs) {
return lhs, true
}
return "", false
}
return "", false
}
// ident_after reads the identifier that follows PREFIX in S.
ident_after :: proc(s, prefix: string) -> (name: string, ok: bool) {
rest := strings.trim_left_space(s[len(prefix):])
end := 0
for end < len(rest) {
c := rest[end]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' {
end += 1
} else {
break
}
}
if end == 0 {
return "", false
}
return rest[:end], true
}
is_ident :: proc(s: string) -> bool {
if len(s) == 0 {
return false
}
for c, i in s {
switch {
case c == '_':
case c >= 'a' && c <= 'z':
case c >= 'A' && c <= 'Z':
case c >= '0' && c <= '9' && i > 0:
case:
return false
}
}
return true
}
|