Skip to content

Commit

Permalink
Apply the patches from ohmjs#465 to fix indentation
Browse files Browse the repository at this point in the history
See ohmjs#465 (comment)

This changes memoization some so it can consume dedents
  • Loading branch information
ianb committed May 31, 2024
1 parent cb3decc commit 39c932d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
24 changes: 20 additions & 4 deletions packages/ohm-js/src/MatchState.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StringBuffer } from './common.js';
import {InputStream} from './InputStream.js';
import {MatchResult} from './MatchResult.js';
import {PosInfo} from './PosInfo.js';
Expand Down Expand Up @@ -234,10 +235,25 @@ export class MatchState {
const actuals = app ? app.args : [];
expr = expr.substituteParams(actuals);
}
return (
this.getMemoizedTraceEntry(pos, expr) ||
new Trace(this.input, pos, this.inputStream.pos, expr, succeeded, bindings, this.trace)
);
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() {
Expand Down
6 changes: 3 additions & 3 deletions packages/ohm-js/src/pexprs-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pexprs.Apply.prototype.handleCycle = function(state) {
const memoKey = this.toMemoKey();
let memoRec = posInfo.memo[memoKey];

if (currentLeftRecursion && currentLeftRecursion.headApplication.toMemoKey() === memoKey) {
if (memoRec && 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();
Expand Down Expand Up @@ -275,7 +275,7 @@ pexprs.Apply.prototype.reallyEval = function(state) {
let memoRec;

if (state.doNotMemoize) {
state.doNotMemoize = false;
// state.doNotMemoize = false;
} else if (isHeadOfLeftRecursion) {
value = this.growSeedResult(body, state, origPos, currentLR, value);
origPosInfo.endLeftRecursion();
Expand Down Expand Up @@ -307,7 +307,7 @@ pexprs.Apply.prototype.reallyEval = function(state) {

// Record trace information in the memo table, so that it is available if the memoized result
// is used later.
if (state.isTracing() && memoRec) {
if (state.isTracing() && memoRec && !state.doNotMemoize) {
const entry = state.getTraceEntry(origPos, this, succeeded, succeeded ? [value] : []);
if (isHeadOfLeftRecursion) {
common.assert(entry.terminatingLREntry != null || !succeeded);
Expand Down

0 comments on commit 39c932d

Please sign in to comment.