refactor: extract modules from bot.zig
This commit creates a bunch of new modules that contain code and tests for various concepts/implementations that used to exist inside bot.zig. Notable amongst these are: - buffer.zig, which contain the circular buffer containing both backlog and outbox messages. - parser.zig, which contain the parser used to parse commands from IRC messages.
This commit is contained in:
parent
508e084ddf
commit
e1e1938359
7 changed files with 694 additions and 285 deletions
54
src/parser.zig
Normal file
54
src/parser.zig
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn init(s: []const u8) Parser {
|
||||
return .init(s);
|
||||
}
|
||||
|
||||
pub const Parser = struct {
|
||||
original: []const u8,
|
||||
rest: []const u8,
|
||||
end_idx: usize,
|
||||
|
||||
pub fn seek(self: *const Parser, skip: usize) Parser {
|
||||
return .{ .original = self.original, .rest = self.rest[skip..], .end_idx = self.end_idx + skip };
|
||||
}
|
||||
|
||||
pub fn init(s: []const u8) Parser {
|
||||
return .{
|
||||
.original = s,
|
||||
.end_idx = 0,
|
||||
.rest = s,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn consume_char(self: *const Parser, c: u8) ?Parser {
|
||||
if (self.rest[0] != c) {
|
||||
return null;
|
||||
}
|
||||
return self.seek(1);
|
||||
}
|
||||
|
||||
pub fn consume_str(self: *const Parser, s: []const u8) ?Parser {
|
||||
const len = s.len;
|
||||
if (self.rest.len < len) {
|
||||
return null;
|
||||
}
|
||||
if (!std.mem.eql(u8, self.rest[0..len], s)) {
|
||||
return null;
|
||||
}
|
||||
return self.seek(len);
|
||||
}
|
||||
|
||||
pub fn take_until_char(self: *const Parser, c: u8) struct { Parser, []const u8 } {
|
||||
const idx = std.mem.indexOfScalar(u8, self.rest, c) orelse unreachable;
|
||||
return .{ self.seek(idx), self.rest[0..idx] };
|
||||
}
|
||||
|
||||
pub fn take_char(self: *const Parser) struct { Parser, u8 } {
|
||||
return .{ self.seek(1), self.rest[0] };
|
||||
}
|
||||
|
||||
pub fn parsed(self: *const Parser) []const u8 {
|
||||
return self.original[0..self.end_idx];
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue