| ... | @@ -11,10 +11,19 @@ const c = @cImport({ | ... | @@ -11,10 +11,19 @@ const c = @cImport({ |
| 11 | | 11 | |
| 12 | pub const Stream = struct { | 12 | pub const Stream = struct { |
| 13 | docs: []const Document, | 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 | pub const Document = struct { | 21 | pub const Document = struct { |
| 17 | mapping: Mapping, | 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 | pub const Item = union(enum) { | 29 | pub const Item = union(enum) { |
| ... | @@ -25,6 +34,22 @@ pub const Item = union(enum) { | ... | @@ -25,6 +34,22 @@ pub const Item = union(enum) { |
| 25 | string: string, | 34 | string: string, |
| 26 | stream: Stream, | 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 | pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { | 53 | pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { |
| 29 | _ = fmt; | 54 | _ = fmt; |
| 30 | _ = options; | 55 | _ = options; |
| ... | @@ -60,6 +85,11 @@ pub const Sequence = []const Item; | ... | @@ -60,6 +85,11 @@ pub const Sequence = []const Item; |
| 60 | pub const Key = struct { | 85 | pub const Key = struct { |
| 61 | key: string, | 86 | key: string, |
| 62 | value: Value, | 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 | pub const Value = union(enum) { | 95 | pub const Value = union(enum) { |
| ... | @@ -67,6 +97,22 @@ pub const Value = union(enum) { | ... | @@ -67,6 +97,22 @@ pub const Value = union(enum) { |
| 67 | mapping: Mapping, | 97 | mapping: Mapping, |
| 68 | sequence: Sequence, | 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 | pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { | 116 | pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { |
| 71 | _ = fmt; | 117 | _ = fmt; |
| 72 | _ = options; | 118 | _ = options; |
| ... | @@ -94,6 +140,11 @@ pub const Value = union(enum) { | ... | @@ -94,6 +140,11 @@ pub const Value = union(enum) { |
| 94 | pub const Mapping = struct { | 140 | pub const Mapping = struct { |
| 95 | items: []const Key, | 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 | pub fn get(self: Mapping, k: string) ?Value { | 148 | pub fn get(self: Mapping, k: string) ?Value { |
| 98 | for (self.items) |item| { | 149 | for (self.items) |item| { |
| 99 | if (std.mem.eql(u8, item.key, k)) { | 150 | if (std.mem.eql(u8, item.key, k)) { |
| ... | @@ -118,7 +169,7 @@ pub const Mapping = struct { | ... | @@ -118,7 +169,7 @@ pub const Mapping = struct { |
| 118 | | 169 | |
| 119 | pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string { | 170 | pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string { |
| 120 | var list = std.ArrayList(string).init(alloc); | 171 | var list = std.ArrayList(string).init(alloc); |
| 121 | defer list.deinit(); | 172 | errdefer list.deinit(); |
| 122 | if (self.get(k)) |val| { | 173 | if (self.get(k)) |val| { |
| 123 | if (val == .sequence) { | 174 | if (val == .sequence) { |
| 124 | for (val.sequence) |item| { | 175 | for (val.sequence) |item| { |
| ... | @@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { | ... | @@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { |
| 161 | defer c.yaml_parser_delete(&parser); | 212 | defer c.yaml_parser_delete(&parser); |
| 162 | | 213 | |
| 163 | const lines = try split(alloc, input, "\n"); | 214 | const lines = try split(alloc, input, "\n"); |
| | 215 | defer alloc.free(lines); |
| 164 | | 216 | |
| 165 | _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len); | 217 | _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len); |
| 166 | | 218 | |
| 167 | var all_events = std.ArrayList(Token).init(alloc); | 219 | var all_events = std.ArrayList(Token).init(alloc); |
| | 220 | defer all_events.deinit(); |
| 168 | var event: Token = undefined; | 221 | var event: Token = undefined; |
| 169 | while (true) { | 222 | while (true) { |
| 170 | const p = c.yaml_parser_parse(&parser, &event); | 223 | const p = c.yaml_parser_parse(&parser, &event); |
| ... | @@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { | ... | @@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document { |
| 188 | .index = 0, | 241 | .index = 0, |
| 189 | }; | 242 | }; |
| 190 | const stream = try p.parse(); | 243 | const stream = try p.parse(); |
| | 244 | defer alloc.free(stream.docs); |
| 191 | return stream.docs[0]; | 245 | return stream.docs[0]; |
| 192 | } | 246 | } |
| 193 | | 247 | |