| ... | ... | @@ -11,10 +11,19 @@ const c = @cImport({ |
| 11 | 11 | |
| 12 | 12 | pub const Stream = struct { |
| 13 | 13 | docs: []const Document, |
| 14 | |
| 15 | pub fn deinit(self: *const Stream, alloc: std.mem.Allocator) void { |
| 16 | for (self.docs) |*item| item.deinit(alloc); |
| 17 | alloc.free(self.docs); |
| 18 | } |
| 14 | 19 | }; |
| 15 | 20 | |
| 16 | 21 | pub const Document = struct { |
| 17 | 22 | mapping: Mapping, |
| 23 | |
| 24 | pub fn deinit(self: *const Document, alloc: std.mem.Allocator) void { |
| 25 | self.mapping.deinit(alloc); |
| 26 | } |
| 18 | 27 | }; |
| 19 | 28 | |
| 20 | 29 | pub const Item = union(enum) { |
| ... | ... | @@ -25,6 +34,22 @@ pub const Item = union(enum) { |
| 25 | 34 | string: string, |
| 26 | 35 | stream: Stream, |
| 27 | 36 | |
| 37 | pub fn deinit(self: *const Item, alloc: std.mem.Allocator) void { |
| 38 | switch (self.*) { |
| 39 | .event => {}, |
| 40 | .kv => |kv| kv.deinit(alloc), |
| 41 | .mapping => |m| m.deinit(alloc), |
| 42 | .sequence => |s| { |
| 43 | for (s) |*item| item.deinit(alloc); |
| 44 | alloc.free(s); |
| 45 | }, |
| 46 | .string => |s| { |
| 47 | _ = s; |
| 48 | }, |
| 49 | .stream => |s| s.deinit(alloc), |
| 50 | } |
| 51 | } |
| 52 | |
| 28 | 53 | pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { |
| 29 | 54 | _ = fmt; |
| 30 | 55 | _ = options; |
| ... | ... | @@ -60,6 +85,11 @@ pub const Sequence = []const Item; |
| 60 | 85 | pub const Key = struct { |
| 61 | 86 | key: string, |
| 62 | 87 | value: Value, |
| 88 | |
| 89 | pub fn deinit(self: *const Key, alloc: std.mem.Allocator) void { |
| 90 | // alloc.free(self.key); |
| 91 | self.value.deinit(alloc); |
| 92 | } |
| 63 | 93 | }; |
| 64 | 94 | |
| 65 | 95 | pub const Value = union(enum) { |
| ... | ... | @@ -67,6 +97,22 @@ pub const Value = union(enum) { |
| 67 | 97 | mapping: Mapping, |
| 68 | 98 | sequence: Sequence, |
| 69 | 99 | |
| 100 | pub fn deinit(self: *const Value, alloc: std.mem.Allocator) void { |
| 101 | switch (self.*) { |
| 102 | .string => |s| { |
| 103 | _ = s; |
| 104 | // alloc.free(s); |
| 105 | }, |
| 106 | .mapping => |*m| { |
| 107 | m.deinit(alloc); |
| 108 | }, |
| 109 | .sequence => |s| { |
| 110 | for (s) |*item| item.deinit(alloc); |
| 111 | alloc.free(s); |
| 112 | }, |
| 113 | } |
| 114 | } |
| 115 | |
| 70 | 116 | pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { |
| 71 | 117 | _ = fmt; |
| 72 | 118 | _ = options; |
| ... | ... | @@ -94,6 +140,11 @@ pub const Value = union(enum) { |
| 94 | 140 | pub const Mapping = struct { |
| 95 | 141 | items: []const Key, |
| 96 | 142 | |
| 143 | pub fn deinit(self: *const Mapping, alloc: std.mem.Allocator) void { |
| 144 | for (self.items) |*item| item.deinit(alloc); |
| 145 | alloc.free(self.items); |
| 146 | } |
| 147 | |
| 97 | 148 | pub fn get(self: Mapping, k: string) ?Value { |
| 98 | 149 | for (self.items) |item| { |
| 99 | 150 | if (std.mem.eql(u8, item.key, k)) { |
| ... | ... | @@ -118,7 +169,7 @@ pub const Mapping = struct { |
| 118 | 169 | |
| 119 | 170 | pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string { |
| 120 | 171 | var list = std.ArrayList(string).init(alloc); |
| 121 | | defer list.deinit(); |
| 172 | errdefer list.deinit(); |
| 122 | 173 | if (self.get(k)) |val| { |
| 123 | 174 | if (val == .sequence) { |
| 124 | 175 | for (val.sequence) |item| { |
| ... | ... | @@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { |
| 161 | 212 | defer c.yaml_parser_delete(&parser); |
| 162 | 213 | |
| 163 | 214 | const lines = try split(alloc, input, "\n"); |
| 215 | defer alloc.free(lines); |
| 164 | 216 | |
| 165 | 217 | _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len); |
| 166 | 218 | |
| 167 | 219 | var all_events = std.ArrayList(Token).init(alloc); |
| 220 | defer all_events.deinit(); |
| 168 | 221 | var event: Token = undefined; |
| 169 | 222 | while (true) { |
| 170 | 223 | const p = c.yaml_parser_parse(&parser, &event); |
| ... | ... | @@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { |
| 188 | 241 | .index = 0, |
| 189 | 242 | }; |
| 190 | 243 | const stream = try p.parse(); |
| 244 | defer alloc.free(stream.docs); |
| 191 | 245 | return stream.docs[0]; |
| 192 | 246 | } |
| 193 | 247 | |