~cricket/zckt

790904a378fc87aa71bf20ee521dde73b35f5302 — c piapiac a month ago 4559919
free memory on errors
3 files changed, 18 insertions(+), 7 deletions(-)

M parser.zig
M tokenizer.zig
M zckt.zig
M parser.zig => parser.zig +4 -2
@@ 217,7 217,7 @@ pub const table_parser = struct {
        pub fn format(
            self: Value,
            comptime _: []const u8,
            _: fmt.FormatOptions,
            options: fmt.FormatOptions,
            writer: anytype,
        ) !void {
            switch (self) {


@@ 278,7 278,7 @@ pub const table_parser = struct {
        pub fn format(
            self: Table,
            comptime _: []const u8,
            _: fmt.FormatOptions,
            options: fmt.FormatOptions,
            writer: anytype,
        ) !void {
            try writer.print("[ ", .{});


@@ 293,6 293,8 @@ pub const table_parser = struct {
        a: *mem.Allocator,
    ) (MemError || StringError || Tokenizer.Error)!Table {
        var table = Table.init(a);
        errdefer table.deinit();

        var key: ?Key = null;
        var value: ?Value = null;
        var i: usize = 0;

M tokenizer.zig => tokenizer.zig +4 -5
@@ 12,7 12,7 @@ pub const Token = union(enum) {
        pub fn format(
            self: String,
            comptime _: []const u8,
            _: fmt.FormatOptions,
            options: fmt.FormatOptions,
            writer: anytype,
        ) !void {
            switch (self) {


@@ 31,7 31,7 @@ pub const Token = union(enum) {
    pub fn format(
        self: Token,
        comptime _: []const u8,
        _: fmt.FormatOptions,
        options: fmt.FormatOptions,
        writer: anytype,
    ) !void {
        switch (self) {


@@ 46,7 46,6 @@ pub const Token = union(enum) {
pub const Tokenizer = struct {
    pub const Error = error{
        EmptyKey,
        InvalidChar,
        UnexpectedNewline,
        UnexpectedEof,
    };


@@ 135,7 134,7 @@ pub const Tokenizer = struct {
                    // remove trailing whitespace
                    // (eg key = value)
                    var end_index = self.index - 1;
                    while (end_index > start_index) {
                    while (end_index >= start_index) {
                        switch (self.content[end_index]) {
                            ' ', '\t', 0x0B, 0x0C, '\r' => end_index -= 1,
                            else => return self.content[start_index .. end_index + 1],


@@ 213,7 212,7 @@ pub const Tokenizer = struct {
                switch (char) {
                    '[' => break :ret Token.table_start,
                    ']' => break :ret Token.table_end,
                    '=' => return error.InvalidChar,
                    '=' => return error.EmptyKey,
                    else => {
                        var value: Token.String = switch (char) {
                            '"' => Token.String{ .quoted = try self.readQuotedString('"') },

M zckt.zig => zckt.zig +10 -0
@@ 56,3 56,13 @@ test "multiline string" {

    testing.expectEqualStrings(table.getString("multiline string").?, multiline_string);
}

test "expect invalid" {
    const zckt =
        \\key;
        \\=some other value
    ;

    const table = table_parser.parse(zckt, alloc);
    testing.expectError(error.EmptyKey, table);
}