@ac-kit/format-regex 0.2.6 → 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 +85 -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
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { Instruction } from "./program.js";
|
|
3
|
+
/**
|
|
4
|
+
* How the Thompson construction sees one syntax node, whatever tree it comes
|
|
5
|
+
* from: the public syntax tree and the I-Regexp front end each describe their
|
|
6
|
+
* nodes in these five shapes, and children stay in the caller's own node type
|
|
7
|
+
* until the compiler reaches them.
|
|
8
|
+
*/
|
|
9
|
+
export type ThompsonShape<TNode> = {
|
|
10
|
+
readonly kind: "emit";
|
|
11
|
+
readonly instruction: Instruction;
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "capture";
|
|
14
|
+
readonly index: number;
|
|
15
|
+
readonly body: TNode;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: "sequence";
|
|
18
|
+
readonly items: readonly TNode[];
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: "choice";
|
|
21
|
+
readonly alternatives: readonly TNode[];
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: "repeat";
|
|
24
|
+
readonly body: TNode;
|
|
25
|
+
readonly min: number;
|
|
26
|
+
/** `undefined` means unbounded. */
|
|
27
|
+
readonly max: number | undefined;
|
|
28
|
+
readonly lazy: boolean;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Emits instructions and patches forward-jump targets once their destination is
|
|
32
|
+
* known.
|
|
33
|
+
*/
|
|
34
|
+
export declare class ProgramBuilder {
|
|
35
|
+
private readonly maxProgramSize;
|
|
36
|
+
private readonly instructions;
|
|
37
|
+
constructor(maxProgramSize: number);
|
|
38
|
+
get length(): number;
|
|
39
|
+
/** @throws {RegexLimitExceededError} If the program outgrows its bound. */
|
|
40
|
+
emit(instruction: Instruction): number;
|
|
41
|
+
patchJmp(index: number, target: number): void;
|
|
42
|
+
patchSplit(index: number, first: number, second: number): void;
|
|
43
|
+
toInstructions(): readonly Instruction[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Emits the Thompson construction of `root` into `builder`, iteratively.
|
|
47
|
+
*
|
|
48
|
+
* An explicit task stack replaces the recursion over the tree, over the
|
|
49
|
+
* alternatives of a choice and over the copies of a counted repetition, so
|
|
50
|
+
* neither a deep tree nor `a{1,100000}` nor `a|a|…|a` grows the JavaScript
|
|
51
|
+
* stack. A task is either a node still to lower or a continuation that emits
|
|
52
|
+
* the instructions following a node and patches the jumps around it; the
|
|
53
|
+
* instruction layout is the one the recursive construction produced.
|
|
54
|
+
*
|
|
55
|
+
* A repetition whose body emitted nothing (an empty group) stops after its
|
|
56
|
+
* first copy: every further copy would emit nothing too, and skipping them
|
|
57
|
+
* keeps `(?:){1000000000}` from looping a billion times.
|
|
58
|
+
*
|
|
59
|
+
* A sequence is scheduled one item at a time, so a wide node costs no more
|
|
60
|
+
* stack than a narrow one. O(tasks) time and O(tree depth + pending
|
|
61
|
+
* continuations) memory: every task is charged one unit to `budget`, every
|
|
62
|
+
* patched jump one more, `shapeOf` charges whatever it builds, and every
|
|
63
|
+
* emitted instruction is checked against the builder's program-size bound.
|
|
64
|
+
*
|
|
65
|
+
* @throws {RegexLimitExceededError} If the program outgrows the builder's
|
|
66
|
+
* bound.
|
|
67
|
+
* @throws Whatever `budget.spend` throws once the work allowance is spent.
|
|
68
|
+
*/
|
|
69
|
+
export declare function emitThompson<TNode>(builder: ProgramBuilder, root: TNode, shapeOf: (node: TNode, budget: WorkBudget) => ThompsonShape<TNode>, budget: WorkBudget): void;
|
|
70
|
+
//# sourceMappingURL=_thompson.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_thompson.d.ts","sourceRoot":"","sources":["../../src/vm/_thompson.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,KAAK,IAC5B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAA;CAAE,GAC5D;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,GAC1E;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,CAAA;CAAE,GAC/D;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,YAAY,EAAE,SAAS,KAAK,EAAE,CAAA;CAAE,GACpE;IACA,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,mCAAmC;IACnC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACtB,CAAC;AAEL;;;GAGG;AACH,qBAAa,cAAc;IAGd,OAAO,CAAC,QAAQ,CAAC,cAAc;IAF3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAqB;IAElD,YAA6B,cAAc,EAAE,MAAM,EAAI;IAEvD,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,2EAA2E;IAC3E,IAAI,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM,CAQrC;IAED,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAE5C;IAED,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAE7D;IAED,cAAc,IAAI,SAAS,WAAW,EAAE,CAEvC;CACD;AAOD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EACjC,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,KAAK,EACX,OAAO,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,KAAK,aAAa,CAAC,KAAK,CAAC,EAClE,MAAM,EAAE,UAAU,GAChB,IAAI,CAyJN"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { RegexLimitExceededError } from "../limits.js";
|
|
2
|
+
/**
|
|
3
|
+
* Emits instructions and patches forward-jump targets once their destination is
|
|
4
|
+
* known.
|
|
5
|
+
*/
|
|
6
|
+
export class ProgramBuilder {
|
|
7
|
+
maxProgramSize;
|
|
8
|
+
instructions = [];
|
|
9
|
+
constructor(maxProgramSize) {
|
|
10
|
+
this.maxProgramSize = maxProgramSize;
|
|
11
|
+
}
|
|
12
|
+
get length() {
|
|
13
|
+
return this.instructions.length;
|
|
14
|
+
}
|
|
15
|
+
/** @throws {RegexLimitExceededError} If the program outgrows its bound. */
|
|
16
|
+
emit(instruction) {
|
|
17
|
+
// Checked here rather than after building: a counted quantifier emits its
|
|
18
|
+
// copies one at a time, so the array never grows past the bound.
|
|
19
|
+
if (this.instructions.length >= this.maxProgramSize) {
|
|
20
|
+
throw new RegexLimitExceededError("programSize", this.maxProgramSize);
|
|
21
|
+
}
|
|
22
|
+
this.instructions.push(instruction);
|
|
23
|
+
return this.instructions.length - 1;
|
|
24
|
+
}
|
|
25
|
+
patchJmp(index, target) {
|
|
26
|
+
this.instructions[index] = { op: "jmp", target };
|
|
27
|
+
}
|
|
28
|
+
patchSplit(index, first, second) {
|
|
29
|
+
this.instructions[index] = { op: "split", first, second };
|
|
30
|
+
}
|
|
31
|
+
toInstructions() {
|
|
32
|
+
return this.instructions;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Emits the Thompson construction of `root` into `builder`, iteratively.
|
|
37
|
+
*
|
|
38
|
+
* An explicit task stack replaces the recursion over the tree, over the
|
|
39
|
+
* alternatives of a choice and over the copies of a counted repetition, so
|
|
40
|
+
* neither a deep tree nor `a{1,100000}` nor `a|a|…|a` grows the JavaScript
|
|
41
|
+
* stack. A task is either a node still to lower or a continuation that emits
|
|
42
|
+
* the instructions following a node and patches the jumps around it; the
|
|
43
|
+
* instruction layout is the one the recursive construction produced.
|
|
44
|
+
*
|
|
45
|
+
* A repetition whose body emitted nothing (an empty group) stops after its
|
|
46
|
+
* first copy: every further copy would emit nothing too, and skipping them
|
|
47
|
+
* keeps `(?:){1000000000}` from looping a billion times.
|
|
48
|
+
*
|
|
49
|
+
* A sequence is scheduled one item at a time, so a wide node costs no more
|
|
50
|
+
* stack than a narrow one. O(tasks) time and O(tree depth + pending
|
|
51
|
+
* continuations) memory: every task is charged one unit to `budget`, every
|
|
52
|
+
* patched jump one more, `shapeOf` charges whatever it builds, and every
|
|
53
|
+
* emitted instruction is checked against the builder's program-size bound.
|
|
54
|
+
*
|
|
55
|
+
* @throws {RegexLimitExceededError} If the program outgrows the builder's
|
|
56
|
+
* bound.
|
|
57
|
+
* @throws Whatever `budget.spend` throws once the work allowance is spent.
|
|
58
|
+
*/
|
|
59
|
+
export function emitThompson(builder, root, shapeOf, budget) {
|
|
60
|
+
const tasks = [{ kind: "node", node: root }];
|
|
61
|
+
const pushNode = (node) => {
|
|
62
|
+
tasks.push({ kind: "node", node });
|
|
63
|
+
};
|
|
64
|
+
const pushThen = (run) => {
|
|
65
|
+
tasks.push({ kind: "then", run });
|
|
66
|
+
};
|
|
67
|
+
const patchSplitsTo = (splits, bodyStarts, lazy) => {
|
|
68
|
+
budget.spend(splits.length);
|
|
69
|
+
const end = builder.length;
|
|
70
|
+
for (let index = 0; index < splits.length; index++) {
|
|
71
|
+
const bodyStart = bodyStarts[index];
|
|
72
|
+
// Greedy prefers another copy (bodyStart first); lazy prefers leaving.
|
|
73
|
+
if (lazy)
|
|
74
|
+
builder.patchSplit(splits[index], end, bodyStart);
|
|
75
|
+
else
|
|
76
|
+
builder.patchSplit(splits[index], bodyStart, end);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const scheduleChoice = (alternatives, index, exitJumps) => {
|
|
80
|
+
if (index === alternatives.length - 1) {
|
|
81
|
+
pushThen(() => {
|
|
82
|
+
budget.spend(exitJumps.length);
|
|
83
|
+
for (const jump of exitJumps)
|
|
84
|
+
builder.patchJmp(jump, builder.length);
|
|
85
|
+
});
|
|
86
|
+
pushNode(alternatives[index]);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
// split first, second ; first: <alt> ; jmp end ; second: <rest...> ; end:
|
|
90
|
+
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
91
|
+
const firstStart = builder.length;
|
|
92
|
+
pushThen(() => {
|
|
93
|
+
exitJumps.push(builder.emit({ op: "jmp", target: -1 }));
|
|
94
|
+
builder.patchSplit(splitIndex, firstStart, builder.length);
|
|
95
|
+
scheduleChoice(alternatives, index + 1, exitJumps);
|
|
96
|
+
});
|
|
97
|
+
pushNode(alternatives[index]);
|
|
98
|
+
};
|
|
99
|
+
// body*: split top, end ; top: <body> ; jmp split ; end:
|
|
100
|
+
const scheduleStar = (body, lazy) => {
|
|
101
|
+
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
102
|
+
const bodyStart = builder.length;
|
|
103
|
+
pushThen(() => {
|
|
104
|
+
builder.emit({ op: "jmp", target: splitIndex });
|
|
105
|
+
patchSplitsTo([splitIndex], [bodyStart], lazy);
|
|
106
|
+
});
|
|
107
|
+
pushNode(body);
|
|
108
|
+
};
|
|
109
|
+
// `count` nested optional copies: skipping copy i also skips every copy
|
|
110
|
+
// after it, so every split exits to the end of the whole chain.
|
|
111
|
+
const scheduleOptionalChain = (body, count, lazy) => {
|
|
112
|
+
const splits = [];
|
|
113
|
+
const bodyStarts = [];
|
|
114
|
+
const scheduleCopy = (remaining) => {
|
|
115
|
+
if (!(remaining > 0)) {
|
|
116
|
+
patchSplitsTo(splits, bodyStarts, lazy);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
splits.push(builder.emit({ op: "split", first: -1, second: -1 }));
|
|
120
|
+
const bodyStart = builder.length;
|
|
121
|
+
bodyStarts.push(bodyStart);
|
|
122
|
+
pushThen(() => {
|
|
123
|
+
if (builder.length === bodyStart) {
|
|
124
|
+
patchSplitsTo(splits, bodyStarts, lazy);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
scheduleCopy(remaining - 1);
|
|
128
|
+
});
|
|
129
|
+
pushNode(body);
|
|
130
|
+
};
|
|
131
|
+
scheduleCopy(count);
|
|
132
|
+
};
|
|
133
|
+
const scheduleRepeat = (body, min, max, lazy) => {
|
|
134
|
+
const scheduleMandatoryCopy = (copy) => {
|
|
135
|
+
if (!(copy < min)) {
|
|
136
|
+
if (max === undefined)
|
|
137
|
+
scheduleStar(body, lazy);
|
|
138
|
+
else
|
|
139
|
+
scheduleOptionalChain(body, max - min, lazy);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const before = builder.length;
|
|
143
|
+
pushThen(() => {
|
|
144
|
+
if (builder.length === before)
|
|
145
|
+
return;
|
|
146
|
+
scheduleMandatoryCopy(copy + 1);
|
|
147
|
+
});
|
|
148
|
+
pushNode(body);
|
|
149
|
+
};
|
|
150
|
+
scheduleMandatoryCopy(0);
|
|
151
|
+
};
|
|
152
|
+
while (tasks.length > 0) {
|
|
153
|
+
budget.spend(1);
|
|
154
|
+
const task = tasks.pop();
|
|
155
|
+
if (task.kind === "then") {
|
|
156
|
+
task.run();
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (task.kind === "items") {
|
|
160
|
+
if (task.next < task.items.length) {
|
|
161
|
+
const item = task.items[task.next];
|
|
162
|
+
task.next++;
|
|
163
|
+
tasks.push(task);
|
|
164
|
+
pushNode(item);
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const shape = shapeOf(task.node, budget);
|
|
169
|
+
switch (shape.kind) {
|
|
170
|
+
case "emit":
|
|
171
|
+
builder.emit(shape.instruction);
|
|
172
|
+
break;
|
|
173
|
+
case "capture": {
|
|
174
|
+
const slot = shape.index * 2;
|
|
175
|
+
builder.emit({ op: "save", slot });
|
|
176
|
+
pushThen(() => {
|
|
177
|
+
builder.emit({ op: "save", slot: slot + 1 });
|
|
178
|
+
});
|
|
179
|
+
pushNode(shape.body);
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case "sequence":
|
|
183
|
+
tasks.push({ kind: "items", items: shape.items, next: 0 });
|
|
184
|
+
break;
|
|
185
|
+
case "choice":
|
|
186
|
+
if (shape.alternatives.length > 0) {
|
|
187
|
+
scheduleChoice(shape.alternatives, 0, []);
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
case "repeat":
|
|
191
|
+
scheduleRepeat(shape.body, shape.min, shape.max, shape.lazy);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=_thompson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_thompson.js","sourceRoot":"","sources":["../../src/vm/_thompson.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,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ACpB,KAAK,MAAM;gBACV,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;gBAChC,MAAM;YACP,KAAK,SAAS,EAAE,CAAC;gBAChB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC;gBAC7B,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;gBACnC,QAAQ,CAAC,GAAG,EAAE;oBACb,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;gBACH,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACrB,MAAM;YACP,CAAC;YACD,KAAK,UAAU;gBACd,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;gBAC3D,MAAM;YACP,KAAK,QAAQ;gBACZ,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACnC,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC3C,CAAC;gBACD,MAAM;YACP,KAAK,QAAQ;gBACZ,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;gBAC7D,MAAM;QACR,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/dist/vm/compile.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type WorkBudget } from "@ac-kit/core";
|
|
1
2
|
import type { RegexPattern } from "../ast.js";
|
|
2
3
|
import type { RegexProgram } from "./program.js";
|
|
3
4
|
/**
|
|
@@ -11,9 +12,20 @@ import type { RegexProgram } from "./program.js";
|
|
|
11
12
|
* open with `^` (those simply fail `assertStart` at every offset but 0, which
|
|
12
13
|
* is correct, just not the fastest possible instruction count for that case).
|
|
13
14
|
*
|
|
15
|
+
* Iterative: neither the depth of the tree, the number of alternatives nor a
|
|
16
|
+
* repetition count grows the JavaScript stack, so a hand-built tree of any
|
|
17
|
+
* shape compiles or fails with a typed error. O(syntax nodes lowered) time,
|
|
18
|
+
* where a counted repetition lowers its body once per copy; O(program size)
|
|
19
|
+
* memory.
|
|
20
|
+
*
|
|
14
21
|
* @param maxProgramSize - Most instructions the program may hold; see
|
|
15
22
|
* `RegexLimits.maxProgramSize`, whose default this takes.
|
|
16
|
-
* @
|
|
23
|
+
* @param budget - Charged one unit per syntax node lowered: a step bound
|
|
24
|
+
* (`RegexLimits.maxSteps`'s default when omitted), or a `WorkBudget`
|
|
25
|
+
* (`@ac-kit/core`) shared with other calls.
|
|
26
|
+
* @throws {RegexLimitExceededError} If the program outgrows `maxProgramSize`,
|
|
27
|
+
* or the lowering outgrows a numeric step bound.
|
|
28
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
17
29
|
*/
|
|
18
|
-
export declare function compileProgram(pattern: RegexPattern, maxProgramSize?: number): RegexProgram;
|
|
30
|
+
export declare function compileProgram(pattern: RegexPattern, maxProgramSize?: number, budget?: number | WorkBudget): RegexProgram;
|
|
19
31
|
//# sourceMappingURL=compile.d.ts.map
|
package/dist/vm/compile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,UAAU,EAEf,MAAM,cAAc,CAAC;AAEtB,OAAO,KAAK,EAGX,YAAY,EAEZ,MAAM,WAAW,CAAC;AAQnB,OAAO,KAAK,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AA6G9D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,YAAY,EACrB,cAAc,GAAE,MAA4C,EAC5D,MAAM,GAAE,MAAM,GAAG,UAA0C,GACzD,YAAY,CAMd"}
|
package/dist/vm/compile.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { isAsciiAlphaNumeric, isAsciiDigit, isAsciiWhitespace, } from "@ac-kit/core";
|
|
2
|
-
import { DEFAULT_REGEX_LIMITS,
|
|
1
|
+
import { isAsciiAlphaNumeric, isAsciiDigit, isAsciiWhitespace, withWorkBudget, } from "@ac-kit/core";
|
|
2
|
+
import { DEFAULT_REGEX_LIMITS, stepsExceeded_ } from "../limits.js";
|
|
3
|
+
import { classPredicate_ } from "./_class-predicate.js";
|
|
4
|
+
import { emitThompson, ProgramBuilder, } from "./_thompson.js";
|
|
3
5
|
const isWordChar = (char) => isAsciiAlphaNumeric(char.charCodeAt(0)) || char === "_";
|
|
4
6
|
const SHORTHAND_PREDICATES = {
|
|
5
7
|
d: (char) => isAsciiDigit(char.charCodeAt(0)),
|
|
@@ -9,175 +11,83 @@ const SHORTHAND_PREDICATES = {
|
|
|
9
11
|
s: (char) => isAsciiWhitespace(char.charCodeAt(0)),
|
|
10
12
|
S: (char) => !isAsciiWhitespace(char.charCodeAt(0)),
|
|
11
13
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/** The shorthand tests of a class, once each, in a fixed order. */
|
|
15
|
+
const SHORTHAND_ORDER_ = [
|
|
16
|
+
"d",
|
|
17
|
+
"D",
|
|
18
|
+
"w",
|
|
19
|
+
"W",
|
|
20
|
+
"s",
|
|
21
|
+
"S",
|
|
22
|
+
];
|
|
23
|
+
function compileCharClass_(negated, items, budget) {
|
|
24
|
+
// Charged before any per-item work, so a huge preparsed class is refused
|
|
25
|
+
// before it is read.
|
|
26
|
+
budget.spend(items.length);
|
|
27
|
+
const bounds = [];
|
|
28
|
+
const shorthands = new Set();
|
|
16
29
|
for (const item of items) {
|
|
17
|
-
if (item.kind === "char")
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
ranges.push({ from: item.from, to: item.to });
|
|
21
|
-
else
|
|
22
|
-
shorthands.push(item.value);
|
|
23
|
-
}
|
|
24
|
-
const test = (char) => {
|
|
25
|
-
if (singles.has(char))
|
|
26
|
-
return true;
|
|
27
|
-
for (const range of ranges) {
|
|
28
|
-
if (char >= range.from && char <= range.to)
|
|
29
|
-
return true;
|
|
30
|
+
if (item.kind === "char") {
|
|
31
|
+
const unit = item.char.codePointAt(0);
|
|
32
|
+
bounds.push(unit, unit);
|
|
30
33
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return true;
|
|
34
|
+
else if (item.kind === "range") {
|
|
35
|
+
bounds.push(item.from.codePointAt(0), item.to.codePointAt(0));
|
|
34
36
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return negated ? (char) => !test(char) : test;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Emits instructions and patches forward-jump targets once their destination is
|
|
41
|
-
* known.
|
|
42
|
-
*/
|
|
43
|
-
class ProgramBuilder {
|
|
44
|
-
maxProgramSize;
|
|
45
|
-
instructions = [];
|
|
46
|
-
constructor(maxProgramSize) {
|
|
47
|
-
this.maxProgramSize = maxProgramSize;
|
|
48
|
-
}
|
|
49
|
-
get length() {
|
|
50
|
-
return this.instructions.length;
|
|
51
|
-
}
|
|
52
|
-
emit(instruction) {
|
|
53
|
-
// Checked here rather than after building: a counted quantifier emits its
|
|
54
|
-
// copies one at a time, so the array never grows past the bound.
|
|
55
|
-
if (this.instructions.length >= this.maxProgramSize) {
|
|
56
|
-
throw new RegexLimitExceededError("programSize", this.maxProgramSize);
|
|
37
|
+
else {
|
|
38
|
+
shorthands.add(item.value);
|
|
57
39
|
}
|
|
58
|
-
this.instructions.push(instruction);
|
|
59
|
-
return this.instructions.length - 1;
|
|
60
40
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
toInstructions() {
|
|
68
|
-
return this.instructions;
|
|
41
|
+
const extraTests = [];
|
|
42
|
+
if (shorthands.size > 0) {
|
|
43
|
+
for (const value of SHORTHAND_ORDER_) {
|
|
44
|
+
if (shorthands.has(value))
|
|
45
|
+
extraTests.push(SHORTHAND_PREDICATES[value]);
|
|
46
|
+
}
|
|
69
47
|
}
|
|
48
|
+
return classPredicate_(negated, bounds, extraTests, negated ? "[^...]" : "[...]", budget);
|
|
70
49
|
}
|
|
71
|
-
function
|
|
50
|
+
function shapeOfRegexNode_(node, budget) {
|
|
72
51
|
switch (node.kind) {
|
|
73
52
|
case "literal":
|
|
74
|
-
|
|
75
|
-
return;
|
|
53
|
+
return { kind: "emit", instruction: { op: "char", char: node.char } };
|
|
76
54
|
case "any":
|
|
77
|
-
|
|
78
|
-
return;
|
|
55
|
+
return { kind: "emit", instruction: { op: "any" } };
|
|
79
56
|
case "shorthand":
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
57
|
+
return {
|
|
58
|
+
kind: "emit",
|
|
59
|
+
instruction: {
|
|
60
|
+
op: "predicate",
|
|
61
|
+
test: SHORTHAND_PREDICATES[node.value],
|
|
62
|
+
label: `\\${node.value}`,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
86
65
|
case "anchor":
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const slot = node.index * 2;
|
|
101
|
-
builder.emit({ op: "save", slot });
|
|
102
|
-
compileNode(builder, node.body);
|
|
103
|
-
builder.emit({ op: "save", slot: slot + 1 });
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
compileNode(builder, node.body);
|
|
107
|
-
}
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
66
|
+
return {
|
|
67
|
+
kind: "emit",
|
|
68
|
+
instruction: node.type === "start" ? { op: "assertStart" } : { op: "assertEnd" },
|
|
69
|
+
};
|
|
70
|
+
case "charClass":
|
|
71
|
+
return {
|
|
72
|
+
kind: "emit",
|
|
73
|
+
instruction: compileCharClass_(node.negated, node.items, budget),
|
|
74
|
+
};
|
|
75
|
+
case "group":
|
|
76
|
+
return node.capturing
|
|
77
|
+
? { kind: "capture", index: node.index, body: node.body }
|
|
78
|
+
: { kind: "sequence", items: [node.body] };
|
|
110
79
|
case "concat":
|
|
111
|
-
|
|
112
|
-
compileNode(builder, item);
|
|
113
|
-
return;
|
|
80
|
+
return { kind: "sequence", items: node.items };
|
|
114
81
|
case "alternation":
|
|
115
|
-
|
|
116
|
-
return;
|
|
82
|
+
return { kind: "choice", alternatives: node.alternatives };
|
|
117
83
|
case "quantified":
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
// split first, second ; first: <alt0> ; jmp end ; second: <rest...> ; end:
|
|
128
|
-
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
129
|
-
const firstStart = builder.length;
|
|
130
|
-
compileNode(builder, alternatives[0]);
|
|
131
|
-
const jmpIndex = builder.emit({ op: "jmp", target: -1 });
|
|
132
|
-
const secondStart = builder.length;
|
|
133
|
-
builder.patchSplit(splitIndex, firstStart, secondStart);
|
|
134
|
-
compileAlternation(builder, alternatives.slice(1));
|
|
135
|
-
builder.patchJmp(jmpIndex, builder.length);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* `body*`/`body+`'s unbounded tail: `split top, end ; top: <body> ; jmp split ;
|
|
139
|
-
* end:`.
|
|
140
|
-
*/
|
|
141
|
-
function compileStar(builder, body, lazy) {
|
|
142
|
-
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
143
|
-
const bodyStart = builder.length;
|
|
144
|
-
compileNode(builder, body);
|
|
145
|
-
builder.emit({ op: "jmp", target: splitIndex });
|
|
146
|
-
const afterLoop = builder.length;
|
|
147
|
-
// Greedy prefers repeating (bodyStart first); lazy prefers exiting (afterLoop first).
|
|
148
|
-
if (lazy)
|
|
149
|
-
builder.patchSplit(splitIndex, afterLoop, bodyStart);
|
|
150
|
-
else
|
|
151
|
-
builder.patchSplit(splitIndex, bodyStart, afterLoop);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* `count` nested optional copies of `body` — the bounded tail of `{min,max}`.
|
|
155
|
-
*
|
|
156
|
-
* Nesting means skipping copy _i_ also skips every copy after it, since they
|
|
157
|
-
* are only reachable through copy _i_'s own body.
|
|
158
|
-
*/
|
|
159
|
-
function compileOptionalChain(builder, body, count, lazy) {
|
|
160
|
-
if (count === 0)
|
|
161
|
-
return;
|
|
162
|
-
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
163
|
-
const bodyStart = builder.length;
|
|
164
|
-
compileNode(builder, body);
|
|
165
|
-
compileOptionalChain(builder, body, count - 1, lazy);
|
|
166
|
-
const afterAll = builder.length;
|
|
167
|
-
if (lazy)
|
|
168
|
-
builder.patchSplit(splitIndex, afterAll, bodyStart);
|
|
169
|
-
else
|
|
170
|
-
builder.patchSplit(splitIndex, bodyStart, afterAll);
|
|
171
|
-
}
|
|
172
|
-
function compileQuantified(builder, node) {
|
|
173
|
-
const { body, min, max, lazy } = node;
|
|
174
|
-
for (let i = 0; i < min; i++)
|
|
175
|
-
compileNode(builder, body);
|
|
176
|
-
if (max === undefined) {
|
|
177
|
-
compileStar(builder, body, lazy);
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
compileOptionalChain(builder, body, max - min, lazy);
|
|
84
|
+
return {
|
|
85
|
+
kind: "repeat",
|
|
86
|
+
body: node.body,
|
|
87
|
+
min: node.min,
|
|
88
|
+
max: node.max,
|
|
89
|
+
lazy: node.lazy,
|
|
90
|
+
};
|
|
181
91
|
}
|
|
182
92
|
}
|
|
183
93
|
/**
|
|
@@ -191,16 +101,30 @@ function compileQuantified(builder, node) {
|
|
|
191
101
|
* open with `^` (those simply fail `assertStart` at every offset but 0, which
|
|
192
102
|
* is correct, just not the fastest possible instruction count for that case).
|
|
193
103
|
*
|
|
104
|
+
* Iterative: neither the depth of the tree, the number of alternatives nor a
|
|
105
|
+
* repetition count grows the JavaScript stack, so a hand-built tree of any
|
|
106
|
+
* shape compiles or fails with a typed error. O(syntax nodes lowered) time,
|
|
107
|
+
* where a counted repetition lowers its body once per copy; O(program size)
|
|
108
|
+
* memory.
|
|
109
|
+
*
|
|
194
110
|
* @param maxProgramSize - Most instructions the program may hold; see
|
|
195
111
|
* `RegexLimits.maxProgramSize`, whose default this takes.
|
|
196
|
-
* @
|
|
112
|
+
* @param budget - Charged one unit per syntax node lowered: a step bound
|
|
113
|
+
* (`RegexLimits.maxSteps`'s default when omitted), or a `WorkBudget`
|
|
114
|
+
* (`@ac-kit/core`) shared with other calls.
|
|
115
|
+
* @throws {RegexLimitExceededError} If the program outgrows `maxProgramSize`,
|
|
116
|
+
* or the lowering outgrows a numeric step bound.
|
|
117
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
197
118
|
*/
|
|
198
|
-
export function compileProgram(pattern, maxProgramSize = DEFAULT_REGEX_LIMITS.maxProgramSize) {
|
|
119
|
+
export function compileProgram(pattern, maxProgramSize = DEFAULT_REGEX_LIMITS.maxProgramSize, budget = DEFAULT_REGEX_LIMITS.maxSteps) {
|
|
120
|
+
return withWorkBudget(budget, (work) => compileWithin_(pattern, maxProgramSize, work), stepsExceeded_);
|
|
121
|
+
}
|
|
122
|
+
function compileWithin_(pattern, maxProgramSize, budget) {
|
|
199
123
|
const builder = new ProgramBuilder(maxProgramSize);
|
|
200
124
|
const scanSplitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
201
125
|
const tryStart = builder.length;
|
|
202
126
|
builder.emit({ op: "save", slot: 0 });
|
|
203
|
-
|
|
127
|
+
emitThompson(builder, pattern.body, shapeOfRegexNode_, budget);
|
|
204
128
|
builder.emit({ op: "save", slot: 1 });
|
|
205
129
|
builder.emit({ op: "match" });
|
|
206
130
|
const consumeStart = builder.length;
|
package/dist/vm/compile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,
|
|
1
|
+
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,EAEjB,cAAc,GACd,MAAM,cAAc,CAAC;AAQtB,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EACN,YAAY,EACZ,cAAc,GAEd,MAAM,gBAAgB,CAAC;AAGxB,MAAM,UAAU,GAAG,CAAC,IAAY,EAAW,EAAE,CAC5C,mBAAmB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,GAAG,CAAC;AAEzD,MAAM,oBAAoB,GAEtB;IACH,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAC7C,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAC9C,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,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