mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
131 lines
16 KiB
JavaScript
131 lines
16 KiB
JavaScript
|
import { compareRangeCovs } from "./compare";
|
||
|
export function emitForest(trees) {
|
||
|
return emitForestLines(trees).join("\n");
|
||
|
}
|
||
|
export function emitForestLines(trees) {
|
||
|
const colMap = getColMap(trees);
|
||
|
const header = emitOffsets(colMap);
|
||
|
return [header, ...trees.map(tree => emitTree(tree, colMap).join("\n"))];
|
||
|
}
|
||
|
function getColMap(trees) {
|
||
|
const eventSet = new Set();
|
||
|
for (const tree of trees) {
|
||
|
const stack = [tree];
|
||
|
while (stack.length > 0) {
|
||
|
const cur = stack.pop();
|
||
|
eventSet.add(cur.start);
|
||
|
eventSet.add(cur.end);
|
||
|
for (const child of cur.children) {
|
||
|
stack.push(child);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
const events = [...eventSet];
|
||
|
events.sort((a, b) => a - b);
|
||
|
let maxDigits = 1;
|
||
|
for (const event of events) {
|
||
|
maxDigits = Math.max(maxDigits, event.toString(10).length);
|
||
|
}
|
||
|
const colWidth = maxDigits + 3;
|
||
|
const colMap = new Map();
|
||
|
for (const [i, event] of events.entries()) {
|
||
|
colMap.set(event, i * colWidth);
|
||
|
}
|
||
|
return colMap;
|
||
|
}
|
||
|
function emitTree(tree, colMap) {
|
||
|
const layers = [];
|
||
|
let nextLayer = [tree];
|
||
|
while (nextLayer.length > 0) {
|
||
|
const layer = nextLayer;
|
||
|
layers.push(layer);
|
||
|
nextLayer = [];
|
||
|
for (const node of layer) {
|
||
|
for (const child of node.children) {
|
||
|
nextLayer.push(child);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return layers.map(layer => emitTreeLayer(layer, colMap));
|
||
|
}
|
||
|
export function parseFunctionRanges(text, offsetMap) {
|
||
|
const result = [];
|
||
|
for (const line of text.split("\n")) {
|
||
|
for (const range of parseTreeLayer(line, offsetMap)) {
|
||
|
result.push(range);
|
||
|
}
|
||
|
}
|
||
|
result.sort(compareRangeCovs);
|
||
|
return result;
|
||
|
}
|
||
|
/**
|
||
|
*
|
||
|
* @param layer Sorted list of disjoint trees.
|
||
|
* @param colMap
|
||
|
*/
|
||
|
function emitTreeLayer(layer, colMap) {
|
||
|
const line = [];
|
||
|
let curIdx = 0;
|
||
|
for (const { start, end, count } of layer) {
|
||
|
const startIdx = colMap.get(start);
|
||
|
const endIdx = colMap.get(end);
|
||
|
if (startIdx > curIdx) {
|
||
|
line.push(" ".repeat(startIdx - curIdx));
|
||
|
}
|
||
|
line.push(emitRange(count, endIdx - startIdx));
|
||
|
curIdx = endIdx;
|
||
|
}
|
||
|
return line.join("");
|
||
|
}
|
||
|
function parseTreeLayer(text, offsetMap) {
|
||
|
const result = [];
|
||
|
const regex = /\[(\d+)-*\)/gs;
|
||
|
while (true) {
|
||
|
const match = regex.exec(text);
|
||
|
if (match === null) {
|
||
|
break;
|
||
|
}
|
||
|
const startIdx = match.index;
|
||
|
const endIdx = startIdx + match[0].length;
|
||
|
const count = parseInt(match[1], 10);
|
||
|
const startOffset = offsetMap.get(startIdx);
|
||
|
const endOffset = offsetMap.get(endIdx);
|
||
|
if (startOffset === undefined || endOffset === undefined) {
|
||
|
throw new Error(`Invalid offsets for: ${JSON.stringify(text)}`);
|
||
|
}
|
||
|
result.push({ startOffset, endOffset, count });
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
function emitRange(count, len) {
|
||
|
const rangeStart = `[${count.toString(10)}`;
|
||
|
const rangeEnd = ")";
|
||
|
const hyphensLen = len - (rangeStart.length + rangeEnd.length);
|
||
|
const hyphens = "-".repeat(Math.max(0, hyphensLen));
|
||
|
return `${rangeStart}${hyphens}${rangeEnd}`;
|
||
|
}
|
||
|
function emitOffsets(colMap) {
|
||
|
let line = "";
|
||
|
for (const [event, col] of colMap) {
|
||
|
if (line.length < col) {
|
||
|
line += " ".repeat(col - line.length);
|
||
|
}
|
||
|
line += event.toString(10);
|
||
|
}
|
||
|
return line;
|
||
|
}
|
||
|
export function parseOffsets(text) {
|
||
|
const result = new Map();
|
||
|
const regex = /\d+/gs;
|
||
|
while (true) {
|
||
|
const match = regex.exec(text);
|
||
|
if (match === null) {
|
||
|
break;
|
||
|
}
|
||
|
result.set(match.index, parseInt(match[0], 10));
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMvYXNjaWkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sV0FBVyxDQUFDO0FBVTdDLE1BQU0sVUFBVSxVQUFVLENBQUMsS0FBdUM7SUFDaEUsT0FBTyxlQUFlLENBQUMsS0FBSyxDQUFDLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDO0FBQzNDLENBQUM7QUFFRCxNQUFNLFVBQVUsZUFBZSxDQUFDLEtBQXVDO0lBQ3JFLE1BQU0sTUFBTSxHQUF3QixTQUFTLENBQUMsS0FBSyxDQUFDLENBQUM7SUFDckQsTUFBTSxNQUFNLEdBQVcsV0FBVyxDQUFDLE1BQU0sQ0FBQyxDQUFDO0lBQzNDLE9BQU8sQ0FBQyxNQUFNLEVBQUUsR0FBRyxLQUFLLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsUUFBUSxDQUFDLElBQUksRUFBRSxNQUFNLENBQUMsQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDO0FBQzNFLENBQUM7QUFFRCxTQUFTLFNBQVMsQ0FBQyxLQUFrQztJQUNuRCxNQUFNLFFBQVEsR0FBZ0IsSUFBSSxHQUFHLEVBQUUsQ0FBQztJQUN4QyxLQUFLLE1BQU0sSUFBSSxJQUFJLEtBQUssRUFBRTtRQUN4QixNQUFNLEtBQUssR0FBd0IsQ0FBQyxJQU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