lib/regex: fold-3 — anchors, escape, postfix ?/*/+, alternation
Ports compile()'s fold-3 arms (ref/hare/regex/regex.ha): \ escape
(286-293), ^ (294-300), $ (301-312, goes live with run_thread's
anchored ha:621-624 pin), | (335-367) over the restored jump_idxs
prologue (241-256 subset: jump_idxs + was_prev_rune_pipe +
group_level verbatim-but-0) + the whole-expression fixup (470-473,
the SIZE_MAX-sentinel overwrite 2a dropped), ? (403-420), * (421-443),
+ (444-459); find_last_groupstart (104-119, whole — error arm is the
live one until the group fold) and shift (123-133). run_thread's
inst_split/inst_jump aborts FLIP live (606-611, all 2b-proven
shapes). Still loud: [ ( ) { in compile; groupstart/groupend/repeat/
charset arms; add_thread's capture-dup bound.
Spelling divergences, each cited at site:
- multi-type case (inst_lit | inst_charset | inst_any) (ha:407) is
loud-rejected both stages (PE5; Hare-parity #13) -> three void arms.
- find_last_groupstart(...)? in the dead groupend arms hits the #38b
>32B-tagged-return propagate loud-stop -> explicit D13 match
(harec's own ? desugaring).
- by-value range over an indexed element (for (let x .. jump_idxs[g]))
SEGFAULTS both stages (NEW, filed ww-core #57) -> D9 index loops at
ha:351-356 + 470-473.
- assert(cond, msg) is wwstage-broken (CALL assert(SB), undefined at
link; NEW, filed ww-core #58) -> if+abort at the ha:355 site.
- Hare's match/if EXPRESSIONS (ha:337-346) -> statement spellings
(the #51 search/scanrune precedent).
Tests: the 11-pattern loud row shrinks to the 4 remaining metachars;
strerror reroutes through a[. New: fold3_programs (exact inst
sequences incl. the a|b sentinel fixup), fold3_compile_errors (12
exact texts incl. Hare's own ab\|^cd ERROR fixture), direct
find_last_groupstart/shift rows, fold3_find_cases (42 rows from
Hare's +test.ha anchors/postfix/alternation blocks, group-free
subset, end=-1 resolved to rune-length; rob's dedup/leftmost/longest
riders now observable: a* over aaaa -> single (0,4), b+ over abab ->
(1,2), b* / ^b* over aaaabbbb -> (4,8)/(0,0); multibyte b+ row keeps
idx != bytesize) + the a*-over-baa findall rider (longest-pick beats
the zero-length candidate; trailing zero-length match takes the
ha:942-945 break). Both drivers green; regex_test.combined.ww cs==ww
byte-identical.
This commit is contained in:
@@ -260,13 +260,12 @@ fn fail() void = { os.exit(signalled + 10); };
|
||||
// fold-boundary text — falling through to the literal default would
|
||||
// silently compile a wrong program, and any OTHER error text would
|
||||
// mean an arm was half-ported. One pattern per deferred arm so the
|
||||
// fold that ports an arm consciously deletes its row. '^' leads its
|
||||
// pattern: the r_idx==0 skip gate (regex.ha:261) must compose with
|
||||
// the loud arm, not bypass it.
|
||||
// fold that ports an arm consciously deletes its row. Fold 3 flipped
|
||||
// \ ^ $ | ? * + positive (fold3_* below); the bracket/group/
|
||||
// repetition four remain.
|
||||
@test fn compile_metachar_loud() void = {
|
||||
let pats: [11]str = [
|
||||
"a\\", "^a", "a$", "a[", "a(", "a)",
|
||||
"a|", "a{", "a?", "a*", "a+",
|
||||
let pats: [4]str = [
|
||||
"a[", "a(", "a)", "a{",
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(pats)) {
|
||||
@@ -420,7 +419,7 @@ type nmexp = struct {
|
||||
// through a REAL compile() error, completing the exported error
|
||||
// surface end to end.
|
||||
@test fn strerror_identity() void = {
|
||||
match (regex.compile("a*")) {
|
||||
match (regex.compile("a[")) {
|
||||
case let e: regex.error => {
|
||||
if (strings.compare(regex.strerror(e),
|
||||
"regex: metacharacter not yet ported") != 0) {
|
||||
@@ -981,7 +980,7 @@ type fdexp = struct {
|
||||
};
|
||||
|
||||
@test fn findall_fields() void = {
|
||||
let exp: [10]fdexp = [
|
||||
let exp: [12]fdexp = [
|
||||
// ("ab", "abxab")
|
||||
fdexp { start = 0, sb = 0, end = 2, eb = 2, content = "ab" },
|
||||
fdexp { start = 3, sb = 3, end = 5, eb = 5, content = "ab" },
|
||||
@@ -1002,8 +1001,16 @@ type fdexp = struct {
|
||||
// ("ab", "xab") — tail match: the post-match seek lands at
|
||||
// end-of-string and the next search returns void
|
||||
fdexp { start = 1, sb = 1, end = 3, eb = 3, content = "ab" },
|
||||
// ("a*", "baa") — fold-3 rider: search's longest-pick beats
|
||||
// the zero-length candidate at 0 (the b*-over-"aaaabbbb"
|
||||
// semantics), so the greedy (1,3) "aa" leads; the trailing
|
||||
// end-of-string zero-length match takes the ha:942-945
|
||||
// break, pinning a real splitting pattern through the 2c
|
||||
// zero-length machinery
|
||||
fdexp { start = 1, sb = 1, end = 3, eb = 3, content = "aa" },
|
||||
fdexp { start = 3, sb = 3, end = 3, eb = 3, content = "" },
|
||||
];
|
||||
let rows: [7]fdcase = [
|
||||
let rows: [8]fdcase = [
|
||||
fdcase { expr = "ab", input = "abxab", eoff = 0, ecnt = 2 },
|
||||
fdcase { expr = "ab", input = "abab", eoff = 2, ecnt = 2 },
|
||||
fdcase { expr = "aa", input = "aaa", eoff = 4, ecnt = 1 },
|
||||
@@ -1012,6 +1019,7 @@ type fdexp = struct {
|
||||
fdcase { expr = "ab", input = "xab", eoff = 9, ecnt = 1 },
|
||||
// no match → empty slice the caller still result_freealls
|
||||
fdcase { expr = "ab", input = "xyz", eoff = 10, ecnt = 0 },
|
||||
fdcase { expr = "a*", input = "baa", eoff = 10, ecnt = 2 },
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(rows)) {
|
||||
@@ -1051,6 +1059,298 @@ type fdexp = struct {
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
// ---- fold 3: anchors / escape / postfix / alternation ----------------
|
||||
|
||||
// instsig — flatten an inst for the table-driven program pins below:
|
||||
// kind base + payload. Takes the 56B inst by value (the #19-landed
|
||||
// is_consuming_inst shape).
|
||||
fn instsig(v: regex.inst) i64 = {
|
||||
match (v) {
|
||||
case let l: regex.inst_lit => return 1000 + ((l: rune): i64);
|
||||
case regex.inst_skip => return 2000;
|
||||
case regex.inst_any => return 3000;
|
||||
case let s: regex.inst_split => return 4000 + ((s: size): i64);
|
||||
case let j: regex.inst_jump => return 5000 + ((j: size): i64);
|
||||
case let m: regex.inst_match => {
|
||||
if ((m: bool)) { return 6001; };
|
||||
return 6000;
|
||||
};
|
||||
case let g: regex.inst_groupstart => return 7000 + ((g: size): i64);
|
||||
case regex.inst_groupend => return 8000;
|
||||
case => return 9999;
|
||||
};
|
||||
};
|
||||
|
||||
// Emitted-program pins for the fold-3 arms — deterministic, engine-
|
||||
// independent: the exact inst sequence (kinds + jump/split targets)
|
||||
// each metachar must compile to. Derived by hand-executing
|
||||
// regex.ha:286-473 (insert-before + shift + the SIZE_MAX-sentinel
|
||||
// jump fixup); the a|b row pins the whole jump_idxs pipeline
|
||||
// including the sentinel overwrite at ha:470-473.
|
||||
type pgmcase = struct {
|
||||
expr: str,
|
||||
soff: i32,
|
||||
scnt: i32,
|
||||
};
|
||||
|
||||
@test fn fold3_programs() void = {
|
||||
let sigs: [25]i64 = [
|
||||
// "^a": anchored — no leading skip
|
||||
1097, 6000,
|
||||
// "a$": skip, lit a, match(TRUE)
|
||||
2000, 1097, 6001,
|
||||
// "a?": split jumps OVER the lit to the match
|
||||
2000, 4003, 1097, 6000,
|
||||
// "a*": split to match; jump back to the split
|
||||
2000, 4004, 1097, 5001, 6000,
|
||||
// "a+": split back to the lit
|
||||
2000, 1097, 4001, 6000,
|
||||
// "a|b": leading split to the second branch's skip; the
|
||||
// first branch's jump lands on the epilogue match (the
|
||||
// fixed-up SIZE_MAX sentinel)
|
||||
4004, 2000, 1097, 5006, 2000, 1098, 6000,
|
||||
];
|
||||
let rows: [6]pgmcase = [
|
||||
pgmcase { expr = "^a", soff = 0, scnt = 2 },
|
||||
pgmcase { expr = "a$", soff = 2, scnt = 3 },
|
||||
pgmcase { expr = "a?", soff = 5, scnt = 4 },
|
||||
pgmcase { expr = "a*", soff = 9, scnt = 5 },
|
||||
pgmcase { expr = "a+", soff = 14, scnt = 4 },
|
||||
pgmcase { expr = "a|b", soff = 18, scnt = 7 },
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(rows)) {
|
||||
let ex: str = rows[i].expr;
|
||||
let c: (regex.regex | regex.error | nomem) = regex.compile(ex);
|
||||
match (c) {
|
||||
case let re: regex.regex => {
|
||||
if (re.insts.len != rows[i].scnt) { fail(); };
|
||||
let k: i32 = 0;
|
||||
for (k < rows[i].scnt) {
|
||||
if (instsig(re.insts[k])
|
||||
!= sigs[rows[i].soff + k]) {
|
||||
fail();
|
||||
};
|
||||
k += 1;
|
||||
};
|
||||
regex.finish(&re);
|
||||
};
|
||||
case => fail();
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
// The fold-3 compile-error surface, exact texts (regex.ha:289 / 296-299
|
||||
// / 303-308 / 405-417 / 423-435 / 446-455). The "ab\|^cd" row is
|
||||
// Hare's own ERROR fixture (+test.ha:634) — the escaped '|' must NOT
|
||||
// set was_prev_rune_pipe, so the following '^' misplaces.
|
||||
type cerow = struct {
|
||||
pat: str,
|
||||
want: str,
|
||||
};
|
||||
|
||||
@test fn fold3_compile_errors() void = {
|
||||
let rows: [12]cerow = [
|
||||
cerow { pat = "\\", want = "Trailing backslash '\\'" },
|
||||
cerow { pat = "a\\", want = "Trailing backslash '\\'" },
|
||||
cerow { pat = "a^",
|
||||
want = "Anchor '^' not at start of whole pattern or alternation" },
|
||||
cerow { pat = "$a",
|
||||
want = "Anchor '$' not at end of whole pattern or alternation" },
|
||||
cerow { pat = "ab\\|^cd",
|
||||
want = "Anchor '^' not at start of whole pattern or alternation" },
|
||||
cerow { pat = "?", want = "Unused '?'" },
|
||||
cerow { pat = "*", want = "Unused '*'" },
|
||||
cerow { pat = "+", want = "Unused '+'" },
|
||||
// '^' appends nothing, so insts is still empty (ha:404's
|
||||
// len check, not the r_idx one)
|
||||
cerow { pat = "^*", want = "Unused '*'" },
|
||||
cerow { pat = "a*?", want = "Misused '?'" },
|
||||
cerow { pat = "a**", want = "Misused '*'" },
|
||||
cerow { pat = "a*+", want = "Misused '+'" },
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(rows)) {
|
||||
let p: str = rows[i].pat;
|
||||
match (regex.compile(p)) {
|
||||
case let e: regex.error => {
|
||||
let w: str = rows[i].want;
|
||||
if (strings.compare((e: str), w) != 0) { fail(); };
|
||||
};
|
||||
case => fail();
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
// find_last_groupstart (regex.ha:104-119) — driven directly (private
|
||||
// fn): no inst_groupstart exists in any fold-3 program, so the error
|
||||
// arm is the live one; pin its exact text. A hand-built groupstart
|
||||
// row pins the success arm the group fold will rely on.
|
||||
@test fn find_last_groupstart_cases() void = {
|
||||
let insts: []regex.inst = [];
|
||||
append(insts, ('a': regex.inst_lit));
|
||||
match (find_last_groupstart(insts)) {
|
||||
case let e: regex.error => {
|
||||
if (strings.compare((e: str), "Unmatched ')'") != 0) {
|
||||
fail();
|
||||
};
|
||||
};
|
||||
case => fail();
|
||||
};
|
||||
append(insts, ((1: size): regex.inst_groupstart));
|
||||
append(insts, ('b': regex.inst_lit));
|
||||
match (find_last_groupstart(insts)) {
|
||||
case let sz: size => { if (sz != 1) { fail(); }; };
|
||||
case => fail();
|
||||
};
|
||||
};
|
||||
|
||||
// shift (regex.ha:123-133) — driven directly over a sub-slice view:
|
||||
// jump/split payloads in the view bump by one, the element before the
|
||||
// view and non-jump kinds are untouched (the PE3/PE4 shapes).
|
||||
@test fn shift_direct() void = {
|
||||
let insts: []regex.inst = [];
|
||||
append(insts, ((3: size): regex.inst_jump));
|
||||
append(insts, ('a': regex.inst_lit));
|
||||
append(insts, ((5: size): regex.inst_split));
|
||||
append(insts, ((7: size): regex.inst_jump));
|
||||
shift(insts[1:]);
|
||||
if (instsig(insts[0]) != 5003) { fail(); };
|
||||
if (instsig(insts[1]) != 1097) { fail(); };
|
||||
if (instsig(insts[2]) != 4006) { fail(); };
|
||||
if (instsig(insts[3]) != 5008) { fail(); };
|
||||
};
|
||||
|
||||
// fold-3 find/test rows — the group-free subset of Hare's own table
|
||||
// (+test.ha:221-256 anchors/postfix, :622-650 whole-expression and
|
||||
// multiple alternation; end == -1 resolved to rune-length per
|
||||
// +test.ha:693-697) plus rob's dedup/leftmost/longest riders:
|
||||
// `a*` over "aaaa" must yield ONE (0,4) (split spawns same-pc threads
|
||||
// every step — the ha:872-889 dedup pin gone observable), `b+` over
|
||||
// "abab" pins the leftmost trim (1,2 not 3,4), `b*`/`^b*` over
|
||||
// "aaaabbbb" pin longest-pick vs anchored zero-length. The multibyte
|
||||
// `b+` row keeps every idx != bytesize (B4). Reuses the fcase shape;
|
||||
// every row also cross-pins test() == (find() matched).
|
||||
@test fn fold3_find_cases() void = {
|
||||
let rows: [42]fcase = [
|
||||
fcase { expr = "^abc$", input = "abc", matches = true,
|
||||
start = 0, sb = 0, end = 3, eb = 3, content = "abc" },
|
||||
fcase { expr = "^abc$", input = "axc", matches = false, ... },
|
||||
fcase { expr = "^.$", input = "x", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "x" },
|
||||
fcase { expr = "^.$", input = "", matches = false, ... },
|
||||
fcase { expr = "^a+$", input = "a", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "a" },
|
||||
fcase { expr = "^a+$", input = "aaa", matches = true,
|
||||
start = 0, sb = 0, end = 3, eb = 3, content = "aaa" },
|
||||
fcase { expr = "^a+$", input = "", matches = false, ... },
|
||||
fcase { expr = "^a*$", input = "", matches = true,
|
||||
start = 0, sb = 0, end = 0, eb = 0, content = "" },
|
||||
fcase { expr = "^a*$", input = "aaaa", matches = true,
|
||||
start = 0, sb = 0, end = 4, eb = 4, content = "aaaa" },
|
||||
fcase { expr = "^a*$", input = "b", matches = false, ... },
|
||||
fcase { expr = "^a?$", input = "", matches = true,
|
||||
start = 0, sb = 0, end = 0, eb = 0, content = "" },
|
||||
fcase { expr = "^a?$", input = "a", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "a" },
|
||||
fcase { expr = "^a?$", input = "b", matches = false, ... },
|
||||
fcase { expr = "^a*", input = "aaaa", matches = true,
|
||||
start = 0, sb = 0, end = 4, eb = 4, content = "aaaa" },
|
||||
fcase { expr = "a*$", input = "aaaa", matches = true,
|
||||
start = 0, sb = 0, end = 4, eb = 4, content = "aaaa" },
|
||||
fcase { expr = "a*", input = "aaaa", matches = true,
|
||||
start = 0, sb = 0, end = 4, eb = 4, content = "aaaa" },
|
||||
fcase { expr = "b*", input = "aaaabbbb", matches = true,
|
||||
start = 4, sb = 4, end = 8, eb = 8, content = "bbbb" },
|
||||
fcase { expr = "^b*", input = "aaaabbbb", matches = true,
|
||||
start = 0, sb = 0, end = 0, eb = 0, content = "" },
|
||||
fcase { expr = "b*$", input = "aaaabbbb", matches = true,
|
||||
start = 4, sb = 4, end = 8, eb = 8, content = "bbbb" },
|
||||
fcase { expr = "b+", input = "abab", matches = true,
|
||||
start = 1, sb = 1, end = 2, eb = 2, content = "b" },
|
||||
// multibyte rider: 2-byte ß before the b's splits every
|
||||
// idx from its bytesize
|
||||
fcase { expr = "b+", input = "aßbb", matches = true,
|
||||
start = 2, sb = 3, end = 4, eb = 5, content = "bb" },
|
||||
fcase { expr = "ab|cd", input = "cd", matches = true,
|
||||
start = 0, sb = 0, end = 2, eb = 2, content = "cd" },
|
||||
fcase { expr = "ab|cd", input = "abc", matches = true,
|
||||
start = 0, sb = 0, end = 2, eb = 2, content = "ab" },
|
||||
fcase { expr = "ab|cd", input = "abcd", matches = true,
|
||||
start = 0, sb = 0, end = 2, eb = 2, content = "ab" },
|
||||
fcase { expr = "ab|cd", input = "bcd", matches = true,
|
||||
start = 1, sb = 1, end = 3, eb = 3, content = "cd" },
|
||||
fcase { expr = "^ab|cd", input = "bcd", matches = true,
|
||||
start = 1, sb = 1, end = 3, eb = 3, content = "cd" },
|
||||
fcase { expr = "^ab|cd", input = "zab", matches = false, ... },
|
||||
fcase { expr = "ab$|cd", input = "ab", matches = true,
|
||||
start = 0, sb = 0, end = 2, eb = 2, content = "ab" },
|
||||
fcase { expr = "ab$|cd", input = "abc", matches = false, ... },
|
||||
fcase { expr = "ab|cd$", input = "cde", matches = false, ... },
|
||||
fcase { expr = "ab|^cd", input = "bcd", matches = false, ... },
|
||||
fcase { expr = "ab|^cd", input = "cde", matches = true,
|
||||
start = 0, sb = 0, end = 2, eb = 2, content = "cd" },
|
||||
fcase { expr = "a|b|c|d|e", input = "e", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "e" },
|
||||
fcase { expr = "a|b|c|d|e", input = "xe", matches = true,
|
||||
start = 1, sb = 1, end = 2, eb = 2, content = "e" },
|
||||
fcase { expr = "a|b$|c$|d$|e", input = "cd", matches = true,
|
||||
start = 1, sb = 1, end = 2, eb = 2, content = "d" },
|
||||
fcase { expr = "a|b$|c$|d$|e", input = "ax", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "a" },
|
||||
fcase { expr = "a|b$|c$|d$|e", input = "cx", matches = false, ... },
|
||||
fcase { expr = "a|b$|c$|d$|e", input = "ex", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "e" },
|
||||
fcase { expr = "a|^b|^c|^d|e", input = "cd", matches = true,
|
||||
start = 0, sb = 0, end = 1, eb = 1, content = "c" },
|
||||
fcase { expr = "a|^b|^c|^d|e", input = "xa", matches = true,
|
||||
start = 1, sb = 1, end = 2, eb = 2, content = "a" },
|
||||
fcase { expr = "a|^b|^c|^d|e", input = "xc", matches = false, ... },
|
||||
fcase { expr = "a|^b|^c|^d|e", input = "xe", matches = true,
|
||||
start = 1, sb = 1, end = 2, eb = 2, content = "e" },
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(rows)) {
|
||||
let ex: str = rows[i].expr;
|
||||
let inp: str = rows[i].input;
|
||||
let c: (regex.regex | regex.error | nomem) = regex.compile(ex);
|
||||
match (c) {
|
||||
case let re: regex.regex => {
|
||||
let fr: (regex.result | nomem) = regex.find(&re, inp);
|
||||
if (!(fr is regex.result)) { fail(); };
|
||||
let res: regex.result = fr as regex.result;
|
||||
if (rows[i].matches) {
|
||||
if (len(res) != 1) { fail(); };
|
||||
if (res[0].start != rows[i].start) { fail(); };
|
||||
if (res[0].start_bytesize != rows[i].sb) {
|
||||
fail();
|
||||
};
|
||||
if (res[0].end != rows[i].end) { fail(); };
|
||||
if (res[0].end_bytesize != rows[i].eb) {
|
||||
fail();
|
||||
};
|
||||
let wc: str = rows[i].content;
|
||||
if (strings.compare(res[0].content, wc) != 0) {
|
||||
fail();
|
||||
};
|
||||
} else {
|
||||
if (len(res) != 0) { fail(); };
|
||||
};
|
||||
let tr: (bool | nomem) = regex.test(&re, inp);
|
||||
if (!(tr is bool)) { fail(); };
|
||||
if ((tr as bool) != (len(res) != 0)) { fail(); };
|
||||
regex.result_free(res);
|
||||
regex.finish(&re);
|
||||
};
|
||||
case => fail();
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
export fn main() i32 = {
|
||||
signalled = 1; lit_and_match();
|
||||
signalled = 2; size_aliases_distinct();
|
||||
@@ -1078,5 +1378,10 @@ export fn main() i32 = {
|
||||
signalled = 24; find_cases();
|
||||
signalled = 25; findall_content();
|
||||
signalled = 26; findall_fields();
|
||||
signalled = 27; fold3_programs();
|
||||
signalled = 28; fold3_compile_errors();
|
||||
signalled = 29; find_last_groupstart_cases();
|
||||
signalled = 30; shift_direct();
|
||||
signalled = 31; fold3_find_cases();
|
||||
return 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user