/** * CstBuilder — accumulates parser events and produces an untyped CST tree. * * The builder receives a flat stream of events from the instrumented parser: * - `startNode(kind)` — opens a new node * - `token(cstToken)` — adds a leaf token * - `endNode()` — closes the current node * * After parsing, `finish()` returns the root `UntypedCstNode`. * * The untyped tree is then converted to the typed `CstNode` hierarchy * by a separate conversion module. */ import type { CstToken } from './types'; export interface StartNodeEvent { type: 'startNode'; kind: string; } export interface TokenEvent { type: 'token'; token: CstToken; } export interface EndNodeEvent { type: 'endNode'; } export type CstEvent = StartNodeEvent | TokenEvent | EndNodeEvent; export interface UntypedCstNode { kind: string; children: (CstToken | UntypedCstNode)[]; } export declare class CstBuilder { /** Stack of nodes being built. The bottom is the root. */ private stack; /** Whether finish() has been called. */ private finished; /** * Open a new CST node of the given kind. All subsequent tokens and * child nodes will be added as children of this node until `endNode()` * is called. */ startNode(kind: string): void; /** * Add a leaf token to the current node. */ token(cstToken: CstToken): void; /** * Save a checkpoint at the current position in the current node's children. * Used with `startNodeAt()` to retroactively wrap already-parsed children * in a new node — needed by the Pratt parser for binary operators, property * access, and function calls where the left operand is parsed before the * wrapper node kind is known. */ checkpoint(): number; /** * Open a new node that retroactively wraps children from a prior checkpoint. * Children from `checkpoint` to the end of the current node's children list * are moved into the new node, which becomes the current node. * Call `endNode()` to close it (like any other node). */ startNodeAt(checkpoint: number, kind: string): void; /** * Close the current node and attach it as a child of its parent. * If this closes the root node, the tree is complete. */ endNode(): void; /** * Return the completed untyped CST tree. * Must be called exactly once after all events have been emitted. */ finish(): UntypedCstNode; /** The node currently being built (top of stack). */ private current; }