-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathMatchState.js
More file actions
417 lines (354 loc) · 12.1 KB
/
Copy pathMatchState.js
File metadata and controls
417 lines (354 loc) · 12.1 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { StringBuffer } from './common.js';
import {InputStream} from './InputStream.js';
import {MatchResult} from './MatchResult.js';
import {PosInfo} from './PosInfo.js';
import {Trace} from './Trace.js';
import * as pexprs from './pexprs.js';
import * as util from './util.js';
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
let builtInApplySyntacticBody;
util.awaitBuiltInRules(builtInRules => {
builtInApplySyntacticBody = builtInRules.rules.applySyntactic.body;
});
const applySpaces = new pexprs.Apply('spaces');
export class MatchState {
constructor(matcher, startExpr, optPositionToRecordFailures) {
this.matcher = matcher;
this.startExpr = startExpr;
this.grammar = matcher.grammar;
this.input = matcher.getInput();
this.inputStream = new InputStream(this.input);
this.memoTable = matcher._memoTable;
this.userData = undefined;
this.doNotMemoize = false;
this._bindings = [];
this._bindingOffsets = [];
this._applicationStack = [];
this._posStack = [0];
this.inLexifiedContextStack = [false];
this.rightmostFailurePosition = -1;
this._rightmostFailurePositionStack = [];
this._recordedFailuresStack = [];
if (optPositionToRecordFailures !== undefined) {
this.positionToRecordFailures = optPositionToRecordFailures;
this.recordedFailures = Object.create(null);
}
}
posToOffset(pos) {
return pos - this._posStack[this._posStack.length - 1];
}
enterApplication(posInfo, app) {
this._posStack.push(this.inputStream.pos);
this._applicationStack.push(app);
this.inLexifiedContextStack.push(false);
posInfo.enter(app);
this._rightmostFailurePositionStack.push(this.rightmostFailurePosition);
this.rightmostFailurePosition = -1;
}
exitApplication(posInfo, optNode) {
const origPos = this._posStack.pop();
this._applicationStack.pop();
this.inLexifiedContextStack.pop();
posInfo.exit();
this.rightmostFailurePosition = Math.max(
this.rightmostFailurePosition,
this._rightmostFailurePositionStack.pop(),
);
if (optNode) {
this.pushBinding(optNode, origPos);
}
}
enterLexifiedContext() {
this.inLexifiedContextStack.push(true);
}
exitLexifiedContext() {
this.inLexifiedContextStack.pop();
}
currentApplication() {
return this._applicationStack[this._applicationStack.length - 1];
}
inSyntacticContext() {
const currentApplication = this.currentApplication();
if (currentApplication) {
return currentApplication.isSyntactic() && !this.inLexifiedContext();
} else {
// The top-level context is syntactic if the start application is.
return this.startExpr.factors[0].isSyntactic();
}
}
inLexifiedContext() {
return this.inLexifiedContextStack[this.inLexifiedContextStack.length - 1];
}
skipSpaces() {
this.pushFailuresInfo();
this.eval(applySpaces);
this.popBinding();
this.popFailuresInfo();
return this.inputStream.pos;
}
skipSpacesIfInSyntacticContext() {
return this.inSyntacticContext() ? this.skipSpaces() : this.inputStream.pos;
}
maybeSkipSpacesBefore(expr) {
if (expr.allowsSkippingPrecedingSpace() && expr !== applySpaces) {
return this.skipSpacesIfInSyntacticContext();
} else {
return this.inputStream.pos;
}
}
pushBinding(node, origPos) {
this._bindings.push(node);
this._bindingOffsets.push(this.posToOffset(origPos));
}
popBinding() {
this._bindings.pop();
this._bindingOffsets.pop();
}
numBindings() {
return this._bindings.length;
}
truncateBindings(newLength) {
// Yes, this is this really faster than setting the `length` property (tested with
// bin/es5bench on Node v6.1.0).
// Update 2021-10-25: still true on v14.15.5 — it's ~20% speedup on es5bench.
while (this._bindings.length > newLength) {
this.popBinding();
}
}
getCurrentPosInfo() {
return this.getPosInfo(this.inputStream.pos);
}
getPosInfo(pos) {
let posInfo = this.memoTable[pos];
if (!posInfo) {
posInfo = this.memoTable[pos] = new PosInfo();
}
return posInfo;
}
processFailure(pos, expr) {
this.rightmostFailurePosition = Math.max(this.rightmostFailurePosition, pos);
if (this.recordedFailures && pos === this.positionToRecordFailures) {
const app = this.currentApplication();
if (app) {
// Substitute parameters with the actual pexprs that were passed to
// the current rule.
expr = expr.substituteParams(app.args);
} else {
// This branch is only reached for the "end-check" that is
// performed after the top-level application. In that case,
// expr === pexprs.end so there is no need to substitute
// parameters.
}
this.recordFailure(expr.toFailure(this.grammar), false);
}
}
recordFailure(failure, shouldCloneIfNew) {
const key = failure.toKey();
if (!this.recordedFailures[key]) {
this.recordedFailures[key] = shouldCloneIfNew ? failure.clone() : failure;
} else if (this.recordedFailures[key].isFluffy() && !failure.isFluffy()) {
this.recordedFailures[key].clearFluffy();
}
}
recordFailures(failures, shouldCloneIfNew) {
Object.keys(failures).forEach(key => {
this.recordFailure(failures[key], shouldCloneIfNew);
});
}
cloneRecordedFailures() {
if (!this.recordedFailures) {
return undefined;
}
const ans = Object.create(null);
Object.keys(this.recordedFailures).forEach(key => {
ans[key] = this.recordedFailures[key].clone();
});
return ans;
}
getRightmostFailurePosition() {
return this.rightmostFailurePosition;
}
_getRightmostFailureOffset() {
return this.rightmostFailurePosition >= 0 ?
this.posToOffset(this.rightmostFailurePosition) :
-1;
}
// Returns the memoized trace entry for `expr` at `pos`, if one exists, `null` otherwise.
getMemoizedTraceEntry(pos, expr) {
const posInfo = this.memoTable[pos];
if (posInfo && expr instanceof pexprs.Apply) {
const memoRec = posInfo.memo[expr.toMemoKey()];
if (memoRec && memoRec.traceEntry) {
const entry = memoRec.traceEntry.cloneWithExpr(expr);
entry.isMemoized = true;
return entry;
}
}
return null;
}
// Returns a new trace entry, with the currently active trace array as its children.
getTraceEntry(pos, expr, succeeded, bindings) {
if (expr instanceof pexprs.Apply) {
const app = this.currentApplication();
const actuals = app ? app.args : [];
expr = expr.substituteParams(actuals);
}
const memoTrace = this.getMemoizedTraceEntry(pos, expr)
let indentsLessThanPos = 0
let indentsLessThanInputPos = 0
if (!memoTrace) {
const input = new StringBuffer()
for (let i = 0; i < this.input.length; i++) {
if (this.inputStream._indentationAt(i) !== 0) {
for (let numPlaces = this.inputStream._indentationAt(i); numPlaces !== 0; numPlaces += (numPlaces < 0 ? 1 : -1)) {
input.append(numPlaces < 0 ? "\u21e6" : "\u21e8")
if (i < pos) indentsLessThanPos += 1;
if (i < this.inputStream.pos) indentsLessThanInputPos += 1;
}
}
input.append(this.inputStream.source[i])
}
return new Trace(input.contents(), pos + indentsLessThanPos, this.inputStream.pos + indentsLessThanInputPos, expr, succeeded, bindings, this.trace)
} else {
return memoTrace;
}
}
isTracing() {
return !!this.trace;
}
hasNecessaryInfo(memoRec) {
if (this.trace && !memoRec.traceEntry) {
return false;
}
if (
this.recordedFailures &&
this.inputStream.pos + memoRec.rightmostFailureOffset === this.positionToRecordFailures
) {
return !!memoRec.failuresAtRightmostPosition;
}
return true;
}
useMemoizedResult(origPos, memoRec) {
if (this.trace) {
this.trace.push(memoRec.traceEntry);
}
const memoRecRightmostFailurePosition =
this.inputStream.pos + memoRec.rightmostFailureOffset;
this.rightmostFailurePosition = Math.max(
this.rightmostFailurePosition,
memoRecRightmostFailurePosition,
);
if (
this.recordedFailures &&
this.positionToRecordFailures === memoRecRightmostFailurePosition &&
memoRec.failuresAtRightmostPosition
) {
this.recordFailures(memoRec.failuresAtRightmostPosition, true);
}
this.inputStream.examinedLength = Math.max(
this.inputStream.examinedLength,
memoRec.examinedLength + origPos,
);
if (memoRec.value) {
this.inputStream.pos += memoRec.matchLength;
this.pushBinding(memoRec.value, origPos);
return true;
}
return false;
}
// Evaluate `expr` and return `true` if it succeeded, `false` otherwise. On success, `bindings`
// will have `expr.getArity()` more elements than before, and the input stream's position may
// have increased. On failure, `bindings` and position will be unchanged.
eval(expr) {
const {inputStream} = this;
const origNumBindings = this._bindings.length;
const origUserData = this.userData;
let origRecordedFailures;
if (this.recordedFailures) {
origRecordedFailures = this.recordedFailures;
this.recordedFailures = Object.create(null);
}
const origPos = inputStream.pos;
const memoPos = this.maybeSkipSpacesBefore(expr);
let origTrace;
if (this.trace) {
origTrace = this.trace;
this.trace = [];
}
// Do the actual evaluation.
const ans = expr.eval(this);
if (this.trace) {
const bindings = this._bindings.slice(origNumBindings);
const traceEntry = this.getTraceEntry(memoPos, expr, ans, bindings);
traceEntry.isImplicitSpaces = expr === applySpaces;
traceEntry.isRootNode = expr === this.startExpr;
origTrace.push(traceEntry);
this.trace = origTrace;
}
if (ans) {
if (this.recordedFailures && inputStream.pos === this.positionToRecordFailures) {
Object.keys(this.recordedFailures).forEach(key => {
this.recordedFailures[key].makeFluffy();
});
}
} else {
// Reset the position, bindings, and userData.
inputStream.pos = origPos;
this.truncateBindings(origNumBindings);
this.userData = origUserData;
}
if (this.recordedFailures) {
this.recordFailures(origRecordedFailures, false);
}
// The built-in applySyntactic rule needs special handling: we want to skip
// trailing spaces, just as with the top-level application of a syntactic rule.
if (expr === builtInApplySyntacticBody) {
this.skipSpaces();
}
return ans;
}
getMatchResult() {
this.grammar._setUpMatchState(this);
this.eval(this.startExpr);
let rightmostFailures;
if (this.recordedFailures) {
rightmostFailures = Object.keys(this.recordedFailures).map(
key => this.recordedFailures[key],
);
}
const cst = this._bindings[0];
if (cst) {
cst.grammar = this.grammar;
}
return new MatchResult(
this.matcher,
this.input,
this.startExpr,
cst,
this._bindingOffsets[0],
this.rightmostFailurePosition,
rightmostFailures,
);
}
getTrace() {
this.trace = [];
const matchResult = this.getMatchResult();
// The trace node for the start rule is always the last entry. If it is a syntactic rule,
// the first entry is for an application of 'spaces'.
// TODO(pdubroy): Clean this up by introducing a special `Match<startAppl>` rule, which will
// ensure that there is always a single root trace node.
const rootTrace = this.trace[this.trace.length - 1];
rootTrace.result = matchResult;
return rootTrace;
}
pushFailuresInfo() {
this._rightmostFailurePositionStack.push(this.rightmostFailurePosition);
this._recordedFailuresStack.push(this.recordedFailures);
}
popFailuresInfo() {
this.rightmostFailurePosition = this._rightmostFailurePositionStack.pop();
this.recordedFailures = this._recordedFailuresStack.pop();
}
}