sailfork / t3code-github / apps/ mobile/ scripts/ generate-highlights.mts

#!/usr/bin/env node

// Hermes has no WebAssembly, so wasm2c supplies C for T3Highlights.
// Token and language IDs are an ABI; generate native code and JS tables from the same version.
// After bumping @pierre/highlights:
// WASM2C=/path/to/wabt-1.0.42/bin/wasm2c vp run --filter @t3tools/mobile generate:highlights

import * as NodeChildProcess from "node:child_process";
import * as NodeCrypto from "node:crypto";
import * as NodeFS from "node:fs";
import * as NodeModule from "node:module";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import * as NodeURL from "node:url";

const WASM2C_VERSION = "1.0.42";
const THEMES = { light: "pierre-light", dark: "pierre-dark" } as const;

const mobileRoot = NodePath.resolve(import.meta.dirname, "..");
const moduleRoot = NodePath.join(mobileRoot, "modules/t3-highlights");
const generatedDataPath = NodePath.join(mobileRoot, "src/lib/highlights.generated.ts");

interface ThemeStyle {
  readonly color?: string;
  readonly italic?: boolean;
  readonly weight?: number;
}

interface PreparedTheme {
  readonly fg: string;
  readonly styles: ReadonlyArray<ThemeStyle | null>;
}

function resolveHighlightsPackage() {
  const require = NodeModule.createRequire(NodePath.join(mobileRoot, "package.json"));
  const root = NodePath.dirname(require.resolve("@pierre/highlights/package.json"));
  const { version } = JSON.parse(NodeFS.readFileSync(NodePath.join(root, "package.json"), "utf8"));
  const wasm = NodeFS.readFileSync(NodePath.join(root, "dist/highlights.wasm"));
  return {
    root,
    version: version as string,
    wasmSha256: NodeCrypto.createHash("sha256").update(wasm).digest("hex"),
  };
}

function importDist<T>(root: string, file: string): Promise<T> {
  // The package exports neither its language table nor its theme resolver.
  return import(NodeURL.pathToFileURL(NodePath.join(root, "dist", file)).href);
}

function generatedBanner(source: ReturnType<typeof resolveHighlightsPackage>): string {
  return `Generated by apps/mobile/scripts/generate-highlights.mts from @pierre/highlights ${source.version} (highlights.wasm sha256 ${source.wasmSha256}). Do not edit.`;
}

export async function computeHighlightsData() {
  const source = resolveHighlightsPackage();
  const { default: languageIds } = await importDist<{ default: Record<string, number> }>(
    source.root,
    "languages.js",
  );
  const { prepareTheme } = await importDist<{ prepareTheme: (theme: unknown) => PreparedTheme }>(
    source.root,
    "theme.js",
  );

  const themeStyles: Record<string, Array<readonly [string, number]>> = {};
  for (const [scheme, name] of Object.entries(THEMES)) {
    const { default: theme } = await importDist<{ default: unknown }>(
      source.root,
      `themes/${name}.js`,
    );
    const { fg, styles } = prepareTheme(theme);
    // Mirrors `lineRecordsToTokens`: unstyled tokens take the theme foreground.
    themeStyles[scheme] = styles.map((style, tokenId) => {
      const color = style?.color ?? fg;
      if (!/^#[0-9a-f]{6}([0-9a-f]{2})?$/iu.test(color)) {
        throw new Error(`${name} token ${tokenId} has a color React Native cannot draw: ${color}`);
      }
      const fontStyle = (style?.italic === true ? 1 : 0) | ((style?.weight ?? 0) >= 600 ? 2 : 0);
      return [color, fontStyle] as const;
    });
  }

  return { source, languageIds, themeStyles };
}

function renderHighlightsData(data: Awaited<ReturnType<typeof computeHighlightsData>>): string {
  return [
    `// ${generatedBanner(data.source)}`,
    "",
    `export const HIGHLIGHTS_LANGUAGE_IDS: Readonly<Record<string, number>> = ${JSON.stringify(data.languageIds)};`,
    "",
    "/** `[color, fontStyle]` by token ID. fontStyle bits: italic 1, bold 2. */",
    `export const HIGHLIGHTS_THEME_STYLES: Readonly<Record<"light" | "dark", ReadonlyArray<readonly [string, number]>>> = ${JSON.stringify(data.themeStyles)};`,
    "",
  ].join("\n");
}

