Repair (?) error when searching for the very last position in a file, such as for a missing close parenthesis

This commit is contained in:
Tony Garnock-Jones 2021-12-09 22:11:11 +01:00
parent a37879695e
commit 50eaec69ef
1 changed files with 4 additions and 4 deletions

View File

@ -33,17 +33,17 @@ export class SpanIndex<T> {
let lo = 0;
let hi = this.index.length;
// console.log(`\nsearching for ${target}`);
// console.log(`\nsearching for ${pos}`);
while (true) {
if (lo === hi) {
if (lo === 0) return null;
const e = this.index[lo - 1];
if (e[0] > pos) throw new Error("INTERNAL ERROR: bad binary search (1)");
if (this.index[lo]?.[0] <= pos) throw new Error("INTERNAL ERROR: bad binary search (2)");
// console.log(`found ${JSON.stringify(e)}, ${JSON.stringify(items[lo] ?? null)}`);
// console.log(`found ${JSON.stringify(e)}, ${JSON.stringify(this.index[lo] ?? null)}`);
const r = new SpanResult<T>(pos, e[0]);
e[1].forEach(([end, item]) => {
if (pos < end) {
if (pos <= end) {
r.items.push({ end, item });
}
});
@ -53,7 +53,7 @@ export class SpanIndex<T> {
const mid = (lo + hi) >> 1;
const e = this.index[mid];
// console.log(`${target} lo ${lo} hi ${hi} mid ${mid} probe ${JSON.stringify([e[0], e[1].target])}`);
// console.log(`${pos} lo ${lo} hi ${hi} mid ${mid} probe ${JSON.stringify(e)}`);
if (e[0] <= pos) {
lo = mid + 1;