| 364 | * name character: `[\w:\s]*?\s*\{` backtracks through one quadratically.) |
| 365 | */ |
| 366 | export function cppNamespaceBlocks(text: string): Array<{ start: number; end: number; path: readonly string[] }> { |
| 367 | const blocks: Array<{ start: number; end: number; path: readonly string[] }> = []; |
| 368 | const open: Array<{ start: number; end: number; path: readonly string[] } | null> = []; |
| 369 | let path: readonly string[] = []; |
| 370 | for (const m of text.matchAll(/\bnamespace\b(?:\s+([A-Za-z_][\w:]*(?:\s+[\w:]+)*))?\s*\{|[{}]/g)) { |
| 371 | if (m[0] === '{') open.push(null); |
| 372 | else if (m[0] === '}') { |
| 373 | const block = open.pop(); |
| 374 | if (block) { |
| 375 | block.end = m.index!; |
| 376 | path = open.reduce<readonly string[]>((p, b) => b?.path ?? p, []); |
| 377 | } |
| 378 | } else { |
| 379 | const names = m[1]?.replace(/\s+/g, '') ?? ''; |
| 380 | const block = { start: m.index!, end: text.length, path: names ? [...path, ...names.split('::')] : path }; |
| 381 | blocks.push(block); |
| 382 | open.push(block); |
| 383 | path = block.path; |
| 384 | } |
| 385 | } |
| 386 | return blocks; |
| 387 | } |
| 388 | |
| 389 | class TemplateInstantiations { |
| 390 | private readonly sources = new LRUCache<string, Source | null>(MAX_FILES_HELD); |