summaryrefslogtreecommitdiff
path: root/docs/mvzr-group-backtrack.md
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
committerGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
commit6d8da8721141284cf33bb901168b26135893eb86 (patch)
tree791d63401d4e85fbca1d597a8ce0326e70efd545 /docs/mvzr-group-backtrack.md
parentefd43a3a2cf03bb741366331158b368abf9ac583 (diff)
parentbf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff)
downloadpardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz
pardes-6d8da8721141284cf33bb901168b26135893eb86.zip
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'docs/mvzr-group-backtrack.md')
-rw-r--r--docs/mvzr-group-backtrack.md86
1 files changed, 86 insertions, 0 deletions
diff --git a/docs/mvzr-group-backtrack.md b/docs/mvzr-group-backtrack.md
new file mode 100644
index 00000000..867336a3
--- /dev/null
+++ b/docs/mvzr-group-backtrack.md
@@ -0,0 +1,86 @@
+# mvzr: a repeat inside a group never gives back to what follows the group
+
+A report for upstream (https://github.com/mnemnion/mvzr), against trunk
+3efcfe3 (0.3.9). pardes patches its fetched copy at build time (build.zig)
+until it is fixed there.
+
+## What happens
+
+```zig
+const mvzr = @import("mvzr");
+// a+ab matches aab; the same with the repeat in a group does not
+std.debug.assert(mvzr.compile("a+ab").?.match("aab") != null);
+std.debug.assert(mvzr.compile("(a+)ab").?.match("aab") == null); // expected a match
+```
+
+The same for `(a*)ab` on `aab`, `(.+)_area` and `([a-z_]+)_area` on
+`the_total_area`. `\w+_area` and `(\w)+_area` match, as does
+`([a-z]+)_area` (whose class cannot eat the `_`, so no giving back is
+needed).
+
+## Why
+
+`hasAlt` returns true when its scan reaches the group's own closing
+`.right` at depth 0:
+
+```zig
+.right => {
+ if (pump == 0)
+ return true // the group's own `)`, not an alternative
+ else
+ pump -= 1;
+},
+```
+
+so every group with more pattern after it goes down `matchGroup`'s
+alternatives branch. That branch takes each alternative's single match (a
+greedy repeat's longest) and, when the pattern after the group fails from
+there, moves on to the next alternative; it never tries a shorter match of
+the same alternative. The alt-free branch has the same shape.
+
+## A fix
+
+Where the pattern after the group fails from an alternative's match, try
+that alternative's shorter matches, longest first, before the next
+alternative. pardes's patch, in `matchGroup`'s loop:
+
+```zig
+} else { // Try our next pattern, if any.
+ var e = m1.i;
+ while (e > i) {
+ e -= 1;
+ const shorter = matchAlt(remaining_patt, sets, haystack[0..e], i) orelse continue;
+ if (shorter.i != e) continue;
+ if (matchPattern(next_patt, sets, haystack, e)) |whole| return whole;
+ }
+ remaining_patt = m1.j;
+}
+```
+
+Matching the alternative against `haystack[0..e]` makes `$` and `\b` see a
+string end at `e`, so a group ending in either can accept a cut the full
+string would not; a continuation-passing matcher (each repeat trying the
+rest of the whole pattern) would be the full fix. `hasAlt` returning false
+at the group's own `)` is worth fixing too.
+
+# mvzr: `$` before `|` is a bad pattern
+
+A second report, same trunk. `compile` refuses `$` anywhere but at the
+very end of the pattern, so an anchored alternative must come last:
+
+```zig
+std.debug.assert(mvzr.compile("zz|h$") != null);
+std.debug.assert(mvzr.compile("h$|zz") == null); // expected a regex
+std.debug.assert(mvzr.compile("(h$|zz)") == null); // likewise
+```
+
+The parser's check is `if (i + 1 < in.len) bad_string = true`. The
+matcher already handles `.end` inside an alternative: `matchAlt` and
+`matchGroup` hand `matchPattern` each alternative as its own slice, and
+`.end` returns an empty rest, which ends that slice. So `$` can stand
+before `|` or `)` as well as last; pardes's patch:
+
+```zig
+'$' => {
+ if (i + 1 < in.len and in[i + 1] != '|' and in[i + 1] != ')') {
+```