@ac-kit/format-regex 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.
- package/LICENSE +28 -0
- package/README.md +31 -0
- package/dist/ast.d.ts +89 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +2 -0
- package/dist/ast.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/lexer.d.ts +17 -0
- package/dist/lexer.d.ts.map +1 -0
- package/dist/lexer.js +295 -0
- package/dist/lexer.js.map +1 -0
- package/dist/parser.d.ts +9 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +231 -0
- package/dist/parser.js.map +1 -0
- package/dist/printer.d.ts +4 -0
- package/dist/printer.d.ts.map +1 -0
- package/dist/printer.js +87 -0
- package/dist/printer.js.map +1 -0
- package/dist/regex.d.ts +19 -0
- package/dist/regex.d.ts.map +1 -0
- package/dist/regex.js +23 -0
- package/dist/regex.js.map +1 -0
- package/dist/token.d.ts +29 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +24 -0
- package/dist/token.js.map +1 -0
- package/dist/vm/compile.d.ts +15 -0
- package/dist/vm/compile.d.ts.map +1 -0
- package/dist/vm/compile.js +201 -0
- package/dist/vm/compile.js.map +1 -0
- package/dist/vm/pike-vm.d.ts +28 -0
- package/dist/vm/pike-vm.d.ts.map +1 -0
- package/dist/vm/pike-vm.js +139 -0
- package/dist/vm/pike-vm.js.map +1 -0
- package/dist/vm/program.d.ts +49 -0
- package/dist/vm/program.d.ts.map +1 -0
- package/dist/vm/program.js +2 -0
- package/dist/vm/program.js.map +1 -0
- package/dist/walk.d.ts +8 -0
- package/dist/walk.d.ts.map +1 -0
- package/dist/walk.js +24 -0
- package/dist/walk.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { isAsciiAlphaNumeric, isAsciiDigit, isAsciiWhitespace, } from "@ac-kit/core";
|
|
2
|
+
const isWordChar = (char) => isAsciiAlphaNumeric(char.charCodeAt(0)) || char === "_";
|
|
3
|
+
const SHORTHAND_PREDICATES = {
|
|
4
|
+
d: (char) => isAsciiDigit(char.charCodeAt(0)),
|
|
5
|
+
D: (char) => !isAsciiDigit(char.charCodeAt(0)),
|
|
6
|
+
w: isWordChar,
|
|
7
|
+
W: (char) => !isWordChar(char),
|
|
8
|
+
s: (char) => isAsciiWhitespace(char.charCodeAt(0)),
|
|
9
|
+
S: (char) => !isAsciiWhitespace(char.charCodeAt(0)),
|
|
10
|
+
};
|
|
11
|
+
function compileCharClassPredicate(negated, items) {
|
|
12
|
+
const singles = new Set();
|
|
13
|
+
const ranges = [];
|
|
14
|
+
const shorthands = [];
|
|
15
|
+
for (const item of items) {
|
|
16
|
+
if (item.kind === "char")
|
|
17
|
+
singles.add(item.char);
|
|
18
|
+
else if (item.kind === "range")
|
|
19
|
+
ranges.push({ from: item.from, to: item.to });
|
|
20
|
+
else
|
|
21
|
+
shorthands.push(item.value);
|
|
22
|
+
}
|
|
23
|
+
const test = (char) => {
|
|
24
|
+
if (singles.has(char))
|
|
25
|
+
return true;
|
|
26
|
+
for (const range of ranges) {
|
|
27
|
+
if (char >= range.from && char <= range.to)
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
for (const shorthand of shorthands) {
|
|
31
|
+
if (SHORTHAND_PREDICATES[shorthand](char))
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
return negated ? (char) => !test(char) : test;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Emits instructions and patches forward-jump targets once their destination is
|
|
40
|
+
* known.
|
|
41
|
+
*/
|
|
42
|
+
class ProgramBuilder {
|
|
43
|
+
instructions = [];
|
|
44
|
+
get length() {
|
|
45
|
+
return this.instructions.length;
|
|
46
|
+
}
|
|
47
|
+
emit(instruction) {
|
|
48
|
+
this.instructions.push(instruction);
|
|
49
|
+
return this.instructions.length - 1;
|
|
50
|
+
}
|
|
51
|
+
patchJmp(index, target) {
|
|
52
|
+
this.instructions[index] = { op: "jmp", target };
|
|
53
|
+
}
|
|
54
|
+
patchSplit(index, first, second) {
|
|
55
|
+
this.instructions[index] = { op: "split", first, second };
|
|
56
|
+
}
|
|
57
|
+
toInstructions() {
|
|
58
|
+
return this.instructions;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function compileNode(builder, node) {
|
|
62
|
+
switch (node.kind) {
|
|
63
|
+
case "literal":
|
|
64
|
+
builder.emit({ op: "char", char: node.char });
|
|
65
|
+
return;
|
|
66
|
+
case "any":
|
|
67
|
+
builder.emit({ op: "any" });
|
|
68
|
+
return;
|
|
69
|
+
case "shorthand":
|
|
70
|
+
builder.emit({
|
|
71
|
+
op: "predicate",
|
|
72
|
+
test: SHORTHAND_PREDICATES[node.value],
|
|
73
|
+
label: `\\${node.value}`,
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
case "anchor":
|
|
77
|
+
builder.emit(node.type === "start" ? { op: "assertStart" } : { op: "assertEnd" });
|
|
78
|
+
return;
|
|
79
|
+
case "charClass": {
|
|
80
|
+
const test = compileCharClassPredicate(node.negated, node.items);
|
|
81
|
+
builder.emit({
|
|
82
|
+
op: "predicate",
|
|
83
|
+
test,
|
|
84
|
+
label: node.negated ? "[^...]" : "[...]",
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
case "group": {
|
|
89
|
+
if (node.capturing) {
|
|
90
|
+
const slot = node.index * 2;
|
|
91
|
+
builder.emit({ op: "save", slot });
|
|
92
|
+
compileNode(builder, node.body);
|
|
93
|
+
builder.emit({ op: "save", slot: slot + 1 });
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
compileNode(builder, node.body);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
case "concat":
|
|
101
|
+
for (const item of node.items)
|
|
102
|
+
compileNode(builder, item);
|
|
103
|
+
return;
|
|
104
|
+
case "alternation":
|
|
105
|
+
compileAlternation(builder, node.alternatives);
|
|
106
|
+
return;
|
|
107
|
+
case "quantified":
|
|
108
|
+
compileQuantified(builder, node);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function compileAlternation(builder, alternatives) {
|
|
113
|
+
if (alternatives.length === 1) {
|
|
114
|
+
compileNode(builder, alternatives[0]);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// split first, second ; first: <alt0> ; jmp end ; second: <rest...> ; end:
|
|
118
|
+
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
119
|
+
const firstStart = builder.length;
|
|
120
|
+
compileNode(builder, alternatives[0]);
|
|
121
|
+
const jmpIndex = builder.emit({ op: "jmp", target: -1 });
|
|
122
|
+
const secondStart = builder.length;
|
|
123
|
+
builder.patchSplit(splitIndex, firstStart, secondStart);
|
|
124
|
+
compileAlternation(builder, alternatives.slice(1));
|
|
125
|
+
builder.patchJmp(jmpIndex, builder.length);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* `body*`/`body+`'s unbounded tail: `split top, end ; top: <body> ; jmp split ;
|
|
129
|
+
* end:`.
|
|
130
|
+
*/
|
|
131
|
+
function compileStar(builder, body, lazy) {
|
|
132
|
+
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
133
|
+
const bodyStart = builder.length;
|
|
134
|
+
compileNode(builder, body);
|
|
135
|
+
builder.emit({ op: "jmp", target: splitIndex });
|
|
136
|
+
const afterLoop = builder.length;
|
|
137
|
+
// Greedy prefers repeating (bodyStart first); lazy prefers exiting (afterLoop first).
|
|
138
|
+
if (lazy)
|
|
139
|
+
builder.patchSplit(splitIndex, afterLoop, bodyStart);
|
|
140
|
+
else
|
|
141
|
+
builder.patchSplit(splitIndex, bodyStart, afterLoop);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* `count` nested optional copies of `body` — the bounded tail of `{min,max}`.
|
|
145
|
+
*
|
|
146
|
+
* Nesting means skipping copy _i_ also skips every copy after it, since they
|
|
147
|
+
* are only reachable through copy _i_'s own body.
|
|
148
|
+
*/
|
|
149
|
+
function compileOptionalChain(builder, body, count, lazy) {
|
|
150
|
+
if (count === 0)
|
|
151
|
+
return;
|
|
152
|
+
const splitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
153
|
+
const bodyStart = builder.length;
|
|
154
|
+
compileNode(builder, body);
|
|
155
|
+
compileOptionalChain(builder, body, count - 1, lazy);
|
|
156
|
+
const afterAll = builder.length;
|
|
157
|
+
if (lazy)
|
|
158
|
+
builder.patchSplit(splitIndex, afterAll, bodyStart);
|
|
159
|
+
else
|
|
160
|
+
builder.patchSplit(splitIndex, bodyStart, afterAll);
|
|
161
|
+
}
|
|
162
|
+
function compileQuantified(builder, node) {
|
|
163
|
+
const { body, min, max, lazy } = node;
|
|
164
|
+
for (let i = 0; i < min; i++)
|
|
165
|
+
compileNode(builder, body);
|
|
166
|
+
if (max === undefined) {
|
|
167
|
+
compileStar(builder, body, lazy);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
compileOptionalChain(builder, body, max - min, lazy);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Compiles a parsed pattern to a {@link RegexProgram}.
|
|
175
|
+
*
|
|
176
|
+
* Every program embeds an unanchored left-to-right scan (`split <try match
|
|
177
|
+
* here>, <consume one char and retry>`) so a single VM run finds the leftmost
|
|
178
|
+
* match without the caller looping over start offsets. This always produces the
|
|
179
|
+
* same result as scanning start offsets one at a time — it just folds the scan
|
|
180
|
+
* into the bytecode instead — at the cost of not special-casing patterns that
|
|
181
|
+
* open with `^` (those simply fail `assertStart` at every offset but 0, which
|
|
182
|
+
* is correct, just not the fastest possible instruction count for that case).
|
|
183
|
+
*/
|
|
184
|
+
export function compileProgram(pattern) {
|
|
185
|
+
const builder = new ProgramBuilder();
|
|
186
|
+
const scanSplitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
187
|
+
const tryStart = builder.length;
|
|
188
|
+
builder.emit({ op: "save", slot: 0 });
|
|
189
|
+
compileNode(builder, pattern.body);
|
|
190
|
+
builder.emit({ op: "save", slot: 1 });
|
|
191
|
+
builder.emit({ op: "match" });
|
|
192
|
+
const consumeStart = builder.length;
|
|
193
|
+
builder.emit({ op: "anyByte" });
|
|
194
|
+
builder.emit({ op: "jmp", target: scanSplitIndex });
|
|
195
|
+
builder.patchSplit(scanSplitIndex, tryStart, consumeStart);
|
|
196
|
+
return {
|
|
197
|
+
instructions: builder.toInstructions(),
|
|
198
|
+
groupCount: pattern.groupCount,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=compile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,GACjB,MAAM,cAAc,CAAC;AAWtB,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,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC;IAC9B,CAAC,EAAE,CAAC,IAAI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,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC;IACpD,OAAO,CAAC,UAAU,CAAC,cAAc,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC;IAE3D,OAAO;QACN,YAAY,EAAE,OAAO,CAAC,cAAc,EAAE;QACtC,UAAU,EAAE,OAAO,CAAC,UAAU;KAC9B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RegexProgram } from "./program.js";
|
|
2
|
+
export interface RegexGroupMatch {
|
|
3
|
+
readonly start: number;
|
|
4
|
+
readonly end: number;
|
|
5
|
+
readonly text: string;
|
|
6
|
+
}
|
|
7
|
+
export interface RegexMatch {
|
|
8
|
+
readonly index: number;
|
|
9
|
+
readonly length: number;
|
|
10
|
+
readonly text: string;
|
|
11
|
+
/**
|
|
12
|
+
* `groups[0]` is always the overall match; `groups[i]` is capture group `i`,
|
|
13
|
+
* or `undefined` if it didn't participate.
|
|
14
|
+
*/
|
|
15
|
+
readonly groups: ReadonlyArray<RegexGroupMatch | undefined>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Runs a {@link RegexProgram} against `input`, returning the leftmost match
|
|
19
|
+
* (and, among threads starting at the same position, the one preferred by
|
|
20
|
+
* greedy/lazy quantifier priority) or `undefined` if there is none.
|
|
21
|
+
*
|
|
22
|
+
* This is a Pike VM: every thread advances one input character per step in
|
|
23
|
+
* lockstep, so this is O(program size × input length) with no backtracking — a
|
|
24
|
+
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
25
|
+
* behavior here the way it can in a backtracking engine.
|
|
26
|
+
*/
|
|
27
|
+
export declare function execProgram(program: RegexProgram, input: string): RegexMatch | undefined;
|
|
28
|
+
//# sourceMappingURL=pike-vm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pike-vm.d.ts","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAAA,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;AA6HD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAC1B,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,MAAM,GACX,UAAU,GAAG,SAAS,CAsCxB"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One run of the Pike VM: tracks which `pc`s have already been added this step,
|
|
3
|
+
* to keep each step O(program size).
|
|
4
|
+
*/
|
|
5
|
+
class ThreadList {
|
|
6
|
+
threads = [];
|
|
7
|
+
visitedAt;
|
|
8
|
+
generation = 0;
|
|
9
|
+
constructor(programLength) {
|
|
10
|
+
this.visitedAt = new Int32Array(programLength).fill(-1);
|
|
11
|
+
}
|
|
12
|
+
get all() {
|
|
13
|
+
return this.threads;
|
|
14
|
+
}
|
|
15
|
+
reset() {
|
|
16
|
+
this.threads.length = 0;
|
|
17
|
+
this.generation++;
|
|
18
|
+
}
|
|
19
|
+
hasVisited(pc) {
|
|
20
|
+
if (this.visitedAt[pc] === this.generation)
|
|
21
|
+
return true;
|
|
22
|
+
this.visitedAt[pc] = this.generation;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Follows `jmp`/`split`/`save`/assertions (zero-width) until a consuming or
|
|
27
|
+
* terminal instruction is reached.
|
|
28
|
+
*/
|
|
29
|
+
add(program, pc, saved, pos, inputLength) {
|
|
30
|
+
if (this.hasVisited(pc))
|
|
31
|
+
return;
|
|
32
|
+
const instruction = program.instructions[pc];
|
|
33
|
+
switch (instruction.op) {
|
|
34
|
+
case "jmp":
|
|
35
|
+
this.add(program, instruction.target, saved, pos, inputLength);
|
|
36
|
+
return;
|
|
37
|
+
case "split":
|
|
38
|
+
this.add(program, instruction.first, saved, pos, inputLength);
|
|
39
|
+
this.add(program, instruction.second, saved, pos, inputLength);
|
|
40
|
+
return;
|
|
41
|
+
case "save": {
|
|
42
|
+
const next = saved.slice();
|
|
43
|
+
next[instruction.slot] = pos;
|
|
44
|
+
this.add(program, pc + 1, next, pos, inputLength);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
case "assertStart":
|
|
48
|
+
if (pos === 0)
|
|
49
|
+
this.add(program, pc + 1, saved, pos, inputLength);
|
|
50
|
+
return;
|
|
51
|
+
case "assertEnd":
|
|
52
|
+
if (pos === inputLength)
|
|
53
|
+
this.add(program, pc + 1, saved, pos, inputLength);
|
|
54
|
+
return;
|
|
55
|
+
default:
|
|
56
|
+
this.threads.push({ pc, saved });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function matchesChar(instruction, char) {
|
|
61
|
+
switch (instruction.op) {
|
|
62
|
+
case "char":
|
|
63
|
+
return char === instruction.char;
|
|
64
|
+
case "any":
|
|
65
|
+
return char !== "\n";
|
|
66
|
+
case "anyByte":
|
|
67
|
+
return true;
|
|
68
|
+
case "predicate":
|
|
69
|
+
return instruction.test(char);
|
|
70
|
+
default:
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function extractMatch(saved, input, groupCount) {
|
|
75
|
+
const start = saved[0];
|
|
76
|
+
const end = saved[1];
|
|
77
|
+
const groups = [
|
|
78
|
+
{ start, end, text: input.slice(start, end) },
|
|
79
|
+
];
|
|
80
|
+
for (let group = 1; group <= groupCount; group++) {
|
|
81
|
+
const groupStart = saved[group * 2];
|
|
82
|
+
const groupEnd = saved[group * 2 + 1];
|
|
83
|
+
groups.push(groupStart === undefined ||
|
|
84
|
+
groupEnd === undefined ||
|
|
85
|
+
groupStart < 0 ||
|
|
86
|
+
groupEnd < 0
|
|
87
|
+
? undefined
|
|
88
|
+
: {
|
|
89
|
+
start: groupStart,
|
|
90
|
+
end: groupEnd,
|
|
91
|
+
text: input.slice(groupStart, groupEnd),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
index: start,
|
|
96
|
+
length: end - start,
|
|
97
|
+
text: input.slice(start, end),
|
|
98
|
+
groups,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Runs a {@link RegexProgram} against `input`, returning the leftmost match
|
|
103
|
+
* (and, among threads starting at the same position, the one preferred by
|
|
104
|
+
* greedy/lazy quantifier priority) or `undefined` if there is none.
|
|
105
|
+
*
|
|
106
|
+
* This is a Pike VM: every thread advances one input character per step in
|
|
107
|
+
* lockstep, so this is O(program size × input length) with no backtracking — a
|
|
108
|
+
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
109
|
+
* behavior here the way it can in a backtracking engine.
|
|
110
|
+
*/
|
|
111
|
+
export function execProgram(program, input) {
|
|
112
|
+
const slotCount = (program.groupCount + 1) * 2;
|
|
113
|
+
const inputLength = input.length;
|
|
114
|
+
let clist = new ThreadList(program.instructions.length);
|
|
115
|
+
let nlist = new ThreadList(program.instructions.length);
|
|
116
|
+
clist.add(program, 0, Array.from({ length: slotCount }, () => -1), 0, inputLength);
|
|
117
|
+
let matched;
|
|
118
|
+
for (let pos = 0; pos <= inputLength; pos++) {
|
|
119
|
+
if (clist.all.length === 0)
|
|
120
|
+
break;
|
|
121
|
+
nlist.reset();
|
|
122
|
+
const threads = clist.all;
|
|
123
|
+
for (const thread of threads) {
|
|
124
|
+
const instruction = program.instructions[thread.pc];
|
|
125
|
+
if (instruction.op === "match") {
|
|
126
|
+
matched = thread.saved;
|
|
127
|
+
break; // Lower-priority threads at this step are preempted.
|
|
128
|
+
}
|
|
129
|
+
if (pos < inputLength && matchesChar(instruction, input[pos])) {
|
|
130
|
+
nlist.add(program, thread.pc + 1, thread.saved, pos + 1, inputLength);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
[clist, nlist] = [nlist, clist];
|
|
134
|
+
}
|
|
135
|
+
return matched === undefined
|
|
136
|
+
? undefined
|
|
137
|
+
: extractMatch(matched, input, program.groupCount);
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=pike-vm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pike-vm.js","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAwBA;;;GAGG;AACH,MAAM,UAAU;IACE,OAAO,GAAa,EAAE,CAAC;IACvB,SAAS,CAAa;IAC/B,UAAU,GAAG,CAAC,CAAC;IAEvB,YAAY,aAAqB;QAChC,IAAI,CAAC,SAAS,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC;IAED,IAAI,GAAG;QACN,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;QACxB,IAAI,CAAC,UAAU,EAAE,CAAC;IACnB,CAAC;IAEO,UAAU,CAAC,EAAU;QAC5B,IAAI,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC,UAAU;YAAE,OAAO,IAAI,CAAC;QACxD,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC;QACrC,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;OAGG;IACH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CAAC;YACrD,IAAI,WAAW,CAAC,EAAE,KAAK,OAAO,EAAE,CAAC;gBAChC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;gBACvB,MAAM,CAAC,qDAAqD;YAC7D,CAAC;YACD,IAAI,GAAG,GAAG,WAAW,IAAI,WAAW,CAAC,WAAW,EAAE,KAAK,CAAC,GAAG,CAAE,CAAC,EAAE,CAAC;gBAChE,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC;YACvE,CAAC;QACF,CAAC;QAED,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,OAAO,KAAK,SAAS;QAC3B,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single compiled instruction — Thompson/Pike-style bytecode.
|
|
3
|
+
*
|
|
4
|
+
* `any` is the user-visible `.` (excludes a line terminator); `anyByte`
|
|
5
|
+
* unconditionally consumes one character regardless of value and is never
|
|
6
|
+
* produced from pattern syntax — {@link compileProgram} uses it only to advance
|
|
7
|
+
* the implicit unanchored-scan start position.
|
|
8
|
+
*/
|
|
9
|
+
export type Instruction = {
|
|
10
|
+
readonly op: "char";
|
|
11
|
+
readonly char: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly op: "any";
|
|
14
|
+
} | {
|
|
15
|
+
readonly op: "anyByte";
|
|
16
|
+
} | {
|
|
17
|
+
readonly op: "predicate";
|
|
18
|
+
readonly test: (char: string) => boolean;
|
|
19
|
+
readonly label: string;
|
|
20
|
+
} | {
|
|
21
|
+
readonly op: "assertStart";
|
|
22
|
+
} | {
|
|
23
|
+
readonly op: "assertEnd";
|
|
24
|
+
} | {
|
|
25
|
+
readonly op: "jmp";
|
|
26
|
+
readonly target: number;
|
|
27
|
+
} | {
|
|
28
|
+
readonly op: "split";
|
|
29
|
+
readonly first: number;
|
|
30
|
+
readonly second: number;
|
|
31
|
+
} | {
|
|
32
|
+
readonly op: "save";
|
|
33
|
+
readonly slot: number;
|
|
34
|
+
} | {
|
|
35
|
+
readonly op: "match";
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A compiled regex program: a flat instruction array plus how many capture
|
|
39
|
+
* slots it uses.
|
|
40
|
+
*/
|
|
41
|
+
export interface RegexProgram {
|
|
42
|
+
readonly instructions: readonly Instruction[];
|
|
43
|
+
/**
|
|
44
|
+
* Number of capturing groups (slots 0/1 are the overall match, not counted
|
|
45
|
+
* here).
|
|
46
|
+
*/
|
|
47
|
+
readonly groupCount: number;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=program.d.ts.map
|
|
@@ -0,0 +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,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;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,YAAY,EAAE,SAAS,WAAW,EAAE,CAAC;IAC9C;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"program.js","sourceRoot":"","sources":["../../src/vm/program.ts"],"names":[],"mappings":""}
|
package/dist/walk.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnyRegexNode } from "./ast.js";
|
|
2
|
+
import { RegexWalkControl } from "./index.js";
|
|
3
|
+
export { DfsWalkContinue as RegexWalkContinue, DfsWalkControl as RegexWalkControl, DfsWalkSkipChildren as RegexWalkSkipChildren, DfsWalkStop as RegexWalkStop, } from "@ac-kit/algo";
|
|
4
|
+
/** Called for each node visited by {@link walkRegex}. */
|
|
5
|
+
export type RegexNodeVisitor = (node: AnyRegexNode) => RegexWalkControl | undefined;
|
|
6
|
+
/** Walks a regex AST in pre-order. */
|
|
7
|
+
export declare function walkRegex(root: AnyRegexNode, visitor: RegexNodeVisitor): boolean;
|
|
8
|
+
//# sourceMappingURL=walk.d.ts.map
|
|
@@ -0,0 +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,sCAAsC;AACtC,wBAAgB,SAAS,CACxB,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,gBAAgB,GACvB,OAAO,CAET"}
|
package/dist/walk.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { dfs } from "@ac-kit/algo";
|
|
2
|
+
export { DfsWalkContinue as RegexWalkContinue, DfsWalkSkipChildren as RegexWalkSkipChildren, DfsWalkStop as RegexWalkStop, } from "@ac-kit/algo";
|
|
3
|
+
function childrenOf(node) {
|
|
4
|
+
switch (node.kind) {
|
|
5
|
+
case "literal":
|
|
6
|
+
case "any":
|
|
7
|
+
case "shorthand":
|
|
8
|
+
case "anchor":
|
|
9
|
+
case "charClass":
|
|
10
|
+
return undefined;
|
|
11
|
+
case "group":
|
|
12
|
+
case "quantified":
|
|
13
|
+
return [node.body];
|
|
14
|
+
case "alternation":
|
|
15
|
+
return node.alternatives;
|
|
16
|
+
case "concat":
|
|
17
|
+
return node.items;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Walks a regex AST in pre-order. */
|
|
21
|
+
export function walkRegex(root, visitor) {
|
|
22
|
+
return dfs(root, { childrenOf, onEnter: visitor });
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=walk.js.map
|
package/dist/walk.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walk.js","sourceRoot":"","sources":["../src/walk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAKnC,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,sCAAsC;AACtC,MAAM,UAAU,SAAS,CACxB,IAAkB,EAClB,OAAyB;IAEzB,OAAO,GAAG,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;AACpD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ac-kit/format-regex",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A regex-dialect lexer, parser and printer, with a Thompson-construction and Pike-VM matcher.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"automaton",
|
|
7
|
+
"parser",
|
|
8
|
+
"pike-vm",
|
|
9
|
+
"regex",
|
|
10
|
+
"thompson"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://anthochamp.github.io/node-essentials/api/format-regex/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/anthochamp/node-essentials/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "BSD-3-Clause",
|
|
17
|
+
"author": "Anthony Champagne <dev@anthonychampagne.fr>",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/anthochamp/node-essentials.git",
|
|
21
|
+
"directory": "packages/format-regex"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"compile": "tsc -p ./tsconfig.build.json -outDir dist",
|
|
41
|
+
"compile:check": "tsc -noEmit -p ./tsconfig.json",
|
|
42
|
+
"clean": "rimraf dist",
|
|
43
|
+
"build": "yarn clean && yarn compile",
|
|
44
|
+
"test": "yarn run -T vitest run",
|
|
45
|
+
"prepack": "yarn build"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@ac-kit/algo": "^0.1.0",
|
|
49
|
+
"@ac-kit/core": "^0.1.0",
|
|
50
|
+
"@types/node": "^26.5.1"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@ac-kit/tsconfig": "0.1.0",
|
|
54
|
+
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
|
+
"@ac-kit/vitest-config": "0.1.0",
|
|
56
|
+
"@typescript/native": "typescript@7.0.2",
|
|
57
|
+
"rimraf": "6.1.3",
|
|
58
|
+
"typedoc": "0.28.20",
|
|
59
|
+
"typescript": "@typescript/typescript6@6.0.2"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=24"
|
|
63
|
+
}
|
|
64
|
+
}
|