authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-07 05:37:18 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-07 05:37:18 -07:00
logdd5c32c4fd294bfe52cfd12a6fa92573cb7e8847
tree2ddb5c9a4897a173052c3909c1fd87f3cd4d8385
parenteaf70895de1efa8d01d58268919c4f7737554824

util/yaml


1 files changed, 8 insertions(+), 7 deletions(-)

src/util/yaml.zig+8-7
...@@ -20,7 +20,7 @@ pub const Document = struct {...@@ -20,7 +20,7 @@ pub const Document = struct {
20};20};
2121
22pub const Item = union(enum) {22pub const Item = union(enum) {
23 event: c.yaml_event_t,23 event: Token,
24 kv: Key,24 kv: Key,
25 mapping: Mapping,25 mapping: Mapping,
26 sequence: Sequence,26 sequence: Sequence,
...@@ -130,7 +130,8 @@ pub const Mapping = struct {...@@ -130,7 +130,8 @@ pub const Mapping = struct {
130 }130 }
131};131};
132132
133pub const TokenList = []const c.yaml_event_t;133pub const Token = c.yaml_event_t;
134pub const TokenList = []const Token;
134135
135//136//
136//137//
...@@ -143,8 +144,8 @@ pub fn parse(alloc: *std.mem.Allocator, input: []const u8) !Document {...@@ -143,8 +144,8 @@ pub fn parse(alloc: *std.mem.Allocator, input: []const u8) !Document {
143144
144 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);145 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);
145146
146 const all_events = &std.ArrayList(c.yaml_event_t).init(alloc);147 const all_events = &std.ArrayList(Token).init(alloc);
147 var event: c.yaml_event_t = undefined;148 var event: Token = undefined;
148 while (true) {149 while (true) {
149 const p = c.yaml_parser_parse(&parser, &event);150 const p = c.yaml_parser_parse(&parser, &event);
150 if (p == 0) {151 if (p == 0) {
...@@ -183,7 +184,7 @@ pub const Parser = struct {...@@ -183,7 +184,7 @@ pub const Parser = struct {
183 return item.stream;184 return item.stream;
184 }185 }
185186
186 fn next(self: *Parser) ?c.yaml_event_t {187 fn next(self: *Parser) ?Token {
187 if (self.index >= self.tokens.len) {188 if (self.index >= self.tokens.len) {
188 return null;189 return null;
189 }190 }
...@@ -196,7 +197,7 @@ pub const Error =...@@ -196,7 +197,7 @@ pub const Error =
196 std.mem.Allocator.Error ||197 std.mem.Allocator.Error ||
197 error{YamlUnexpectedToken};198 error{YamlUnexpectedToken};
198199
199fn parse_item(p: *Parser, start: ?c.yaml_event_t) Error!Item {200fn parse_item(p: *Parser, start: ?Token) Error!Item {
200 const tok = start orelse p.next();201 const tok = start orelse p.next();
201 return switch (tok.?.type) {202 return switch (tok.?.type) {
202 .YAML_STREAM_START_EVENT => Item{ .stream = try parse_stream(p) },203 .YAML_STREAM_START_EVENT => Item{ .stream = try parse_stream(p) },
...@@ -280,7 +281,7 @@ fn parse_sequence(p: *Parser) Error!Sequence {...@@ -280,7 +281,7 @@ fn parse_sequence(p: *Parser) Error!Sequence {
280 }281 }
281}282}
282283
283fn get_event_string(event: c.yaml_event_t, lines: Array) []const u8 {284fn get_event_string(event: Token, lines: Array) []const u8 {
284 const sm = event.start_mark;285 const sm = event.start_mark;
285 const em = event.end_mark;286 const em = event.end_mark;
286 return lines[sm.line][sm.column..em.column];287 return lines[sm.line][sm.column..em.column];