-
Notifications
You must be signed in to change notification settings - Fork 150
/
lut.js
126 lines (110 loc) · 3.02 KB
/
lut.js
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
// lut.js
// MIT licensed, see LICENSE file
// Copyright (c) 2013-2016 Olov Lassus <[email protected]>
"use strict";
const assert = require("assert");
const traverse = require("ordered-ast-traverse");
const is = require("simple-is");
module.exports = Lut;
function Lut(ast, src) {
assert(this instanceof Lut);
const sparseBegins = new Array(src.length);
const begins = [];
const sparseEnds = new Array(src.length);
const ends = [];
let p = 0;
const t0 = Date.now();
traverse(ast, {pre: function(node) {
// assert (node.range[0] >= p);
if (node.type === "Program") {
return;
}
p = node.range[0];
if (!sparseBegins[p]) {
sparseBegins[p] = node;
}
p = node.range[1];
if (!sparseEnds[p]) {
sparseEnds[p] = node;
}
}});
for (let i in sparseBegins) {
begins.push(sparseBegins[i]);
}
for (let i in sparseEnds) {
ends.push(sparseEnds[i]);
}
const t1 = Date.now();
// console.error(t1-t0)
// begins and ends are compact arrays with nodes,
// sorted on node.range[0/1] (unique)
this.begins = begins;
this.ends = ends;
}
Lut.prototype.findNodeFromPos = findNodeFromPos;
Lut.prototype.findNodeBeforePos = findNodeBeforePos;
// binary search lut to find node beginning at pos
// or as close after pos as possible. null if none
function findNodeFromPos(pos) {
const lut = this.begins;
assert(is.finitenumber(pos) && pos >= 0);
let left = 0;
let right = lut.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
assert(mid >= 0 && mid < lut.length);
if (pos > lut[mid].range[0]) {
left = mid + 1;
}
else {
right = mid;
}
}
if (left > right) {
assert(last(lut).range[0] < pos);
return null;
}
const found = left;
const foundPos = lut[found].range[0];
assert(foundPos >= pos);
if (found >= 1) {
const prevPos = lut[found - 1].range[0];
assert(prevPos < pos);
}
return lut[found];
}
// binary search lut to find node ending (as in range[1]
// at or before pos. null if none
function findNodeBeforePos(pos) {
const lut = this.ends;
assert(is.finitenumber(pos) && pos >= 0);
let left = 0;
let right = lut.length - 1;
while (left < right) {
const mid = Math.ceil((left + right) / 2);
assert(mid >= 0 && mid < lut.length);
if (pos < lut[mid].range[1]) {
right = mid - 1;
}
else {
left = mid;
}
}
if (left > right) {
assert(lut[0].range[1] > pos);
return null;
}
const found = left;
const foundPos = lut[found].range[1];
if(foundPos > pos) {
return null;
}
if (found <= lut.length - 2) {
const nextPos = lut[found + 1].range[1];
assert(nextPos > pos);
}
return lut[found];
}
function last(arr) {
return arr[arr.length - 1];
}