import { FormulaDefinitionModel, IAction, IGrid } from '../base/interface'; export declare type FormulaValue = string | number | boolean | Date | null | undefined | FormulaError; export declare type TokenType = 'operator' | 'delimiter' | 'number' | 'string' | 'reference' | 'function' | 'equals' | 'comparison'; export declare type FormulaResult = FormulaValue | FormulaValue[] | FormulaReferenceValue; export interface FormulaReferenceValue { __isFormulaRefValue: true; ref: string; value: FormulaResult; } export interface Token { type: TokenType; value: string | number; position: number; } export interface EvaluationContext { getCell(reference: string): FormulaValue; getRange(startReference: string, endReference: string): FormulaValue[]; getRowData(rowIndex?: number | string): Record; getFieldReference?(fieldName: string, rowIndex: number | string): string | undefined; } export interface AbsoluteFlags { col: boolean; row: boolean; } export interface ParsedReference { col: number; row: number; isAbsolute: AbsoluteFlags; } export interface CacheEntry { ast?: ASTNode; value?: FormulaValue; error?: FormulaError; formula?: string; } export interface CustomFormulaArgs { values: FormulaValue[]; args: FormulaResult[]; context: EvaluationContext; } export interface CustomFormulaEventArgs { name: string; params: CustomFormulaArgs; result?: FormulaResult; error?: string | Error; requestType: string; cancel?: boolean; } export declare type CustomFunction = ((params: CustomFormulaArgs) => FormulaResult) | { func: (params: CustomFormulaArgs) => FormulaResult; } | undefined; /** Error codes for formula evaluation */ export declare enum FormulaErrorCode { PARSE = "#PARSE!", REF = "#REF!", NAME = "#NAME?", CIRCREF = "#CIRCREF!", VALUE = "#VALUE!", DIVZERO = "#DIV/0!", ERROR = "#ERROR!" } /** Base error class for all formula-related errors */ export declare class FormulaError extends Error { code: FormulaErrorCode; constructor(code: FormulaErrorCode, message: string); toString(): string; } export declare class ParseError extends FormulaError { position?: number; constructor(message: string, position?: number); } export declare class ReferenceError extends FormulaError { constructor(message: string); } export declare class CycleError extends FormulaError { constructor(message: string); } export declare class DivideByZeroError extends FormulaError { constructor(message?: string); } export declare class TypeMismatchError extends FormulaError { constructor(message: string); } export declare class UnknownFunctionError extends FormulaError { constructor(message: string); } /** Base abstract class for all AST nodes */ export declare abstract class ASTNode { abstract accept(visitor: ASTVisitor): FormulaResult; } /** Literal value node (number, string, boolean) */ export declare class LiteralNode extends ASTNode { value: FormulaValue; constructor(value: FormulaValue); accept(visitor: ASTVisitor): FormulaResult; } /** Cell reference node (e.g., A1, $B$2) */ export declare class CellRefNode extends ASTNode { ref: string; isAbsolute: AbsoluteFlags; constructor(ref: string, isAbsolute?: AbsoluteFlags); accept(visitor: ASTVisitor): FormulaResult; } /** Range reference node (e.g., A1:B5) */ export declare class RangeRefNode extends ASTNode { start: ASTNode; end: ASTNode; constructor(start: ASTNode, end: ASTNode); accept(visitor: ASTVisitor): FormulaResult; } /** Binary operation node (e.g., A + B) */ export declare class BinaryOpNode extends ASTNode { operator: string; left: ASTNode; right: ASTNode; constructor(operator: string, left: ASTNode, right: ASTNode); accept(visitor: ASTVisitor): FormulaResult; } /** Unary operation node (e.g., -A, +A) */ export declare class UnaryOpNode extends ASTNode { operator: string; operand: ASTNode; constructor(operator: string, operand: ASTNode); accept(visitor: ASTVisitor): FormulaResult; } /** Function call node (e.g., SUM(A1:A5)) */ export declare class FunctionCallNode extends ASTNode { name: string; args: ASTNode[]; constructor(name: string, args: ASTNode[]); accept(visitor: ASTVisitor): FormulaResult; } /** Visitor interface for traversing AST */ export interface ASTVisitor { visitLiteral(node: LiteralNode): FormulaResult; visitCellRef(node: CellRefNode): FormulaResult; visitRangeRef(node: RangeRefNode): FormulaResult; visitBinaryOp(node: BinaryOpNode): FormulaResult; visitUnaryOperator(node: UnaryOpNode): FormulaResult; visitFunctionCall(node: FunctionCallNode): FormulaResult; } /** ReferenceConverter: Convert between A1 notation and (col, row) coordinates */ export declare class ReferenceConverter { static parseReference(reference: string): ParsedReference; static convertIndexToReference(columnIndex: number, rowIndex: number | string): string; static adjustReferences(formula: string, rowOffset: number, columnOffset: number): string; private static buildReference; } /** Parser: Build an AST from a token stream using recursive descent */ declare class Parser { private tokens; private position; parse(formula: string): ASTNode; private parseExpression; private parseRange; private parseComparison; private parseAdditive; private parseMultiplicative; private parseExponentiation; private parseUnary; private parsePrimary; private parseReferenceOrFunction; private convertReferenceToNode; private match; private check; private advance; private isAtEnd; private peek; private previous; } /** DependencyGraph: Track cell dependencies and detect cycles */ declare class DependencyGraph { private readonly graph; addDependency(from: string, to: string): void; removeDependency(from: string, to: string): void; clear(): void; detectCycles(): Set; private hasCycleFromNode; } /** Evaluator: Execute AST with spreadsheet type coercion */ declare class Evaluator implements ASTVisitor { private context; private formulaEngine; setFormulaEngine(formulaEngine: Formula): void; evaluate(astNode: ASTNode, evaluationContext: EvaluationContext): FormulaValue; private resolveReferenceNode; visitLiteral(node: LiteralNode): FormulaValue; visitCellRef(node: CellRefNode): FormulaValue; visitRangeRef(node: RangeRefNode): FormulaResult; visitBinaryOp(node: BinaryOpNode): number; visitUnaryOperator(node: UnaryOpNode): number; visitFunctionCall(node: FunctionCallNode): FormulaResult; private executeCustomFunction; isFormulaReferenceValue(value: FormulaResult | FormulaReferenceValue): value is FormulaReferenceValue; private convertToNumber; private convertToBoolean; private extractValues; private appendConcatenatedValue; private matchesCriteria; private tryParseNumber; private convertToReference; } /** Coordinator for formula parsing, evaluation, and dependency tracking */ export declare class Formula implements IAction { parser: Parser; evaluator: Evaluator; dependencyGraph: DependencyGraph; formulaCache: Map; parent: IGrid; customFunctions: Map; primaryKeyValue: string | number; constructor(grid: IGrid); /** * @returns {void} * @hidden */ addEventListener(): void; /** * @returns {void} * @hidden */ removeEventListener(): void; /** * Initializes custom formula functions from the component settings. * * @returns {void} */ private initializeCustomFunctions; registerCustomFunction(name: string, func: CustomFunction): void; getCustomFunction(name: string): CustomFunction; isCustomFunction(name: string): boolean; /** * For internal use only - Get the module name. * * @returns {string} returns the module name * @private */ getModuleName(): string; /** * Sets a formula for a cell. * * @param {number | string} primaryKeyValue - The row index or primary key of the record. * @param {string} field - The field name for which the formula is being set. * @param {string | undefined} formula - The formula to store, or undefined to clear the formula. * @returns {void} */ setCellFormula(primaryKeyValue: number | string, field: string, formula: string | undefined): void; /** * Gets a stored formula. * * @param {number | string} primaryKeyValue - The row index or primary key of the record. * @param {string} field - The field name containing the formula. * @returns {string | undefined} The stored formula, or undefined if no formula exists. */ getCellFormula(primaryKeyValue: number | string, field: string): string | undefined; /** * Gets the evaluated value of a formula. * * @param {number | string} primaryKeyValue - The row index or primary key of the record. * @param {string} field - The field name containing the formula. * @returns {FormulaValue | undefined} The evaluated formula value, or undefined if no value is available. */ getFormulaValue(primaryKeyValue: number | string, field: string): FormulaValue | undefined; /** * Extracts all cell references from an abstract syntax tree (AST). * For ranges, extracts all cells within the range to track dependencies. * * @param {ASTNode} astNode - The AST node from which to extract cell references. * @returns {string[]} A collection of extracted cell references. */ private extractReferences; /** * Resolve a cell value from the grid. * * @param {string} cellReference - The cell reference to resolve. * @returns {FormulaValue | undefined} The resolved cell value, or undefined if not found. */ private resolveCellValue; private getRowId; /** * Resolves a range of values from the grid. Handles ranges such as A1:A5 and B2:D10. * * @param {string} startReference - The starting cell reference of the range. * @param {string} endReference - The ending cell reference of the range. * @returns {FormulaValue[]} The values contained within the specified range. */ private resolveRangeValues; /** * Creates an evaluation context for formula processing. * * @returns {EvaluationContext} A new evaluation context. */ private createContext; private getCacheKey; /** * Gets all formulas defined in the grid. * * @returns {FormulaDefinitionModel[]} Array of formula definitions with their values. */ getFormulas(): FormulaDefinitionModel[]; /** * Checks if a formula exists for the specified cell. * * @param {number} primaryKeyValue - The row index of the cell. * @param {string} field - The field name of the cell. * @returns {boolean} True if a formula exists for the cell, otherwise false. */ hasFormula(primaryKeyValue: number, field: string): boolean; /** * Adds a custom formula function. * * @param {string} name - The name of the custom formula function. * @param {Function} handler - The handler function for the custom formula. * @returns {void} */ addFormula(name: string, handler: Function): void; /** * Removes a custom formula function. * * @param {string} name - The name of the custom formula function to remove. * @returns {void} */ removeFormula(name: string): void; /** * Destroys the formula module. * * @returns {void} */ destroy(): void; } export {};