The names a Rust file binds (RustBinding), read from what readRustScopes kept of it.
(scopes: RustScopes)
| 504 | |
| 505 | /** The names a Rust file binds (RustBinding), read from what readRustScopes kept of it. */ |
| 506 | function readRustBindings(scopes: RustScopes): Map<string, RustBinding[]> { |
| 507 | const { code, close, events, openAfter } = scopes; |
| 508 | const n = code.length; |
| 509 | const bindings = new Map<string, RustBinding[]>(); |
| 510 | const add = (name: string, binding: RustBinding): void => { |
| 511 | const list = bindings.get(name); |
| 512 | if (list) list.push(binding); |
| 513 | else bindings.set(name, [binding]); |
| 514 | }; |
| 515 | const bindPattern = (from: number, to: number, kind: RustBinding['kind'], scopeFrom: number, scopeTo: number, scrutinee?: string): void => { |
| 516 | const emit: Emit = (name, at, site) => add(name, { at, kind, from: scopeFrom, to: scopeTo, site, scrutinee }); |
| 517 | // A tuple pattern, `(k, v)` or `(i, mut x)`: a name alone at its top is |
| 518 | // the tuple's field at its place (a site with no path). |
| 519 | const open = skipSpace(code, from); |
| 520 | const end = code[open] === '(' ? close.get(open) : undefined; |
| 521 | if (end !== undefined && end < to && skipSpace(code, end + 1) >= to) { |
| 522 | let index = 0; |
| 523 | for (const [a, b] of splitTop(code, open + 1, end, close, false)) { |
| 524 | const text = code.slice(a, b); |
| 525 | const simple = /^\s*(?:ref\s+)?(?:mut\s+)?([A-Za-z_]\w*)\s*$/.exec(text); |
| 526 | if (simple && isBindingName(simple[1]!)) emit(simple[1]!, a + text.lastIndexOf(simple[1]!), { path: '', index }); |
| 527 | else walkPattern(code, a, b, close, undefined, emit); |
| 528 | index++; |
| 529 | } |
| 530 | return; |
| 531 | } |
| 532 | walkPattern(code, from, to, close, undefined, emit); |
| 533 | }; |
| 534 | // A parameter `pattern: Type`, or a closure's `pattern`. |
| 535 | const param = (from: number, to: number, kind: 'param' | 'closure', scopeFrom: number, scopeTo: number): void => { |
| 536 | const colon = topColon(code, from, to, close); |
| 537 | const pattern = code.slice(from, colon < 0 ? to : colon); |
| 538 | const simple = /^\s*(?:mut\s+)?([A-Za-z_]\w*)\s*$/.exec(pattern); |
| 539 | if (simple) { |
| 540 | if (isBindingName(simple[1]!)) { |
| 541 | add(simple[1]!, { |
| 542 | at: from + pattern.lastIndexOf(simple[1]!), kind, from: scopeFrom, to: scopeTo, |
| 543 | type: colon < 0 ? undefined : code.slice(colon + 1, to).trim(), |
| 544 | }); |
| 545 | } |
| 546 | return; |
| 547 | } |
| 548 | bindPattern(from, colon < 0 ? to : colon, kind, scopeFrom, scopeTo); |
| 549 | }; |
| 550 | |
| 551 | for (const f of scopes.fns) { |
| 552 | for (const [a, b] of splitTop(code, f.from + 1, f.paramsEnd, close, true)) param(a, b, 'param', f.body, f.to); |
| 553 | } |
| 554 | |
| 555 | for (let i = code.indexOf('|'); i >= 0; i = code.indexOf('|', i + 1)) { |
| 556 | // `||` — no parameters, or an `or` — and `|=`. |
| 557 | if (code[i + 1] === '|') { i++; continue; } |
| 558 | if (code[i + 1] === '=' || !opensClosure(code, i)) continue; |
| 559 | let end = -1; |
| 560 | let depth = 0; |
| 561 | for (let j = i + 1; j < n && j < i + 2000; j++) { |
| 562 | const c = code[j]!; |
| 563 | if (c === '(' || c === '[' || c === '{') { |
no test coverage detected