-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathpexprs-eval.js
More file actions
405 lines (358 loc) · 13 KB
/
Copy pathpexprs-eval.js
File metadata and controls
405 lines (358 loc) · 13 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
import {Trace} from './Trace.js';
import * as common from './common.js';
import * as errors from './errors.js';
import {IterationNode, NonterminalNode, TerminalNode} from './nodes.js';
import * as pexprs from './pexprs-main.js';
import {MAX_CODE_POINT} from './InputStream.js';
// --------------------------------------------------------------------
// Operations
// --------------------------------------------------------------------
/*
Evaluate the expression and return `true` if it succeeds, `false` otherwise. This method should
only be called directly by `State.prototype.eval(expr)`, which also updates the data structures
that are used for tracing. (Making those updates in a method of `State` enables the trace-specific
data structures to be "secrets" of that class, which is good for modularity.)
The contract of this method is as follows:
* When the return value is `true`,
- the state object will have `expr.getArity()` more bindings than it did before the call.
* When the return value is `false`,
- the state object may have more bindings than it did before the call, and
- its input stream's position may be anywhere.
Note that `State.prototype.eval(expr)`, unlike this method, guarantees that neither the state
object's bindings nor its input stream's position will change if the expression fails to match.
*/
pexprs.PExpr.prototype.eval = common.abstract('eval'); // function(state) { ... }
pexprs.any.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
const cp = inputStream.nextCodePoint();
if (cp !== undefined) {
state.pushBinding(new TerminalNode(String.fromCodePoint(cp).length), origPos);
return true;
} else {
state.processFailure(origPos, this);
return false;
}
};
pexprs.end.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
if (inputStream.atEnd()) {
state.pushBinding(new TerminalNode(0), origPos);
return true;
} else {
state.processFailure(origPos, this);
return false;
}
};
pexprs.Terminal.prototype.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
if (!inputStream.matchString(this.obj)) {
state.processFailure(origPos, this);
return false;
} else {
state.pushBinding(new TerminalNode(this.obj.length), origPos);
return true;
}
};
pexprs.Range.prototype.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
// A range can operate in one of two modes: matching a single, 16-bit _code unit_,
// or matching a _code point_. (Code points over 0xFFFF take up two 16-bit code units.)
const cp = this.matchCodePoint ? inputStream.nextCodePoint() : inputStream.nextCharCode();
// Always compare by code point value to get the correct result in all scenarios.
// Note that for strings of length 1, codePointAt(0) and charPointAt(0) are equivalent.
if (cp !== undefined && this.from.codePointAt(0) <= cp && cp <= this.to.codePointAt(0)) {
state.pushBinding(new TerminalNode(String.fromCodePoint(cp).length), origPos);
return true;
} else {
state.processFailure(origPos, this);
return false;
}
};
pexprs.Param.prototype.eval = function (state) {
return state.eval(state.currentApplication().args[this.index]);
};
pexprs.Lex.prototype.eval = function (state) {
state.enterLexifiedContext();
const ans = state.eval(this.expr);
state.exitLexifiedContext();
return ans;
};
pexprs.Alt.prototype.eval = function (state) {
for (let idx = 0; idx < this.terms.length; idx++) {
if (state.eval(this.terms[idx])) {
return true;
}
}
return false;
};
pexprs.Seq.prototype.eval = function (state) {
for (let idx = 0; idx < this.factors.length; idx++) {
const factor = this.factors[idx];
if (!state.eval(factor)) {
return false;
}
}
return true;
};
pexprs.Iter.prototype.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
const arity = this.getArity();
const cols = [];
const colOffsets = [];
while (cols.length < arity) {
cols.push([]);
colOffsets.push([]);
}
let numMatches = 0;
let prevPos = origPos;
let idx;
while (numMatches < this.maxNumMatches && state.eval(this.expr)) {
if (inputStream.pos === prevPos) {
throw errors.kleeneExprHasNullableOperand(this, state._applicationStack);
}
prevPos = inputStream.pos;
numMatches++;
const row = state._bindings.splice(state._bindings.length - arity, arity);
const rowOffsets = state._bindingOffsets.splice(
state._bindingOffsets.length - arity,
arity
);
for (idx = 0; idx < row.length; idx++) {
cols[idx].push(row[idx]);
colOffsets[idx].push(rowOffsets[idx]);
}
}
if (numMatches < this.minNumMatches) {
return false;
}
let offset = state.posToOffset(origPos);
let matchLength = 0;
if (numMatches > 0) {
const lastCol = cols[arity - 1];
const lastColOffsets = colOffsets[arity - 1];
const endOffset =
lastColOffsets[lastColOffsets.length - 1] + lastCol[lastCol.length - 1].matchLength;
offset = colOffsets[0][0];
matchLength = endOffset - offset;
}
const isOptional = this instanceof pexprs.Opt;
for (idx = 0; idx < cols.length; idx++) {
state._bindings.push(
new IterationNode(cols[idx], colOffsets[idx], matchLength, isOptional)
);
state._bindingOffsets.push(offset);
}
return true;
};
pexprs.Not.prototype.eval = function (state) {
/*
TODO:
- Right now we're just throwing away all of the failures that happen inside a `not`, and
recording `this` as a failed expression.
- Double negation should be equivalent to lookahead, but that's not the case right now wrt
failures. E.g., ~~'foo' produces a failure for ~~'foo', but maybe it should produce
a failure for 'foo' instead.
*/
const {inputStream} = state;
const origPos = inputStream.pos;
state.pushFailuresInfo();
const ans = state.eval(this.expr);
state.popFailuresInfo();
if (ans) {
state.processFailure(origPos, this);
return false;
}
inputStream.pos = origPos;
return true;
};
pexprs.Lookahead.prototype.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
if (state.eval(this.expr)) {
inputStream.pos = origPos;
return true;
} else {
return false;
}
};
pexprs.Apply.prototype.eval = function (state) {
const caller = state.currentApplication();
const actuals = caller ? caller.args : [];
const app = this.substituteParams(actuals);
const posInfo = state.getCurrentPosInfo();
if (posInfo.isActive(app)) {
// This rule is already active at this position, i.e., it is left-recursive.
return app.handleCycle(state);
}
const memoKey = app.toMemoKey();
const memoRec = posInfo.memo[memoKey];
if (memoRec && posInfo.shouldUseMemoizedResult(memoRec)) {
if (state.hasNecessaryInfo(memoRec)) {
return state.useMemoizedResult(state.inputStream.pos, memoRec);
}
posInfo.memo[memoKey] = undefined;
}
return app.reallyEval(state);
};
pexprs.Apply.prototype.handleCycle = function (state) {
const posInfo = state.getCurrentPosInfo();
const {currentLeftRecursion} = posInfo;
const memoKey = this.toMemoKey();
let memoRec = posInfo.memo[memoKey];
if (currentLeftRecursion && currentLeftRecursion.headApplication.toMemoKey() === memoKey) {
// We already know about this left recursion, but it's possible there are "involved
// applications" that we don't already know about, so...
memoRec.updateInvolvedApplicationMemoKeys();
} else if (!memoRec) {
// New left recursion detected! Memoize a failure to try to get a seed parse.
memoRec = posInfo.memoize(memoKey, {
matchLength: 0,
examinedLength: 0,
value: false,
rightmostFailureOffset: -1,
});
posInfo.startLeftRecursion(this, memoRec);
}
return state.useMemoizedResult(state.inputStream.pos, memoRec);
};
pexprs.Apply.prototype.reallyEval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
const origPosInfo = state.getCurrentPosInfo();
const ruleInfo = state.grammar.rules[this.ruleName];
const {body} = ruleInfo;
const {description} = ruleInfo;
state.enterApplication(origPosInfo, this);
if (description) {
state.pushFailuresInfo();
}
// Reset the input stream's examinedLength property so that we can track
// the examined length of this particular application.
const origInputStreamExaminedLength = inputStream.examinedLength;
inputStream.examinedLength = 0;
let value = this.evalOnce(body, state);
const currentLR = origPosInfo.currentLeftRecursion;
const memoKey = this.toMemoKey();
const isHeadOfLeftRecursion = currentLR && currentLR.headApplication.toMemoKey() === memoKey;
let memoRec;
if (state.doNotMemoize) {
state.doNotMemoize = false;
} else if (isHeadOfLeftRecursion) {
value = this.growSeedResult(body, state, origPos, currentLR, value);
origPosInfo.endLeftRecursion();
memoRec = currentLR;
memoRec.examinedLength = inputStream.examinedLength - origPos;
memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
origPosInfo.memoize(memoKey, memoRec); // updates origPosInfo's maxExaminedLength
} else if (!currentLR || !currentLR.isInvolved(memoKey)) {
// This application is not involved in left recursion, so it's ok to memoize it.
memoRec = origPosInfo.memoize(memoKey, {
matchLength: inputStream.pos - origPos,
examinedLength: inputStream.examinedLength - origPos,
value,
failuresAtRightmostPosition: state.cloneRecordedFailures(),
rightmostFailureOffset: state._getRightmostFailureOffset(),
});
}
const succeeded = !!value;
if (description) {
state.popFailuresInfo();
if (!succeeded) {
state.processFailure(origPos, this);
}
if (memoRec) {
memoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
}
}
// Record trace information in the memo table, so that it is available if the memoized result
// is used later.
if (state.isTracing() && memoRec) {
const entry = state.getTraceEntry(origPos, this, succeeded, succeeded ? [value] : []);
if (isHeadOfLeftRecursion) {
common.assert(entry.terminatingLREntry != null || !succeeded);
entry.isHeadOfLeftRecursion = true;
}
memoRec.traceEntry = entry;
}
// Fix the input stream's examinedLength -- it should be the maximum examined length
// across all applications, not just this one.
inputStream.examinedLength = Math.max(
inputStream.examinedLength,
origInputStreamExaminedLength
);
state.exitApplication(origPosInfo, value);
return succeeded;
};
pexprs.Apply.prototype.evalOnce = function (expr, state) {
const {inputStream} = state;
const origPos = inputStream.pos;
if (state.eval(expr)) {
const arity = expr.getArity();
const bindings = state._bindings.splice(state._bindings.length - arity, arity);
const offsets = state._bindingOffsets.splice(state._bindingOffsets.length - arity, arity);
const matchLength = inputStream.pos - origPos;
return new NonterminalNode(this.ruleName, bindings, offsets, matchLength);
} else {
return false;
}
};
pexprs.Apply.prototype.growSeedResult = function (body, state, origPos, lrMemoRec, newValue) {
if (!newValue) {
return false;
}
const {inputStream} = state;
while (true) {
lrMemoRec.matchLength = inputStream.pos - origPos;
lrMemoRec.value = newValue;
lrMemoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
if (state.isTracing()) {
// Before evaluating the body again, add a trace node for this application to the memo entry.
// Its only child is a copy of the trace node from `newValue`, which will always be the last
// element in `state.trace`.
const seedTrace = state.trace[state.trace.length - 1];
lrMemoRec.traceEntry = new Trace(
state.input,
origPos,
inputStream.pos,
this,
true,
[newValue],
[seedTrace.clone()]
);
}
inputStream.pos = origPos;
newValue = this.evalOnce(body, state);
if (inputStream.pos - origPos <= lrMemoRec.matchLength) {
break;
}
if (state.isTracing()) {
state.trace.splice(-2, 1); // Drop the trace for the old seed.
}
}
if (state.isTracing()) {
// The last entry is for an unused result -- pop it and save it in the "real" entry.
lrMemoRec.traceEntry.recordLRTermination(state.trace.pop(), newValue);
}
inputStream.pos = origPos + lrMemoRec.matchLength;
return lrMemoRec.value;
};
pexprs.UnicodeChar.prototype.eval = function (state) {
const {inputStream} = state;
const origPos = inputStream.pos;
const cp = inputStream.nextCodePoint();
if (cp !== undefined && cp <= MAX_CODE_POINT) {
const ch = String.fromCodePoint(cp);
if (this.pattern.test(ch)) {
state.pushBinding(new TerminalNode(ch.length), origPos);
return true;
}
}
state.processFailure(origPos, this);
return false;
};