@sciexpr/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,420 @@
1
+ /** 表达式分类 */
2
+ declare enum ExpressionKind {
3
+ /** 数学公式(默认) */
4
+ Math = "math",
5
+ /** 化学分子式 */
6
+ Molecule = "molecule",
7
+ /** 化学反应方程式 */
8
+ ChemicalEquation = "chemical-equation",
9
+ /** 矩阵/数组 */
10
+ Matrix = "matrix",
11
+ /** 纯文本 */
12
+ Text = "text",
13
+ /** 混合表达式(含多种类型片段) */
14
+ Mixed = "mixed"
15
+ }
16
+ /** 源码位置(用于错误提示和高亮) */
17
+ interface SourceLocation {
18
+ start: {
19
+ line: number;
20
+ column: number;
21
+ offset: number;
22
+ };
23
+ end: {
24
+ line: number;
25
+ column: number;
26
+ offset: number;
27
+ };
28
+ }
29
+ /** 所有 AST 节点的基类 */
30
+ interface ExpressionNode {
31
+ /** 节点类型标识(discriminant) */
32
+ kind: string;
33
+ /** 可选的唯一标识 */
34
+ id?: string;
35
+ /** 源码位置 */
36
+ source?: SourceLocation;
37
+ }
38
+ /** 顶层表达式根节点 */
39
+ interface ExpressionRoot extends ExpressionNode {
40
+ kind: 'root';
41
+ /** 表达式分类 */
42
+ type: ExpressionKind;
43
+ /** 子节点列表 */
44
+ children: ExpressionNode[];
45
+ /** 元数据 */
46
+ metadata?: ExpressionMetadata;
47
+ }
48
+ /** 表达式元数据 */
49
+ interface ExpressionMetadata {
50
+ /** 是否为 display 模式(块级公式) */
51
+ displayMode?: boolean;
52
+ /** 表达式编号(如 \tag{...}) */
53
+ label?: string;
54
+ /** 自定义样式 */
55
+ style?: Record<string, string>;
56
+ /** 原始输入(用于调试和格式化) */
57
+ originalSource?: string;
58
+ }
59
+ /** 符号类型 */
60
+ declare enum SymbolType {
61
+ Greek = "greek",
62
+ Operator = "operator",
63
+ Relation = "relation",
64
+ Arrow = "arrow",
65
+ Accent = "accent",
66
+ Ordinary = "ordinary",
67
+ Variable = "variable",
68
+ Number = "number"
69
+ }
70
+ /** 原子符号/标识符 */
71
+ interface SymbolNode extends ExpressionNode {
72
+ kind: 'symbol';
73
+ /** 符号文本(LaTeX 命令或 Unicode 字符) */
74
+ value: string;
75
+ /** 符号分类 */
76
+ symbolType: SymbolType;
77
+ }
78
+ /** 分数 */
79
+ interface FractionNode extends ExpressionNode {
80
+ kind: 'frac';
81
+ numerator: ExpressionNode;
82
+ denominator: ExpressionNode;
83
+ }
84
+ /** 上下标 */
85
+ interface ScriptNode extends ExpressionNode {
86
+ kind: 'script';
87
+ base: ExpressionNode;
88
+ sub?: ExpressionNode;
89
+ super?: ExpressionNode;
90
+ }
91
+ /** 根式 */
92
+ interface RadicalNode extends ExpressionNode {
93
+ kind: 'radical';
94
+ /** 根指数(如立方根为 3),省略则为平方根 */
95
+ index?: ExpressionNode;
96
+ /** 被开方数 */
97
+ radicand: ExpressionNode;
98
+ }
99
+ /** 括号/定界符 */
100
+ interface DelimiterNode extends ExpressionNode {
101
+ kind: 'delimiter';
102
+ /** 左定界符 */
103
+ left: string;
104
+ /** 右定界符 */
105
+ right: string;
106
+ /** 包裹的内容 */
107
+ body: ExpressionNode;
108
+ }
109
+ /** 矩阵/数组 */
110
+ interface MatrixNode extends ExpressionNode {
111
+ kind: 'matrix';
112
+ /** 矩阵元素,row-major */
113
+ rows: ExpressionNode[][];
114
+ /** 列对齐 */
115
+ columnAlign?: ('l' | 'c' | 'r')[];
116
+ /** 矩阵类型 */
117
+ matrixType?: 'matrix' | 'pmatrix' | 'bmatrix' | 'Bmatrix' | 'vmatrix' | 'Vmatrix';
118
+ }
119
+ /** 文本节点 */
120
+ interface TextNode extends ExpressionNode {
121
+ kind: 'text';
122
+ content: string;
123
+ }
124
+ /** 空格节点 */
125
+ interface SpaceNode extends ExpressionNode {
126
+ kind: 'space';
127
+ /** 空格宽度(em) */
128
+ width: number;
129
+ }
130
+ /** 重音符号 */
131
+ interface AccentNode extends ExpressionNode {
132
+ kind: 'accent';
133
+ /** 重音类型 */
134
+ accentType: 'hat' | 'tilde' | 'bar' | 'dot' | 'ddot' | 'vec' | 'widehat' | 'widetilde';
135
+ base: ExpressionNode;
136
+ }
137
+ /** 求和/求积/积分等大型运算符 */
138
+ interface LargeOpNode extends ExpressionNode {
139
+ kind: 'largeop';
140
+ /** 运算符类型 */
141
+ opType: 'sum' | 'prod' | 'int' | 'oint' | 'iint' | 'iiint' | 'coprod' | 'bigcup' | 'bigcap' | 'bigvee' | 'bigwedge';
142
+ /** 下限 */
143
+ lower?: ExpressionNode;
144
+ /** 上限 */
145
+ upper?: ExpressionNode;
146
+ /** 被积/被运算体 */
147
+ body?: ExpressionNode;
148
+ }
149
+ /** 颜色节点 */
150
+ interface ColorNode extends ExpressionNode {
151
+ kind: 'color';
152
+ /** 颜色值 */
153
+ color: string;
154
+ body: ExpressionNode;
155
+ }
156
+ /** 分组节点({...},无渲染含义) */
157
+ interface GroupNode extends ExpressionNode {
158
+ kind: 'group';
159
+ children: ExpressionNode[];
160
+ }
161
+ /** 化学元素原子 */
162
+ interface ChemElementNode extends ExpressionNode {
163
+ kind: 'chem-element';
164
+ /** 元素符号,如 'H', 'Na', 'Cl' */
165
+ symbol: string;
166
+ /** 原子个数(下标) */
167
+ count?: number;
168
+ /** 同位素质量数(左上标) */
169
+ isotope?: number;
170
+ /** 电荷/氧化态(右上标) */
171
+ charge?: number;
172
+ chargeSign?: '+' | '-';
173
+ }
174
+ /** 化学键 */
175
+ interface ChemBondNode extends ExpressionNode {
176
+ kind: 'chem-bond';
177
+ bondType: 'single' | 'double' | 'triple' | 'aromatic' | 'coordinate';
178
+ left: ExpressionNode;
179
+ right: ExpressionNode;
180
+ }
181
+ /** 化学反应箭头类型 */
182
+ type ChemArrowType = '->' | '<-' | '<=>' | '<->' | '->[' | '<-[' | '<=>>' | '<<=>';
183
+ /** 化学反应方程式 */
184
+ interface ChemReactionNode extends ExpressionNode {
185
+ kind: 'chem-reaction';
186
+ reactants: ExpressionNode[];
187
+ products: ExpressionNode[];
188
+ /** 反应条件(加热/催化剂等) */
189
+ conditions?: ExpressionNode;
190
+ arrowType: ChemArrowType;
191
+ }
192
+ /** 化学基团(官能团) */
193
+ interface ChemGroupNode extends ExpressionNode {
194
+ kind: 'chem-group';
195
+ /** 基团名称 */
196
+ name?: string;
197
+ atoms: ChemElementNode[];
198
+ }
199
+ /** 所有节点类型的联合 */
200
+ type ASTNode = ExpressionRoot | SymbolNode | FractionNode | ScriptNode | RadicalNode | DelimiterNode | MatrixNode | TextNode | SpaceNode | AccentNode | LargeOpNode | ColorNode | GroupNode | ChemElementNode | ChemBondNode | ChemReactionNode | ChemGroupNode;
201
+ /** 根据 kind 获取对应节点类型 */
202
+ type NodeByKind<K extends ASTNode['kind']> = Extract<ASTNode, {
203
+ kind: K;
204
+ }>;
205
+
206
+ /** 验证器接口 */
207
+ interface IValidator {
208
+ readonly id: string;
209
+ readonly name: string;
210
+ /** 验证 AST 节点 */
211
+ validate(node: ExpressionNode): ValidationResult;
212
+ }
213
+ /** 验证结果 */
214
+ interface ValidationResult {
215
+ valid: boolean;
216
+ errors: ValidationError[];
217
+ warnings: ValidationWarning[];
218
+ }
219
+ /** 验证错误 */
220
+ interface ValidationError {
221
+ message: string;
222
+ node?: ExpressionNode;
223
+ /** 错误码 */
224
+ code: string;
225
+ /** 严重程度 */
226
+ severity: 'error';
227
+ }
228
+ /** 验证警告 */
229
+ interface ValidationWarning {
230
+ message: string;
231
+ code: string;
232
+ severity: 'warning';
233
+ }
234
+ /** 验证错误码 */
235
+ declare enum ValidationErrorCode {
236
+ UNEXPECTED_NODE = "UNEXPECTED_NODE",
237
+ EMPTY_EXPRESSION = "EMPTY_EXPRESSION",
238
+ UNBALANCED_BRACE = "UNBALANCED_BRACE",
239
+ UNBALANCED_BRACKET = "UNBALANCED_BRACKET",
240
+ INVALID_SCRIPT = "INVALID_SCRIPT",
241
+ DIVISION_BY_ZERO = "DIVISION_BY_ZERO",
242
+ MATRIX_DIMENSION_MISMATCH = "MATRIX_DIMENSION_MISMATCH",
243
+ NEGATIVE_RADICAL = "NEGATIVE_RADICAL",
244
+ INVALID_ELEMENT_SYMBOL = "INVALID_ELEMENT_SYMBOL",
245
+ INVALID_CHEMICAL_FORMULA = "INVALID_CHEMICAL_FORMULA",
246
+ CHARGE_IMBALANCE = "CHARGE_IMBALANCE",
247
+ ELEMENT_IMBALANCE = "ELEMENT_IMBALANCE",
248
+ INVALID_BOND = "INVALID_BOND"
249
+ }
250
+
251
+ /** 管线上下文 */
252
+ interface PipelineContext {
253
+ /** 原始输入 */
254
+ input: string;
255
+ /** 解析后的 AST */
256
+ ast?: ExpressionRoot;
257
+ /** 渲染结果 */
258
+ result?: string;
259
+ /** 自定义元数据 */
260
+ metadata: Map<string, unknown>;
261
+ /** 累积的错误 */
262
+ errors: PipelineError[];
263
+ }
264
+ /** 管线错误 */
265
+ interface PipelineError {
266
+ message: string;
267
+ stage: string;
268
+ code?: string;
269
+ }
270
+ /** 管线阶段 */
271
+ interface PipelineStage {
272
+ readonly name: string;
273
+ process(ctx: PipelineContext, next: () => Promise<void>): Promise<void>;
274
+ }
275
+ /** 管线配置选项 */
276
+ interface PipelineOptions {
277
+ /** 严格模式 */
278
+ strict?: boolean;
279
+ /** 是否在第一个错误时停止 */
280
+ failFast?: boolean;
281
+ /** 自定义参数 */
282
+ params?: Record<string, unknown>;
283
+ }
284
+
285
+ /** 生成唯一节点 ID */
286
+ declare function generateNodeId(): string;
287
+ /** 重置节点 ID 计数器(主要用于测试) */
288
+ declare function resetNodeIdCounter(): void;
289
+ declare function createRoot(type: ExpressionKind, children: ExpressionNode[], metadata?: ExpressionMetadata): ExpressionRoot;
290
+ declare function createSymbol(value: string, symbolType: SymbolType): SymbolNode;
291
+ declare function createFraction(numerator: ExpressionNode, denominator: ExpressionNode): FractionNode;
292
+ declare function createScript(base: ExpressionNode, options?: {
293
+ sub?: ExpressionNode;
294
+ super?: ExpressionNode;
295
+ }): ScriptNode;
296
+ declare function createRadical(radicand: ExpressionNode, index?: ExpressionNode): RadicalNode;
297
+ declare function createDelimiter(left: string, right: string, body: ExpressionNode): DelimiterNode;
298
+ declare function createMatrix(rows: ExpressionNode[][], options?: {
299
+ columnAlign?: ('l' | 'c' | 'r')[];
300
+ matrixType?: 'matrix' | 'pmatrix' | 'bmatrix' | 'Bmatrix' | 'vmatrix' | 'Vmatrix';
301
+ }): MatrixNode;
302
+ declare function createText(content: string): TextNode;
303
+ declare function createSpace(width: number): SpaceNode;
304
+ declare function createAccent(accentType: AccentNode['accentType'], base: ExpressionNode): AccentNode;
305
+ declare function createLargeOp(opType: LargeOpNode['opType'], body?: ExpressionNode, options?: {
306
+ lower?: ExpressionNode;
307
+ upper?: ExpressionNode;
308
+ }): LargeOpNode;
309
+ declare function createColor(color: string, body: ExpressionNode): ColorNode;
310
+ declare function createGroup(children: ExpressionNode[]): GroupNode;
311
+ declare function createChemElement(symbol: string, options?: {
312
+ count?: number;
313
+ isotope?: number;
314
+ charge?: number;
315
+ chargeSign?: '+' | '-';
316
+ }): ChemElementNode;
317
+ declare function createChemBond(bondType: ChemBondNode['bondType'], left: ExpressionNode, right: ExpressionNode): ChemBondNode;
318
+ declare function createChemReaction(reactants: ExpressionNode[], products: ExpressionNode[], arrowType: ChemArrowType, conditions?: ExpressionNode): ChemReactionNode;
319
+ declare function createChemGroup(atoms: ChemElementNode[], name?: string): ChemGroupNode;
320
+ /** 深度克隆节点 */
321
+ declare function cloneNode<T extends ExpressionNode>(node: T): T;
322
+ /** 类型守卫:检查节点是否为特定 kind */
323
+ declare function isNodeType<K extends ExpressionNode['kind']>(node: ExpressionNode, kind: K): node is Extract<ExpressionNode, {
324
+ kind: K;
325
+ }>;
326
+ /** 获取节点下所有子节点的扁平列表 */
327
+ declare function flattenNode(node: ExpressionNode): ExpressionNode[];
328
+ /** 获取节点的直接子节点 */
329
+ declare function getNodeChildren(node: ExpressionNode): ExpressionNode[];
330
+
331
+ /** AST 遍历器接口(访问者模式) */
332
+ interface ASTVisitor<T = void> {
333
+ visitRoot?(node: ExpressionRoot): T;
334
+ visitSymbol?(node: SymbolNode): T;
335
+ visitFraction?(node: FractionNode): T;
336
+ visitScript?(node: ScriptNode): T;
337
+ visitRadical?(node: RadicalNode): T;
338
+ visitDelimiter?(node: DelimiterNode): T;
339
+ visitMatrix?(node: MatrixNode): T;
340
+ visitText?(node: TextNode): T;
341
+ visitSpace?(node: SpaceNode): T;
342
+ visitAccent?(node: AccentNode): T;
343
+ visitLargeOp?(node: LargeOpNode): T;
344
+ visitColor?(node: ColorNode): T;
345
+ visitGroup?(node: GroupNode): T;
346
+ visitChemElement?(node: ChemElementNode): T;
347
+ visitChemBond?(node: ChemBondNode): T;
348
+ visitChemReaction?(node: ChemReactionNode): T;
349
+ visitChemGroup?(node: ChemGroupNode): T;
350
+ /** 默认处理(未匹配到特定 visitor 时调用) */
351
+ visitDefault?(node: ExpressionNode): T;
352
+ }
353
+ /** AST 遍历选项 */
354
+ interface TraverseOptions {
355
+ /** 遍历顺序:'pre' 先访问父节点再子节点,'post' 先子节点后父节点 */
356
+ order?: 'pre' | 'post';
357
+ }
358
+ /**
359
+ * 遍历 AST(深度优先)
360
+ */
361
+ declare function traverseAST<T = void>(node: ExpressionNode, visitor: ASTVisitor<T>, options?: TraverseOptions): T | undefined;
362
+ /**
363
+ * 在 AST 中收集所有满足条件的节点
364
+ */
365
+ declare function collectNodes<T extends ExpressionNode = ExpressionNode>(root: ExpressionNode, predicate: (node: ExpressionNode) => boolean): T[];
366
+ /**
367
+ * 在 AST 中查找第一个满足条件的节点
368
+ */
369
+ declare function findNode<T extends ExpressionNode = ExpressionNode>(root: ExpressionNode, predicate: (node: ExpressionNode) => boolean): T | undefined;
370
+ /**
371
+ * 计算 AST 中节点的总数
372
+ */
373
+ declare function countNodes(root: ExpressionNode): number;
374
+
375
+ /**
376
+ * 序列化 AST 为 JSON 字符串
377
+ */
378
+ declare function serializeAST(root: ExpressionRoot): string;
379
+ /**
380
+ * 从 JSON 字符串反序列化为 AST
381
+ * @throws 如果 JSON 格式无效
382
+ */
383
+ declare function deserializeAST(json: string): ExpressionRoot;
384
+ /**
385
+ * 安全反序列化(不抛异常)
386
+ */
387
+ declare function safeDeserializeAST(json: string): ExpressionRoot | null;
388
+ /**
389
+ * AST 简化输出(压缩 JSON)
390
+ */
391
+ declare function serializeASTCompact(root: ExpressionRoot): string;
392
+ /**
393
+ * AST 美化输出
394
+ */
395
+ declare function serializeASTPretty(root: ExpressionRoot): string;
396
+
397
+ /**
398
+ * 表达式分类器
399
+ * 根据 AST 内容自动判定表达式类型
400
+ */
401
+ declare class ExpressionClassifier {
402
+ /**
403
+ * 分类表达式
404
+ */
405
+ classify(root: ExpressionRoot): ExpressionKind;
406
+ /**
407
+ * 检测是否包含化学相关节点
408
+ */
409
+ hasChemistryNodes(root: ExpressionRoot): boolean;
410
+ /**
411
+ * 检测是否包含数学相关节点
412
+ */
413
+ hasMathNodes(root: ExpressionRoot): boolean;
414
+ /**
415
+ * 检测是否为 display 模式(块级公式)
416
+ */
417
+ isDisplayMode(root: ExpressionRoot): boolean;
418
+ }
419
+
420
+ export { type ASTNode, type ASTVisitor, type AccentNode, type ChemArrowType, type ChemBondNode, type ChemElementNode, type ChemGroupNode, type ChemReactionNode, type ColorNode, type DelimiterNode, ExpressionClassifier, ExpressionKind, type ExpressionMetadata, type ExpressionNode, type ExpressionRoot, type FractionNode, type GroupNode, type IValidator, type LargeOpNode, type MatrixNode, type NodeByKind, type PipelineContext, type PipelineError, type PipelineOptions, type PipelineStage, type RadicalNode, type ScriptNode, type SourceLocation, type SpaceNode, type SymbolNode, SymbolType, type TextNode, type TraverseOptions, type ValidationError, ValidationErrorCode, type ValidationResult, type ValidationWarning, cloneNode, collectNodes, countNodes, createAccent, createChemBond, createChemElement, createChemGroup, createChemReaction, createColor, createDelimiter, createFraction, createGroup, createLargeOp, createMatrix, createRadical, createRoot, createScript, createSpace, createSymbol, createText, deserializeAST, findNode, flattenNode, generateNodeId, getNodeChildren, isNodeType, resetNodeIdCounter, safeDeserializeAST, serializeAST, serializeASTCompact, serializeASTPretty, traverseAST };