import { Parser } from './parser.js'; /** * Abstract base class for incremental parsers that can process input as it becomes available. * Supports buffering, lookahead, and backtracking for complex parsing scenarios. * * @typeParam I - The input item type * @typeParam O - The output item type */ export declare abstract class IncrementalParser extends Parser { /** Current position in the input stream */ protected inputOffset: number; /** Number of outputs generated */ protected outputOffset: number; /** Buffer holding input items */ protected buffer: I[]; /** Current position in the buffer */ protected bufferIndex: number; /** Whether end of file has been signaled */ eof: boolean; /** * Feeds input items into the parser buffer. * * @param input - Input items to add to the buffer */ feed(...input: (I | I[])[]): void; /** * Checks if end of file has been reached and buffer is exhausted. * * @returns True if no more input is available */ protected atEof(): boolean; /** * Peeks at an item in the buffer without consuming it. * * @param ahead - Number of positions to look ahead (default: 0) * @returns The item at the peek position, or null if at EOF * @throws NoMoreTokensError if more input is needed and EOF not signaled */ protected peek(ahead?: number): I | null; /** * Consumes and returns the next item from the buffer. * * @returns The next item * @throws NoMoreTokensError if more input is needed and EOF not signaled * @throws EofReachedError if at EOF and no more items available */ protected next(): I; private expectValue; /** * Consumes and validates the next item against an expected type or value. * * @typeParam T - The expected item type * @param itemType - Constructor or value to match against * @returns The consumed item cast to the expected type * @throws Error if the item doesn't match the expected type/value */ protected expect(itemType: (new (...args: any[]) => T) | T): T; /** * Tries multiple parsing functions and returns the first successful result. * Automatically backtracks on failure. * * @param fn - Array of parsing functions to try * @returns The result from the first successful parsing function * @throws NoMoreTokensError if any function needs more input * @throws Error if all parsing functions fail */ protected oneOf(...fn: (() => O)[]): O; /** * Compacts the buffer by removing consumed items */ protected compact(): void; /** * Override to customize when to compact the buffer. * Compacts when more than 1000 items have been consumed AND we've consumed * at least half the buffer. The second condition prevents O(n²) behaviour * when a single large chunk is fed all at once: without it, compact() is * called O(n/1000) times, each copying O(n) elements → O(n²) total. * By requiring bufferIndex ≥ buffer.length/2 we ensure at most O(log n) * compactions, for O(n) total copies. * * @returns boolean indicating whether to compact the buffer */ protected canCompact(): boolean; /** * Generates parsed output items from the buffer. * Handles backtracking when more input is needed. * * @returns A generator yielding parsed output items */ nextItems(): Generator; /** * Abstract method to parse the next output item from the buffer. * Subclasses must implement this to define parsing logic. * * @returns The parsed output item */ protected abstract parse(): O; }