From a92951b11ab3aa2cdf759b3b743f8af006c21b95 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:03:55 -0300 Subject: regexp, sam_edit, ninep/addr: a pattern's compile error is told by Regex.message(err), which replaces the five e_ constants and the two switches that mapped errors onto them Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/addr.zig | 13 +++---------- src/regexp.zig | 19 +++++++++++++------ src/sam_edit.zig | 6 +----- 3 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig index 710dca38..72a2d815 100644 --- a/src/ninep/addr.zig +++ b/src/ninep/addr.zig @@ -285,14 +285,7 @@ pub const Addr = struct { return null; } var rx = regexp_.Regex.compile(pat) catch |err| { - a.err = switch (err) { - error.Anchor => regexp_.Regex.e_anchor, - error.TooLong => regexp_.Regex.e_long, - error.WideRange => regexp_.Regex.e_wide, - error.NegatedRunes => regexp_.Regex.e_negated, - error.Mixed => regexp_.Regex.e_mixed, - error.Bad => e_regexp, - }; + a.err = if (err == error.Bad) e_regexp else regexp_.Regex.message(err); return null; }; // sam's nextmatch (editors/sam/address.c:97-119): an empty match @@ -392,7 +385,7 @@ test "an alternation anchoring some branches and not others is refused with why, for ([_][]const u8{ "/^def|x/", "/foo$|^zèbre/" }) |pat| { const r = th.wr(p, node, pat); try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings(regexp_.Regex.e_mixed, r.reply.ename); + try testing.expectEqualStrings(regexp_.Regex.message(error.Mixed), r.reply.ename); } try testing.expectEqual(Status.ok, th.wr(p, node, "/foo$|def/").reply.status); } @@ -402,7 +395,7 @@ test "a pattern with a newline and an anchor mvzr would misread is refused EINVA defer p.deinit(); const r = th.wr(p, Node.of(th.serialOf(p), .addr), "/(^|\\n)foo/"); try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings(regexp_.Regex.e_anchor, r.reply.ename); + try testing.expectEqualStrings(regexp_.Regex.message(error.Anchor), r.reply.ename); const e = th.wr(p, Node.of(th.serialOf(p), .ctl), "Edit ,x/(^|\\n)foo/d\n"); try testing.expectEqual(E.INVAL, e.errno()); } diff --git a/src/regexp.zig b/src/regexp.zig index 6c858dea..7bcbceff 100644 --- a/src/regexp.zig +++ b/src/regexp.zig @@ -28,7 +28,7 @@ const mvzr = @import("mvzr"); /// one. /// mvzr's own `Regex` holds 64 operations, some 64 pattern characters; a /// search pattern is often longer. Past these a pattern is refused naming -/// the limit (`e_long`). +/// the limit (`message`). pub const max_ops = 512; const Compiled = mvzr.SizedRegex(max_ops, 64); @@ -62,11 +62,18 @@ pub const Regex = struct { /// is ten times slower at it, about two seconds). pub const budget: u64 = 32_000_000; - pub const e_long = std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops }); - pub const e_anchor = "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n"; - pub const e_wide = std.fmt.comptimePrint("bad regular expression: a range of runes wider than {d} in [...] is not supported", .{max_range}); - pub const e_negated = "bad regular expression: a [^...] with non-ASCII runes is not supported (mvzr's classes hold bytes)"; - pub const e_mixed = "bad regular expression: an alternation anchors every branch with ^ or none (^a|^b, not ^a|b)"; + /// What a pattern that does not compile is told; `Bad`'s words are + /// its caller's (which command, which address). + pub fn message(err: Error) []const u8 { + return switch (err) { + error.TooLong => std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops }), + error.Anchor => "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n", + error.WideRange => std.fmt.comptimePrint("bad regular expression: a range of runes wider than {d} in [...] is not supported", .{max_range}), + error.NegatedRunes => "bad regular expression: a [^...] with non-ASCII runes is not supported (mvzr's classes hold bytes)", + error.Mixed => "bad regular expression: an alternation anchors every branch with ^ or none (^a|^b, not ^a|b)", + error.Bad => "bad regular expression", + }; + } /// The most runes a non-ASCII range in a class is spelled out as. pub const max_range = 256; diff --git a/src/sam_edit.zig b/src/sam_edit.zig index b06379f9..a6ed5772 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -611,15 +611,11 @@ const Exec = struct { fn compile(ex: *Exec, pat: []const u8, c: u8) Failure!regexp.Regex { return regexp.Regex.compile(pat) catch |err| switch (err) { - error.Anchor => fail(ex.why, "{s}", .{regexp.Regex.e_anchor}), - error.TooLong => fail(ex.why, "{s}", .{regexp.Regex.e_long}), - error.WideRange => fail(ex.why, "{s}", .{regexp.Regex.e_wide}), - error.NegatedRunes => fail(ex.why, "{s}", .{regexp.Regex.e_negated}), - error.Mixed => fail(ex.why, "{s}", .{regexp.Regex.e_mixed}), error.Bad => if (c == '"' or c == 'X' or c == 'Y') fail(ex.why, "bad regexp in file match", .{}) else fail(ex.why, "bad regexp in {c} command", .{c}), + else => fail(ex.why, "{s}", .{regexp.Regex.message(err)}), }; } -- cgit v1.3