summaryrefslogtreecommitdiff
path: root/src/zisp/io.zig
blob: 0a7e5dd459f62e80e90f7e5adbbf3df25ffce909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const std = @import("std");

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");

// 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);
}

// Decode

pub fn decode(v: Value) !Value {
    _ = v;
    @panic("not implemented");
}

// Encode

pub fn encode(v: Value) !Value {
    _ = v;
    @panic("not implemented");
}

// Read & Write

pub fn read(alloc: Alloc, reader: *Reader) !Value {
    return decode(try parse(alloc, reader));
}

pub fn write(v: Value) !void {
    return print(try encode(v));
}