diff options
Diffstat (limited to 'src/zisp')
| -rw-r--r-- | src/zisp/io/Parser.zig | 83 | ||||
| -rw-r--r-- | src/zisp/io/parse.zig | 52 | ||||
| -rw-r--r-- | src/zisp/io/read.zig | 8 |
3 files changed, 85 insertions, 58 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index d18150e..9833baa 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -120,33 +120,23 @@ pub const Alloc = struct { chars: std.mem.Allocator, }; -pub fn Sfa(init_stack: usize, init_chars: usize) type { +pub fn StackFallbackAlloc(ctx_stack_cap: usize, str_chars_cap: usize) type { return struct { - const Self = @This(); + pub const ctx_stack_bytes = ctx_stack_cap * @sizeOf(Context); + pub const str_chars_bytes = str_chars_cap; - pub const init_stack_capacity: usize = init_stack; - pub const init_chars_capacity: usize = init_chars; + stack: std.heap.StackFallbackAllocator(ctx_stack_bytes), + chars: std.heap.StackFallbackAllocator(str_chars_bytes), - 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 fn alloc(self: *@This()) Alloc { + return .{ .stack = self.stack.get(), .chars = self.chars.get() }; } }; } -pub const DefaultSfa = Sfa(32, 2048); +const def_ctx_stack_cap = 32; +const def_str_chars_cap = 2048; +const DefaultSfa = StackFallbackAlloc(def_ctx_stack_cap, def_str_chars_cap); alloc: Alloc, io: std.Io, @@ -159,29 +149,54 @@ result: Value = undefined, unread_char: ?u8 = null, err_msg: []const u8 = undefined, -pub fn init( +pub fn init(alloc: std.mem.Allocator, io: std.Io) !struct { + Parser, + DefaultSfa, +} { + return initWithSfa(def_ctx_stack_cap, def_str_chars_cap, alloc, io); +} + +pub fn initWithSfa( + comptime init_ctx_stack_cap: usize, + comptime init_str_chars_cap: usize, + alloc: std.mem.Allocator, + io: std.Io, +) !struct { + Parser, + StackFallbackAlloc(init_ctx_stack_cap, init_str_chars_cap), +} { + const Sfa = StackFallbackAlloc(init_ctx_stack_cap, init_str_chars_cap); + var sfa: Sfa = .{ + .stack = std.heap.stackFallback(Sfa.ctx_stack_bytes, alloc), + .chars = std.heap.stackFallback(Sfa.str_chars_bytes, alloc), + }; + const parser: Parser = try initCustom( + sfa.alloc(), + io, + init_ctx_stack_cap, + init_str_chars_cap, + ); + return .{ parser, sfa }; +} + +pub fn initCustom( alloc: Alloc, io: std.Io, - init_stack_capacity: usize, - init_chars_capacity: usize, + init_ctx_stack_cap: usize, + init_str_chars_cap: usize, ) !Parser { var p: Parser = .{ .alloc = alloc, .io = io }; - p.stack = try .initCapacity(alloc.stack, init_stack_capacity); - p.chars = try .initCapacity(alloc.chars, init_chars_capacity); + p.stack = try .initCapacity(alloc.stack, init_ctx_stack_cap); + p.chars = try .initCapacity(alloc.chars, init_str_chars_cap); 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, sfa: anytype) void { + p.deinitCustom(); + _ = sfa; } -pub fn deinit(p: *Parser) void { +pub fn deinitCustom(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 4d00ca9..6e84b55 100644 --- a/src/zisp/io/parse.zig +++ b/src/zisp/io/parse.zig @@ -10,30 +10,38 @@ const Value = value.Value; const is_test = builtin.is_test; const is_debug = builtin.mode == .Debug; -pub fn fromReader(input: *std.Io.Reader) Value { - return _fromReader(input, true); +const ParserErrors = error{ + ParseError, + OutOfMemory, +}; + +pub fn fromReader( + alloc: std.mem.Allocator, + io: std.Io, + input: *std.Io.Reader, +) ParserErrors!Value { + var p, const sfa = try Parser.init(alloc, io); + defer p.deinit(sfa); + return p.run(input); } -pub fn _fromReader( +pub fn fromReaderNoError( + alloc: std.mem.Allocator, + io: std.Io, input: *std.Io.Reader, - comptime panic: bool, -) if (panic) Value else error{ ParseError, OutOfMemory }!Value { - const alloc = std.heap.smp_allocator; - 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"; - const unread: [4]u8 = - if (p.unread_char) |c| - "0x".* ++ std.fmt.hex(c) - else - "none".*; - std.debug.panic(format, .{ p.err_msg, unread }); - } else { - return e; +) Value { + var p, const sfa = Parser.init(alloc, io) catch @panic("OOM"); + defer p.deinit(sfa); + return p.run(input) catch |e| switch (e) { + error.OutOfMemory => @panic("OOM"), + error.ParseError => { + const format = "Parse error: {s}, unread_char: {s}\n"; + const unread: [4]u8 = + if (p.unread_char) |c| + "0x".* ++ std.fmt.hex(c) + else + "none".*; + std.debug.panic(format, .{ p.err_msg, unread }); + }, }; } diff --git a/src/zisp/io/read.zig b/src/zisp/io/read.zig index 5fa647e..23f0ea8 100644 --- a/src/zisp/io/read.zig +++ b/src/zisp/io/read.zig @@ -5,6 +5,10 @@ const decode = @import("decode.zig"); const Value = @import("../value.zig").Value; -pub fn fromReader(reader: *std.Io.Reader) Value { - return decode.decode(parse.fromReader(reader)); +pub fn fromReader( + alloc: std.mem.Allocator, + io: std.Io, + reader: *std.Io.Reader, +) Value { + return decode.decode(parse.fromReader(alloc, io, reader)); } |
