#Parsig zon file problem

1 messages · Page 1 of 1 (latest)

cyan lodge
#

Hello, I'm trying to use zon file as a configuration file for my project, I'm almost there but my problem is my code panics when in config file is an array and i can't check its tag becouse it fails when i want to get node from fields.

fn process_ast(config: *Config, ast: std.zig.Ast) !void {
    var buf: [2]std.zig.Ast.Node.Index = undefined;
    const root = ast.fullStructInit(&buf, ast.nodes.items(.data)[0].lhs) orelse {
        return error.ParseError;
    };

    const allocator = config._arena.allocator();

    for (root.ast.fields) |field_idx| {
        const field_name = ast.tokenSlice(ast.firstToken(field_idx) - 2);

        const whitelist = std.ArrayList(std.net.Address).init(allocator);
        errdefer whitelist.deinit();

        const node = root.ast.fields[field_idx - 1];
        const info: std.zig.Ast.Node = ast.nodes.get(node);

        std.debug.print("{s}:{any}\n", .{ field_name, info.tag });

        switch (info.tag) {
            std.zig.Ast.Node.Tag.number_literal => {
                const value = utils.parse_number(ast, field_idx);

                try assign_field_value(config, field_name, value.int);
            },
            std.zig.Ast.Node.Tag.string_literal => {
                const value = try utils.parse_string(allocator, ast, field_idx);
                defer allocator.free(value);

                try assign_field_value(config, field_name, value);
            },

            else => continue,
        }
    }
}
#

config file:

.{
    .address = "127.0.0.1",
    .port = 5556,
    .maxclients = 123,
    .maxmemory = 0, // 0 means unlimited
    .proto_max_bulk_len = 0,
    .workers = 4,
    .cbuffer = 4096,
    .whitelist = .{
        "127.0.0.1",
    },
}
#

error:

address:zig.Ast.Node.Tag.string_literal
port:zig.Ast.Node.Tag.number_literal
maxclients:zig.Ast.Node.Tag.number_literal
maxmemory:zig.Ast.Node.Tag.number_literal
proto_max_bulk_len:zig.Ast.Node.Tag.number_literal
workers:zig.Ast.Node.Tag.number_literal
cbuffer:zig.Ast.Node.Tag.number_literal
error: # server panicked, please check logs in ./log/zcached.log
thread 8143 panic: index out of bounds: index 8, len 8
/home/sectasy/zcached/src/server/config.zig:287:37: 0x1094000 in process_ast (zcached)
        const node = root.ast.fields[field_idx - 1];
                                    ^
/home/sectasy/zcached/src/server/config.zig:225:20: 0x10976b2 in load (zcached)
    try process_ast(&config, ast);
                   ^
/home/sectasy/zcached/src/main.zig:83:31: 0x10571b3 in main (zcached)
    const config = Config.load(
                              ^
/home/sectasy/zig-0.12/lib/std/start.zig:501:22: 0x10569a9 in main (zcached)
            root.main();
                     ^
../csu/libc-start.c:308:16: 0x7fa902287082 in __libc_start_main (../sysdeps/x86/libc-start.c)
???:?:?: 0x105665d in ??? (???)
???:?:?: 0x0 in ??? (???)
run
#

I know what that error means, what I don't understand is why the entry isn't there when everything else is returning as it should?

spring bison
#

shouldn't that be just field_idx without -1, you are probably substracting from 0

cyan lodge
#

It returnednumber_literal tag but here for address key is string_literal