seqex 0.1.1
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 +21 -0
- package/README.md +305 -0
- package/dist/ast.d.ts +24 -0
- package/dist/engine.d.ts +14 -0
- package/dist/index.cjs +517 -0
- package/dist/index.cjs.map +15 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +485 -0
- package/dist/index.js.map +15 -0
- package/dist/matcher.d.ts +13 -0
- package/dist/nfa.d.ts +15 -0
- package/dist/pattern.d.ts +24 -0
- package/dist/scanner.d.ts +19 -0
- package/package.json +54 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
// src/ast.ts
|
|
2
|
+
function isGreedy(node) {
|
|
3
|
+
switch (node.type) {
|
|
4
|
+
case "predicate":
|
|
5
|
+
case "anchor":
|
|
6
|
+
case "any":
|
|
7
|
+
return true;
|
|
8
|
+
case "sequence":
|
|
9
|
+
return node.children.every(isGreedy);
|
|
10
|
+
case "quantifier":
|
|
11
|
+
return node.greedy && isGreedy(node.child);
|
|
12
|
+
case "alternation":
|
|
13
|
+
return isGreedy(node.left) && isGreedy(node.right);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/engine.ts
|
|
18
|
+
function epsilonClosure(states, position, seqLength) {
|
|
19
|
+
const result = new Set;
|
|
20
|
+
const stack = [...states];
|
|
21
|
+
while (stack.length > 0) {
|
|
22
|
+
const state = stack.pop();
|
|
23
|
+
if (result.has(state))
|
|
24
|
+
continue;
|
|
25
|
+
if (state.anchorCheck && !state.anchorCheck(position, seqLength)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
result.add(state);
|
|
29
|
+
for (const target of state.epsilons) {
|
|
30
|
+
if (!result.has(target)) {
|
|
31
|
+
stack.push(target);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
function simulate(nfa, sequence, offset, greedy = true) {
|
|
38
|
+
let active = epsilonClosure(new Set([nfa.start]), offset, sequence.length);
|
|
39
|
+
let lastAcceptLength = active.has(nfa.accept) ? 0 : -1;
|
|
40
|
+
if (!greedy && lastAcceptLength >= 0) {
|
|
41
|
+
return { matched: true, length: 0 };
|
|
42
|
+
}
|
|
43
|
+
for (let i = offset;i < sequence.length; i++) {
|
|
44
|
+
const element = sequence[i];
|
|
45
|
+
const next = new Set;
|
|
46
|
+
for (const state of active) {
|
|
47
|
+
for (const transition of state.transitions) {
|
|
48
|
+
if (transition.predicate(element)) {
|
|
49
|
+
next.add(transition.target);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
active = epsilonClosure(next, i + 1, sequence.length);
|
|
54
|
+
if (active.size === 0)
|
|
55
|
+
break;
|
|
56
|
+
if (active.has(nfa.accept)) {
|
|
57
|
+
lastAcceptLength = i - offset + 1;
|
|
58
|
+
if (!greedy)
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
matched: lastAcceptLength >= 0,
|
|
64
|
+
length: Math.max(lastAcceptLength, 0)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function findAll(nfa, sequence, greedy = true) {
|
|
68
|
+
const results = [];
|
|
69
|
+
let pos = 0;
|
|
70
|
+
while (pos < sequence.length) {
|
|
71
|
+
const result = simulate(nfa, sequence, pos, greedy);
|
|
72
|
+
if (result.matched && result.length > 0) {
|
|
73
|
+
results.push({
|
|
74
|
+
start: pos,
|
|
75
|
+
end: pos + result.length - 1,
|
|
76
|
+
data: sequence.slice(pos, pos + result.length)
|
|
77
|
+
});
|
|
78
|
+
pos += result.length;
|
|
79
|
+
} else {
|
|
80
|
+
pos += 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
}
|
|
85
|
+
function findFirst(nfa, sequence, greedy = true) {
|
|
86
|
+
let pos = 0;
|
|
87
|
+
while (pos < sequence.length) {
|
|
88
|
+
const result = simulate(nfa, sequence, pos, greedy);
|
|
89
|
+
if (result.matched && result.length > 0) {
|
|
90
|
+
return {
|
|
91
|
+
start: pos,
|
|
92
|
+
end: pos + result.length - 1,
|
|
93
|
+
data: sequence.slice(pos, pos + result.length)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
pos += 1;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
function test(nfa, sequence, greedy = true) {
|
|
101
|
+
return findFirst(nfa, sequence, greedy) !== null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/scanner.ts
|
|
105
|
+
class Scanner {
|
|
106
|
+
nfa;
|
|
107
|
+
greedy;
|
|
108
|
+
position = 0;
|
|
109
|
+
scanStart = 0;
|
|
110
|
+
buffer = [];
|
|
111
|
+
active = new Set;
|
|
112
|
+
lastAcceptLength = -1;
|
|
113
|
+
simulationAlive = false;
|
|
114
|
+
constructor(nfa, greedy) {
|
|
115
|
+
this.nfa = nfa;
|
|
116
|
+
this.greedy = greedy;
|
|
117
|
+
this.initSimulation(0, Infinity);
|
|
118
|
+
}
|
|
119
|
+
push(element) {
|
|
120
|
+
this.buffer.push(element);
|
|
121
|
+
if (this.simulationAlive) {
|
|
122
|
+
this.step(element, this.position, Infinity);
|
|
123
|
+
}
|
|
124
|
+
this.position++;
|
|
125
|
+
return this.drain(Infinity);
|
|
126
|
+
}
|
|
127
|
+
end() {
|
|
128
|
+
const totalLength = this.position;
|
|
129
|
+
const results = [];
|
|
130
|
+
if (this.simulationAlive && this.active.size > 0) {
|
|
131
|
+
const finalActive = epsilonClosure(this.active, this.position, totalLength);
|
|
132
|
+
if (finalActive.has(this.nfa.accept)) {
|
|
133
|
+
this.lastAcceptLength = this.position - this.scanStart;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (this.lastAcceptLength > 0) {
|
|
137
|
+
results.push(this.emitMatch());
|
|
138
|
+
} else if (this.scanStart < totalLength) {
|
|
139
|
+
this.scanStart++;
|
|
140
|
+
}
|
|
141
|
+
while (this.scanStart < totalLength) {
|
|
142
|
+
this.initSimulation(this.scanStart, totalLength);
|
|
143
|
+
const bufferOffset = this.scanStart - (this.position - this.buffer.length);
|
|
144
|
+
for (let i = this.scanStart;i < totalLength; i++) {
|
|
145
|
+
const element = this.buffer[bufferOffset + (i - this.scanStart)];
|
|
146
|
+
this.step(element, i, totalLength);
|
|
147
|
+
if (!this.simulationAlive)
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
if (this.simulationAlive && this.active.size > 0) {
|
|
151
|
+
const finalActive = epsilonClosure(this.active, totalLength, totalLength);
|
|
152
|
+
if (finalActive.has(this.nfa.accept)) {
|
|
153
|
+
this.lastAcceptLength = totalLength - this.scanStart;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (this.lastAcceptLength > 0) {
|
|
157
|
+
results.push(this.emitMatch());
|
|
158
|
+
} else {
|
|
159
|
+
this.scanStart++;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return results;
|
|
163
|
+
}
|
|
164
|
+
initSimulation(startPos, seqLength) {
|
|
165
|
+
this.active = epsilonClosure(new Set([this.nfa.start]), startPos, seqLength);
|
|
166
|
+
this.lastAcceptLength = this.active.has(this.nfa.accept) ? 0 : -1;
|
|
167
|
+
this.simulationAlive = true;
|
|
168
|
+
}
|
|
169
|
+
step(element, absolutePosition, seqLength) {
|
|
170
|
+
const next = new Set;
|
|
171
|
+
for (const state of this.active) {
|
|
172
|
+
for (const transition of state.transitions) {
|
|
173
|
+
if (transition.predicate(element)) {
|
|
174
|
+
next.add(transition.target);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
this.active = epsilonClosure(next, absolutePosition + 1, seqLength);
|
|
179
|
+
if (this.active.size === 0) {
|
|
180
|
+
this.simulationAlive = false;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (this.active.has(this.nfa.accept)) {
|
|
184
|
+
this.lastAcceptLength = absolutePosition - this.scanStart + 1;
|
|
185
|
+
if (!this.greedy) {
|
|
186
|
+
this.simulationAlive = false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
drain(seqLength) {
|
|
191
|
+
const results = [];
|
|
192
|
+
while (!this.simulationAlive && this.scanStart < this.position) {
|
|
193
|
+
if (this.lastAcceptLength > 0) {
|
|
194
|
+
results.push(this.emitMatch());
|
|
195
|
+
} else {
|
|
196
|
+
this.scanStart++;
|
|
197
|
+
}
|
|
198
|
+
if (this.scanStart >= this.position)
|
|
199
|
+
break;
|
|
200
|
+
this.initSimulation(this.scanStart, seqLength);
|
|
201
|
+
const bufferStart = this.position - this.buffer.length;
|
|
202
|
+
for (let i = this.scanStart;i < this.position; i++) {
|
|
203
|
+
if (!this.simulationAlive)
|
|
204
|
+
break;
|
|
205
|
+
this.step(this.buffer[i - bufferStart], i, seqLength);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!this.simulationAlive && this.scanStart === this.position) {
|
|
209
|
+
this.initSimulation(this.scanStart, seqLength);
|
|
210
|
+
}
|
|
211
|
+
const consumed = this.scanStart - (this.position - this.buffer.length);
|
|
212
|
+
if (consumed > 0) {
|
|
213
|
+
this.buffer = this.buffer.slice(consumed);
|
|
214
|
+
}
|
|
215
|
+
return results;
|
|
216
|
+
}
|
|
217
|
+
emitMatch() {
|
|
218
|
+
const bufferStart = this.position - this.buffer.length;
|
|
219
|
+
const matchStart = this.scanStart;
|
|
220
|
+
const matchLength = this.lastAcceptLength;
|
|
221
|
+
const dataStart = matchStart - bufferStart;
|
|
222
|
+
const result = {
|
|
223
|
+
start: matchStart,
|
|
224
|
+
end: matchStart + matchLength - 1,
|
|
225
|
+
data: this.buffer.slice(dataStart, dataStart + matchLength)
|
|
226
|
+
};
|
|
227
|
+
this.scanStart = matchStart + matchLength;
|
|
228
|
+
this.lastAcceptLength = -1;
|
|
229
|
+
this.simulationAlive = false;
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/matcher.ts
|
|
235
|
+
class Matcher {
|
|
236
|
+
nfa;
|
|
237
|
+
greedy;
|
|
238
|
+
constructor(nfa, greedy) {
|
|
239
|
+
this.nfa = nfa;
|
|
240
|
+
this.greedy = greedy;
|
|
241
|
+
}
|
|
242
|
+
findAll(sequence) {
|
|
243
|
+
if (Array.isArray(sequence)) {
|
|
244
|
+
return findAll(this.nfa, sequence, this.greedy);
|
|
245
|
+
}
|
|
246
|
+
const scanner = this.scanner();
|
|
247
|
+
const results = [];
|
|
248
|
+
for (const element of sequence) {
|
|
249
|
+
results.push(...scanner.push(element));
|
|
250
|
+
}
|
|
251
|
+
results.push(...scanner.end());
|
|
252
|
+
return results;
|
|
253
|
+
}
|
|
254
|
+
find(sequence) {
|
|
255
|
+
if (Array.isArray(sequence)) {
|
|
256
|
+
return findFirst(this.nfa, sequence, this.greedy);
|
|
257
|
+
}
|
|
258
|
+
const scanner = this.scanner();
|
|
259
|
+
for (const element of sequence) {
|
|
260
|
+
const results2 = scanner.push(element);
|
|
261
|
+
if (results2.length > 0)
|
|
262
|
+
return results2[0];
|
|
263
|
+
}
|
|
264
|
+
const results = scanner.end();
|
|
265
|
+
return results.length > 0 ? results[0] : null;
|
|
266
|
+
}
|
|
267
|
+
test(sequence) {
|
|
268
|
+
if (Array.isArray(sequence)) {
|
|
269
|
+
return test(this.nfa, sequence, this.greedy);
|
|
270
|
+
}
|
|
271
|
+
return this.find(sequence) !== null;
|
|
272
|
+
}
|
|
273
|
+
scanner() {
|
|
274
|
+
return new Scanner(this.nfa, this.greedy);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/nfa.ts
|
|
279
|
+
function compile(node) {
|
|
280
|
+
let nextId = 0;
|
|
281
|
+
function createState() {
|
|
282
|
+
return { id: nextId++, epsilons: [], transitions: [] };
|
|
283
|
+
}
|
|
284
|
+
function compileNode(node2) {
|
|
285
|
+
switch (node2.type) {
|
|
286
|
+
case "predicate":
|
|
287
|
+
return compilePredicate(node2.fn);
|
|
288
|
+
case "sequence":
|
|
289
|
+
return compileSequence(node2.children);
|
|
290
|
+
case "quantifier":
|
|
291
|
+
return compileQuantifier(node2.child, node2.min, node2.max, node2.greedy);
|
|
292
|
+
case "alternation":
|
|
293
|
+
return compileAlternation(node2.left, node2.right);
|
|
294
|
+
case "anchor":
|
|
295
|
+
return compileAnchor(node2.position);
|
|
296
|
+
case "any":
|
|
297
|
+
return compilePredicate(() => true);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function compilePredicate(fn) {
|
|
301
|
+
const start = createState();
|
|
302
|
+
const accept = createState();
|
|
303
|
+
start.transitions.push({ predicate: fn, target: accept });
|
|
304
|
+
return { start, accept };
|
|
305
|
+
}
|
|
306
|
+
function compileSequence(children) {
|
|
307
|
+
if (children.length === 0) {
|
|
308
|
+
const start = createState();
|
|
309
|
+
const accept = createState();
|
|
310
|
+
start.epsilons.push(accept);
|
|
311
|
+
return { start, accept };
|
|
312
|
+
}
|
|
313
|
+
if (children.length === 1) {
|
|
314
|
+
return compileNode(children[0]);
|
|
315
|
+
}
|
|
316
|
+
const fragments = children.map((child) => compileNode(child));
|
|
317
|
+
for (let i = 0;i < fragments.length - 1; i++) {
|
|
318
|
+
fragments[i].accept.epsilons.push(fragments[i + 1].start);
|
|
319
|
+
}
|
|
320
|
+
return { start: fragments[0].start, accept: fragments[fragments.length - 1].accept };
|
|
321
|
+
}
|
|
322
|
+
function compileQuantifier(child, min, max, greedy) {
|
|
323
|
+
const start = createState();
|
|
324
|
+
const accept = createState();
|
|
325
|
+
let lastState = start;
|
|
326
|
+
for (let i = 0;i < min; i++) {
|
|
327
|
+
const fragment = compileNode(child);
|
|
328
|
+
lastState.epsilons.push(fragment.start);
|
|
329
|
+
lastState = fragment.accept;
|
|
330
|
+
}
|
|
331
|
+
if (max === Infinity) {
|
|
332
|
+
const loopFragment = compileNode(child);
|
|
333
|
+
lastState.epsilons.push(...greedy ? [loopFragment.start, accept] : [accept, loopFragment.start]);
|
|
334
|
+
loopFragment.accept.epsilons.push(lastState);
|
|
335
|
+
} else {
|
|
336
|
+
for (let i = min;i < max; i++) {
|
|
337
|
+
const fragment = compileNode(child);
|
|
338
|
+
lastState.epsilons.push(...greedy ? [fragment.start, accept] : [accept, fragment.start]);
|
|
339
|
+
lastState = fragment.accept;
|
|
340
|
+
}
|
|
341
|
+
lastState.epsilons.push(accept);
|
|
342
|
+
}
|
|
343
|
+
return { start, accept };
|
|
344
|
+
}
|
|
345
|
+
function compileAlternation(left, right) {
|
|
346
|
+
const start = createState();
|
|
347
|
+
const accept = createState();
|
|
348
|
+
const leftNFA = compileNode(left);
|
|
349
|
+
const rightNFA = compileNode(right);
|
|
350
|
+
start.epsilons.push(leftNFA.start, rightNFA.start);
|
|
351
|
+
leftNFA.accept.epsilons.push(accept);
|
|
352
|
+
rightNFA.accept.epsilons.push(accept);
|
|
353
|
+
return { start, accept };
|
|
354
|
+
}
|
|
355
|
+
function compileAnchor(position) {
|
|
356
|
+
const start = createState();
|
|
357
|
+
const accept = createState();
|
|
358
|
+
start.anchorCheck = position === "start" ? (index) => index === 0 : (index, length) => index === length;
|
|
359
|
+
start.epsilons.push(accept);
|
|
360
|
+
return { start, accept };
|
|
361
|
+
}
|
|
362
|
+
return compileNode(node);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// src/pattern.ts
|
|
366
|
+
class Pattern {
|
|
367
|
+
ast;
|
|
368
|
+
constructor(ast) {
|
|
369
|
+
this.ast = ast;
|
|
370
|
+
}
|
|
371
|
+
static where(fn) {
|
|
372
|
+
return new Pattern({
|
|
373
|
+
type: "sequence",
|
|
374
|
+
children: [{ type: "predicate", fn }]
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
static any() {
|
|
378
|
+
return new Pattern({
|
|
379
|
+
type: "sequence",
|
|
380
|
+
children: [{ type: "any" }]
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
static oneOf(...alternatives) {
|
|
384
|
+
if (alternatives.length === 0) {
|
|
385
|
+
throw new Error("Pattern.oneOf() requires at least one alternative");
|
|
386
|
+
}
|
|
387
|
+
const toNode = (alt) => alt instanceof Pattern ? alt.ast : { type: "predicate", fn: alt };
|
|
388
|
+
let node = toNode(alternatives[0]);
|
|
389
|
+
for (let i = 1;i < alternatives.length; i++) {
|
|
390
|
+
node = { type: "alternation", left: node, right: toNode(alternatives[i]) };
|
|
391
|
+
}
|
|
392
|
+
return new Pattern(node);
|
|
393
|
+
}
|
|
394
|
+
followedBy(fnOrPattern) {
|
|
395
|
+
const seq = this.getSequence();
|
|
396
|
+
const node = this.resolveNode(fnOrPattern);
|
|
397
|
+
return new Pattern({
|
|
398
|
+
type: "sequence",
|
|
399
|
+
children: [...seq, node]
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
followedByAny() {
|
|
403
|
+
const seq = this.getSequence();
|
|
404
|
+
return new Pattern({
|
|
405
|
+
type: "sequence",
|
|
406
|
+
children: [...seq, { type: "any" }]
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
oneOrMore(greedy = true) {
|
|
410
|
+
return this.applyQuantifier(1, Infinity, greedy);
|
|
411
|
+
}
|
|
412
|
+
zeroOrMore(greedy = true) {
|
|
413
|
+
return this.applyQuantifier(0, Infinity, greedy);
|
|
414
|
+
}
|
|
415
|
+
optional(greedy = true) {
|
|
416
|
+
return this.applyQuantifier(0, 1, greedy);
|
|
417
|
+
}
|
|
418
|
+
times(n) {
|
|
419
|
+
return this.applyQuantifier(n, n, true);
|
|
420
|
+
}
|
|
421
|
+
between(min, max, greedy = true) {
|
|
422
|
+
return this.applyQuantifier(min, max, greedy);
|
|
423
|
+
}
|
|
424
|
+
or(fnOrPattern) {
|
|
425
|
+
return new Pattern({
|
|
426
|
+
type: "alternation",
|
|
427
|
+
left: this.ast,
|
|
428
|
+
right: this.resolveNode(fnOrPattern)
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
atStart() {
|
|
432
|
+
const seq = this.getSequence();
|
|
433
|
+
return new Pattern({
|
|
434
|
+
type: "sequence",
|
|
435
|
+
children: [{ type: "anchor", position: "start" }, ...seq]
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
atEnd() {
|
|
439
|
+
const seq = this.getSequence();
|
|
440
|
+
return new Pattern({
|
|
441
|
+
type: "sequence",
|
|
442
|
+
children: [...seq, { type: "anchor", position: "end" }]
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
compile() {
|
|
446
|
+
return new Matcher(compile(this.ast), isGreedy(this.ast));
|
|
447
|
+
}
|
|
448
|
+
toAST() {
|
|
449
|
+
return this.ast;
|
|
450
|
+
}
|
|
451
|
+
resolveNode(fnOrPattern) {
|
|
452
|
+
return fnOrPattern instanceof Pattern ? fnOrPattern.ast : { type: "predicate", fn: fnOrPattern };
|
|
453
|
+
}
|
|
454
|
+
getSequence() {
|
|
455
|
+
if (this.ast.type === "sequence") {
|
|
456
|
+
return [...this.ast.children];
|
|
457
|
+
}
|
|
458
|
+
return [this.ast];
|
|
459
|
+
}
|
|
460
|
+
applyQuantifier(min, max, greedy) {
|
|
461
|
+
const seq = this.getSequence();
|
|
462
|
+
if (seq.length === 0) {
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
const rest = seq.slice(0, -1);
|
|
466
|
+
const quantified = {
|
|
467
|
+
type: "quantifier",
|
|
468
|
+
child: seq[seq.length - 1],
|
|
469
|
+
min,
|
|
470
|
+
max,
|
|
471
|
+
greedy
|
|
472
|
+
};
|
|
473
|
+
return new Pattern({
|
|
474
|
+
type: "sequence",
|
|
475
|
+
children: [...rest, quantified]
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
export {
|
|
480
|
+
Scanner,
|
|
481
|
+
Pattern,
|
|
482
|
+
Matcher
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
//# debugId=9657E6188A58006B64756E2164756E21
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/ast.ts", "../src/engine.ts", "../src/scanner.ts", "../src/matcher.ts", "../src/nfa.ts", "../src/pattern.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"export type Predicate<T> = (element: T) => boolean\n\nexport function isGreedy<T>(node: PatternNode<T>): boolean {\n switch (node.type) {\n case 'predicate':\n case 'anchor':\n case 'any':\n return true\n case 'sequence':\n return node.children.every(isGreedy)\n case 'quantifier':\n return node.greedy && isGreedy(node.child)\n case 'alternation':\n return isGreedy(node.left) && isGreedy(node.right)\n }\n}\n\nexport type PatternNode<T> =\n | { type: 'predicate'; fn: Predicate<T> }\n | { type: 'sequence'; children: PatternNode<T>[] }\n | {\n type: 'quantifier'\n child: PatternNode<T>\n min: number\n max: number\n greedy: boolean\n }\n | { type: 'alternation'; left: PatternNode<T>; right: PatternNode<T> }\n | { type: 'anchor'; position: 'start' | 'end' }\n | { type: 'any' }\n",
|
|
6
|
+
"import type { NFA, NFAState } from './nfa'\n\nexport interface MatchResult<T> {\n start: number\n end: number\n data: T[]\n}\n\nexport function epsilonClosure<T>(\n states: Set<NFAState<T>>,\n position: number,\n seqLength: number,\n): Set<NFAState<T>> {\n const result = new Set<NFAState<T>>()\n const stack = [...states]\n\n while (stack.length > 0) {\n const state = stack.pop()!\n if (result.has(state)) continue\n\n if (state.anchorCheck && !state.anchorCheck(position, seqLength)) {\n continue\n }\n\n result.add(state)\n\n for (const target of state.epsilons) {\n if (!result.has(target)) {\n stack.push(target)\n }\n }\n }\n\n return result\n}\n\nexport function simulate<T>(\n nfa: NFA<T>,\n sequence: T[],\n offset: number,\n greedy = true,\n): { matched: boolean; length: number } {\n let active = epsilonClosure(new Set([nfa.start]), offset, sequence.length)\n\n let lastAcceptLength = active.has(nfa.accept) ? 0 : -1\n\n if (!greedy && lastAcceptLength >= 0) {\n return { matched: true, length: 0 }\n }\n\n for (let i = offset; i < sequence.length; i++) {\n const element = sequence[i]\n const next = new Set<NFAState<T>>()\n\n for (const state of active) {\n for (const transition of state.transitions) {\n if (transition.predicate(element)) {\n next.add(transition.target)\n }\n }\n }\n\n active = epsilonClosure(next, i + 1, sequence.length)\n\n if (active.size === 0) break\n\n if (active.has(nfa.accept)) {\n lastAcceptLength = i - offset + 1\n if (!greedy) break\n }\n }\n\n return {\n matched: lastAcceptLength >= 0,\n length: Math.max(lastAcceptLength, 0),\n }\n}\n\nexport function findAll<T>(nfa: NFA<T>, sequence: T[], greedy = true): MatchResult<T>[] {\n const results: MatchResult<T>[] = []\n let pos = 0\n\n while (pos < sequence.length) {\n const result = simulate(nfa, sequence, pos, greedy)\n if (result.matched && result.length > 0) {\n results.push({\n start: pos,\n end: pos + result.length - 1,\n data: sequence.slice(pos, pos + result.length),\n })\n pos += result.length\n } else {\n pos += 1\n }\n }\n\n return results\n}\n\nexport function findFirst<T>(nfa: NFA<T>, sequence: T[], greedy = true): MatchResult<T> | null {\n let pos = 0\n\n while (pos < sequence.length) {\n const result = simulate(nfa, sequence, pos, greedy)\n if (result.matched && result.length > 0) {\n return {\n start: pos,\n end: pos + result.length - 1,\n data: sequence.slice(pos, pos + result.length),\n }\n }\n pos += 1\n }\n\n return null\n}\n\nexport function test<T>(nfa: NFA<T>, sequence: T[], greedy = true): boolean {\n return findFirst(nfa, sequence, greedy) !== null\n}\n",
|
|
7
|
+
"import type { NFA, NFAState } from './nfa'\nimport { epsilonClosure, type MatchResult } from './engine'\n\nexport class Scanner<T> {\n private nfa: NFA<T>\n private greedy: boolean\n\n private position = 0\n private scanStart = 0\n private buffer: T[] = []\n private active: Set<NFAState<T>> = new Set()\n private lastAcceptLength = -1\n private simulationAlive = false\n\n constructor(nfa: NFA<T>, greedy: boolean) {\n this.nfa = nfa\n this.greedy = greedy\n this.initSimulation(0, Infinity)\n }\n\n push(element: T): MatchResult<T>[] {\n this.buffer.push(element)\n\n if (this.simulationAlive) {\n this.step(element, this.position, Infinity)\n }\n\n this.position++\n return this.drain(Infinity)\n }\n\n end(): MatchResult<T>[] {\n const totalLength = this.position\n const results: MatchResult<T>[] = []\n\n // Check if current simulation has a pending match with end anchors\n if (this.simulationAlive && this.active.size > 0) {\n const finalActive = epsilonClosure(this.active, this.position, totalLength)\n if (finalActive.has(this.nfa.accept)) {\n this.lastAcceptLength = this.position - this.scanStart\n }\n }\n\n // Emit pending match if any\n if (this.lastAcceptLength > 0) {\n results.push(this.emitMatch())\n } else if (this.scanStart < totalLength) {\n this.scanStart++\n }\n\n // Process remaining buffer with known total length\n while (this.scanStart < totalLength) {\n this.initSimulation(this.scanStart, totalLength)\n\n const bufferOffset = this.scanStart - (this.position - this.buffer.length)\n for (let i = this.scanStart; i < totalLength; i++) {\n const element = this.buffer[bufferOffset + (i - this.scanStart)]\n this.step(element, i, totalLength)\n if (!this.simulationAlive) break\n }\n\n // Final end-anchor check\n if (this.simulationAlive && this.active.size > 0) {\n const finalActive = epsilonClosure(this.active, totalLength, totalLength)\n if (finalActive.has(this.nfa.accept)) {\n this.lastAcceptLength = totalLength - this.scanStart\n }\n }\n\n if (this.lastAcceptLength > 0) {\n results.push(this.emitMatch())\n } else {\n this.scanStart++\n }\n }\n\n return results\n }\n\n private initSimulation(startPos: number, seqLength: number): void {\n this.active = epsilonClosure(new Set([this.nfa.start]), startPos, seqLength)\n this.lastAcceptLength = this.active.has(this.nfa.accept) ? 0 : -1\n this.simulationAlive = true\n }\n\n private step(element: T, absolutePosition: number, seqLength: number): void {\n const next = new Set<NFAState<T>>()\n\n for (const state of this.active) {\n for (const transition of state.transitions) {\n if (transition.predicate(element)) {\n next.add(transition.target)\n }\n }\n }\n\n this.active = epsilonClosure(next, absolutePosition + 1, seqLength)\n\n if (this.active.size === 0) {\n this.simulationAlive = false\n return\n }\n\n if (this.active.has(this.nfa.accept)) {\n this.lastAcceptLength = absolutePosition - this.scanStart + 1\n if (!this.greedy) {\n this.simulationAlive = false\n }\n }\n }\n\n private drain(seqLength: number): MatchResult<T>[] {\n const results: MatchResult<T>[] = []\n\n while (!this.simulationAlive && this.scanStart < this.position) {\n if (this.lastAcceptLength > 0) {\n results.push(this.emitMatch())\n } else {\n this.scanStart++\n }\n\n if (this.scanStart >= this.position) break\n\n // Start new simulation and replay remaining buffer\n this.initSimulation(this.scanStart, seqLength)\n\n const bufferStart = this.position - this.buffer.length\n for (let i = this.scanStart; i < this.position; i++) {\n if (!this.simulationAlive) break\n this.step(this.buffer[i - bufferStart], i, seqLength)\n }\n }\n\n // Ensure simulation is ready for next push\n if (!this.simulationAlive && this.scanStart === this.position) {\n this.initSimulation(this.scanStart, seqLength)\n }\n\n // Trim consumed buffer\n const consumed = this.scanStart - (this.position - this.buffer.length)\n if (consumed > 0) {\n this.buffer = this.buffer.slice(consumed)\n }\n\n return results\n }\n\n private emitMatch(): MatchResult<T> {\n const bufferStart = this.position - this.buffer.length\n const matchStart = this.scanStart\n const matchLength = this.lastAcceptLength\n const dataStart = matchStart - bufferStart\n\n const result: MatchResult<T> = {\n start: matchStart,\n end: matchStart + matchLength - 1,\n data: this.buffer.slice(dataStart, dataStart + matchLength),\n }\n\n this.scanStart = matchStart + matchLength\n this.lastAcceptLength = -1\n this.simulationAlive = false\n\n return result\n }\n}\n",
|
|
8
|
+
"import type { NFA } from './nfa'\nimport { findAll, findFirst, test, type MatchResult } from './engine'\nimport { Scanner } from './scanner'\n\nexport { type MatchResult } from './engine'\n\nexport class Matcher<T> {\n private nfa: NFA<T>\n private greedy: boolean\n\n constructor(nfa: NFA<T>, greedy: boolean) {\n this.nfa = nfa\n this.greedy = greedy\n }\n\n findAll(sequence: Iterable<T>): MatchResult<T>[] {\n if (Array.isArray(sequence)) {\n return findAll(this.nfa, sequence, this.greedy)\n }\n const scanner = this.scanner()\n const results: MatchResult<T>[] = []\n for (const element of sequence) {\n results.push(...scanner.push(element))\n }\n results.push(...scanner.end())\n return results\n }\n\n find(sequence: Iterable<T>): MatchResult<T> | null {\n if (Array.isArray(sequence)) {\n return findFirst(this.nfa, sequence, this.greedy)\n }\n const scanner = this.scanner()\n for (const element of sequence) {\n const results = scanner.push(element)\n if (results.length > 0) return results[0]\n }\n const results = scanner.end()\n return results.length > 0 ? results[0] : null\n }\n\n test(sequence: Iterable<T>): boolean {\n if (Array.isArray(sequence)) {\n return test(this.nfa, sequence, this.greedy)\n }\n return this.find(sequence) !== null\n }\n\n scanner(): Scanner<T> {\n return new Scanner(this.nfa, this.greedy)\n }\n}\n",
|
|
9
|
+
"import type { PatternNode, Predicate } from './ast'\n\nexport interface NFAState<T> {\n id: number\n epsilons: NFAState<T>[]\n transitions: Array<{ predicate: Predicate<T>; target: NFAState<T> }>\n anchorCheck?: (index: number, length: number) => boolean\n}\n\nexport interface NFA<T> {\n start: NFAState<T>\n accept: NFAState<T>\n}\n\nexport function compile<T>(node: PatternNode<T>): NFA<T> {\n let nextId = 0\n\n function createState(): NFAState<T> {\n return { id: nextId++, epsilons: [], transitions: [] }\n }\n\n function compileNode(node: PatternNode<T>): NFA<T> {\n switch (node.type) {\n case 'predicate':\n return compilePredicate(node.fn)\n case 'sequence':\n return compileSequence(node.children)\n case 'quantifier':\n return compileQuantifier(node.child, node.min, node.max, node.greedy)\n case 'alternation':\n return compileAlternation(node.left, node.right)\n case 'anchor':\n return compileAnchor(node.position)\n case 'any':\n return compilePredicate(() => true)\n }\n }\n\n function compilePredicate(fn: Predicate<T>): NFA<T> {\n const start = createState()\n const accept = createState()\n start.transitions.push({ predicate: fn, target: accept })\n return { start, accept }\n }\n\n function compileSequence(children: PatternNode<T>[]): NFA<T> {\n if (children.length === 0) {\n const start = createState()\n const accept = createState()\n start.epsilons.push(accept)\n return { start, accept }\n }\n\n if (children.length === 1) {\n return compileNode(children[0])\n }\n\n const fragments = children.map(child => compileNode(child))\n\n for (let i = 0; i < fragments.length - 1; i++) {\n fragments[i].accept.epsilons.push(fragments[i + 1].start)\n }\n\n return { start: fragments[0].start, accept: fragments[fragments.length - 1].accept }\n }\n\n function compileQuantifier(\n child: PatternNode<T>,\n min: number,\n max: number,\n greedy: boolean,\n ): NFA<T> {\n const start = createState()\n const accept = createState()\n\n let lastState = start\n\n for (let i = 0; i < min; i++) {\n const fragment = compileNode(child)\n lastState.epsilons.push(fragment.start)\n lastState = fragment.accept\n }\n\n if (max === Infinity) {\n const loopFragment = compileNode(child)\n lastState.epsilons.push(\n ...(greedy ? [loopFragment.start, accept] : [accept, loopFragment.start]),\n )\n loopFragment.accept.epsilons.push(lastState)\n } else {\n for (let i = min; i < max; i++) {\n const fragment = compileNode(child)\n lastState.epsilons.push(...(greedy ? [fragment.start, accept] : [accept, fragment.start]))\n lastState = fragment.accept\n }\n lastState.epsilons.push(accept)\n }\n\n return { start, accept }\n }\n\n function compileAlternation(left: PatternNode<T>, right: PatternNode<T>): NFA<T> {\n const start = createState()\n const accept = createState()\n const leftNFA = compileNode(left)\n const rightNFA = compileNode(right)\n\n start.epsilons.push(leftNFA.start, rightNFA.start)\n leftNFA.accept.epsilons.push(accept)\n rightNFA.accept.epsilons.push(accept)\n\n return { start, accept }\n }\n\n function compileAnchor(position: 'start' | 'end'): NFA<T> {\n const start = createState()\n const accept = createState()\n\n start.anchorCheck =\n position === 'start'\n ? (index: number) => index === 0\n : (index: number, length: number) => index === length\n\n start.epsilons.push(accept)\n\n return { start, accept }\n }\n\n return compileNode(node)\n}\n",
|
|
10
|
+
"import { type PatternNode, type Predicate, isGreedy } from './ast'\nimport { Matcher } from './matcher'\nimport { compile } from './nfa'\n\nexport class Pattern<T> {\n private ast: PatternNode<T>\n\n private constructor(ast: PatternNode<T>) {\n this.ast = ast\n }\n\n static where<T>(fn: Predicate<T>): Pattern<T> {\n return new Pattern<T>({\n type: 'sequence',\n children: [{ type: 'predicate', fn }],\n })\n }\n\n static any<T>(): Pattern<T> {\n return new Pattern<T>({\n type: 'sequence',\n children: [{ type: 'any' }],\n })\n }\n\n static oneOf<T>(...alternatives: (Predicate<T> | Pattern<T>)[]): Pattern<T> {\n if (alternatives.length === 0) {\n throw new Error('Pattern.oneOf() requires at least one alternative')\n }\n\n const toNode = (alt: Predicate<T> | Pattern<T>): PatternNode<T> =>\n alt instanceof Pattern ? alt.ast : { type: 'predicate', fn: alt }\n\n let node = toNode(alternatives[0])\n for (let i = 1; i < alternatives.length; i++) {\n node = { type: 'alternation', left: node, right: toNode(alternatives[i]) }\n }\n\n return new Pattern<T>(node)\n }\n\n followedBy(fnOrPattern: Predicate<T> | Pattern<T>): Pattern<T> {\n const seq = this.getSequence()\n const node = this.resolveNode(fnOrPattern)\n return new Pattern<T>({\n type: 'sequence',\n children: [...seq, node],\n })\n }\n\n followedByAny(): Pattern<T> {\n const seq = this.getSequence()\n return new Pattern<T>({\n type: 'sequence',\n children: [...seq, { type: 'any' }],\n })\n }\n\n oneOrMore(greedy = true): Pattern<T> {\n return this.applyQuantifier(1, Infinity, greedy)\n }\n\n zeroOrMore(greedy = true): Pattern<T> {\n return this.applyQuantifier(0, Infinity, greedy)\n }\n\n optional(greedy = true): Pattern<T> {\n return this.applyQuantifier(0, 1, greedy)\n }\n\n times(n: number): Pattern<T> {\n return this.applyQuantifier(n, n, true)\n }\n\n between(min: number, max: number, greedy = true): Pattern<T> {\n return this.applyQuantifier(min, max, greedy)\n }\n\n or(fnOrPattern: Predicate<T> | Pattern<T>): Pattern<T> {\n return new Pattern<T>({\n type: 'alternation',\n left: this.ast,\n right: this.resolveNode(fnOrPattern),\n })\n }\n\n atStart(): Pattern<T> {\n const seq = this.getSequence()\n return new Pattern<T>({\n type: 'sequence',\n children: [{ type: 'anchor', position: 'start' }, ...seq],\n })\n }\n\n atEnd(): Pattern<T> {\n const seq = this.getSequence()\n return new Pattern<T>({\n type: 'sequence',\n children: [...seq, { type: 'anchor', position: 'end' }],\n })\n }\n\n compile(): Matcher<T> {\n return new Matcher<T>(compile(this.ast), isGreedy(this.ast))\n }\n\n toAST(): PatternNode<T> {\n return this.ast\n }\n\n private resolveNode(fnOrPattern: Predicate<T> | Pattern<T>): PatternNode<T> {\n return fnOrPattern instanceof Pattern ? fnOrPattern.ast : { type: 'predicate', fn: fnOrPattern }\n }\n\n private getSequence(): PatternNode<T>[] {\n if (this.ast.type === 'sequence') {\n return [...this.ast.children]\n }\n return [this.ast]\n }\n\n private applyQuantifier(min: number, max: number, greedy: boolean): Pattern<T> {\n const seq = this.getSequence()\n if (seq.length === 0) {\n return this\n }\n\n const rest = seq.slice(0, -1)\n const quantified: PatternNode<T> = {\n type: 'quantifier',\n child: seq[seq.length - 1],\n min,\n max,\n greedy,\n }\n\n return new Pattern<T>({\n type: 'sequence',\n children: [...rest, quantified],\n })\n }\n}\n"
|
|
11
|
+
],
|
|
12
|
+
"mappings": ";AAEO,SAAS,QAAW,CAAC,MAA+B;AAAA,EACzD,QAAQ,KAAK;AAAA,SACN;AAAA,SACA;AAAA,SACA;AAAA,MACH,OAAO;AAAA,SACJ;AAAA,MACH,OAAO,KAAK,SAAS,MAAM,QAAQ;AAAA,SAChC;AAAA,MACH,OAAO,KAAK,UAAU,SAAS,KAAK,KAAK;AAAA,SACtC;AAAA,MACH,OAAO,SAAS,KAAK,IAAI,KAAK,SAAS,KAAK,KAAK;AAAA;AAAA;;;ACLhD,SAAS,cAAiB,CAC/B,QACA,UACA,WACkB;AAAA,EAClB,MAAM,SAAS,IAAI;AAAA,EACnB,MAAM,QAAQ,CAAC,GAAG,MAAM;AAAA,EAExB,OAAO,MAAM,SAAS,GAAG;AAAA,IACvB,MAAM,QAAQ,MAAM,IAAI;AAAA,IACxB,IAAI,OAAO,IAAI,KAAK;AAAA,MAAG;AAAA,IAEvB,IAAI,MAAM,eAAe,CAAC,MAAM,YAAY,UAAU,SAAS,GAAG;AAAA,MAChE;AAAA,IACF;AAAA,IAEA,OAAO,IAAI,KAAK;AAAA,IAEhB,WAAW,UAAU,MAAM,UAAU;AAAA,MACnC,IAAI,CAAC,OA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;AAAA,IAC7B,OAAO,IAAI,QAAW;AAAA,MACpB,MAAM;AAAA,MACN,UAAU,CAAC,GAAG,KAAK,EAAE,MAAM,UAAU,UAAU,MAAM,CAAC;AAAA,IACxD,CAAC;AAAA;AAAA,EAGH,OAAO,GAAe;AAAA,IACpB,OAAO,IAAI,QAAW,QAAQ,KAAK,GAAG,GAAG,SAAS,KAAK,GAAG,CAAC;AAAA;AAAA,EAG7D,KAAK,GAAmB;AAAA,IACtB,OAAO,KAAK;AAAA;AAAA,EAGN,WAAW,CAAC,aAAwD;AAAA,IAC1E,OAAO,uBAAuB,UAAU,YAAY,MAAM,EAAE,MAAM,aAAa,IAAI,YAAY;AAAA;AAAA,EAGzF,WAAW,GAAqB;AAAA,IACtC,IAAI,KAAK,IAAI,SAAS,YAAY;AAAA,MAChC,OAAO,CAAC,GAAG,KAAK,IAAI,QAAQ;AAAA,IAC9B;AAAA,IACA,OAAO,CAAC,KAAK,GAAG;AAAA;AAAA,EAGV,eAAe,CAAC,KAAa,KAAa,QAA6B;AAAA,IAC7E,MAAM,MAAM,KAAK,YAAY;AAAA,IAC7B,IAAI,IAAI,WAAW,GAAG;AAAA,MACpB,OAAO;AAAA,IACT;AAAA,IAEA,MAAM,OAAO,IAAI,MAAM,GAAG,EAAE;AAAA,IAC5B,MAAM,aAA6B;AAAA,MACjC,MAAM;AAAA,MACN,OAAO,IAAI,IAAI,SAAS;AAAA,MACxB;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IAEA,OAAO,IAAI,QAAW;AAAA,MACpB,MAAM;AAAA,MACN,UAAU,CAAC,GAAG,MAAM,UAAU;AAAA,IAChC,CAAC;AAAA;AAEL;",
|
|
13
|
+
"debugId": "9657E6188A58006B64756E2164756E21",
|
|
14
|
+
"names": []
|
|
15
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { NFA } from './nfa';
|
|
2
|
+
import { type MatchResult } from './engine';
|
|
3
|
+
import { Scanner } from './scanner';
|
|
4
|
+
export { type MatchResult } from './engine';
|
|
5
|
+
export declare class Matcher<T> {
|
|
6
|
+
private nfa;
|
|
7
|
+
private greedy;
|
|
8
|
+
constructor(nfa: NFA<T>, greedy: boolean);
|
|
9
|
+
findAll(sequence: Iterable<T>): MatchResult<T>[];
|
|
10
|
+
find(sequence: Iterable<T>): MatchResult<T> | null;
|
|
11
|
+
test(sequence: Iterable<T>): boolean;
|
|
12
|
+
scanner(): Scanner<T>;
|
|
13
|
+
}
|
package/dist/nfa.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PatternNode, Predicate } from './ast';
|
|
2
|
+
export interface NFAState<T> {
|
|
3
|
+
id: number;
|
|
4
|
+
epsilons: NFAState<T>[];
|
|
5
|
+
transitions: Array<{
|
|
6
|
+
predicate: Predicate<T>;
|
|
7
|
+
target: NFAState<T>;
|
|
8
|
+
}>;
|
|
9
|
+
anchorCheck?: (index: number, length: number) => boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface NFA<T> {
|
|
12
|
+
start: NFAState<T>;
|
|
13
|
+
accept: NFAState<T>;
|
|
14
|
+
}
|
|
15
|
+
export declare function compile<T>(node: PatternNode<T>): NFA<T>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type PatternNode, type Predicate } from './ast';
|
|
2
|
+
import { Matcher } from './matcher';
|
|
3
|
+
export declare class Pattern<T> {
|
|
4
|
+
private ast;
|
|
5
|
+
private constructor();
|
|
6
|
+
static where<T>(fn: Predicate<T>): Pattern<T>;
|
|
7
|
+
static any<T>(): Pattern<T>;
|
|
8
|
+
static oneOf<T>(...alternatives: (Predicate<T> | Pattern<T>)[]): Pattern<T>;
|
|
9
|
+
followedBy(fnOrPattern: Predicate<T> | Pattern<T>): Pattern<T>;
|
|
10
|
+
followedByAny(): Pattern<T>;
|
|
11
|
+
oneOrMore(greedy?: boolean): Pattern<T>;
|
|
12
|
+
zeroOrMore(greedy?: boolean): Pattern<T>;
|
|
13
|
+
optional(greedy?: boolean): Pattern<T>;
|
|
14
|
+
times(n: number): Pattern<T>;
|
|
15
|
+
between(min: number, max: number, greedy?: boolean): Pattern<T>;
|
|
16
|
+
or(fnOrPattern: Predicate<T> | Pattern<T>): Pattern<T>;
|
|
17
|
+
atStart(): Pattern<T>;
|
|
18
|
+
atEnd(): Pattern<T>;
|
|
19
|
+
compile(): Matcher<T>;
|
|
20
|
+
toAST(): PatternNode<T>;
|
|
21
|
+
private resolveNode;
|
|
22
|
+
private getSequence;
|
|
23
|
+
private applyQuantifier;
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { NFA } from './nfa';
|
|
2
|
+
import { type MatchResult } from './engine';
|
|
3
|
+
export declare class Scanner<T> {
|
|
4
|
+
private nfa;
|
|
5
|
+
private greedy;
|
|
6
|
+
private position;
|
|
7
|
+
private scanStart;
|
|
8
|
+
private buffer;
|
|
9
|
+
private active;
|
|
10
|
+
private lastAcceptLength;
|
|
11
|
+
private simulationAlive;
|
|
12
|
+
constructor(nfa: NFA<T>, greedy: boolean);
|
|
13
|
+
push(element: T): MatchResult<T>[];
|
|
14
|
+
end(): MatchResult<T>[];
|
|
15
|
+
private initSimulation;
|
|
16
|
+
private step;
|
|
17
|
+
private drain;
|
|
18
|
+
private emitMatch;
|
|
19
|
+
}
|