Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation fixes: dedent consumption from #465 and ignore blank lines #481

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion packages/ohm-js/src/findIndentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export function findIndentation(input) {

const indentPos = pos + indentSize;

if (indentSize > prevSize) {
if (!line.trim()) {
// Empty line, cannot be an indent or dedent.
} else if (indentSize > prevSize) {
// Indent -- always only 1.
stack.push(indentSize);
result[indentPos] = 1;
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