diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/mvzr-empty-group.md | 132 | ||||
| -rw-r--r-- | docs/typ/builtins-gui.json | 3 | ||||
| -rw-r--r-- | docs/typ/builtins-tui.json | 3 | ||||
| -rw-r--r-- | docs/typ/cheatsheet.typ | 3 | ||||
| -rw-r--r-- | docs/typ/guide.typ | 69 |
5 files changed, 190 insertions, 20 deletions
diff --git a/docs/mvzr-empty-group.md b/docs/mvzr-empty-group.md new file mode 100644 index 00000000..a3028fc7 --- /dev/null +++ b/docs/mvzr-empty-group.md @@ -0,0 +1,132 @@ +# mvzr: three kinds of pattern compile into a program the matcher walks off an empty slice + +A report for upstream (https://github.com/mnemnion/mvzr), against trunk +3efcfe3 (0.3.9; its trunk when last fetched here, 2026-11-19). pardes +patches its fetched copy at build time (build/patches.zig) until it is +fixed there. + +## What happens + +```zig +const mvzr = @import("mvzr"); +// each of these panics in Debug and ReleaseSafe: index out of bounds, +// or (`^*`) reached unreachable code +_ = mvzr.compile("()a").?.match("b"); +_ = mvzr.compile("a()b").?.match("a"); +_ = mvzr.compile("a|*").?.match("a"); +_ = mvzr.compile("|*").?.match("b"); +_ = mvzr.compile("|??").?.match(""); +_ = mvzr.compile("^*").?.match("a"); +_ = mvzr.compile("({1))").?.match("a"); +// and these compile to something else: `a{1b` to `a` (it matches "a"), +// `a|{2}` to a program that matches "" at the start of "a" +``` + +A program that searches with what a person types, at each key (an +editor's incremental search), reaches the first as soon as the person has +typed `()` and one character more, wherever a line ends before a match. + +Searched from every offset of three short texts (each line its own +haystack), every pattern up to seven characters over `()|*+?a` (960,799): +18,746 panic; up to six over `()|*+?a^$.{}1` (5,229,042): 68,627; up to +five over `()[]{},*+?|\^$.-a1xnbf` (5,399,042): 40,135. + +## Why + +**An empty group with more after it.** At the end of the haystack +`matchPattern` asks `groupAcceptsEmpty` whether a group may match nothing. +`hasAlt` answers true at the group's own `)` when more pattern follows it +(docs/mvzr-group-backtrack.md, the same quirk), so a group with no `|` +takes the alternatives branch, whose last line is +`return (patternAcceptsEmpty(inner_patt));`, and `patternAcceptsEmpty` +switches on `patt[0]` of the empty inside. (The branch without +alternatives checks `inner.len == 0` first; this one does not.) + +**A repeat right after `|` or `^`.** `prefixModifier` refuses a repeat with +nothing before it by `if (j == 0 or patt[j] == .begin)`, but `patt[j]` is +the slot about to be written and is never `.begin`. So `^*` compiles to +`star, begin`, and the matcher meets `.begin` where it is `unreachable`; +and a repeat after `|` takes the `.alt` for what it repeats: `|*` compiles +to `star, alt`. Split at the `|`, the first alternative is a lone `star`, +and `thisPattern` (from `matchStar`) or `nextPattern` (`|??`) reads +`patt[0]` of the empty slice after it. + +**A count that ends in neither `,` nor `}`.** After `{` and a number, +`compileRegex` handles `,` and `}`; any other character writes no +operation, and the dispatch loop's step moves past it, so the program has +an `.unused` slot there, which `findPatternEnd` takes for its end. `a{1b` +runs as `a`; `({1))` (its first `)` skipped, its second closing the `(`) +runs as a lone `.left`, and `sliceGroup` reads past it. + +## The fix + +An empty pattern accepts the empty string; a repeat after `^` or `|` is a +bad string, as one at the start of the pattern is (`patt[j - 1]`, as the +`.begin` test seems to have meant); a count followed by anything but `,` +or `}` is a bad string, as the other unfinished counts are. With it the +same patterns make no panic (0 of 11,588,883), and mvzr's own tests pass +with the one below (32 of 32; the copy tested had its four uses of `**` +replaced, which Zig 0.17 needs: PR #16; without the fix the new test +aborts at `patternAcceptsEmpty`). + +```diff +diff --git a/src/mvzr.zig b/src/mvzr.zig +index d4ea747..791664e 100644 +--- a/src/mvzr.zig ++++ b/src/mvzr.zig +@@ -983,6 +983,7 @@ fn thisGroup(patt: []const RegOp) []const RegOp { + } + + fn patternAcceptsEmpty(patt: []const RegOp) bool { ++ if (patt.len == 0) return true; + switch (patt[0]) { + .optional, + .star, +@@ -1190,7 +1191,7 @@ pub fn resourcesNeeded(comptime in: []const u8) struct { comptime_int, comptime_ + + /// Move modifiers to prefix position. + fn prefixModifier(patt: []RegOp, j: usize, op: RegOp) !usize { +- if (j == 0 or patt[j] == .begin) return BadString; ++ if (j == 0 or patt[j - 1] == .begin or patt[j - 1] == .alt) return BadString; + var find_j = j - 1; + // travel back before last group + switch (patt[find_j]) { +@@ -1518,7 +1519,10 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT { + bad_string = true; + break :dispatch; + }; +- } else if (in[i] == '}') { ++ } else if (in[i] != '}') { ++ bad_string = true; ++ break :dispatch; ++ } else { + // {M} + j += prefixModifier(patt, j, RegOp{ .some = c1 }) catch { + bad_string = true; +@@ -2552,3 +2556,15 @@ test "iterator with empty alternation terminates" { + while (r_iter.next()) |_| {} + try expectEqual(null, r_iter.next()); + } ++ ++test "an empty group, a repeat after | or ^, a count without its }: no walk off an empty slice" { ++ // An empty group with more after it may match nothing. ++ try expect(compile("()a").?.match("b") == null); ++ try expect(compile("a()b").?.match("a") == null); ++ try expectEqual(1, compile("()a").?.match("ba").?.start); ++ try expectEqual(3, compile("a()b").?.match("xab").?.end); ++ // A repeat needs something to repeat; a count needs its `}`. ++ for ([_][]const u8{ "a|*", "|+", "(|?)", "|??", "^*", "^+?", "a|{2}", "({1))", "a{1b" }) |patt| { ++ try expect(compile(patt) == null); ++ } ++} +``` + +## Left as they are + +- `patternAcceptsEmpty` judges a pattern by its first operation: `x(a*b)` + matches `x` (the group is taken to accept the empty string because `a*` + does), and `(()a)` now matches the empty string at the end of `b` where + it panicked. Asking it of every piece of the sequence is the full fix. +- A repeat right after `(` takes the `(` for what it repeats: `(*a)` + compiles as `(a)*` and matches `aa`. Testing `.left` in the same line + refuses it; nothing panics on it, so pardes leaves it. diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index ad1f98df..aeaf6953 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -10,7 +10,8 @@ {"name": "Callstack", "leader": "SPC l b", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show outermost callers first, with signatures and call sites (gc)."}, {"name": "CallstackDepth", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "Caller levels collected by Callstack, 1 through 64; default 8. Bare, it says the current limit."}, {"name": "Changelog", "leader": "SPC h c", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "This build's version and what changed to reach it, printed into an output buffer the same way Config prints the live settings."}, - {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green, what the file no longer has drawn where it stood (ghost rows: a line taken away whole red, each change's description after its last line); bare, shown here or not (none pinned yet: `@`)."}, + {"name": "Changes", "leader": "SPC t c", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green, what the file no longer has drawn where it stood (ghost rows: a line taken away whole red, each change's description after its last line); bare, shown here or not (none pinned yet: `@`)."}, + {"name": "Churn", "leader": "SPC t u", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Count, for each declaration of a file's pane, the changes in `window` (jj) that touched it, and show it in this view: a heat lane after the line number and a strip by the scroll thumb, the line numbers' grey to the theme's red on a log scale; `window` a revset or a span of time (`3d`, `2w`, `6m`, `1y`); bare, shown here or not (none counted yet: the file's whole history)."}, {"name": "ClipPaste", "leader": "SPC p", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard after the selection."}, {"name": "ClipPasteBefore", "leader": "SPC P", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard before the selection."}, {"name": "ClipReplace", "leader": "SPC R", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Replace the selection with the system clipboard."}, diff --git a/docs/typ/builtins-tui.json b/docs/typ/builtins-tui.json index 3ec4ea07..2263df44 100644 --- a/docs/typ/builtins-tui.json +++ b/docs/typ/builtins-tui.json @@ -10,7 +10,8 @@ {"name": "Callstack", "leader": "SPC l b", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show outermost callers first, with signatures and call sites (gc)."}, {"name": "CallstackDepth", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "Caller levels collected by Callstack, 1 through 64; default 8. Bare, it says the current limit."}, {"name": "Changelog", "leader": "SPC h c", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "This build's version and what changed to reach it, printed into an output buffer the same way Config prints the live settings."}, - {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green, what the file no longer has drawn where it stood (ghost rows: a line taken away whole red, each change's description after its last line); bare, shown here or not (none pinned yet: `@`)."}, + {"name": "Changes", "leader": "SPC t c", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green, what the file no longer has drawn where it stood (ghost rows: a line taken away whole red, each change's description after its last line); bare, shown here or not (none pinned yet: `@`)."}, + {"name": "Churn", "leader": "SPC t u", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Count, for each declaration of a file's pane, the changes in `window` (jj) that touched it, and show it in this view: a heat lane after the line number and a strip by the scroll thumb, the line numbers' grey to the theme's red on a log scale; `window` a revset or a span of time (`3d`, `2w`, `6m`, `1y`); bare, shown here or not (none counted yet: the file's whole history)."}, {"name": "ClipPaste", "leader": "SPC p", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard after the selection."}, {"name": "ClipPasteBefore", "leader": "SPC P", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard before the selection."}, {"name": "ClipReplace", "leader": "SPC R", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Replace the selection with the system clipboard."}, diff --git a/docs/typ/cheatsheet.typ b/docs/typ/cheatsheet.typ index 2df92b00..d65ca6d9 100644 --- a/docs/typ/cheatsheet.typ +++ b/docs/typ/cheatsheet.typ @@ -101,6 +101,8 @@ Helix-style: motions *select*, then an edit acts on the selection. Mode box: bla key("SPC t t"), word("Themes"), key("SPC t n"), word("NextColor"), key("SPC t w"), word("Wrap"), + key("SPC t c"), word("Changes"), + key("SPC t u"), word("Churn"), key("SPC c n"), word("Newcol"), key("SPC s d"), word("Dump"), key("SPC s r"), word("Restore"), @@ -117,6 +119,7 @@ Helix-style: motions *select*, then an edit acts on the selection. Mode box: bla == Reviewing diffs `git diff` output is a diff: #btn("B3") on a file line, an `@@` or a line's `+`/`-` jumps there. #doc("tags", section: "reviewing-diffs") +#word("Changes") `revset` on a file's pane: a lane a change, ghost rows for what it took away, #keys("] g", "[ g") from change to change; #word("Churn") `3m`: each declaration's changes, grey to red; `a.zig@main:12` is a file at a revision. #doc("guide", section: "changes") == Finding things - A query pane under the pane: line 1 the query, its first word the source: `/pat`, `Find name` (fuzzy), `Grep text` (literal), `Recent`, `Panes`, `Symbols`, `All`. #keys("Ctrl-n", "Ctrl-p") step, #key("Enter") lands and keeps the rows, #key("Esc") puts things back. #word("Help"), #word("Tutor"). diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index 0d55c6a6..e73eabaf 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -295,24 +295,57 @@ the revision has not) is said in jj's words. jj reads the working copy as it last saw it (`@`): pardes never snapshots it. #word("Syntax") (#key("SPC t h")) turns a text's syntax colours off in this view, and on again. -#word("Changes") with a revset pins its changes on a file's pane (or a -revision's) and shows them in this view (one set a pane: another view's -revset replaces it there too); bare, they are hidden in this view or shown -again, and with none pinned yet it is `@`, the change against its parent. Each change that touched the file gets a lane after the -line number, from its first line to its last, in the theme's accents in -hue order, oldest first; a change that rewrote lines inside another's -steps a lane to the right on those rows; a line one added whole is green -across the line. What the file no longer has is drawn where it stood, a -ghost row with no number: a line a change took away whole, red across; -each change's description (`id title author age`) after its last line, in -its lane's colour. The cursor steps over ghost rows, search and selection -do not see them, and a Look on one opens its line in the revision it came -from. `]g` and `[g` go to the next and the previous change. jj answers -once, when the word is run: the lanes and the ghost rows ride with the -lines through edits made since. A line a change changed is not green across: its -old text stands right above it as a ghost row, the words that went red, -and on the line the words that came are green (the patch view's -alignment says which lines are one line changed). + += Changes on the source view <changes> + +jj's changes drawn on a file's own pane, as layers each view wears or +not: the text stays the text (the patch view, `+Diff`, is unchanged). +Nothing is shown, and nothing asked of jj, until a word is run. + +#pairs( + [#word("Changes") `revset` (#key("SPC t c"))], [pins the revset's changes that touched the file on its pane (or a revision's) and shows them in this view; one set a pane (another view's revset replaces it there too). Bare: hidden in this view or shown again; with none pinned yet, `@`, the change against its parent], + [#word("Churn") `window` (#key("SPC t u"))], [counts, for each declaration, the window's changes that touched it, and shows them in this view; the window a revset, or a span of time back from now (`3d`, `2w`, `6m`, `1y`). Bare: hidden or shown again; with none counted yet, the file's whole history], + [#keys("] g", "[ g")], [the next and the previous change's first line], + [`path@rev`, `path@rev:line`], [the file at a revision (#doc("guide", section: "revision")): a #word("Look") anywhere opens it], +) + +*Lanes.* Each pinned change gets a lane after the line number, from its +first line to its last, in the theme's accents in hue order (a colour too +close to the one before it skipped), oldest first; a change that rewrote +lines inside another's steps a lane to the right on those rows. A line +one added whole is green across the line. + +*Ghost rows.* What the file no longer has is drawn where it stood, a row +of its own with no number: a line a change took away whole, red across; +a line a change changed, its old text right above it with the words that +went red, and on the line the words that came green (the patch view's +alignment says which lines are one line changed); each change's +description, `id title author age` (`3w`, `4m`), after its last line, in +its lane's colour, at one place after that change's own ghost rows. A +ghost row is not text: the cursor steps over it, search, selection and +the line numbers do not see it, and a #btn("B3") on one opens its line in +the revision it came from (`path@id-:line`; a description, `path@id`). It +moves the rows below down in this view alone. It is one row, cut where +the pane ends (never wrapped); in a narrow pane a description's title is +cut, its author and age kept. A hunk that took away more than 1,024 +lines shows none of them. + +*Churn.* A cell after the line number, from the line numbers' grey to the +theme's red on a log scale of the count against the file's hottest +declaration, the declaration a line is in being the innermost +tree-sitter finds (a line in none has none); beside the scroll thumb a +strip, each of its rows the hottest of the lines it stands for. With +#word("Changes") on too, the churn's cell comes first and the lanes stay +next to the text they mark. A file's history is followed by its name: +what it had under another name before a move is not counted. + +jj answers once, when the word is run, on a worker (the editor goes on); +its answer is lined up with the pane's text as it is then (lines typed +before jj's last snapshot or while it ran), and the lanes, ghost rows and +counts ride with the lines through the edits made since. A place inside +lines rewritten since jj's snapshot has no ghost row. A change between +two pinned ones that the revset does not pin still moves the older ones' +ghost rows to where they stand. = Call hierarchy <callstack> |
