From 344ab30c2155b855a1fcd98027199026d4f467d7 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Wed, 7 Jan 2026 03:30:11 +0100 Subject: Code cleanup. --- src/zisp/io.zig | 2 ++ src/zisp/io/Parser.zig | 38 ++++++++++++++++++++++++++++++++++++++ src/zisp/io/parse.zig | 47 +++++------------------------------------------ 3 files changed, 45 insertions(+), 42 deletions(-) (limited to 'src/zisp') diff --git a/src/zisp/io.zig b/src/zisp/io.zig index bedf07f..e6f7c78 100644 --- a/src/zisp/io.zig +++ b/src/zisp/io.zig @@ -6,3 +6,5 @@ pub const encode = @import("io/encode.zig"); pub const read = @import("io/read.zig"); pub const write = @import("io/write.zig"); + +pub const Parser = @import("io/Parser.zig"); 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); diff --git a/src/zisp/io/parse.zig b/src/zisp/io/parse.zig index 0d7cfa8..4d00ca9 100644 --- a/src/zisp/io/parse.zig +++ b/src/zisp/io/parse.zig @@ -10,46 +10,6 @@ const Value = value.Value; const is_test = builtin.is_test; const is_debug = builtin.mode == .Debug; -const ctx_size = @sizeOf(Parser.Context); - -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 * ctx_size; - pub const init_chars_mem: usize = init_chars; - - stack: std.heap.StackFallbackAllocator(init_stack_mem), - chars: std.heap.StackFallbackAllocator(init_chars_mem), - - pub fn init(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) Parser.Alloc { - return .{ .stack = s.stack.get(), .chars = s.chars.get() }; - } - }; -} - -pub const DefaultSfa = Sfa(32, 2048); - -pub fn initSfa(alloc: anytype) !Parser { - const SfaType = @typeInfo(@TypeOf(alloc)).pointer.child; - return Parser.init( - alloc.allocator(), - std.Io.Threaded.global_single_threaded.io(), - SfaType.init_stack_capacity, - SfaType.init_chars_capacity, - ); -} - pub fn fromReader(input: *std.Io.Reader) Value { return _fromReader(input, true); } @@ -59,8 +19,11 @@ pub fn _fromReader( comptime panic: bool, ) if (panic) Value else error{ ParseError, OutOfMemory }!Value { const alloc = std.heap.smp_allocator; - var sfa = DefaultSfa.init(alloc); - var p = initSfa(&sfa) catch |e| if (panic) @panic("OOM") else return e; + const io = std.Io.Threaded.global_single_threaded.io(); + var sfa = Parser.DefaultSfa.withFallback(alloc); + var p = Parser.initWithSfa(&sfa, io) catch |e| { + if (panic) @panic("OOM") else return e; + }; defer p.deinit(); return p.run(input) catch |e| if (panic) { const format = "Parse error: {s}, unread_char: {s}\n"; -- cgit v1.2.3