summaryrefslogtreecommitdiff
path: root/src/zisp/io/Parser.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/io/Parser.zig')
-rw-r--r--src/zisp/io/Parser.zig38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig
index 2c381e1..2bb7624 100644
--- a/src/zisp/io/Parser.zig
+++ b/src/zisp/io/Parser.zig
@@ -118,6 +118,34 @@ pub const Alloc = struct {
chars: std.mem.Allocator,
};
+pub fn Sfa(init_stack: usize, init_chars: usize) type {
+ return struct {
+ const Self = @This();
+
+ pub const init_stack_capacity: usize = init_stack;
+ pub const init_chars_capacity: usize = init_chars;
+
+ pub const init_stack_mem: usize = init_stack * @sizeOf(Context);
+ pub const init_chars_mem: usize = init_chars;
+
+ stack: std.heap.StackFallbackAllocator(init_stack_mem),
+ chars: std.heap.StackFallbackAllocator(init_chars_mem),
+
+ pub fn withFallback(fallback: std.mem.Allocator) Self {
+ return .{
+ .stack = std.heap.stackFallback(init_stack_mem, fallback),
+ .chars = std.heap.stackFallback(init_chars_mem, fallback),
+ };
+ }
+
+ pub fn allocator(s: *Self) Alloc {
+ return .{ .stack = s.stack.get(), .chars = s.chars.get() };
+ }
+ };
+}
+
+pub const DefaultSfa = Sfa(32, 2048);
+
alloc: Alloc,
io: std.Io,
input: *std.Io.Reader = undefined,
@@ -141,6 +169,16 @@ pub fn init(
return p;
}
+pub fn initWithSfa(sfa: anytype, io: std.Io) !Parser {
+ const SfaType = @typeInfo(@TypeOf(sfa)).pointer.child;
+ return init(
+ sfa.allocator(),
+ io,
+ SfaType.init_stack_capacity,
+ SfaType.init_chars_capacity,
+ );
+}
+
pub fn deinit(p: *Parser) void {
p.stack.deinit(p.alloc.stack);
p.chars.deinit(p.alloc.chars);