aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / site / verify-unicode.sh
| 1 | #!/bin/sh |
| 2 | # Verification-only host tool. Emits om/site/verify-unicode.om, the Unicode |
| 3 | # class tables the Omega verifier needs to reproduce the Rust oracle's regex |
| 4 | # semantics exactly: \w (and therefore \b), \d and \s of the regex crate are |
| 5 | # Unicode classes taken from regex-syntax's generated tables. This script |
| 6 | # copies those tables, it does not compute them; the oracle's own crate |
| 7 | # source is the authority. |
| 8 | # |
| 9 | # sh om/site/verify-unicode.sh [REGEX_SYNTAX_SRC] > om/site/verify-unicode.om |
| 10 | # sh om/site/verify-unicode.sh --check [REGEX_SYNTAX_SRC] # exit 1 on drift |
| 11 | # |
| 12 | # Default source: the newest regex-syntax 0.8.x in ~/.cargo/registry, which is |
| 13 | # what `regex = "1"` in crates/site-builder/Cargo.toml resolves to (no lock file |
| 14 | # is committed). The emitted header records the version and Unicode version. |
| 15 | set -eu |
| 16 | check=0 |
| 17 | if [ "${1:-}" = "--check" ]; then check=1; shift; fi |
| 18 | src=${1:-} |
| 19 | if [ -z "$src" ]; then |
| 20 | src=$(ls -d "$HOME"/.cargo/registry/src/*/regex-syntax-0.8.* 2>/dev/null | sort -V | tail -n 1) |
| 21 | fi |
| 22 | tables="$src/src/unicode_tables" |
| 23 | for f in perl_word perl_decimal perl_space; do |
| 24 | [ -f "$tables/$f.rs" ] || { echo "verify-unicode: missing $tables/$f.rs" >&2; exit 2; } |
| 25 | done |
| 26 | gen() { |
| 27 | python3 - "$tables" "$(basename "$src")" <<'PY' |
| 28 | import ast, re, sys |
| 29 | tables, version = sys.argv[1], sys.argv[2] |
| 30 | def ranges(name): |
| 31 | text = open(f"{tables}/{name}.rs", encoding="utf-8").read() |
| 32 | uni = re.search(r"Unicode version: ([0-9]+(?:\.[0-9]+)*)", text).group(1) |
| 33 | body = text[text.index("&[", text.index("pub const", text.index("BY_NAME") if "BY_NAME" in text else 0)):] |
| 34 | out = [] |
| 35 | for a, b in re.findall(r"\('((?:\\.|[^'\\])+)', '((?:\\.|[^'\\])+)'\)", body): |
| 36 | out.append((code(a), code(b))) |
| 37 | return uni, out |
| 38 | def code(lit): |
| 39 | m = re.fullmatch(r"\\u\{([0-9a-fA-F]+)\}", lit) |
| 40 | if m: |
| 41 | return int(m.group(1), 16) |
| 42 | value = ast.literal_eval("'" + lit + "'") |
| 43 | assert len(value) == 1, lit |
| 44 | return ord(value) |
| 45 | rows = [] |
| 46 | for name, tag in (("perl_word", "vuw"), ("perl_decimal", "vud"), ("perl_space", "vus")): |
| 47 | uni, rs = ranges(name) |
| 48 | for lo, hi in rs: |
| 49 | assert lo <= hi |
| 50 | for (a, b), (c, d) in zip(rs, rs[1:]): |
| 51 | assert b < c, (name, a, b, c, d) |
| 52 | rows.append((tag, name, uni, rs)) |
| 53 | print("note GENERATED by om/site/verify-unicode.sh from " + version + " src/unicode_tables; do not edit.") |
| 54 | for tag, name, uni, rs in rows: |
| 55 | print(f"note {tag}: {name}.rs, Unicode {uni}, {len(rs)} ranges as decimal lo hi pairs.") |
| 56 | for tag, name, uni, rs in rows: |
| 57 | print(f"const {tag.upper()}N ulong {len(rs)}") |
| 58 | for tag, name, uni, rs in rows: |
| 59 | print(f"global {tag}r ulong {2 * len(rs)}") |
| 60 | for tag, name, uni, rs in rows: |
| 61 | print(f'text {tag} "' + " ".join(f"{lo} {hi}" for lo, hi in rs) + '"') |
| 62 | PY |
| 63 | } |
| 64 | if [ "$check" -eq 1 ]; then |
| 65 | here=$(dirname "$0") |
| 66 | tmp=$(mktemp) |
| 67 | trap 'rm -f "$tmp"' EXIT |
| 68 | gen > "$tmp" |
| 69 | if cmp -s "$tmp" "$here/verify-unicode.om"; then echo "verify-unicode: tables match $src"; exit 0; fi |
| 70 | echo "verify-unicode: om/site/verify-unicode.om differs from $src" >&2 |
| 71 | exit 1 |
| 72 | fi |
| 73 | gen |