opencode/packages/cli-rs/script/gen/entities.ts
Simon Klee 6b1df0cceb
feat(cli-rs): add the native CLI and TUI (#63477)
Add packages/cli-rs, an OpenCode client written in Rust on OpenTUI's Rust crates. It is a client only.
2026-11-22 00:07:01 +01:00

104 lines
4 KiB
TypeScript

// Generates strict HTML named references from the packed trie in `entities`, checking each name against its decoder.
import { createRequire } from "node:module"
const require = createRequire(new URL("../../../merman/package.json", import.meta.url).pathname)
const entities: {
htmlDecodeTree: Uint16Array
decodeHTMLStrict: (value: string) => string
} = require("entities/decode")
const tree = entities.htmlDecodeTree
// `BinTrieFlags` of the package.
const VALUE_LENGTH = 0b1100_0000_0000_0000
const FLAG13 = 0b0010_0000_0000_0000
const BRANCH_LENGTH = 0b0001_1111_1000_0000
const JUMP_TABLE = 0b0000_0000_0111_1111
const found = new Map<string, string>()
const walk = (start: number, prefix: string) => {
let index = start
let name = prefix
let current = tree[index]!
// A compact run (no value, flag 13): its characters, the first inline and the rest packed two per word.
while ((current & VALUE_LENGTH) === 0 && (current & FLAG13) !== 0) {
const length = (current & BRANCH_LENGTH) >> 7
name += String.fromCharCode(current & JUMP_TABLE)
for (let k = 0; k < length - 1; k++) {
const word = tree[index + 1 + (k >> 1)]!
name += String.fromCharCode(k % 2 === 0 ? word & 0xff : (word >> 8) & 0xff)
}
index += 1 + (length >> 1)
current = tree[index]!
}
const reference = `&${name};`
const value = entities.decodeHTMLStrict(reference)
if (value !== reference) found.set(name, value)
const valueLength = (current & VALUE_LENGTH) >> 14
// A one-word value is stored in the node's own branch bits: a leaf.
if (valueLength === 1) return
const node = index + Math.max(1, valueLength)
const count = (current & BRANCH_LENGTH) >> 7
const jump = current & JUMP_TABLE
const branches: [number, number][] =
count === 0
? jump === 0
? []
: [[jump, node]]
: jump !== 0
? Array.from({ length: count }, (_, k): [number, number] => [jump + k, tree[node + k]! - 1])
: Array.from({ length: count }, (_, k): [number, number] => [
(tree[node + (k >> 1)]! >> ((k & 1) * 8)) & 0xff,
tree[node + ((count + 1) >> 1) + k]!,
])
for (const [char, target] of branches) {
// `;` ends a name; the name itself was checked above.
if (target >= 0 && char !== 59) walk(target, name + String.fromCharCode(char))
}
}
walk(0, "")
const cases = [
"&amp;&lt;&gt;&quot;&apos;&nbsp;",
"&amp &lt no semicolon &AMP;",
"&notin; &not; &notinva; &nothing;",
"&#65;&#x42;&#X43;&#0067;",
"&#0; &#128; &#x9F; &#xD800; &#x110000; &#99999999999999999999;",
"&#; &#x; &#65 &#x41 &#xG;",
"&unknown; & ; &&amp; &amp;amp;",
"&NotNestedGreaterGreater; &bne; &fjlig;",
"trailing &amp",
"trailing &#65",
"trailing &",
"&#x1F600; &#128512;",
"a&b<c>d",
]
const literal = (value: string) =>
`"${[...value]
.map((char) => {
if (char === '"' || char === "\\") return `\\${char}`
if (char === "\n") return "\\n"
if (char === "\t") return "\\t"
// Controls, format characters and spaces other than U+0020 are invisible in source.
return char !== " " && /[\p{Cc}\p{Cf}\p{Z}\p{M}]/u.test(char) ? `\\u{${char.codePointAt(0)!.toString(16)}}` : char
})
.join("")}"`
const names = [...found.keys()].toSorted((a, b) => (a < b ? -1 : a > b ? 1 : 0))
const lines = [
"// Generated by script/gen/entities.ts from the `entities` package. Do not edit.",
"",
"/// `(name, value)` of every `&name;` that `decodeHTMLStrict` decodes, sorted by name.",
"pub const ENTITIES: &[(&str, &str)] = &[",
...names.map((name) => ` (${literal(name)}, ${literal(found.get(name)!)}),`),
"];",
"",
"/// `(input, decodeHTMLStrict(input))`.",
"#[cfg(test)]",
"pub const CASES: &[(&str, &str)] = &[",
...cases.map((input) => ` (${literal(input)}, ${literal(entities.decodeHTMLStrict(input))}),`),
"];",
"",
]
const file = new URL("../../crates/mermaid/src/core/entities.rs", import.meta.url).pathname
await Bun.write(file, lines.join("\n"))
console.log(`entities: ${names.length} names`)