/** * Tree-sitter parser initialization and utilities * * This module provides a universal parser that works in Node.js, browsers, and Workers. */ import { Parser, Language, Tree, Node } from 'web-tree-sitter'; export { Language, Tree }; export type { Node }; export type SyntaxNode = Node; export type SupportedLanguage = 'java' | 'c' | 'cpp' | 'javascript' | 'typescript' | 'tsx' | 'python' | 'rust' | 'bash' | 'html' | 'vue' | 'go'; interface ParserOptions { /** * Custom path/URL to the tree-sitter.wasm file. * In Node.js, defaults to the web-tree-sitter package location. * In browsers/workers, must be provided. */ wasmPath?: string; /** * Pre-compiled WebAssembly.Module for tree-sitter.wasm. * Use this for Cloudflare Workers where dynamic WASM compilation is blocked. * Takes precedence over wasmPath when provided. */ wasmModule?: WebAssembly.Module; /** * Custom paths/URLs to language grammar WASM files. * Key is the language name, value is the path/URL. */ languagePaths?: Partial>; /** * Pre-compiled WebAssembly.Module for language grammars. * Use this for Cloudflare Workers where dynamic WASM compilation is blocked. * Takes precedence over languagePaths when provided. */ languageModules?: Partial>; } /** * Initialize the Tree-sitter parser runtime. * Must be called before parsing any code. * Thread-safe: handles concurrent initialization attempts. */ export declare function initParser(options?: ParserOptions): Promise; /** * Load a language grammar for parsing. * Thread-safe: handles concurrent load attempts for the same language. */ export declare function loadLanguage(language: SupportedLanguage, wasmPath?: string): Promise; /** * Get a parser instance configured for the specified language. The Parser is * cached per-language and reused across calls — see `cachedParsers` above. * Use `createFreshParser()` if you need a non-shared instance. */ export declare function createParser(language: SupportedLanguage): Promise; /** * Create a non-cached parser. Caller is responsible for `parser.delete()` when * done. Prefer `createParser` unless isolation is required. */ export declare function createFreshParser(language: SupportedLanguage): Promise; /** * Parse source code and return the syntax tree. * * IMPORTANT: The returned Tree holds memory in the tree-sitter WASM heap. Call * `disposeTree(tree)` once you no longer need its nodes to free that memory. * Failing to dispose causes unbounded WASM heap growth at scale (#16). */ export declare function parse(code: string, language: SupportedLanguage): Promise; /** * Walk a parsed tree once and report its parse health (issue #27). * * Tree-sitter's `parse()` returns a Tree even when the input doesn't match * the grammar — error-recovery inserts ERROR / MISSING nodes around the * unrecognized region and keeps going. The Tree object itself carries * `rootNode.hasError`, but no count or per-location report; circle-ir's * extractors then run on the partial tree and silently drop whatever was * under the ERROR nodes. This helper surfaces a structured signal that the * top-level `analyze()` can attach to the IR so consumers can distinguish * "no findings" from "no findings because half the file didn't parse". * * Locations are capped at 50 entries to bound memory on pathological inputs. */ export declare function extractParseStatus(tree: Tree): { success: boolean; has_errors: boolean; error_count: number; error_locations: Array<{ line: number; column: number; }>; }; /** * Free the WASM memory backing a parsed Tree. Safe to call multiple times. */ export declare function disposeTree(tree: Tree | null | undefined): void; /** * Walk the syntax tree and call the visitor for each node. * * Iterative pre-order DFS — visits the parent before its children and walks * children left-to-right, matching the semantics of the previous recursive * implementation. The iterative form is required because tree-sitter parses * left-associative binary expressions (e.g. long `"a" + "b" + "c" + ...` * concatenations as found in generated Java sources like CoreNLP's * `DefaultTeXHyphenData.java`, 4500+ segments) into a deeply nested AST. * A recursive walker blows V8's call stack at ~5K depth and crashes the * entire analysis with `RangeError: Maximum call stack size exceeded` * (cognium-ai#88). The iterative walker has no such limit. */ export declare function walkTree(node: Node, visitor: (node: Node) => void): void; /** * Find all nodes of a specific type in the tree. */ export declare function findNodes(node: Node, type: string): Node[]; /** * Cached node collection from a single tree traversal. * Use this to avoid multiple traversals of the same tree. */ export type NodeCache = Map; /** * Collect all nodes of specified types in a single tree traversal. * Much more efficient than calling findNodes multiple times. */ export declare function collectAllNodes(node: Node, types: Set): NodeCache; /** * Get nodes from cache, falling back to findNodes if not cached. */ export declare function getNodesFromCache(node: Node, type: string, cache?: NodeCache): Node[]; /** * Find the first ancestor of a node that matches the given type. */ export declare function findAncestor(node: Node, type: string): Node | null; /** * Get the text of a node from the source code. */ export declare function getNodeText(node: Node): string; /** * Check if the parser has been initialized. */ export declare function isInitialized(): boolean; /** * Check if a language has been loaded. */ export declare function isLanguageLoaded(language: SupportedLanguage): boolean; /** * Reset the parser state (mainly for testing). * * Disposes any cached Parser instances and clears the language cache so the * next `initParser()` call starts with a clean WASM heap. */ export declare function resetParser(): void; //# sourceMappingURL=parser.d.ts.map