/** * FormulaParser - 公式解析器 * 负责解析、验证和执行数学公式,支持动态成本计算 * * ## 支持的功能 * * ### 变量占位符 * 使用 `{variableName}` 格式定义变量,变量名必须: * - 以字母开头 * - 只包含字母、数字和下划线 * - 区分大小写 * * ### 运算符 * - 算术运算符:`+` (加), `-` (减), `*` (乘), `/` (除) * - 括号:`(` `)` 用于控制运算优先级 * - 三元运算符:`condition ? value1 : value2` 用于条件计算 * - 比较运算符:`<`, `>`, `<=`, `>=`, `==`, `!=` * * ### 数字常量 * - 整数:`10`, `100`, `1000` * - 小数:`0.5`, `0.001`, `3.14` * - 负数:`-10`, `-0.5` * * ## 使用场景 * * ### 1. Token 计费 * ```typescript * const parser = new FormulaParser(); * * // 基础 token 计费:每 token 0.001 credit + 10 基础费用 * const cost1 = parser.evaluate('{token} * 0.001 + 10', { token: 3500 }); * console.log(cost1); // 13.5 * * // 会员折扣:premium 用户享受 20% 折扣 * const cost2 = parser.evaluate('({token} * 0.001 + 10) * 0.8', { token: 3500 }); * console.log(cost2); // 10.8 * ``` * * ### 2. 阶梯计费 * ```typescript * // 小于 1000 行:每行 0.1 credit * // 大于 1000 行:前 1000 行 100 credit,之后每行 0.05 credit * const formula = '{rows} <= 1000 ? {rows} * 0.1 : 100 + ({rows} - 1000) * 0.05'; * * const cost1 = parser.evaluate(formula, { rows: 500 }); * console.log(cost1); // 50 * * const cost2 = parser.evaluate(formula, { rows: 2000 }); * console.log(cost2); // 150 * ``` * * ### 3. 多变量计费 * ```typescript * // 视频处理:基于时长和分辨率 * const formula = '{duration} * 2 + {resolution} * 0.5'; * const cost = parser.evaluate(formula, { duration: 120, resolution: 1080 }); * console.log(cost); // 780 * ``` * * ### 4. 公式验证 * ```typescript * // 验证有效公式 * parser.validate('{token} * 0.5'); // 通过 * * // 验证无效公式 * try { * parser.validate('{token * 0.5'); // 括号不匹配 * } catch (error) { * console.error(error.message); // "Mismatched braces: unclosed opening braces" * } * * try { * parser.validate('{token-count} * 0.5'); // 变量名包含连字符 * } catch (error) { * console.error(error.message); // "Invalid variable name 'token-count'..." * } * ``` * * ## 错误处理 * * ### ConfigurationError * 公式语法无效时抛出: * - 括号不匹配 * - 变量名不符合规范 * - 包含非法字符 * * ### MissingVariableError * 计算时缺少必需变量: * ```typescript * try { * parser.evaluate('{token} * 0.001', {}); // 缺少 token 变量 * } catch (error) { * console.error(error.message); * // "Formula '{token} * 0.001' requires variable 'token', but only [] were provided" * } * ``` * * ### FormulaEvaluationError * 计算过程中发生错误: * - 除零错误 * - 结果为 NaN 或 Infinity * - 其他运算错误 * * ```typescript * try { * parser.evaluate('{amount} / {count}', { amount: 100, count: 0 }); * } catch (error) { * console.error(error.message); * // "Failed to evaluate formula... resulted in Infinity (possible division by zero)" * } * ``` * * @see {@link DynamicCostFormula} 用于集成到成本计算系统 * @see {@link MissingVariableError} 缺少变量错误 * @see {@link FormulaEvaluationError} 公式计算错误 * @see {@link ConfigurationError} 配置错误 */ /** * 解析后的公式对象 * 包含原始公式、提取的变量和编译后的计算函数 */ export interface ParsedFormula { /** 原始公式字符串 */ raw: string; /** 提取的变量名列表 */ variables: string[]; /** 编译后的计算函数 */ compute: (variables: Record) => number; } /** * 公式解析器类 * 负责解析、验证和执行数学公式 */ export declare class FormulaParser { /** * 变量名正则表达式 * 匹配字母开头,后跟字母、数字或下划线 */ private readonly VARIABLE_NAME_PATTERN; /** * 变量占位符正则表达式 * 匹配 {variableName} 格式 */ private readonly VARIABLE_PLACEHOLDER_PATTERN; /** * 创建一个新的 FormulaParser 实例 */ constructor(); /** * 解析公式字符串 * * 解析过程: * 1. 验证公式语法 * 2. 提取所有变量名 * 3. 构建计算函数 * * @param formula - 公式字符串,如 "{token} * 0.5 + 100" * @returns 解析后的公式对象 * @throws {ConfigurationError} 当公式语法无效时 * * @example * ```typescript * const parser = new FormulaParser(); * const parsed = parser.parse('{token} * 0.001 + {duration} * 0.5'); * console.log(parsed.variables); // ['token', 'duration'] * console.log(parsed.raw); // '{token} * 0.001 + {duration} * 0.5' * const result = parsed.compute({ token: 1000, duration: 60 }); * console.log(result); // 31 * ``` */ parse(formula: string): ParsedFormula; /** * 验证公式语法 * * 验证规则: * 1. 公式不能为空 * 2. 括号必须匹配 * 3. 变量名必须符合命名规范 * 4. 不能包含非法字符 * * @param formula - 公式字符串 * @throws {ConfigurationError} 当公式语法无效时 * * @example * ```typescript * parser.validate('{token} * 0.5'); // 通过 * parser.validate('{token * 0.5'); // 抛出错误:括号不匹配 * parser.validate('{token-count} * 0.5'); // 抛出错误:变量名包含连字符 * ``` */ validate(formula: string): void; /** * 提取公式中的变量名 * * @param formula - 公式字符串 * @returns 变量名数组(去重) * * @example * ```typescript * const vars1 = parser.extractVariables('{token} * 0.5 + {duration}'); * console.log(vars1); // ['token', 'duration'] * * const vars2 = parser.extractVariables('{token} + {token} * 2'); * console.log(vars2); // ['token'] (去重) * ``` */ extractVariables(formula: string): string[]; /** * 计算公式值 * * @param formula - 公式字符串 * @param variables - 变量值映射 * @returns 计算结果 * @throws {MissingVariableError} 当缺少必需变量时 * @throws {FormulaEvaluationError} 当计算出错时 * * @example * ```typescript * const result1 = parser.evaluate('{token} * 0.001', { token: 3500 }); * console.log(result1); // 3.5 * * // 缺少变量 * parser.evaluate('{token} * 0.001', {}); // 抛出 MissingVariableError * * // 除零错误 * parser.evaluate('{amount} / {count}', { amount: 100, count: 0 }); // 抛出 FormulaEvaluationError * ``` */ evaluate(formula: string, variables: Record): number; /** * 构建计算函数 * 将公式字符串编译为可执行的函数 * * @param formula - 公式字符串 * @param variables - 变量名列表 * @returns 计算函数 * @private */ private buildComputeFunction; } //# sourceMappingURL=FormulaParser.d.ts.map