authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-09-18 01:02:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-09-18 01:02:05 -07:00
logd68545faf237ef3c319d85cfb2dfdf894df61f08
tree523e92bd39732d6e5c0a9016e9fecc5e3d1f01cb
parentd051451e520a8364013bc5ff4d037f493b5a452e

replace vendored yaml parsing with shared package


8 files changed, 16 insertions(+), 322 deletions(-)

deps.zig+8-1
......@@ -48,6 +48,7 @@ pub fn fetch(exe: *std.build.LibExeObjStep) void {
4848 const root = if (@field(package_data, decl.name).store) |_| b.cache_root else ".";
4949 if (path.* != null) path.* = b.fmt("{s}/zigmod/deps{s}", .{ root, path.*.? });
5050 }
51 exe.step.dependOn(&GitExactStep.create(b, "https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc", "0911d65210fe14467fa73afd5b09d781a3995adc").step);
5152 exe.step.dependOn(&GitExactStep.create(b, "https://github.com/MasterQ32/zig-uri", "e879df3a236869f92298fbe2db3c25e6e84cfd4c").step);
5253 exe.step.dependOn(&GitExactStep.create(b, "https://github.com/marlersoft/zigwin32", "209f07cc5861c7bd9c3010a37f32bf6244f9a158").step);
5354 exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/arqv-ini", "1a9b2e90379895e197893b6e19c93bd213ad36e6").step);
......@@ -193,6 +194,12 @@ pub const package_data = struct {
193194 .entry = "/git/github.com/nektro/zig-extras/01fae956e2f17aa992e717e041a3dd457d440b31/src/lib.zig",
194195 .deps = &[_]*Package{ &_tnj3qf44tpeq },
195196 };
197 pub var _g982zq6e8wsv = Package{
198 .store = "/git/gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc/0911d65210fe14467fa73afd5b09d781a3995adc",
199 .name = "yaml",
200 .entry = "/git/gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc/0911d65210fe14467fa73afd5b09d781a3995adc/yaml.zig",
201 .deps = &[_]*Package{ &_f7dubzb7cyqe },
202 };
196203 pub var _c1xirp1ota5p = Package{
197204 .store = "/git/github.com/nektro/zig-inquirer/14c3492c46f9765c3e77436741794d1a3118cbee",
198205 .name = "inquirer",
......@@ -242,7 +249,7 @@ pub const package_data = struct {
242249 pub var _89ujp8gq842x = Package{
243250 .name = "zigmod",
244251 .entry = "/../..//src/lib.zig",
245 .deps = &[_]*Package{ &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm },
252 .deps = &[_]*Package{ &_g982zq6e8wsv, &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm },
246253 };
247254 pub var _root = Package{
248255 };
licenses.txt+3-2
......@@ -1,7 +1,10 @@
11MIT:
22= https://spdx.org/licenses/MIT
33- This
4- git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc
45- git https://github.com/yaml/libyaml
6- git https://github.com/nektro/zig-extras
7- git https://github.com/nektro/zig-range
58- git https://github.com/nektro/zig-ansi
69- git https://github.com/ziglibs/known-folders
710- git https://github.com/nektro/zig-licenses
......@@ -10,8 +13,6 @@ MIT:
1013- git https://github.com/nektro/iguanaTLS
1114- git https://github.com/MasterQ32/zig-uri
1215- git https://github.com/nektro/zig-json
13- git https://github.com/nektro/zig-extras
14- git https://github.com/nektro/zig-range
1516- git https://github.com/nektro/zig-detect-license
1617- git https://github.com/nektro/zig-licenses-text
1718- git https://github.com/nektro/zig-leven
src/util/dep.zig+1-1
......@@ -1,10 +1,10 @@
11const std = @import("std");
22const string = []const u8;
33const builtin = @import("builtin");
4const yaml = @import("yaml");
45
56const zigmod = @import("../lib.zig");
67const u = @import("index.zig");
7const yaml = @import("./yaml.zig");
88
99//
1010//
src/util/modfile.zig+1-1
......@@ -1,9 +1,9 @@
11const std = @import("std");
22const string = []const u8;
3const yaml = @import("yaml");
34
45const zigmod = @import("../lib.zig");
56const u = @import("index.zig");
6const yaml = @import("./yaml.zig");
77
88//
99//
src/util/module.zig+1-1
......@@ -1,10 +1,10 @@
11const std = @import("std");
22const string = []const u8;
33const builtin = @import("builtin");
4const yaml = @import("yaml");
45
56const zigmod = @import("../lib.zig");
67const u = @import("index.zig");
7const yaml = @import("./yaml.zig");
88const common = @import("./../common.zig");
99
1010//
src/util/yaml.zig deleted-295
......@@ -1,295 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3
4const c = @cImport({
5 @cInclude("yaml.h");
6});
7const u = @import("./index.zig");
8
9//
10//
11
12pub const Stream = struct {
13 docs: []const Document,
14};
15
16pub const Document = struct {
17 mapping: Mapping,
18};
19
20pub const Item = union(enum) {
21 event: Token,
22 kv: Key,
23 mapping: Mapping,
24 sequence: Sequence,
25 document: Document,
26 string: string,
27 stream: Stream,
28
29 pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
30 _ = fmt;
31 _ = options;
32
33 try writer.writeAll("Item{");
34 switch (self) {
35 .event => {
36 // try std.fmt.format(writer, "{s}", .{@tagName(self.event.type)});
37 try std.fmt.format(writer, "event {d}", .{self.event});
38 },
39 .kv, .document, .stream => {
40 unreachable;
41 },
42 .mapping => {
43 try std.fmt.format(writer, "{}", .{self.mapping});
44 },
45 .sequence => {
46 try writer.writeAll("[ ");
47 for (self.sequence) |it| {
48 try std.fmt.format(writer, "{}, ", .{it});
49 }
50 try writer.writeAll("]");
51 },
52 .string => {
53 try std.fmt.format(writer, "{s}", .{self.string});
54 },
55 }
56 try writer.writeAll("}");
57 }
58};
59
60pub const Sequence = []const Item;
61
62pub const Key = struct {
63 key: string,
64 value: Value,
65};
66
67pub const Value = union(enum) {
68 string: string,
69 mapping: Mapping,
70 sequence: Sequence,
71
72 pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
73 _ = fmt;
74 _ = options;
75
76 try writer.writeAll("Value{");
77 switch (self) {
78 .string => {
79 try std.fmt.format(writer, "{s}", .{self.string});
80 },
81 .mapping => {
82 try std.fmt.format(writer, "{}", .{self.mapping});
83 },
84 .sequence => {
85 try writer.writeAll("[ ");
86 for (self.sequence) |it| {
87 try std.fmt.format(writer, "{}, ", .{it});
88 }
89 try writer.writeAll("]");
90 },
91 }
92 try writer.writeAll("}");
93 }
94};
95
96pub const Mapping = struct {
97 items: []const Key,
98
99 pub fn get(self: Mapping, k: string) ?Value {
100 for (self.items) |item| {
101 if (std.mem.eql(u8, item.key, k)) {
102 return item.value;
103 }
104 }
105 return null;
106 }
107
108 pub fn get_string(self: Mapping, k: string) string {
109 return if (self.get(k)) |v| v.string else "";
110 }
111
112 pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string {
113 var list = std.ArrayList(string).init(alloc);
114 defer list.deinit();
115 if (self.get(k)) |val| {
116 if (val == .sequence) {
117 for (val.sequence) |item| {
118 if (item != .string) {
119 continue;
120 }
121 try list.append(item.string);
122 }
123 }
124 }
125 return list.toOwnedSlice();
126 }
127
128 pub fn format(self: Mapping, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
129 _ = fmt;
130 _ = options;
131
132 try writer.writeAll("{ ");
133 for (self.items) |it| {
134 try std.fmt.format(writer, "{s}: ", .{it.key});
135 try std.fmt.format(writer, "{}, ", .{it.value});
136 }
137 try writer.writeAll("}");
138 }
139};
140
141pub const Token = c.yaml_event_t;
142pub const TokenList = []const Token;
143
144//
145//
146
147pub fn parse(alloc: std.mem.Allocator, input: string) !Document {
148 var parser: c.yaml_parser_t = undefined;
149 _ = c.yaml_parser_initialize(&parser);
150 defer c.yaml_parser_delete(&parser);
151
152 const lines = try u.split(alloc, input, "\n");
153
154 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);
155
156 var all_events = std.ArrayList(Token).init(alloc);
157 var event: Token = undefined;
158 while (true) {
159 const p = c.yaml_parser_parse(&parser, &event);
160 if (p == 0) {
161 break;
162 }
163
164 const et = event.type;
165 try all_events.append(event);
166 c.yaml_event_delete(&event);
167
168 if (et == c.YAML_STREAM_END_EVENT) {
169 break;
170 }
171 }
172
173 var p = Parser{
174 .alloc = alloc,
175 .tokens = all_events.items,
176 .lines = lines,
177 .index = 0,
178 };
179 const stream = try p.parse();
180 return stream.docs[0];
181}
182
183pub const Parser = struct {
184 alloc: std.mem.Allocator,
185 tokens: TokenList,
186 lines: []const string,
187 index: usize,
188
189 pub fn parse(self: *Parser) !Stream {
190 const item = try parse_item(self, null);
191 return item.stream;
192 }
193
194 fn next(self: *Parser) ?Token {
195 if (self.index >= self.tokens.len) {
196 return null;
197 }
198 defer self.index += 1;
199 return self.tokens[self.index];
200 }
201};
202
203pub const Error =
204 std.mem.Allocator.Error ||
205 error{YamlUnexpectedToken};
206
207fn parse_item(p: *Parser, start: ?Token) Error!Item {
208 const tok = start orelse p.next();
209 return switch (tok.?.type) {
210 c.YAML_STREAM_START_EVENT => Item{ .stream = try parse_stream(p) },
211 c.YAML_DOCUMENT_START_EVENT => Item{ .document = try parse_document(p) },
212 c.YAML_MAPPING_START_EVENT => Item{ .mapping = try parse_mapping(p) },
213 c.YAML_SEQUENCE_START_EVENT => Item{ .sequence = try parse_sequence(p) },
214 c.YAML_SCALAR_EVENT => Item{ .string = get_event_string(tok.?, p.lines) },
215 else => unreachable,
216 };
217}
218
219fn parse_stream(p: *Parser) Error!Stream {
220 var res = std.ArrayList(Document).init(p.alloc);
221 defer res.deinit();
222
223 while (true) {
224 const tok = p.next();
225 if (tok.?.type == c.YAML_STREAM_END_EVENT) {
226 return Stream{ .docs = res.toOwnedSlice() };
227 }
228 if (tok.?.type != c.YAML_DOCUMENT_START_EVENT) {
229 return error.YamlUnexpectedToken;
230 }
231 const item = try parse_item(p, tok);
232 try res.append(item.document);
233 }
234}
235
236fn parse_document(p: *Parser) Error!Document {
237 const tok = p.next();
238 if (tok.?.type != c.YAML_MAPPING_START_EVENT) {
239 return error.YamlUnexpectedToken;
240 }
241 const item = try parse_item(p, tok);
242
243 if (p.next().?.type != c.YAML_DOCUMENT_END_EVENT) {
244 return error.YamlUnexpectedToken;
245 }
246 return Document{ .mapping = item.mapping };
247}
248
249fn parse_mapping(p: *Parser) Error!Mapping {
250 var res = std.ArrayList(Key).init(p.alloc);
251 defer res.deinit();
252
253 while (true) {
254 const tok = p.next();
255 if (tok.?.type == c.YAML_MAPPING_END_EVENT) {
256 return Mapping{ .items = res.toOwnedSlice() };
257 }
258 if (tok.?.type != c.YAML_SCALAR_EVENT) {
259 return error.YamlUnexpectedToken;
260 }
261 try res.append(Key{
262 .key = get_event_string(tok.?, p.lines),
263 .value = try parse_value(p),
264 });
265 }
266}
267
268fn parse_value(p: *Parser) Error!Value {
269 const item = try parse_item(p, null);
270 return switch (item) {
271 .mapping => |x| Value{ .mapping = x },
272 .sequence => |x| Value{ .sequence = x },
273 .string => |x| Value{ .string = x },
274 else => unreachable,
275 };
276}
277
278fn parse_sequence(p: *Parser) Error!Sequence {
279 var res = std.ArrayList(Item).init(p.alloc);
280 defer res.deinit();
281
282 while (true) {
283 const tok = p.next();
284 if (tok.?.type == c.YAML_SEQUENCE_END_EVENT) {
285 return res.toOwnedSlice();
286 }
287 try res.append(try parse_item(p, tok));
288 }
289}
290
291fn get_event_string(event: Token, lines: []const string) string {
292 const sm = event.start_mark;
293 const em = event.end_mark;
294 return lines[sm.line][sm.column..em.column];
295}
zig.mod+1-21
......@@ -5,27 +5,7 @@ license: MIT
55description: A package manager for the Zig programming language.
66min_zig_version: 0.10.0-dev.3998+c25ce5bba
77dependencies:
8 - src: git https://github.com/yaml/libyaml tag-0.2.5
9 id: 8mdbh0zuneb0i3hs5jby5je0heem1i6yxusl7c8y8qx68hqc
10 license: MIT
11 c_include_dirs:
12 - include
13 c_source_flags:
14 - -DYAML_VERSION_MAJOR=0
15 - -DYAML_VERSION_MINOR=2
16 - -DYAML_VERSION_PATCH=5
17 - -DYAML_VERSION_STRING="0.2.5"
18 - -DYAML_DECLARE_STATIC=1
19 c_source_files:
20 - src/api.c
21 - src/dumper.c
22 - src/emitter.c
23 - src/loader.c
24 - src/parser.c
25 - src/reader.c
26 - src/scanner.c
27 - src/writer.c
28
8 - src: git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc # yaml
299 - src: git https://github.com/nektro/zig-ansi
3010 - src: git https://github.com/ziglibs/known-folders
3111 - src: git https://github.com/nektro/zig-licenses
zigmod.lock+1
......@@ -1,4 +1,5 @@
112
2git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc commit-0911d65210fe14467fa73afd5b09d781a3995adc
23git https://github.com/MasterQ32/zig-uri commit-e879df3a236869f92298fbe2db3c25e6e84cfd4c
34git https://github.com/marlersoft/zigwin32 commit-209f07cc5861c7bd9c3010a37f32bf6244f9a158
45git https://github.com/nektro/arqv-ini commit-1a9b2e90379895e197893b6e19c93bd213ad36e6