const std = @import("std"); pub const ParserError = error{ Unexpected, EndOfString }; str: []const u8, pos: u32 = 0, const Self = @This(); pub fn peek(self: Self) ?u8 { if (self.pos >= self.str.len) return null; return self.str[self.pos]; } pub fn consume(self: *Self, c: u8) ParserError!void { if (self.peek()) |pk| { if (pk == c) { self.pos += 1; return; } return ParserError.Unexpected; } return ParserError.EndOfString; } pub fn skip(self: *Self) void { self.pos += 1; } pub fn skipMany(self: *Self, n: usize) void { for (0..n) |_| self.skip(); } pub fn takeUntil(self: *Self, c: u8) ParserError![]const u8 { const i = self.pos; while (self.peek() != null and self.peek().? != c) self.skip(); self.skip(); return self.str[i .. self.pos - 1]; }