const std = @import("std"); const gc = @import("gc.zig"); const value = @import("value.zig"); const Alloc = std.mem.Allocator; const Reader = std.Io.Reader; const Writer = std.Io.Writer; const Value = value.Value; pub const Parser = @import("io/Parser.zig"); pub const Printer = @import("io/Printer.zig"); pub const Decoder = @import("io/Decoder.zig"); pub const Encoder = @import("io/Encoder.zig"); var main_io: std.Io = undefined; var main_decoder: Decoder = undefined; var main_encoder: Encoder = undefined; var init_done = false; pub fn init() !void { if (init_done) return; defer init_done = true; main_io = std.Io.Threaded.global_single_threaded.io(); main_decoder = try .init(gc.mainAlloc()); main_encoder = try .init(gc.mainAlloc()); } pub fn mainIo() std.Io { return main_io; } pub fn mainDecoder() *Decoder { init() catch @panic("OOM"); // TODO this is only here for the test suite return &main_decoder; } pub fn mainEncoder() *Encoder { init() catch @panic("OOM"); // TODO this is only here for the test suite return &main_encoder; } // Parse const ParserErrors = error{ ParseError, OutOfMemory, }; pub fn parse(alloc: Alloc, reader: *Reader) ParserErrors!Value { var p = try Parser.init(alloc); defer p.deinit(); return p.run(reader); } pub fn parseNoError(alloc: Alloc, reader: *Reader) Value { var p = Parser.init(alloc) catch @panic("OOM"); defer p.deinit(); return p.run(reader) 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 }); }, }; } // Print pub fn print(writer: *Writer, v: Value) !void { var p = Printer.init(writer); try p.print(v); } pub fn printStdout(v: Value) !void { var stdout_buffer: [4096]u8 = undefined; var stdout_writer = std.Io.File.stdout().writer(main_io, &stdout_buffer); const writer = &stdout_writer.interface; try print(writer, v); try writer.writeByte('\n'); try writer.flush(); }