Skip to content

Commit 1895b4e

Browse files
committed
fix: bug with errors from partial failures in described rules
1 parent 51339f8 commit 1895b4e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

packages/ohm-js/src/pexprs-eval.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ pexprs.Apply.prototype.reallyEval = function (state) {
303303
}
304304
if (memoRec) {
305305
memoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
306+
memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
306307
}
307308
}
308309

packages/ohm-js/test/test-errors.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,33 @@ test('wrongNumberOfArguments includes the interval', t => {
401401
{message}
402402
);
403403
});
404+
405+
// When a described rule partially matches, getRightmostFailures should return
406+
// the description failure. The bug was that memoRec.rightmostFailureOffset wasn't
407+
// updated after processFailure for described rules, causing hasNecessaryInfo to
408+
// incorrectly consider the memo valid.
409+
test('getRightmostFailures with described rule that partially matches', t => {
410+
const g = ohm.grammar(`G {
411+
foo (a foo) = "x" "y"
412+
}`);
413+
414+
// At position 0
415+
const r1 = g.match('x', 'foo');
416+
t.is(r1.getRightmostFailurePosition(), 0);
417+
t.deepEqual(
418+
r1.getRightmostFailures().map(f => f.toString()),
419+
['a foo']
420+
);
421+
422+
// At position 5 (via a wrapper rule)
423+
const g2 = ohm.grammar(`G {
424+
start = "abcde" foo
425+
foo (a foo) = "x" "y"
426+
}`);
427+
const r2 = g2.match('abcdex', 'start');
428+
t.is(r2.getRightmostFailurePosition(), 5);
429+
t.deepEqual(
430+
r2.getRightmostFailures().map(f => f.toString()),
431+
['a foo']
432+
);
433+
});

packages/ohm-js/test/test-ohm-syntax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test('unicode code point escapes', t => {
119119

120120
// More than 6 hex digits is just a parse error. (We'd like to make this nicer.)
121121
t.throws(() => ohm.grammar(String.raw`G { start = "\u{0000000} }`), {
122-
message: /Expected "\\"" or not "\\\\"/,
122+
message: /Expected "\\"", not "\\\\", or an escape sequence/,
123123
});
124124

125125
t.throws(() => ohm.grammar('G { start = "\\u{FFFFFF}" }'), {

0 commit comments

Comments
 (0)