Skip to content

Commit 9b11591

Browse files
author
Peter Murray
committed
added peek functionality.
1 parent 075ecd1 commit 9b11591

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

mecha.zig

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn Parser(comptime _T: type) type {
3030
pub const mapConst = mecha.mapConst;
3131
pub const map = mecha.map;
3232
pub const opt = mecha.opt;
33+
pub const peek = mecha.peek;
3334
};
3435
}
3536

@@ -361,6 +362,27 @@ test "opt" {
361362
try expectOk(?u8, 0, null, try p1.parse(fa, "1"));
362363
}
363364

365+
pub fn peek(comptime parser: anytype) mecha.Parser(void) {
366+
const Res = mecha.Result(void);
367+
return .{ .parse = struct {
368+
fn parse(allocator: mem.Allocator, str: []const u8) mecha.Error!Res {
369+
const res = try parser.parse(allocator, str);
370+
return switch (res.value) {
371+
.ok => Res.ok(0, {}),
372+
.err => Res.err(0),
373+
};
374+
}
375+
}.parse };
376+
}
377+
378+
test "peek" {
379+
const fa = testing.failing_allocator;
380+
const p1 = comptime ascii.range('a', 'z').peek();
381+
try expectOk(void, 0, {}, try p1.parse(fa, "a"));
382+
try expectOk(void, 0, {}, try p1.parse(fa, "aa"));
383+
try expectErr(void, 0, try p1.parse(fa, "1"));
384+
}
385+
364386
fn parsersTypes(comptime parsers: anytype) []const type {
365387
var types: []const type = &[_]type{};
366388
for (parsers) |parser| {

0 commit comments

Comments
 (0)