Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 193 lines · 5.0 KiB · at 2e2b18f

// Position bookkeeping for the preprocessor.//// Every lowering pass writes its output through an emitter that records, for// each run of output text, where it came from in the input://// - copy(text, srcStart): the text is a verbatim slice of the input starting at//   srcStart, so every offset inside maps exactly.// - synth(text, anchor): generated glue (helper names, arrows, temporaries);//   every offset inside maps to the single input offset `anchor`.//// Passes chain (tsus -> match-lowered -> try-lowered -> with runtime import),// so an output offset is mapped back by composing each pass's lookup.export const createEmitter = () => {  let out = "";  const segments = [];  const push = (text, src, exact) => {    if (text.length === 0) {      return;    }    segments.push({ gen: out.length, len: text.length, src, exact });    out += text;  };  return {    copy: (text, srcStart) => push(text, srcStart, true),    synth: (text, anchor) => push(text, anchor, false),    // Splice another emitter's output in; its segments already point at the    // same input, so only their output offsets shift.    append: (other) => {      const base = out.length;      for (const s of other.segments) {        segments.push({ ...s, gen: s.gen + base });      }      out += other.text();    },    text: () => out,    segments,    length: () => out.length,  };};// Turns an emitter's segments into an offset lookup (output -> input).export const segmentLookup = (segments) => (offset) => {  if (segments.length === 0) {    return offset;  }  let lo = 0;  let hi = segments.length - 1;  while (lo < hi) {    const mid = (lo + hi + 1) >> 1;    if (segments[mid].gen <= offset) {      lo = mid;    } else {      hi = mid - 1;    }  }  const s = segments[lo];  if (offset < s.gen) {    return s.src;  }  if (!s.exact) {    return s.src;  }  return s.src + Math.min(offset - s.gen, s.len);};export const lineStarts = (text) => {  const starts = [0];  for (let i = 0; i < text.length; i += 1) {    const ch = text.charCodeAt(i);    if (ch === 10) {      starts.push(i + 1);    } else if (ch === 13) {      if (text.charCodeAt(i + 1) === 10) {        i += 1;      }      starts.push(i + 1);    }  }  return starts;};// Zero-based { line, character } for an offset.export const positionOf = (starts, offset) => {  let lo = 0;  let hi = starts.length - 1;  while (lo < hi) {    const mid = (lo + hi + 1) >> 1;    if (starts[mid] <= offset) {      lo = mid;    } else {      hi = mid - 1;    }  }  return { line: lo, character: offset - starts[lo] };};// --- Source Map v3 VLQ ---const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";const B64_INDEX = new Map([...B64].map((c, i) => [c, i]));const encodeVlq = (value) => {  let v = value < 0 ? (-value << 1) | 1 : value << 1;  let out = "";  do {    let digit = v & 31;    v >>>= 5;    if (v > 0) {      digit |= 32;    }    out += B64[digit];  } while (v > 0);  return out;};// Returns lines of segments: [genCol, srcIndex, srcLine, srcCol, nameIndex?]// with absolute (not delta) values.export const decodeMappings = (mappings) => {  const lines = [];  let srcIndex = 0;  let srcLine = 0;  let srcCol = 0;  let nameIndex = 0;  for (const lineText of mappings.split(";")) {    const segs = [];    let genCol = 0;    if (lineText !== "") {      for (const segText of lineText.split(",")) {        const values = [];        let shift = 0;        let acc = 0;        for (const ch of segText) {          const digit = B64_INDEX.get(ch);          acc += (digit & 31) << shift;          if (digit & 32) {            shift += 5;          } else {            values.push(acc & 1 ? -(acc >>> 1) : acc >>> 1);            acc = 0;            shift = 0;          }        }        genCol += values[0];        const seg = [genCol];        if (values.length >= 4) {          srcIndex += values[1];          srcLine += values[2];          srcCol += values[3];          seg.push(srcIndex, srcLine, srcCol);          if (values.length >= 5) {            nameIndex += values[4];            seg.push(nameIndex);          }        }        segs.push(seg);      }    }    lines.push(segs);  }  return lines;};export const encodeMappings = (lines) => {  let srcIndex = 0;  let srcLine = 0;  let srcCol = 0;  let nameIndex = 0;  return lines    .map((segs) => {      let genCol = 0;      return segs        .map((seg) => {          let out = encodeVlq(seg[0] - genCol);          genCol = seg[0];          if (seg.length >= 4) {            out += encodeVlq(seg[1] - srcIndex);            out += encodeVlq(seg[2] - srcLine);            out += encodeVlq(seg[3] - srcCol);            srcIndex = seg[1];            srcLine = seg[2];            srcCol = seg[3];            if (seg.length >= 5) {              out += encodeVlq(seg[4] - nameIndex);              nameIndex = seg[4];            }          }          return out;        })        .join(",");    })    .join(";");};