Repository navigation
Expand file tree
/
Copy pathinstalledLibraryDefinitions.ts
More file actions
75 lines (73 loc) · 3.57 KB
/
Copy pathinstalledLibraryDefinitions.ts
File metadata and controls
75 lines (73 loc) · 3.57 KB
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
import { inspectLeanDeclarationRegions, inspectScopedLeanDeclarations } from '../../src/math/leanSource';
import { leanScopeCommands, maskLeanCommentsAndStrings } from '../../src/math/leanSyntax';
import type {
InstalledLibraryDeclarationContext,
InstalledLibraryDefinition,
InstalledLibraryEntry,
} from './installedLibraryTypes';
/** Project source scopes and definition text from the digest-checked module; the catalog stays unchanged. */
export function installedLibraryDeclarations(source: string, entries: readonly InstalledLibraryEntry[]) {
const definitions: Record<string, InstalledLibraryDefinition> = {};
const contexts: Record<string, InstalledLibraryDeclarationContext> = {};
const regions = inspectLeanDeclarationRegions(source),
scoped = inspectScopedLeanDeclarations(source);
const masked = maskLeanCommentsAndStrings(source);
const commands = [...leanScopeCommands(masked)];
for (const entry of entries) {
const declaration = scoped.find(item => item.qualifiedName === entry.name);
const region = declaration && regions.find(item => item.startOffset === declaration.startOffset);
if (!declaration || !region) throw new Error(`Declaration source is missing: ${entry.name}`);
const start = source.lastIndexOf('\n', region.startOffset - 1) + 1;
const frames: string[][] = [[]];
for (const command of commands) {
if (command.index >= region.startOffset) break;
if (regions.some(item => command.index >= item.startOffset && command.index < item.endOffset)) continue;
const kind = command[1];
if (kind === 'namespace' || kind === 'section' || kind === 'mutual') frames.push([]);
else if (kind === 'end') {
if (frames.length > 1) frames.pop();
} else if (kind === 'variable') {
let end = command.index + command[0].length;
// Lean permits a variable command to continue on indented binder lines.
let continuation: RegExpExecArray | null;
while ((continuation = /^\n([ \t]+[^\n]*)/u.exec(masked.slice(end)))) {
if (!/^\s*[([{⦃]/u.test(continuation[1])) break;
end += continuation[0].length;
}
frames.at(-1)!.push(source.slice(command.index, end).trim());
}
}
const before = source.slice(0, start).trimEnd(),
docStart = before.lastIndexOf('/--');
const doc = docStart < 0 ? undefined : /^\/--([\s\S]*?)-\/$/u.exec(before.slice(docStart));
const context = {
documentation: doc?.[1].trim() ?? '',
context: frames.flat(),
line: source.slice(0, start).split('\n').length,
};
contexts[entry.name] = context;
if (entry.kind !== 'definition') continue;
let bodyStart = region.bodyOffset;
// The source inspector treats the first field's := as the boundary of a `where` definition.
// Include the complete record construction, including fields without explicit assignments.
const header = masked.slice(region.startOffset, bodyStart);
const where = /\bwhere\b/u.exec(header);
if (where) bodyStart = region.startOffset + where.index;
definitions[entry.name] = {
source: source.slice(start, region.endOffset).trimEnd(),
body: source.slice(bodyStart, region.endOffset).trim(),
...context,
};
}
return { definitions, contexts };
}
/** Definition-only view retained for callers that need bodies. */
export function installedLibraryDefinitions(
source: string,
entries: readonly InstalledLibraryEntry[],
): Record<string, InstalledLibraryDefinition> {
return installedLibraryDeclarations(
source,
entries.filter(entry => entry.kind === 'definition'),
).definitions;
}