Skip to content

Commit bdb8413

Browse files
author
Peter Murray
committed
added not() version of peek
1 parent 9b11591 commit bdb8413

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: mecha.zig

+12-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn Parser(comptime _T: type) type {
3131
pub const map = mecha.map;
3232
pub const opt = mecha.opt;
3333
pub const peek = mecha.peek;
34+
pub const not = mecha.not;
3435
};
3536
}
3637

@@ -362,14 +363,22 @@ test "opt" {
362363
try expectOk(?u8, 0, null, try p1.parse(fa, "1"));
363364
}
364365

365-
pub fn peek(comptime parser: anytype) mecha.Parser(void) {
366+
pub fn not(comptime parser: anytype) Parser(void) {
367+
return _peek(parser, true);
368+
}
369+
370+
pub fn peek(comptime parser: anytype) Parser(void) {
371+
return _peek(parser, false);
372+
}
373+
374+
pub fn _peek(comptime parser: anytype, _not: bool) mecha.Parser(void) {
366375
const Res = mecha.Result(void);
367376
return .{ .parse = struct {
368377
fn parse(allocator: mem.Allocator, str: []const u8) mecha.Error!Res {
369378
const res = try parser.parse(allocator, str);
370379
return switch (res.value) {
371-
.ok => Res.ok(0, {}),
372-
.err => Res.err(0),
380+
.ok => if (_not) Res.err(0) else Res.ok(0, {}),
381+
.err => if (_not) Res.ok(0, {}) else Res.err(0),
373382
};
374383
}
375384
}.parse };

0 commit comments

Comments
 (0)