@sciexpr/parser 0.1.1 → 0.1.2
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.
- package/dist/index.cjs +24 -7
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +24 -7
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -139,8 +139,6 @@ var LatexTokenizer = class {
|
|
|
139
139
|
this.pos++;
|
|
140
140
|
return { type: "ampersand" /* Ampersand */, value: "&", position: start, length: 1 };
|
|
141
141
|
}
|
|
142
|
-
if (ch === "\\" && this.input[this.pos + 1] === "\\") {
|
|
143
|
-
}
|
|
144
142
|
if (/[0-9]/.test(ch)) {
|
|
145
143
|
this.pos++;
|
|
146
144
|
while (this.pos < this.input.length && /[0-9.]/.test(this.input[this.pos])) {
|
|
@@ -465,9 +463,17 @@ var ChemicalParser = class {
|
|
|
465
463
|
}
|
|
466
464
|
groupCount = parseInt(numStr, 10);
|
|
467
465
|
if (groupCount && groupCount > 1) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
466
|
+
const multiplier = groupCount;
|
|
467
|
+
const scaledAtoms = groupAtoms.map(
|
|
468
|
+
(atom) => (0, import_core4.createChemElement)(atom.symbol, {
|
|
469
|
+
count: (atom.count ?? 1) * multiplier,
|
|
470
|
+
isotope: atom.isotope,
|
|
471
|
+
charge: atom.charge,
|
|
472
|
+
chargeSign: atom.chargeSign
|
|
473
|
+
})
|
|
474
|
+
);
|
|
475
|
+
nodes.push((0, import_core4.createChemGroup)(scaledAtoms));
|
|
476
|
+
continue;
|
|
471
477
|
}
|
|
472
478
|
}
|
|
473
479
|
nodes.push((0, import_core4.createChemGroup)(groupAtoms));
|
|
@@ -485,8 +491,12 @@ var ChemicalParser = class {
|
|
|
485
491
|
const chargeMatch = chargeStr.match(/^(\d*)([+\-])$/);
|
|
486
492
|
if (chargeMatch) {
|
|
487
493
|
const el = lastNode;
|
|
488
|
-
|
|
489
|
-
|
|
494
|
+
nodes[nodes.length - 1] = (0, import_core4.createChemElement)(el.symbol, {
|
|
495
|
+
count: el.count,
|
|
496
|
+
isotope: el.isotope,
|
|
497
|
+
charge: parseInt(chargeMatch[1] || "1", 10),
|
|
498
|
+
chargeSign: chargeMatch[2]
|
|
499
|
+
});
|
|
490
500
|
}
|
|
491
501
|
}
|
|
492
502
|
continue;
|
|
@@ -507,6 +517,7 @@ var ChemicalParser = class {
|
|
|
507
517
|
};
|
|
508
518
|
|
|
509
519
|
// src/registry.ts
|
|
520
|
+
var import_core5 = require("@sciexpr/core");
|
|
510
521
|
var ParserRegistry = class {
|
|
511
522
|
parsers = /* @__PURE__ */ new Map();
|
|
512
523
|
defaultParsers;
|
|
@@ -547,6 +558,9 @@ var ParserRegistry = class {
|
|
|
547
558
|
* @returns 解析后的 AST
|
|
548
559
|
*/
|
|
549
560
|
parse(input, options) {
|
|
561
|
+
if (!input || input.trim().length === 0) {
|
|
562
|
+
return (0, import_core5.createRoot)(import_core5.ExpressionKind.Text, []);
|
|
563
|
+
}
|
|
550
564
|
if (options?.hint && options.hint !== "auto") {
|
|
551
565
|
const parser2 = this.parsers.get(options.hint);
|
|
552
566
|
if (parser2) {
|
|
@@ -557,6 +571,9 @@ var ParserRegistry = class {
|
|
|
557
571
|
if (parser) {
|
|
558
572
|
return parser.parse(input, options);
|
|
559
573
|
}
|
|
574
|
+
if (options?.hint === "physics") {
|
|
575
|
+
return this.defaultParsers[0].parse(input, options);
|
|
576
|
+
}
|
|
560
577
|
return this.defaultParsers[0].parse(input, options);
|
|
561
578
|
}
|
|
562
579
|
/** 列出所有已注册的解析器 ID */
|
package/dist/index.d.cts
CHANGED
|
@@ -14,7 +14,7 @@ interface IParser {
|
|
|
14
14
|
/** 解析选项 */
|
|
15
15
|
interface ParseOptions {
|
|
16
16
|
/** 输入格式提示 */
|
|
17
|
-
hint?: 'latex' | 'chemical' | 'unicode' | 'auto';
|
|
17
|
+
hint?: 'latex' | 'chemical' | 'physics' | 'unicode' | 'auto';
|
|
18
18
|
/** 是否为 display 模式 */
|
|
19
19
|
displayMode?: boolean;
|
|
20
20
|
/** 自定义宏 */
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ interface IParser {
|
|
|
14
14
|
/** 解析选项 */
|
|
15
15
|
interface ParseOptions {
|
|
16
16
|
/** 输入格式提示 */
|
|
17
|
-
hint?: 'latex' | 'chemical' | 'unicode' | 'auto';
|
|
17
|
+
hint?: 'latex' | 'chemical' | 'physics' | 'unicode' | 'auto';
|
|
18
18
|
/** 是否为 display 模式 */
|
|
19
19
|
displayMode?: boolean;
|
|
20
20
|
/** 自定义宏 */
|
package/dist/index.js
CHANGED
|
@@ -109,8 +109,6 @@ var LatexTokenizer = class {
|
|
|
109
109
|
this.pos++;
|
|
110
110
|
return { type: "ampersand" /* Ampersand */, value: "&", position: start, length: 1 };
|
|
111
111
|
}
|
|
112
|
-
if (ch === "\\" && this.input[this.pos + 1] === "\\") {
|
|
113
|
-
}
|
|
114
112
|
if (/[0-9]/.test(ch)) {
|
|
115
113
|
this.pos++;
|
|
116
114
|
while (this.pos < this.input.length && /[0-9.]/.test(this.input[this.pos])) {
|
|
@@ -444,9 +442,17 @@ var ChemicalParser = class {
|
|
|
444
442
|
}
|
|
445
443
|
groupCount = parseInt(numStr, 10);
|
|
446
444
|
if (groupCount && groupCount > 1) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
445
|
+
const multiplier = groupCount;
|
|
446
|
+
const scaledAtoms = groupAtoms.map(
|
|
447
|
+
(atom) => createChemElement(atom.symbol, {
|
|
448
|
+
count: (atom.count ?? 1) * multiplier,
|
|
449
|
+
isotope: atom.isotope,
|
|
450
|
+
charge: atom.charge,
|
|
451
|
+
chargeSign: atom.chargeSign
|
|
452
|
+
})
|
|
453
|
+
);
|
|
454
|
+
nodes.push(createChemGroup(scaledAtoms));
|
|
455
|
+
continue;
|
|
450
456
|
}
|
|
451
457
|
}
|
|
452
458
|
nodes.push(createChemGroup(groupAtoms));
|
|
@@ -464,8 +470,12 @@ var ChemicalParser = class {
|
|
|
464
470
|
const chargeMatch = chargeStr.match(/^(\d*)([+\-])$/);
|
|
465
471
|
if (chargeMatch) {
|
|
466
472
|
const el = lastNode;
|
|
467
|
-
|
|
468
|
-
|
|
473
|
+
nodes[nodes.length - 1] = createChemElement(el.symbol, {
|
|
474
|
+
count: el.count,
|
|
475
|
+
isotope: el.isotope,
|
|
476
|
+
charge: parseInt(chargeMatch[1] || "1", 10),
|
|
477
|
+
chargeSign: chargeMatch[2]
|
|
478
|
+
});
|
|
469
479
|
}
|
|
470
480
|
}
|
|
471
481
|
continue;
|
|
@@ -486,6 +496,7 @@ var ChemicalParser = class {
|
|
|
486
496
|
};
|
|
487
497
|
|
|
488
498
|
// src/registry.ts
|
|
499
|
+
import { createRoot as createRoot3, ExpressionKind as ExpressionKind3 } from "@sciexpr/core";
|
|
489
500
|
var ParserRegistry = class {
|
|
490
501
|
parsers = /* @__PURE__ */ new Map();
|
|
491
502
|
defaultParsers;
|
|
@@ -526,6 +537,9 @@ var ParserRegistry = class {
|
|
|
526
537
|
* @returns 解析后的 AST
|
|
527
538
|
*/
|
|
528
539
|
parse(input, options) {
|
|
540
|
+
if (!input || input.trim().length === 0) {
|
|
541
|
+
return createRoot3(ExpressionKind3.Text, []);
|
|
542
|
+
}
|
|
529
543
|
if (options?.hint && options.hint !== "auto") {
|
|
530
544
|
const parser2 = this.parsers.get(options.hint);
|
|
531
545
|
if (parser2) {
|
|
@@ -536,6 +550,9 @@ var ParserRegistry = class {
|
|
|
536
550
|
if (parser) {
|
|
537
551
|
return parser.parse(input, options);
|
|
538
552
|
}
|
|
553
|
+
if (options?.hint === "physics") {
|
|
554
|
+
return this.defaultParsers[0].parse(input, options);
|
|
555
|
+
}
|
|
539
556
|
return this.defaultParsers[0].parse(input, options);
|
|
540
557
|
}
|
|
541
558
|
/** 列出所有已注册的解析器 ID */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sciexpr/parser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Scientific Expression Engine - LaTeX & Chemical formula parser",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@sciexpr/core": "0.1.
|
|
46
|
+
"@sciexpr/core": "0.1.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"tsup": "^8.0.0",
|