interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / site / verify-unicode.sh

73 lines · 3075 bytes Raw Blame History
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.
15set -eu
16check=0
17if [ "${1:-}" = "--check" ]; then check=1; shift; fi
18src=${1:-}
19if [ -z "$src" ]; then
20 src=$(ls -d "$HOME"/.cargo/registry/src/*/regex-syntax-0.8.* 2>/dev/null | sort -V | tail -n 1)
21fi
22tables="$src/src/unicode_tables"
23for f in perl_word perl_decimal perl_space; do
24 [ -f "$tables/$f.rs" ] || { echo "verify-unicode: missing $tables/$f.rs" >&2; exit 2; }
25done
26gen() {
27 python3 - "$tables" "$(basename "$src")" <<'PY'
28import ast, re, sys
29tables, version = sys.argv[1], sys.argv[2]
30def 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
38def 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)
45rows = []
46for 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))
53print("note GENERATED by om/site/verify-unicode.sh from " + version + " src/unicode_tables; do not edit.")
54for tag, name, uni, rs in rows:
55 print(f"note {tag}: {name}.rs, Unicode {uni}, {len(rs)} ranges as decimal lo hi pairs.")
56for tag, name, uni, rs in rows:
57 print(f"const {tag.upper()}N ulong {len(rs)}")
58for tag, name, uni, rs in rows:
59 print(f"global {tag}r ulong {2 * len(rs)}")
60for tag, name, uni, rs in rows:
61 print(f'text {tag} "' + " ".join(f"{lo} {hi}" for lo, hi in rs) + '"')
62PY
63}
64if [ "$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
72fi
73gen