-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
325 lines (264 loc) · 7.92 KB
/
engine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* @macrome
* @generatedby @macrome/generator-typescript
* @generatedfrom ./engine.ts#1647109390480
* This file is autogenerated. Please do not edit it directly.
* When editing run `npx macrome watch` then change the file this is generated from.
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
(exports.Sequence = (exports.Expression = (exports.Engine = void 0)));
var _literals = require("./literals");
var _pattern = require("../pattern");
const cloneMatchState = (state) => {
const { result, captureStack, captureList, repetitionStates } = state;
return { result, captureStack, captureList, repetitionStates };
};
const noContext = {};
class Sequence {
constructor(state, matchState, expr) {
this.state = state;
this.matchState = matchState;
this.parentExpr = expr;
this.better = null;
this.worse = null;
}
maybeHoist() {
const { best, isRoot, parentSeq } = this.parentExpr;
if (best.worse === null && !isRoot) {
// if we have an expression of one sequence
parentSeq.state = best.state;
parentSeq.matchState = best.matchState;
if (best.state.type === 'expr') {
best.state.expr.parentSeq = parentSeq;
}
return parentSeq;
} else {
return this;
}
}
remove() {
const { worse, better, parentExpr } = this;
let seq = null;
let seqIsBetter = false;
if (better !== null) {
better.worse = worse;
seq = better;
seqIsBetter = true;
} else {
parentExpr.best = worse;
}
if (worse !== null) {
worse.better = better;
seq = worse;
seqIsBetter = false;
}
seq = seq.maybeHoist();
this.better = this.worse = null;
return seqIsBetter ? seq.next : seq;
}
removeWorse() {
if (this.worse !== null) this.worse.better = null;
this.worse = null;
return this.maybeHoist();
}
fail() {
if (this.state.type === 'expr') {
throw new Error('Expressions only fail when all their sequences fail');
}
const { worse, better, parentExpr } = this;
if (parentExpr.isRoot && better === null && worse === null) {
return parentExpr.terminate(null);
} else {
const seq = this.remove();
if (seq !== null && seq.state.type === 'success') {
return parentExpr.parentSeq.succeed(seq.state);
}
return seq;
}
}
succeed(successState) {
let seq = this;
let { parentExpr } = seq;
while (seq.better === null && parentExpr.parentSeq !== null && !parentExpr.isRoot) {
seq = parentExpr.parentSeq;
({ parentExpr } = seq);
}
seq.state = successState;
// stop matching against any less preferable alternate sequences
seq = seq.removeWorse();
// Fixup the reference which we bound too early
if (successState.expr !== null) {
successState.expr.parentSeq = seq;
}
if (parentExpr.isRoot && seq.better === null) {
return parentExpr.terminate(successState);
} else {
return seq;
}
}
replaceWith(result, context) {
const { engine, globalIdx } = this.parentExpr;
if (result.type === 'success') {
const { type, global, captures } = result;
const expr = global
? new Expression(engine, engine.makeRootState(context), globalIdx + 1, this)
: null;
return this.succeed({ type, expr, captures: [captures] });
} else if (result.type === 'expr') {
const expr = new Expression(engine, result, globalIdx, this);
this.state = { type: 'expr', expr };
return expr.best;
} else {
this.state = result;
return this;
}
}
get next() {
let seq = this;
while (seq !== null && seq.worse === null) seq = seq.parentExpr.parentSeq;
return seq === null ? null : seq.worse;
}
}
exports.Sequence = Sequence;
class Expression {
constructor(
engine,
result,
globalIdx,
parentSeq = null,
) {
this.engine = engine;
this.parentSeq = parentSeq;
this.globalIdx = globalIdx;
this.isRoot = parentSeq === null || parentSeq.parentExpr.globalIdx !== globalIdx;
const matchState = parentSeq === null ? engine.initialMatchState : parentSeq.matchState;
const best = new Sequence(null, matchState, this);
let prev = best;
for (const state of result.expr) {
const seq = new Sequence(state, cloneMatchState(matchState), this);
seq.better = prev;
prev.worse = seq;
prev = seq;
}
if (best.worse === null) {
throw new Error('Empty expressions are forbidden');
}
best.worse.better = null;
this.best = best.worse;
}
terminate(state) {
if (!this.isRoot) {
throw new Error('Can only terminate root expressions');
}
const seq = this.parentSeq;
if (seq !== null) {
if (seq.state.type !== 'success') {
throw new Error('root expressions must have success parents or no parents');
}
const seqState = seq.state;
if (state !== null) {
seqState.captures = [...seqState.captures, ...state.captures];
seqState.expr = state.expr;
return seqState.expr !== null ? seq : seq.next;
} else {
seqState.expr = null;
return seq.next;
}
} else {
const { engine } = this;
if (state !== null) {
engine.captures.push(...state.captures);
engine.root = state.expr;
if (state.expr !== null) {
state.expr.parentSeq = null;
return state.expr.best;
}
} else {
engine.root = null;
}
return null;
}
}
}
exports.Expression = Expression;
class Engine {
constructor(pattern) {
const { initialState, matcher } = (0, _pattern.getPatternInternal)(pattern);
this.initialMatchState = initialState;
this.repetitionCount = initialState.repetitionStates.length;
this.matcher = matcher;
this.captures = [];
this.root = null;
this.lastChr = null;
}
makeRootState(context) {
return this.matcher.match(cloneMatchState(this.initialMatchState), context);
}
step0(atStart, atEnd, idx, nextChr = null) {
const { lastChr } = this;
const seenRepetitions = new Array(this.repetitionCount);
const context = {
atStart,
atEnd,
lastChr,
lastCode: lastChr ? (0, _literals.code)(lastChr) : null,
nextChr,
nextCode: nextChr ? (0, _literals.code)(nextChr) : null,
idx,
seenRepetitions,
};
if (atStart) {
this.root = new Expression(this, this.makeRootState(context), 0);
}
let seq = this.root === null ? null : this.root.best;
while (seq !== null) {
const { state, matchState } = seq;
if (state.type !== 'cont') {
seq = state.expr !== null ? state.expr.best : seq.next;
} else {
const { next } = state;
if (next.width === 0) {
const result = next.match(matchState, context);
seq = result === null ? seq.fail() : seq.replaceWith(result, context);
} else if (atEnd) {
seq = seq.fail();
} else {
seq = seq.next;
}
}
this.lastChr = nextChr;
}
const { root, captures } = this;
const done = root === null;
if (captures.length > 0) {
this.captures = [];
return { value: captures, done };
} else {
return { value: null, done };
}
}
step1(chr) {
const { best } = this.root;
let seq = best;
while (seq !== null) {
const { state, matchState } = seq;
if (state.type !== 'cont') {
seq = (state.expr !== null ? state.expr.best : seq.next);
} else {
const { next } = state;
// width must be 1 here
// it should be as step0 should always be run first
const result = (next).match(matchState, chr, (0, _literals.code)(chr), noContext);
if (result === null) {
seq = seq.fail();
} else {
seq = seq.replaceWith(result, noContext);
seq = seq === null ? null : seq.next;
}
}
}
}
}
exports.Engine = Engine;