(source: string)
| 409 | * the first lookup — most files read here are only looked into. |
| 410 | */ |
| 411 | export function readRustScopes(source: string): RustScopes { |
| 412 | const code = blankRustCode(source); |
| 413 | const n = code.length; |
| 414 | const lineStarts = [0]; |
| 415 | for (let i = code.indexOf('\n'); i >= 0; i = code.indexOf('\n', i + 1)) lineStarts.push(i + 1); |
| 416 | const close = pairBrackets(code); |
| 417 | |
| 418 | const fns: RustFn[] = []; |
| 419 | for (const m of code.matchAll(/\bfn\b/g)) { |
| 420 | let p = skipSpace(code, m.index! + 2); |
| 421 | const name = identAt(code, p); |
| 422 | if (!name) continue; |
| 423 | p = skipSpace(code, p + name.length); |
| 424 | let generics = ''; |
| 425 | if (code[p] === '<') { |
| 426 | const end = angleClose(code, p); |
| 427 | if (end < 0) continue; |
| 428 | generics = code.slice(p + 1, end); |
| 429 | p = skipSpace(code, end + 1); |
| 430 | } |
| 431 | if (code[p] !== '(') continue; |
| 432 | const paramsEnd = close.get(p); |
| 433 | if (paramsEnd === undefined) continue; |
| 434 | // The body: the first `{` past the return type and `where` clause; a `;` |
| 435 | // first is a declaration, which binds nothing. |
| 436 | let body = -1; |
| 437 | let where = -1; |
| 438 | for (let q = paramsEnd + 1; q < n; q++) { |
| 439 | const c = code[q]!; |
| 440 | if (c === '{') { body = q; break; } |
| 441 | if (c === ';' || c === '}') break; |
| 442 | if (c === '(' || c === '[') { q = close.get(q) ?? n; continue; } |
| 443 | if (where < 0 && c === 'w' && wordAt(code, q, 'where')) where = q + 5; |
| 444 | } |
| 445 | if (body < 0) continue; |
| 446 | fns.push({ at: m.index!, generics, where: where < 0 ? '' : code.slice(where, body), from: p, to: close.get(body) ?? n, paramsEnd, body }); |
| 447 | } |
| 448 | |
| 449 | // `impl<T: Bound> … {` and `trait Name<T>: Super where … {` — an item, not |
| 450 | // the `impl Trait` of a type, which follows a `:`, `(`, `,`, `<`, `&` or `->`. |
| 451 | const impls: RustImplBlock[] = []; |
| 452 | for (const m of code.matchAll(/\b(impl|trait)\b/g)) { |
| 453 | const kind = m[1] as 'impl' | 'trait'; |
| 454 | if (kind === 'impl') { |
| 455 | let k = m.index! - 1; |
| 456 | while (k >= 0 && /\s/.test(code[k]!)) k--; |
| 457 | if (k >= 0 && !'{};]'.includes(code[k]!) && !/\b(?:unsafe|default)$/.test(code.slice(Math.max(0, k - 7), k + 1))) continue; |
| 458 | } |
| 459 | let p = skipSpace(code, m.index! + kind.length); |
| 460 | if (kind === 'trait') { |
| 461 | const name = identAt(code, p); |
| 462 | if (!name) continue; |
| 463 | p = skipSpace(code, p + name.length); |
| 464 | } |
| 465 | let generics = ''; |
| 466 | if (code[p] === '<') { |
| 467 | const end = angleClose(code, p); |
| 468 | if (end < 0) continue; |
nothing calls this directly
no test coverage detected