summaryrefslogtreecommitdiff
path: root/src/zisp/io/parse.zig
blob: 6e84b553582562eb2b491a1c65cdb9df572fbe32 (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
const builtin = @import("builtin");
const std = @import("std");

const value = @import("../value.zig");

const Parser = @import("Parser.zig");

const Value = value.Value;

const is_test = builtin.is_test;
const is_debug = builtin.mode == .Debug;

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 fromReaderNoError(
    alloc: std.mem.Allocator,
    io: std.Io,
    input: *std.Io.Reader,
) 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 });
        },
    };
}