parsec-js 0.3.0

JavaScript for Parsec: a parser for ECMAScript 2025 scripts and modules to a syntax tree with a span on every node, ESTree JSON out, and the lexer under it.
Documentation
"""Writes src/unicode.rs, the ID_Start and ID_Continue tables identifiers use.

    python3 scripts/gen-unicode.py

The data is DerivedCoreProperties.txt from the Unicode Character Database
(Unicode License v3) at the version below, the one crates/parsec's
\\p{...} tables and test262 use. ASCII is left out, since the lexer
decides it with a table of its own. Each table is sorted, merged code
point ranges, written flat as lo, hi.
"""

import subprocess
import urllib.request

VERSION = "17.0.0"
URL = f"https://www.unicode.org/Public/{VERSION}/ucd/DerivedCoreProperties.txt"

ranges = {"ID_Start": [], "ID_Continue": []}
for line in urllib.request.urlopen(URL).read().decode("utf-8").splitlines():
    line = line.split("#", 1)[0].strip()
    if not line:
        continue
    cps, prop = [f.strip() for f in line.split(";")[:2]]
    if prop in ranges:
        lo, _, hi = cps.partition("..")
        lo, hi = int(lo, 16), int(hi or lo, 16)
        if hi >= 0x80:
            ranges[prop].append([max(lo, 0x80), hi])


def merge(rs):
    out = []
    for lo, hi in sorted(rs):
        if out and lo <= out[-1][1] + 1:
            out[-1][1] = max(out[-1][1], hi)
        else:
            out.append([lo, hi])
    return out


out = [
    "// SPDX-License-Identifier: Apache-2.0\n",
    "// Copyright 2026 James Conway\n",
    "\n",
    f"// Generated by scripts/gen-unicode.py from the Unicode {VERSION} UCD. Don't edit.\n",
    "\n",
    "//! ID_Start and ID_Continue above ASCII, as sorted, merged ranges,\n",
    "//! flat: lo, hi, lo, hi.\n",
    "\n",
]
for name, const in [("ID_Start", "ID_START"), ("ID_Continue", "ID_CONTINUE")]:
    merged = merge(ranges[name])
    flat = ", ".join(f"{lo:#x}, {hi:#x}" for lo, hi in merged)
    out.append(f"pub(crate) static {const}: &[u32] = &[{flat}];\n\n")

with open("src/unicode.rs", "w") as f:
    f.write("".join(out).rstrip("\n") + "\n")
subprocess.run(["rustfmt", "--edition", "2024", "src/unicode.rs"], check=True)
print({k: len(merge(v)) for k, v in ranges.items()}, "ranges")