// The lexers ask the host whether a non-ASCII code point can start or continue
// an identifier. These are the classes the JavaScript glue tests with regexes.
function renderIdRanges(): string {
  const classes = [
    ["id_start", /^\p{ID_Start}$/u],
    ["id_continue", /^[‌‍\p{ID_Continue}]$/u],
  ] as const;
  let out = `/* Unicode ${process.versions.unicode} identifier classes. */\n`;
  for (const [name, pattern] of classes) {
    const ranges: Array<[number, number]> = [];
    let start = -1;
    for (let codePoint = 0; codePoint <= 0x110000; codePoint += 1) {
      const member =
        codePoint < 0x110000 &&
        (codePoint < 0xd800 || codePoint > 0xdfff) &&
        pattern.test(String.fromCodePoint(codePoint));
      if (member && start < 0) start = codePoint;
      if (!member && start >= 0) {
        ranges.push([start, codePoint - 1]);
        start = -1;
      }
    }
    out += `static const uint32_t ${name}_ranges[][2] = {\n`;
    for (let index = 0; index < ranges.length; index += 4) {
      const row = ranges.slice(index, index + 4);
      out += `  ${row.map(([from, to]) => `{0x${from.toString(16)}, 0x${to.toString(16)}}`).join(", ")},\n`;
    }
    out += "};\n";
  }
  return out;
}

function resolveWasm2c(): { readonly binary: string; readonly prefix: string } {
  const binary = process.env.WASM2C ?? "wasm2c";
  const version = NodeChildProcess.execFileSync(binary, ["--version"], { encoding: "utf8" }).trim();
  if (version !== WASM2C_VERSION) {
    throw new Error(`Expected wasm2c ${WASM2C_VERSION}, found ${version}. Set WASM2C.`);
  }
  const resolved = NodeFS.realpathSync(
    NodePath.isAbsolute(binary)
      ? binary
      : NodeChildProcess.execFileSync("which", [binary], { encoding: "utf8" }).trim(),
  );
  return { binary, prefix: NodePath.resolve(NodePath.dirname(resolved), "..") };
}

async function generate() {
  const source = resolveHighlightsPackage();
  const wasm2c = resolveWasm2c();
  const scratch = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-highlights-"));
  try {
    NodeChildProcess.execFileSync(wasm2c.binary, [
      NodePath.join(source.root, "dist/highlights.wasm"),
      "-n",
      "highlights",
      "-o",
      NodePath.join(scratch, "highlights.c"),
    ]);
    const banner = `/* ${generatedBanner(source)} */\n`;
    for (const file of ["highlights.c", "highlights.h"]) {
      const contents = NodeFS.readFileSync(NodePath.join(scratch, file), "utf8");
      NodeFS.writeFileSync(NodePath.join(moduleRoot, "Vendor/highlights", file), banner + contents);
    }
  } finally {
    NodeFS.rmSync(scratch, { recursive: true, force: true });
  }

  // The runtime must come from the same wabt release as the generated code.
  const runtime = [
    ["include/wasm-rt.h", "wasm-rt.h"],
    ["share/wabt/wasm2c/wasm-rt-impl.h", "wasm-rt-impl.h"],
    ["share/wabt/wasm2c/wasm-rt-impl.c", "wasm-rt-impl.c"],
    ["share/wabt/wasm2c/wasm-rt-impl-tableops.inc", "wasm-rt-impl-tableops.inc"],
    ["share/wabt/wasm2c/wasm-rt-mem-impl.c", "wasm-rt-mem-impl.c"],
    ["share/wabt/wasm2c/wasm-rt-mem-impl-helper.inc", "wasm-rt-mem-impl-helper.inc"],
  ] as const;
  for (const [from, to] of runtime) {
    NodeFS.copyFileSync(
      NodePath.join(wasm2c.prefix, from),
      NodePath.join(moduleRoot, "Vendor/wasm2c", to),
    );
  }

  NodeFS.writeFileSync(NodePath.join(moduleRoot, "cpp/generated/id_ranges.inc"), renderIdRanges());
  NodeFS.writeFileSync(generatedDataPath, renderHighlightsData(await computeHighlightsData()));
  NodeChildProcess.execFileSync("vp", ["fmt", generatedDataPath], { stdio: "inherit" });
}

if (import.meta.main) {
  await generate();
}