ascii.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { compareRangeCovs } from "./compare";
  2. export function emitForest(trees) {
  3. return emitForestLines(trees).join("\n");
  4. }
  5. export function emitForestLines(trees) {
  6. const colMap = getColMap(trees);
  7. const header = emitOffsets(colMap);
  8. return [header, ...trees.map(tree => emitTree(tree, colMap).join("\n"))];
  9. }
  10. function getColMap(trees) {
  11. const eventSet = new Set();
  12. for (const tree of trees) {
  13. const stack = [tree];
  14. while (stack.length > 0) {
  15. const cur = stack.pop();
  16. eventSet.add(cur.start);
  17. eventSet.add(cur.end);
  18. for (const child of cur.children) {
  19. stack.push(child);
  20. }
  21. }
  22. }
  23. const events = [...eventSet];
  24. events.sort((a, b) => a - b);
  25. let maxDigits = 1;
  26. for (const event of events) {
  27. maxDigits = Math.max(maxDigits, event.toString(10).length);
  28. }
  29. const colWidth = maxDigits + 3;
  30. const colMap = new Map();
  31. for (const [i, event] of events.entries()) {
  32. colMap.set(event, i * colWidth);
  33. }
  34. return colMap;
  35. }
  36. function emitTree(tree, colMap) {
  37. const layers = [];
  38. let nextLayer = [tree];
  39. while (nextLayer.length > 0) {
  40. const layer = nextLayer;
  41. layers.push(layer);
  42. nextLayer = [];
  43. for (const node of layer) {
  44. for (const child of node.children) {
  45. nextLayer.push(child);
  46. }
  47. }
  48. }
  49. return layers.map(layer => emitTreeLayer(layer, colMap));
  50. }
  51. export function parseFunctionRanges(text, offsetMap) {
  52. const result = [];
  53. for (const line of text.split("\n")) {
  54. for (const range of parseTreeLayer(line, offsetMap)) {
  55. result.push(range);
  56. }
  57. }
  58. result.sort(compareRangeCovs);
  59. return result;
  60. }
  61. /**
  62. *
  63. * @param layer Sorted list of disjoint trees.
  64. * @param colMap
  65. */
  66. function emitTreeLayer(layer, colMap) {
  67. const line = [];
  68. let curIdx = 0;
  69. for (const { start, end, count } of layer) {
  70. const startIdx = colMap.get(start);
  71. const endIdx = colMap.get(end);
  72. if (startIdx > curIdx) {
  73. line.push(" ".repeat(startIdx - curIdx));
  74. }
  75. line.push(emitRange(count, endIdx - startIdx));
  76. curIdx = endIdx;
  77. }
  78. return line.join("");
  79. }
  80. function parseTreeLayer(text, offsetMap) {
  81. const result = [];
  82. const regex = /\[(\d+)-*\)/gs;
  83. while (true) {
  84. const match = regex.exec(text);
  85. if (match === null) {
  86. break;
  87. }
  88. const startIdx = match.index;
  89. const endIdx = startIdx + match[0].length;
  90. const count = parseInt(match[1], 10);
  91. const startOffset = offsetMap.get(startIdx);
  92. const endOffset = offsetMap.get(endIdx);
  93. if (startOffset === undefined || endOffset === undefined) {
  94. throw new Error(`Invalid offsets for: ${JSON.stringify(text)}`);
  95. }
  96. result.push({ startOffset, endOffset, count });
  97. }
  98. return result;
  99. }
  100. function emitRange(count, len) {
  101. const rangeStart = `[${count.toString(10)}`;
  102. const rangeEnd = ")";
  103. const hyphensLen = len - (rangeStart.length + rangeEnd.length);
  104. const hyphens = "-".repeat(Math.max(0, hyphensLen));
  105. return `${rangeStart}${hyphens}${rangeEnd}`;
  106. }
  107. function emitOffsets(colMap) {
  108. let line = "";
  109. for (const [event, col] of colMap) {
  110. if (line.length < col) {
  111. line += " ".repeat(col - line.length);
  112. }
  113. line += event.toString(10);
  114. }
  115. return line;
  116. }
  117. export function parseOffsets(text) {
  118. const result = new Map();
  119. const regex = /\d+/gs;
  120. while (true) {
  121. const match = regex.exec(text);
  122. if (match === null) {
  123. break;
  124. }
  125. result.set(match.index, parseInt(match[0], 10));
  126. }
  127. return result;
  128. }
  129. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMvYXNjaWkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sV0FBVyxDQUFDO0FBVTdDLE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