tarsec 0.4.3 → 0.4.4
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/position.js +15 -2
- package/package.json +2 -2
package/dist/position.js
CHANGED
|
@@ -13,6 +13,19 @@ export function buildLineTable(source) {
|
|
|
13
13
|
}
|
|
14
14
|
return lineStarts;
|
|
15
15
|
}
|
|
16
|
+
// One-entry cache so `withSpan` / `getPosition` don't rebuild the line
|
|
17
|
+
// table on every invocation. Parses run sequentially with a single
|
|
18
|
+
// `setInputStr` source, so a most-recently-seen cache is sufficient and
|
|
19
|
+
// avoids O(num_calls * source.length) work during a parse.
|
|
20
|
+
let cachedSource = null;
|
|
21
|
+
let cachedLineTable = [0];
|
|
22
|
+
function getLineTable(source) {
|
|
23
|
+
if (source === cachedSource)
|
|
24
|
+
return cachedLineTable;
|
|
25
|
+
cachedSource = source;
|
|
26
|
+
cachedLineTable = buildLineTable(source);
|
|
27
|
+
return cachedLineTable;
|
|
28
|
+
}
|
|
16
29
|
/**
|
|
17
30
|
* Convert an absolute offset into a line and column using a precomputed line table.
|
|
18
31
|
* Both line and column are 0-based.
|
|
@@ -51,7 +64,7 @@ export const getOffset = (input) => {
|
|
|
51
64
|
export const getPosition = (input) => {
|
|
52
65
|
const source = getInputStr();
|
|
53
66
|
const offset = source.length - input.length;
|
|
54
|
-
const lineTable =
|
|
67
|
+
const lineTable = getLineTable(source);
|
|
55
68
|
return success(offsetToPosition(lineTable, offset), input);
|
|
56
69
|
};
|
|
57
70
|
/**
|
|
@@ -68,7 +81,7 @@ export const getPosition = (input) => {
|
|
|
68
81
|
export function withSpan(parser) {
|
|
69
82
|
return (input) => {
|
|
70
83
|
const source = getInputStr();
|
|
71
|
-
const lineTable =
|
|
84
|
+
const lineTable = getLineTable(source);
|
|
72
85
|
const startOffset = source.length - input.length;
|
|
73
86
|
const result = parser(input);
|
|
74
87
|
if (!result.success)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tarsec",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "A parser combinator library for TypeScript, inspired by Parsec.",
|
|
5
5
|
"homepage": "https://github.com/egonSchiele/tarsec",
|
|
6
6
|
"scripts": {
|
|
@@ -43,4 +43,4 @@
|
|
|
43
43
|
"typescript": "^5.4.2",
|
|
44
44
|
"vitest": "^1.4.0"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
}
|