summaryrefslogtreecommitdiff
path: root/src/zisp/io/parse.zig
blob: 4d00ca93d071ed225859c62a7de8f1eba5c0dcf2 (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
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;

pub fn fromReader(input: *std.Io.Reader) Value {
    return _fromReader(input, true);
}

pub fn _fromReader(
    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;
    };
}