|
| 1 | +type Range = { |
| 2 | + start: { line: number, column: number, offset: number }, |
| 3 | + end: { line: number, column: number, offset: number } |
| 4 | +}; |
| 5 | + |
| 6 | +type AstNode = { |
| 7 | + kind: string; |
| 8 | + range: Range; |
| 9 | + name?: string; |
| 10 | + tokenText?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type Ast = { |
| 14 | + root: number; |
| 15 | + nodes: AstNode[]; |
| 16 | + edges: [number, number][]; |
| 17 | +}; |
| 18 | + |
| 19 | +export type SymbolInfo = { |
| 20 | + id: number; |
| 21 | + kind: 'function'|'method'|'class'|'struct'|'enum'|'variable'; |
| 22 | + name: string; |
| 23 | + range: Range; |
| 24 | + parentId?: number; |
| 25 | + typeAnnotation?: string; |
| 26 | +}; |
| 27 | + |
| 28 | +export type CallInfo = { |
| 29 | + id: number; |
| 30 | + name: string; |
| 31 | + receiver?: string; |
| 32 | + argsCount: number; |
| 33 | + range: Range; |
| 34 | + refersToId?: number; |
| 35 | +}; |
| 36 | + |
| 37 | +export type RefInfo = { |
| 38 | + id: number; |
| 39 | + name: string; |
| 40 | + range: Range; |
| 41 | + refersToId?: number; |
| 42 | +}; |
| 43 | + |
| 44 | +export type Analysis = { |
| 45 | + symbols: Map<number, SymbolInfo>; |
| 46 | + calls: CallInfo[]; |
| 47 | + refs: RefInfo[]; |
| 48 | + byName: Map<string, number[]>; |
| 49 | + findCallsByName: (name: string) => CallInfo[]; |
| 50 | + resolveNameAt: (name: string, offset: number) => SymbolInfo | undefined; |
| 51 | +}; |
| 52 | + |
| 53 | +function slice(source: string, r: Range): string { |
| 54 | + return source.slice(r.start.offset, r.end.offset); |
| 55 | +} |
| 56 | + |
| 57 | +function isScope(kind: string): boolean { |
| 58 | + return kind === 'FunctionDeclSyntax' || kind === 'ClassDeclSyntax' || kind === 'StructDeclSyntax' || kind === 'EnumDeclSyntax'; |
| 59 | +} |
| 60 | + |
| 61 | +function classifyDeclKind(kind: string): SymbolInfo['kind'] | undefined { |
| 62 | + if (kind === 'FunctionDeclSyntax') return 'function'; |
| 63 | + if (kind === 'ClassDeclSyntax') return 'class'; |
| 64 | + if (kind === 'StructDeclSyntax') return 'struct'; |
| 65 | + if (kind === 'EnumDeclSyntax') return 'enum'; |
| 66 | + if (kind === 'VariableDeclSyntax') return 'variable'; |
| 67 | + return undefined; |
| 68 | +} |
| 69 | + |
| 70 | +function extractTypeAnnotation(text: string): string | undefined { |
| 71 | + // naive: capture text after ':' up to '=' or end of declaration |
| 72 | + const m = text.match(/:\s*([^=\n\r\{]+?)(?=\s*(=|$|\n|\r|\{))/); |
| 73 | + return m ? m[1].trim() : undefined; |
| 74 | +} |
| 75 | + |
| 76 | +function extractCallFromText(text: string): { name: string, receiver?: string, argsCount: number } | undefined { |
| 77 | + // naive: match receiver.optional + dotted path or identifier then '(' |
| 78 | + const m = text.match(/([A-Za-z_][A-Za-z0-9_\.]*?)\s*\(/); |
| 79 | + if (!m) return undefined; |
| 80 | + const full = m[1]; |
| 81 | + const parts = full.split('.'); |
| 82 | + const name = parts.pop() || full; |
| 83 | + const receiver = parts.length ? parts.join('.') : undefined; |
| 84 | + // count arguments by commas at top level inside the first (...) pair |
| 85 | + const open = text.indexOf('('); |
| 86 | + if (open === -1) return { name, receiver, argsCount: 0 }; |
| 87 | + let depth = 0, i = open, end = -1; |
| 88 | + for (; i < text.length; i++) { |
| 89 | + const ch = text[i]; |
| 90 | + if (ch === '(') depth++; |
| 91 | + else if (ch === ')') { depth--; if (depth === 0) { end = i; break; } } |
| 92 | + } |
| 93 | + const inside = end !== -1 ? text.slice(open + 1, end) : ''; |
| 94 | + const argsCount = inside.trim() === '' ? 0 : inside.split(',').length; |
| 95 | + return { name, receiver, argsCount }; |
| 96 | +} |
| 97 | + |
| 98 | +export function analyzeAst(ast: Ast, source: string): Analysis { |
| 99 | + const children = new Map<number, number[]>(); |
| 100 | + const parent = new Map<number, number>(); |
| 101 | + for (const [p, c] of ast.edges) { |
| 102 | + const arr = children.get(p) ?? []; |
| 103 | + arr.push(c); |
| 104 | + children.set(p, arr); |
| 105 | + parent.set(c, p); |
| 106 | + } |
| 107 | + |
| 108 | + const symbols = new Map<number, SymbolInfo>(); |
| 109 | + const byName = new Map<string, number[]>(); |
| 110 | + const calls: CallInfo[] = []; |
| 111 | + const refs: RefInfo[] = []; |
| 112 | + |
| 113 | + // DFS with scope stack of maps: name -> symbolId |
| 114 | + const scopeStack: Array<Map<string, number>> = [new Map()]; |
| 115 | + |
| 116 | + function addSymbol(id: number, info: SymbolInfo) { |
| 117 | + symbols.set(id, info); |
| 118 | + const ids = byName.get(info.name) ?? []; |
| 119 | + ids.push(id); |
| 120 | + byName.set(info.name, ids); |
| 121 | + const top = scopeStack[scopeStack.length - 1]; |
| 122 | + top.set(info.name, id); |
| 123 | + } |
| 124 | + |
| 125 | + function resolveName(name: string): number | undefined { |
| 126 | + for (let i = scopeStack.length - 1; i >= 0; i--) { |
| 127 | + const id = scopeStack[i].get(name); |
| 128 | + if (id !== undefined) return id; |
| 129 | + } |
| 130 | + const global = byName.get(name); |
| 131 | + return global && global.length ? global[0] : undefined; |
| 132 | + } |
| 133 | + |
| 134 | + function walk(id: number) { |
| 135 | + const node = ast.nodes[id]; |
| 136 | + const kind = node.kind; |
| 137 | + const kids = children.get(id) ?? []; |
| 138 | + |
| 139 | + // Declarations |
| 140 | + const declKind = classifyDeclKind(kind); |
| 141 | + if (declKind) { |
| 142 | + const info: SymbolInfo = { |
| 143 | + id, |
| 144 | + kind: declKind, |
| 145 | + name: node.name ?? '', |
| 146 | + range: node.range, |
| 147 | + parentId: parent.get(id) |
| 148 | + }; |
| 149 | + if (declKind === 'variable') { |
| 150 | + const text = slice(source, node.range); |
| 151 | + info.typeAnnotation = extractTypeAnnotation(text); |
| 152 | + } |
| 153 | + addSymbol(id, info); |
| 154 | + } |
| 155 | + |
| 156 | + // Push scope for scope-introducing nodes |
| 157 | + if (isScope(kind)) scopeStack.push(new Map()); |
| 158 | + |
| 159 | + // Calls and references |
| 160 | + if (kind === 'FunctionCallExprSyntax') { |
| 161 | + const text = slice(source, node.range); |
| 162 | + const c = extractCallFromText(text); |
| 163 | + if (c) { |
| 164 | + const refersToId = resolveName(c.name); |
| 165 | + calls.push({ id, name: c.name, receiver: c.receiver, argsCount: c.argsCount, range: node.range, refersToId }); |
| 166 | + } |
| 167 | + } |
| 168 | + if (kind === 'DeclReferenceExprSyntax') { |
| 169 | + const text = slice(source, node.range); |
| 170 | + const m = text.match(/[A-Za-z_][A-Za-z0-9_]*/); |
| 171 | + const name = m ? m[0] : (node.name ?? ''); |
| 172 | + const refersToId = name ? resolveName(name) : undefined; |
| 173 | + refs.push({ id, name, range: node.range, refersToId }); |
| 174 | + } |
| 175 | + |
| 176 | + for (const k of kids) walk(k); |
| 177 | + |
| 178 | + if (isScope(kind)) scopeStack.pop(); |
| 179 | + } |
| 180 | + |
| 181 | + walk(ast.root); |
| 182 | + |
| 183 | + return { |
| 184 | + symbols, |
| 185 | + calls, |
| 186 | + refs, |
| 187 | + byName, |
| 188 | + findCallsByName: (name: string) => calls.filter(c => c.name === name), |
| 189 | + resolveNameAt: (name: string, _offset: number) => { |
| 190 | + const ids = byName.get(name); |
| 191 | + return ids && ids[0] !== undefined ? symbols.get(ids[0]) : undefined; |
| 192 | + } |
| 193 | + }; |
| 194 | +} |
0 commit comments