@ac-kit/format-regex 0.2.7 → 0.3.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.
- package/CHANGELOG.md +77 -0
- package/README.md +89 -13
- package/dist/_iregexp-parse.d.ts +50 -0
- package/dist/_iregexp-parse.d.ts.map +1 -0
- package/dist/_iregexp-parse.js +367 -0
- package/dist/_iregexp-parse.js.map +1 -0
- package/dist/_unicode-category.d.ts +12 -0
- package/dist/_unicode-category.d.ts.map +1 -0
- package/dist/_unicode-category.js +49 -0
- package/dist/_unicode-category.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/iregexp-syntax-error.d.ts +13 -0
- package/dist/iregexp-syntax-error.d.ts.map +1 -0
- package/dist/iregexp-syntax-error.js +16 -0
- package/dist/iregexp-syntax-error.js.map +1 -0
- package/dist/iregexp.d.ts +91 -0
- package/dist/iregexp.d.ts.map +1 -0
- package/dist/iregexp.js +111 -0
- package/dist/iregexp.js.map +1 -0
- package/dist/limits.d.ts +25 -8
- package/dist/limits.d.ts.map +1 -1
- package/dist/limits.js +16 -6
- package/dist/limits.js.map +1 -1
- package/dist/parser.d.ts +16 -3
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +100 -66
- package/dist/parser.js.map +1 -1
- package/dist/printer.d.ts +12 -1
- package/dist/printer.d.ts.map +1 -1
- package/dist/printer.js +40 -21
- package/dist/printer.js.map +1 -1
- package/dist/regex.d.ts +43 -6
- package/dist/regex.d.ts.map +1 -1
- package/dist/regex.js +25 -10
- package/dist/regex.js.map +1 -1
- package/dist/vm/_class-predicate.d.ts +27 -0
- package/dist/vm/_class-predicate.d.ts.map +1 -0
- package/dist/vm/_class-predicate.js +65 -0
- package/dist/vm/_class-predicate.js.map +1 -0
- package/dist/vm/_thompson.d.ts +70 -0
- package/dist/vm/_thompson.d.ts.map +1 -0
- package/dist/vm/_thompson.js +196 -0
- package/dist/vm/_thompson.js.map +1 -0
- package/dist/vm/compile.d.ts +14 -2
- package/dist/vm/compile.d.ts.map +1 -1
- package/dist/vm/compile.js +83 -159
- package/dist/vm/compile.js.map +1 -1
- package/dist/vm/pike-vm.d.ts +38 -8
- package/dist/vm/pike-vm.d.ts.map +1 -1
- package/dist/vm/pike-vm.js +228 -102
- package/dist/vm/pike-vm.js.map +1 -1
- package/dist/vm/program.d.ts +29 -4
- package/dist/vm/program.d.ts.map +1 -1
- package/dist/walk.d.ts +7 -1
- package/dist/walk.d.ts.map +1 -1
- package/dist/walk.js +9 -3
- package/dist/walk.js.map +1 -1
- package/package.json +4 -3
package/dist/vm/pike-vm.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type WorkBudget } from "@ac-kit/core";
|
|
1
2
|
import type { RegexProgram } from "./program.js";
|
|
2
3
|
export interface RegexGroupMatch {
|
|
3
4
|
readonly start: number;
|
|
@@ -19,16 +20,45 @@ export interface RegexMatch {
|
|
|
19
20
|
* (and, among threads starting at the same position, the one preferred by
|
|
20
21
|
* greedy/lazy quantifier priority) or `undefined` if there is none.
|
|
21
22
|
*
|
|
22
|
-
* This is a Pike VM: every thread advances one input
|
|
23
|
-
*
|
|
23
|
+
* This is a Pike VM: every thread advances one input unit per step in lockstep,
|
|
24
|
+
* so this is O(program size × input length) with no backtracking — a
|
|
24
25
|
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
25
|
-
* behavior here the way it can in a backtracking engine.
|
|
26
|
+
* behavior here the way it can in a backtracking engine. Zero-width chains
|
|
27
|
+
* (`jmp`, `split`, `save`, assertions) are followed with an explicit stack, so
|
|
28
|
+
* `(a|){16000}` cannot overflow the JavaScript stack.
|
|
26
29
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
30
|
+
* Captures are materialised: each `save` a thread passes copies its capture
|
|
31
|
+
* slots (2 × (groups + 1) numbers), so a run is O(program size × input length ×
|
|
32
|
+
* slots) time in the worst case, with O(program size × slots) slot arrays live
|
|
33
|
+
* per step, and the result is built in O(groups). {@link testProgram} answers
|
|
34
|
+
* the boolean question without any of this.
|
|
35
|
+
*
|
|
36
|
+
* Steps by UTF-16 code unit, or by code point when `program.unit` is
|
|
37
|
+
* `"codePoint"`; offsets in the result are code-unit offsets either way.
|
|
38
|
+
*
|
|
39
|
+
* @param budget - Most work units this run may spend (`RegexLimits.maxSteps`'s
|
|
40
|
+
* default when omitted), or a `WorkBudget` (`@ac-kit/core`) shared with other
|
|
41
|
+
* calls. The run spends a few units per instruction up front for its tables,
|
|
42
|
+
* then one per thread activation, one per slot for every capture copy and for
|
|
43
|
+
* the result, and a predicate's `cost` each time it is tested.
|
|
44
|
+
* @throws {RegexLimitExceededError} If the run outgrows a numeric bound. Linear
|
|
45
|
+
* is not the same as bounded: the work is program size × input length, and a
|
|
31
46
|
* caller holding neither factor needs a ceiling on the product.
|
|
47
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
48
|
+
*/
|
|
49
|
+
export declare function execProgram(program: RegexProgram, input: string, budget?: number | WorkBudget): RegexMatch | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Answers whether `program` matches `input`, without capture slots.
|
|
52
|
+
*
|
|
53
|
+
* Same automaton and same answer as `execProgram(…) !== undefined`, but `save`
|
|
54
|
+
* instructions are stepped over rather than copied, threads are bare
|
|
55
|
+
* instruction indexes in preallocated lists, and the run stops at the first
|
|
56
|
+
* thread that reaches `match`. O(program size × input length) time, O(program
|
|
57
|
+
* size) memory, no allocation per step.
|
|
58
|
+
*
|
|
59
|
+
* @param budget - As for {@link execProgram}.
|
|
60
|
+
* @throws {RegexLimitExceededError} As for {@link execProgram}.
|
|
61
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
32
62
|
*/
|
|
33
|
-
export declare function
|
|
63
|
+
export declare function testProgram(program: RegexProgram, input: string, budget?: number | WorkBudget): boolean;
|
|
34
64
|
//# sourceMappingURL=pike-vm.d.ts.map
|
package/dist/vm/pike-vm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pike-vm.d.ts","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pike-vm.d.ts","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,UAAU,EAEf,MAAM,cAAc,CAAC;AAGtB,OAAO,KAAK,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AAE9D,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,eAAe,GAAG,SAAS,CAAC,CAAC;CAC5D;AA4FD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,WAAW,CAC1B,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,UAA0C,GACzD,UAAU,GAAG,SAAS,CAMxB;AAuHD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAC1B,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,UAA0C,GACzD,OAAO,CAMT"}
|
package/dist/vm/pike-vm.js
CHANGED
|
@@ -1,90 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
class StepBudget {
|
|
4
|
-
limit;
|
|
5
|
-
remaining;
|
|
6
|
-
constructor(limit) {
|
|
7
|
-
this.limit = limit;
|
|
8
|
-
this.remaining = limit;
|
|
9
|
-
}
|
|
10
|
-
spend() {
|
|
11
|
-
if (this.remaining-- <= 0) {
|
|
12
|
-
throw new RegexLimitExceededError("steps", this.limit);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
1
|
+
import { SUPPLEMENTARY_PLANE_START, withWorkBudget, } from "@ac-kit/core";
|
|
2
|
+
import { DEFAULT_REGEX_LIMITS, stepsExceeded_ } from "../limits.js";
|
|
16
3
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
4
|
+
* Per-run tables, charged to the budget before they are allocated: the visited
|
|
5
|
+
* marks and the explicit stack each hold one entry per instruction, and the
|
|
6
|
+
* boolean run adds two thread lists of the same length.
|
|
19
7
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return true;
|
|
39
|
-
this.visitedAt[pc] = this.generation;
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Follows `jmp`/`split`/`save`/assertions (zero-width) until a consuming or
|
|
44
|
-
* terminal instruction is reached.
|
|
45
|
-
*/
|
|
46
|
-
add(program, pc, saved, pos, inputLength) {
|
|
47
|
-
this.budget.spend();
|
|
48
|
-
if (this.hasVisited(pc))
|
|
49
|
-
return;
|
|
50
|
-
const instruction = program.instructions[pc];
|
|
51
|
-
switch (instruction.op) {
|
|
52
|
-
case "jmp":
|
|
53
|
-
this.add(program, instruction.target, saved, pos, inputLength);
|
|
54
|
-
return;
|
|
55
|
-
case "split":
|
|
56
|
-
this.add(program, instruction.first, saved, pos, inputLength);
|
|
57
|
-
this.add(program, instruction.second, saved, pos, inputLength);
|
|
58
|
-
return;
|
|
59
|
-
case "save": {
|
|
60
|
-
const next = saved.slice();
|
|
61
|
-
next[instruction.slot] = pos;
|
|
62
|
-
this.add(program, pc + 1, next, pos, inputLength);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
case "assertStart":
|
|
66
|
-
if (pos === 0)
|
|
67
|
-
this.add(program, pc + 1, saved, pos, inputLength);
|
|
68
|
-
return;
|
|
69
|
-
case "assertEnd":
|
|
70
|
-
if (pos === inputLength)
|
|
71
|
-
this.add(program, pc + 1, saved, pos, inputLength);
|
|
72
|
-
return;
|
|
73
|
-
default:
|
|
74
|
-
this.threads.push({ pc, saved });
|
|
75
|
-
}
|
|
8
|
+
const CAPTURE_RUN_TABLES_ = 2;
|
|
9
|
+
const BOOLEAN_RUN_TABLES_ = 4;
|
|
10
|
+
/**
|
|
11
|
+
* How many code units the VM consumes at `position`: one, or in code-point mode
|
|
12
|
+
* two when a surrogate pair starts there.
|
|
13
|
+
*/
|
|
14
|
+
function unitWidthAt_(input, position, codePoints) {
|
|
15
|
+
return codePoints && input.codePointAt(position) >= SUPPLEMENTARY_PLANE_START
|
|
16
|
+
? 2
|
|
17
|
+
: 1;
|
|
18
|
+
}
|
|
19
|
+
const unitAt_ = (input, position, width) => width === 1 ? input[position] : input.slice(position, position + width);
|
|
20
|
+
/** Charges a predicate's own membership work before it is evaluated. */
|
|
21
|
+
function chargeTest_(instruction, work) {
|
|
22
|
+
if (instruction.op === "predicate" &&
|
|
23
|
+
instruction.cost !== undefined &&
|
|
24
|
+
instruction.cost > 0) {
|
|
25
|
+
work.spend(instruction.cost);
|
|
76
26
|
}
|
|
77
27
|
}
|
|
78
|
-
function
|
|
28
|
+
function matchesUnit_(instruction, unit) {
|
|
79
29
|
switch (instruction.op) {
|
|
80
30
|
case "char":
|
|
81
|
-
return
|
|
31
|
+
return unit === instruction.char;
|
|
82
32
|
case "any":
|
|
83
|
-
return
|
|
33
|
+
return unit !== "\n";
|
|
84
34
|
case "anyByte":
|
|
85
35
|
return true;
|
|
86
36
|
case "predicate":
|
|
87
|
-
return instruction.test(
|
|
37
|
+
return instruction.test(unit);
|
|
88
38
|
default:
|
|
89
39
|
return false;
|
|
90
40
|
}
|
|
@@ -121,44 +71,220 @@ function extractMatch(saved, input, groupCount) {
|
|
|
121
71
|
* (and, among threads starting at the same position, the one preferred by
|
|
122
72
|
* greedy/lazy quantifier priority) or `undefined` if there is none.
|
|
123
73
|
*
|
|
124
|
-
* This is a Pike VM: every thread advances one input
|
|
125
|
-
*
|
|
74
|
+
* This is a Pike VM: every thread advances one input unit per step in lockstep,
|
|
75
|
+
* so this is O(program size × input length) with no backtracking — a
|
|
126
76
|
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
127
|
-
* behavior here the way it can in a backtracking engine.
|
|
77
|
+
* behavior here the way it can in a backtracking engine. Zero-width chains
|
|
78
|
+
* (`jmp`, `split`, `save`, assertions) are followed with an explicit stack, so
|
|
79
|
+
* `(a|){16000}` cannot overflow the JavaScript stack.
|
|
128
80
|
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
81
|
+
* Captures are materialised: each `save` a thread passes copies its capture
|
|
82
|
+
* slots (2 × (groups + 1) numbers), so a run is O(program size × input length ×
|
|
83
|
+
* slots) time in the worst case, with O(program size × slots) slot arrays live
|
|
84
|
+
* per step, and the result is built in O(groups). {@link testProgram} answers
|
|
85
|
+
* the boolean question without any of this.
|
|
86
|
+
*
|
|
87
|
+
* Steps by UTF-16 code unit, or by code point when `program.unit` is
|
|
88
|
+
* `"codePoint"`; offsets in the result are code-unit offsets either way.
|
|
89
|
+
*
|
|
90
|
+
* @param budget - Most work units this run may spend (`RegexLimits.maxSteps`'s
|
|
91
|
+
* default when omitted), or a `WorkBudget` (`@ac-kit/core`) shared with other
|
|
92
|
+
* calls. The run spends a few units per instruction up front for its tables,
|
|
93
|
+
* then one per thread activation, one per slot for every capture copy and for
|
|
94
|
+
* the result, and a predicate's `cost` each time it is tested.
|
|
95
|
+
* @throws {RegexLimitExceededError} If the run outgrows a numeric bound. Linear
|
|
96
|
+
* is not the same as bounded: the work is program size × input length, and a
|
|
133
97
|
* caller holding neither factor needs a ceiling on the product.
|
|
98
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
134
99
|
*/
|
|
135
|
-
export function execProgram(program, input,
|
|
100
|
+
export function execProgram(program, input, budget = DEFAULT_REGEX_LIMITS.maxSteps) {
|
|
101
|
+
return withWorkBudget(budget, (work) => execWithin_(program, input, work), stepsExceeded_);
|
|
102
|
+
}
|
|
103
|
+
function execWithin_(program, input, work) {
|
|
104
|
+
const instructions = program.instructions;
|
|
105
|
+
const programLength = instructions.length;
|
|
136
106
|
const slotCount = (program.groupCount + 1) * 2;
|
|
137
107
|
const inputLength = input.length;
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
108
|
+
const codePoints = program.unit === "codePoint";
|
|
109
|
+
work.spend(CAPTURE_RUN_TABLES_ * programLength + slotCount);
|
|
110
|
+
const visitedAt = new Int32Array(programLength).fill(-1);
|
|
111
|
+
// Each unvisited pop pushes at most two entries, and at most one pop per
|
|
112
|
+
// instruction is unvisited in a step, so the stack never exceeds this.
|
|
113
|
+
const stackPcs = new Int32Array(programLength + 1);
|
|
114
|
+
const stackSaved = [];
|
|
115
|
+
let generation = 0;
|
|
116
|
+
const addThread = (list, startPc, startSaved, position) => {
|
|
117
|
+
stackPcs[0] = startPc;
|
|
118
|
+
stackSaved[0] = startSaved;
|
|
119
|
+
let top = 1;
|
|
120
|
+
while (top > 0) {
|
|
121
|
+
top--;
|
|
122
|
+
const pc = stackPcs[top];
|
|
123
|
+
const saved = stackSaved[top];
|
|
124
|
+
work.spend(1);
|
|
125
|
+
if (visitedAt[pc] === generation)
|
|
126
|
+
continue;
|
|
127
|
+
visitedAt[pc] = generation;
|
|
128
|
+
const instruction = instructions[pc];
|
|
129
|
+
switch (instruction.op) {
|
|
130
|
+
case "jmp":
|
|
131
|
+
stackPcs[top] = instruction.target;
|
|
132
|
+
stackSaved[top++] = saved;
|
|
133
|
+
break;
|
|
134
|
+
case "split":
|
|
135
|
+
// Pushed second first, so the preferred branch is explored first.
|
|
136
|
+
stackPcs[top] = instruction.second;
|
|
137
|
+
stackSaved[top++] = saved;
|
|
138
|
+
stackPcs[top] = instruction.first;
|
|
139
|
+
stackSaved[top++] = saved;
|
|
140
|
+
break;
|
|
141
|
+
case "save": {
|
|
142
|
+
// The copy is as long as the slot array, so it is charged as such.
|
|
143
|
+
work.spend(saved.length);
|
|
144
|
+
const next = saved.slice();
|
|
145
|
+
next[instruction.slot] = position;
|
|
146
|
+
stackPcs[top] = pc + 1;
|
|
147
|
+
stackSaved[top++] = next;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case "assertStart":
|
|
151
|
+
if (position === 0) {
|
|
152
|
+
stackPcs[top] = pc + 1;
|
|
153
|
+
stackSaved[top++] = saved;
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
case "assertEnd":
|
|
157
|
+
if (position === inputLength) {
|
|
158
|
+
stackPcs[top] = pc + 1;
|
|
159
|
+
stackSaved[top++] = saved;
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
list.push({ pc, saved });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
let current = [];
|
|
168
|
+
let next = [];
|
|
169
|
+
addThread(current, 0, Array.from({ length: slotCount }, () => -1), 0);
|
|
142
170
|
let matched;
|
|
143
|
-
for (let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
for (const thread of
|
|
149
|
-
const instruction =
|
|
171
|
+
for (let position = 0; current.length > 0;) {
|
|
172
|
+
generation++;
|
|
173
|
+
next.length = 0;
|
|
174
|
+
const width = position < inputLength ? unitWidthAt_(input, position, codePoints) : 0;
|
|
175
|
+
const unit = width > 0 ? unitAt_(input, position, width) : "";
|
|
176
|
+
for (const thread of current) {
|
|
177
|
+
const instruction = instructions[thread.pc];
|
|
150
178
|
if (instruction.op === "match") {
|
|
151
179
|
matched = thread.saved;
|
|
152
180
|
break; // Lower-priority threads at this step are preempted.
|
|
153
181
|
}
|
|
154
|
-
if (
|
|
155
|
-
|
|
182
|
+
if (width === 0)
|
|
183
|
+
continue;
|
|
184
|
+
chargeTest_(instruction, work);
|
|
185
|
+
if (matchesUnit_(instruction, unit)) {
|
|
186
|
+
addThread(next, thread.pc + 1, thread.saved, position + width);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (width === 0)
|
|
190
|
+
break;
|
|
191
|
+
[current, next] = [next, current];
|
|
192
|
+
position += width;
|
|
193
|
+
}
|
|
194
|
+
if (matched === undefined)
|
|
195
|
+
return undefined;
|
|
196
|
+
work.spend(slotCount);
|
|
197
|
+
return extractMatch(matched, input, program.groupCount);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Answers whether `program` matches `input`, without capture slots.
|
|
201
|
+
*
|
|
202
|
+
* Same automaton and same answer as `execProgram(…) !== undefined`, but `save`
|
|
203
|
+
* instructions are stepped over rather than copied, threads are bare
|
|
204
|
+
* instruction indexes in preallocated lists, and the run stops at the first
|
|
205
|
+
* thread that reaches `match`. O(program size × input length) time, O(program
|
|
206
|
+
* size) memory, no allocation per step.
|
|
207
|
+
*
|
|
208
|
+
* @param budget - As for {@link execProgram}.
|
|
209
|
+
* @throws {RegexLimitExceededError} As for {@link execProgram}.
|
|
210
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
211
|
+
*/
|
|
212
|
+
export function testProgram(program, input, budget = DEFAULT_REGEX_LIMITS.maxSteps) {
|
|
213
|
+
return withWorkBudget(budget, (work) => testWithin_(program, input, work), stepsExceeded_);
|
|
214
|
+
}
|
|
215
|
+
function testWithin_(program, input, work) {
|
|
216
|
+
const instructions = program.instructions;
|
|
217
|
+
const programLength = instructions.length;
|
|
218
|
+
const inputLength = input.length;
|
|
219
|
+
const codePoints = program.unit === "codePoint";
|
|
220
|
+
work.spend(BOOLEAN_RUN_TABLES_ * programLength);
|
|
221
|
+
const visitedAt = new Int32Array(programLength).fill(-1);
|
|
222
|
+
const stack = new Int32Array(programLength + 1);
|
|
223
|
+
let current = new Int32Array(programLength);
|
|
224
|
+
let next = new Int32Array(programLength);
|
|
225
|
+
let currentCount = 0;
|
|
226
|
+
let nextCount = 0;
|
|
227
|
+
let generation = 0;
|
|
228
|
+
// Returns true as soon as a thread reaches `match`.
|
|
229
|
+
const addThread = (startPc, position) => {
|
|
230
|
+
stack[0] = startPc;
|
|
231
|
+
let top = 1;
|
|
232
|
+
while (top > 0) {
|
|
233
|
+
const pc = stack[--top];
|
|
234
|
+
work.spend(1);
|
|
235
|
+
if (visitedAt[pc] === generation)
|
|
236
|
+
continue;
|
|
237
|
+
visitedAt[pc] = generation;
|
|
238
|
+
const instruction = instructions[pc];
|
|
239
|
+
switch (instruction.op) {
|
|
240
|
+
case "jmp":
|
|
241
|
+
stack[top++] = instruction.target;
|
|
242
|
+
break;
|
|
243
|
+
case "split":
|
|
244
|
+
stack[top++] = instruction.second;
|
|
245
|
+
stack[top++] = instruction.first;
|
|
246
|
+
break;
|
|
247
|
+
case "save":
|
|
248
|
+
stack[top++] = pc + 1;
|
|
249
|
+
break;
|
|
250
|
+
case "assertStart":
|
|
251
|
+
if (position === 0)
|
|
252
|
+
stack[top++] = pc + 1;
|
|
253
|
+
break;
|
|
254
|
+
case "assertEnd":
|
|
255
|
+
if (position === inputLength)
|
|
256
|
+
stack[top++] = pc + 1;
|
|
257
|
+
break;
|
|
258
|
+
case "match":
|
|
259
|
+
return true;
|
|
260
|
+
default:
|
|
261
|
+
next[nextCount++] = pc;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return false;
|
|
265
|
+
};
|
|
266
|
+
if (addThread(0, 0))
|
|
267
|
+
return true;
|
|
268
|
+
[current, next] = [next, current];
|
|
269
|
+
currentCount = nextCount;
|
|
270
|
+
for (let position = 0; position < inputLength && currentCount > 0;) {
|
|
271
|
+
generation++;
|
|
272
|
+
nextCount = 0;
|
|
273
|
+
const width = unitWidthAt_(input, position, codePoints);
|
|
274
|
+
const unit = unitAt_(input, position, width);
|
|
275
|
+
for (let index = 0; index < currentCount; index++) {
|
|
276
|
+
const pc = current[index];
|
|
277
|
+
const instruction = instructions[pc];
|
|
278
|
+
chargeTest_(instruction, work);
|
|
279
|
+
if (matchesUnit_(instruction, unit) &&
|
|
280
|
+
addThread(pc + 1, position + width)) {
|
|
281
|
+
return true;
|
|
156
282
|
}
|
|
157
283
|
}
|
|
158
|
-
[
|
|
284
|
+
[current, next] = [next, current];
|
|
285
|
+
currentCount = nextCount;
|
|
286
|
+
position += width;
|
|
159
287
|
}
|
|
160
|
-
return
|
|
161
|
-
? undefined
|
|
162
|
-
: extractMatch(matched, input, program.groupCount);
|
|
288
|
+
return false;
|
|
163
289
|
}
|
|
164
290
|
//# sourceMappingURL=pike-vm.js.map
|
package/dist/vm/pike-vm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pike-vm.js","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,
|
|
1
|
+
{"version":3,"file":"pike-vm.js","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,EAEzB,cAAc,GACd,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAyBpE;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAE9B;;;GAGG;AACH,SAAS,YAAY,CACpB,KAAa,EACb,QAAgB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,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAClC,YAAY,GAAG,SAAS,CAAC;QACzB,QAAQ,IAAI,KAAK,CAAC;IACnB,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
package/dist/vm/program.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A single compiled instruction — Thompson/Pike-style bytecode.
|
|
3
3
|
*
|
|
4
|
-
* `any` is the user-visible `.` (excludes
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* `any` is the user-visible `.` (excludes `\n` only); `anyByte` unconditionally
|
|
5
|
+
* consumes one character regardless of value and is never produced from pattern
|
|
6
|
+
* syntax — {@link compileProgram} uses it only to advance the implicit
|
|
7
|
+
* unanchored-scan start position.
|
|
8
8
|
*/
|
|
9
9
|
export type Instruction = {
|
|
10
10
|
readonly op: "char";
|
|
@@ -15,8 +15,18 @@ export type Instruction = {
|
|
|
15
15
|
readonly op: "anyByte";
|
|
16
16
|
} | {
|
|
17
17
|
readonly op: "predicate";
|
|
18
|
+
/**
|
|
19
|
+
* Receives one unit: a code unit, or in code-point mode a whole code
|
|
20
|
+
* point.
|
|
21
|
+
*/
|
|
18
22
|
readonly test: (char: string) => boolean;
|
|
19
23
|
readonly label: string;
|
|
24
|
+
/**
|
|
25
|
+
* Units charged to the run's budget each time `test` is evaluated, on top
|
|
26
|
+
* of the thread activation, for a test whose work grows with what it was
|
|
27
|
+
* built from (a large class). `0` when absent.
|
|
28
|
+
*/
|
|
29
|
+
readonly cost?: number;
|
|
20
30
|
} | {
|
|
21
31
|
readonly op: "assertStart";
|
|
22
32
|
} | {
|
|
@@ -34,6 +44,19 @@ export type Instruction = {
|
|
|
34
44
|
} | {
|
|
35
45
|
readonly op: "match";
|
|
36
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* What one step of the VM consumes.
|
|
49
|
+
*
|
|
50
|
+
* - `"codeUnit"`: one UTF-16 code unit, as a native `RegExp` without the `u`
|
|
51
|
+
* flag; an astral character is two steps.
|
|
52
|
+
* - `"codePoint"`: one code point, as a native `RegExp` with the `u` flag; an
|
|
53
|
+
* astral character is one step and `char`/`predicate` instructions see the
|
|
54
|
+
* whole surrogate pair. A lone surrogate is one step of its own.
|
|
55
|
+
*
|
|
56
|
+
* Match offsets are UTF-16 code-unit offsets in both modes, so they slice the
|
|
57
|
+
* input directly.
|
|
58
|
+
*/
|
|
59
|
+
export type RegexInputUnit = "codeUnit" | "codePoint";
|
|
37
60
|
/**
|
|
38
61
|
* A compiled regex program: a flat instruction array plus how many capture
|
|
39
62
|
* slots it uses.
|
|
@@ -45,5 +68,7 @@ export interface RegexProgram {
|
|
|
45
68
|
* here).
|
|
46
69
|
*/
|
|
47
70
|
readonly groupCount: number;
|
|
71
|
+
/** What one step consumes; `"codeUnit"` when absent. */
|
|
72
|
+
readonly unit?: RegexInputUnit;
|
|
48
73
|
}
|
|
49
74
|
//# sourceMappingURL=program.d.ts.map
|
package/dist/vm/program.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"program.d.ts","sourceRoot":"","sources":["../../src/vm/program.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GACpB;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAA;CAAE,GACtB;IAAE,QAAQ,CAAC,EAAE,EAAE,SAAS,CAAA;CAAE,GAC1B;IACA,QAAQ,CAAC,EAAE,EAAE,WAAW,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;IACzC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACtB,GACD;IAAE,QAAQ,CAAC,EAAE,EAAE,aAAa,CAAA;CAAE,G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a,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,EAAE,EAAE,WAAW,CAAA;CAAE,GAC5B;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC/C;IAAE,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACzE;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAA;CAAE,CAAC;AAE5B;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,WAAW,CAAC;AAEtD;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,YAAY,EAAE,SAAS,WAAW,EAAE,CAAC;IAC9C;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,IAAI,CAAC,EAAE,cAAc,CAAC;CAC/B"}
|
package/dist/walk.d.ts
CHANGED
|
@@ -3,6 +3,12 @@ import { RegexWalkControl } from "./index.js";
|
|
|
3
3
|
export { DfsWalkContinue as RegexWalkContinue, DfsWalkControl as RegexWalkControl, DfsWalkSkipChildren as RegexWalkSkipChildren, DfsWalkStop as RegexWalkStop, } from "@ac-kit/algo";
|
|
4
4
|
/** Called for each node visited by {@link walkRegex}. */
|
|
5
5
|
export type RegexNodeVisitor = (node: AnyRegexNode) => RegexWalkControl | undefined;
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Walks a regex AST in pre-order, iteratively: a tree of any depth, parsed or
|
|
8
|
+
* built by hand, is walked without growing the JavaScript stack. O(nodes) time,
|
|
9
|
+
* O(depth) memory.
|
|
10
|
+
*
|
|
11
|
+
* @returns `true` if the visitor stopped the walk with `RegexWalkStop`.
|
|
12
|
+
*/
|
|
7
13
|
export declare function walkRegex(root: AnyRegexNode, visitor: RegexNodeVisitor): boolean;
|
|
8
14
|
//# sourceMappingURL=walk.d.ts.map
|
package/dist/walk.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"walk.d.ts","sourceRoot":"","sources":["../src/walk.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,OAAO,EACN,eAAe,IAAI,iBAAiB,EACpC,cAAc,IAAI,gBAAgB,EAClC,mBAAmB,IAAI,qBAAqB,EAC5C,WAAW,IAAI,aAAa,GAC5B,MAAM,cAAc,CAAC;AAEtB,yDAAyD;AACzD,MAAM,MAAM,gBAAgB,GAAG,CAC9B,IAAI,EAAE,YAAY,KACd,gBAAgB,GAAG,SAAS,CAAC;AAoBlC
|
|
1
|
+
{"version":3,"file":"walk.d.ts","sourceRoot":"","sources":["../src/walk.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,OAAO,EACN,eAAe,IAAI,iBAAiB,EACpC,cAAc,IAAI,gBAAgB,EAClC,mBAAmB,IAAI,qBAAqB,EAC5C,WAAW,IAAI,aAAa,GAC5B,MAAM,cAAc,CAAC;AAEtB,yDAAyD;AACzD,MAAM,MAAM,gBAAgB,GAAG,CAC9B,IAAI,EAAE,YAAY,KACd,gBAAgB,GAAG,SAAS,CAAC;AAoBlC;;;;;;GAMG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,gBAAgB,GACvB,OAAO,CAET"}
|
package/dist/walk.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { walkTree } from "@ac-kit/algo";
|
|
2
2
|
export { DfsWalkContinue as RegexWalkContinue, DfsWalkSkipChildren as RegexWalkSkipChildren, DfsWalkStop as RegexWalkStop, } from "@ac-kit/algo";
|
|
3
3
|
function childrenOf(node) {
|
|
4
4
|
switch (node.kind) {
|
|
@@ -17,8 +17,14 @@ function childrenOf(node) {
|
|
|
17
17
|
return node.items;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Walks a regex AST in pre-order, iteratively: a tree of any depth, parsed or
|
|
22
|
+
* built by hand, is walked without growing the JavaScript stack. O(nodes) time,
|
|
23
|
+
* O(depth) memory.
|
|
24
|
+
*
|
|
25
|
+
* @returns `true` if the visitor stopped the walk with `RegexWalkStop`.
|
|
26
|
+
*/
|
|
21
27
|
export function walkRegex(root, visitor) {
|
|
22
|
-
return
|
|
28
|
+
return walkTree(root, childrenOf, (node) => visitor(node));
|
|
23
29
|
}
|
|
24
30
|
//# sourceMappingURL=walk.js.map
|
package/dist/walk.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"walk.js","sourceRoot":"","sources":["../src/walk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"walk.js","sourceRoot":"","sources":["../src/walk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAKxC,OAAO,EACN,eAAe,IAAI,iBAAiB,EAEpC,mBAAmB,IAAI,qBAAqB,EAC5C,WAAW,IAAI,aAAa,GAC5B,MAAM,cAAc,CAAC;AAOtB,SAAS,UAAU,CAAC,IAAkB;IACrC,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,SAAS,CAAC;QACf,KAAK,KAAK,CAAC;QACX,KAAK,WAAW,CAAC;QACjB,KAAK,QAAQ,CAAC;QACd,KAAK,WAAW;YACf,OAAO,SAAS,CAAC;QAClB,KAAK,OAAO,CAAC;QACb,KAAK,YAAY;YAChB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpB,KAAK,aAAa;YACjB,OAAO,IAAI,CAAC,YAAY,CAAC;QAC1B,KAAK,QAAQ;YACZ,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CACxB,IAAkB,EAClB,OAAyB;IAEzB,OAAO,QAAQ,CAAC,IAAI,EAAE,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;AAC5D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/format-regex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A regex-dialect lexer, parser and printer, with a Thompson-construction and Pike-VM matcher.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"automaton",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/algo": "^0.
|
|
49
|
-
"@ac-kit/core": "^0.
|
|
48
|
+
"@ac-kit/algo": "^0.4.0",
|
|
49
|
+
"@ac-kit/core": "^0.10.0",
|
|
50
50
|
"@types/node": "^26.5.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
+
"@ac-kit/math-random": "0.2.0",
|
|
53
54
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
55
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
56
|
"@ac-kit/vitest-config": "0.1.1",
|