From d68545faf237ef3c319d85cfb2dfdf894df61f08 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 18 Sep 2022 01:02:05 -0700 Subject: [PATCH] replace vendored yaml parsing with shared package --- deps.zig | 9 +- licenses.txt | 5 +- src/util/dep.zig | 2 +- src/util/modfile.zig | 2 +- src/util/module.zig | 2 +- src/util/yaml.zig | 295 ------------------------------------------- zig.mod | 22 +--- zigmod.lock | 1 + 8 files changed, 16 insertions(+), 322 deletions(-) delete mode 100644 src/util/yaml.zig diff --git a/deps.zig b/deps.zig index 1afa39c46d3e9cb636187d4fecef7e44df8280c4..7051f080f011c7a63dbe5570e36e4de8f6c4a74c 100644 --- a/deps.zig +++ b/deps.zig @@ -48,6 +48,7 @@ pub fn fetch(exe: *std.build.LibExeObjStep) void { const root = if (@field(package_data, decl.name).store) |_| b.cache_root else "."; if (path.* != null) path.* = b.fmt("{s}/zigmod/deps{s}", .{ root, path.*.? }); } + exe.step.dependOn(&GitExactStep.create(b, "https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc", "0911d65210fe14467fa73afd5b09d781a3995adc").step); exe.step.dependOn(&GitExactStep.create(b, "https://github.com/MasterQ32/zig-uri", "e879df3a236869f92298fbe2db3c25e6e84cfd4c").step); exe.step.dependOn(&GitExactStep.create(b, "https://github.com/marlersoft/zigwin32", "209f07cc5861c7bd9c3010a37f32bf6244f9a158").step); exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/arqv-ini", "1a9b2e90379895e197893b6e19c93bd213ad36e6").step); @@ -193,6 +194,12 @@ pub const package_data = struct { .entry = "/git/github.com/nektro/zig-extras/01fae956e2f17aa992e717e041a3dd457d440b31/src/lib.zig", .deps = &[_]*Package{ &_tnj3qf44tpeq }, }; + pub var _g982zq6e8wsv = Package{ + .store = "/git/gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc/0911d65210fe14467fa73afd5b09d781a3995adc", + .name = "yaml", + .entry = "/git/gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc/0911d65210fe14467fa73afd5b09d781a3995adc/yaml.zig", + .deps = &[_]*Package{ &_f7dubzb7cyqe }, + }; pub var _c1xirp1ota5p = Package{ .store = "/git/github.com/nektro/zig-inquirer/14c3492c46f9765c3e77436741794d1a3118cbee", .name = "inquirer", @@ -242,7 +249,7 @@ pub const package_data = struct { pub var _89ujp8gq842x = Package{ .name = "zigmod", .entry = "/../..//src/lib.zig", - .deps = &[_]*Package{ &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm }, + .deps = &[_]*Package{ &_g982zq6e8wsv, &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm }, }; pub var _root = Package{ }; diff --git a/licenses.txt b/licenses.txt index ed97675085372bb2d9b7b3dfb1db4af2eb36a4c5..b03f7e14d01ba3af4d149745066438c999d25409 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,7 +1,10 @@ MIT: = https://spdx.org/licenses/MIT - This +- git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc - git https://github.com/yaml/libyaml +- git https://github.com/nektro/zig-extras +- git https://github.com/nektro/zig-range - git https://github.com/nektro/zig-ansi - git https://github.com/ziglibs/known-folders - git https://github.com/nektro/zig-licenses @@ -10,8 +13,6 @@ MIT: - git https://github.com/nektro/iguanaTLS - git https://github.com/MasterQ32/zig-uri - git https://github.com/nektro/zig-json -- git https://github.com/nektro/zig-extras -- git https://github.com/nektro/zig-range - git https://github.com/nektro/zig-detect-license - git https://github.com/nektro/zig-licenses-text - git https://github.com/nektro/zig-leven diff --git a/src/util/dep.zig b/src/util/dep.zig index 10785fb15b894f70aa94c5a4d461bebd6b4b22b1..e4417e8c9747065d0496cca2e11485247a0ea304 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -1,10 +1,10 @@ const std = @import("std"); const string = []const u8; const builtin = @import("builtin"); +const yaml = @import("yaml"); const zigmod = @import("../lib.zig"); const u = @import("index.zig"); -const yaml = @import("./yaml.zig"); // // diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 8b919318265f221a882b4fdbdf68c0eee8152c02..1039e5ee759c769bb1fecec740afc10bd2f27323 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -1,9 +1,9 @@ const std = @import("std"); const string = []const u8; +const yaml = @import("yaml"); const zigmod = @import("../lib.zig"); const u = @import("index.zig"); -const yaml = @import("./yaml.zig"); // // diff --git a/src/util/module.zig b/src/util/module.zig index e4736183f27fb3e9ad978288246a0bdc14aa9810..9113ea7263b46af573b5dec0fb86540c731b0a77 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -1,10 +1,10 @@ const std = @import("std"); const string = []const u8; const builtin = @import("builtin"); +const yaml = @import("yaml"); const zigmod = @import("../lib.zig"); const u = @import("index.zig"); -const yaml = @import("./yaml.zig"); const common = @import("./../common.zig"); // diff --git a/src/util/yaml.zig b/src/util/yaml.zig deleted file mode 100644 index cf94bc8dd301362dd287e2c73adb100f7a60e9dd..0000000000000000000000000000000000000000 --- a/src/util/yaml.zig +++ /dev/null @@ -1,295 +0,0 @@ -const std = @import("std"); -const string = []const u8; - -const c = @cImport({ - @cInclude("yaml.h"); -}); -const u = @import("./index.zig"); - -// -// - -pub const Stream = struct { - docs: []const Document, -}; - -pub const Document = struct { - mapping: Mapping, -}; - -pub const Item = union(enum) { - event: Token, - kv: Key, - mapping: Mapping, - sequence: Sequence, - document: Document, - string: string, - stream: Stream, - - pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { - _ = fmt; - _ = options; - - try writer.writeAll("Item{"); - switch (self) { - .event => { - // try std.fmt.format(writer, "{s}", .{@tagName(self.event.type)}); - try std.fmt.format(writer, "event {d}", .{self.event}); - }, - .kv, .document, .stream => { - unreachable; - }, - .mapping => { - try std.fmt.format(writer, "{}", .{self.mapping}); - }, - .sequence => { - try writer.writeAll("[ "); - for (self.sequence) |it| { - try std.fmt.format(writer, "{}, ", .{it}); - } - try writer.writeAll("]"); - }, - .string => { - try std.fmt.format(writer, "{s}", .{self.string}); - }, - } - try writer.writeAll("}"); - } -}; - -pub const Sequence = []const Item; - -pub const Key = struct { - key: string, - value: Value, -}; - -pub const Value = union(enum) { - string: string, - mapping: Mapping, - sequence: Sequence, - - pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { - _ = fmt; - _ = options; - - try writer.writeAll("Value{"); - switch (self) { - .string => { - try std.fmt.format(writer, "{s}", .{self.string}); - }, - .mapping => { - try std.fmt.format(writer, "{}", .{self.mapping}); - }, - .sequence => { - try writer.writeAll("[ "); - for (self.sequence) |it| { - try std.fmt.format(writer, "{}, ", .{it}); - } - try writer.writeAll("]"); - }, - } - try writer.writeAll("}"); - } -}; - -pub const Mapping = struct { - items: []const Key, - - pub fn get(self: Mapping, k: string) ?Value { - for (self.items) |item| { - if (std.mem.eql(u8, item.key, k)) { - return item.value; - } - } - return null; - } - - pub fn get_string(self: Mapping, k: string) string { - return if (self.get(k)) |v| v.string else ""; - } - - pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string { - var list = std.ArrayList(string).init(alloc); - defer list.deinit(); - if (self.get(k)) |val| { - if (val == .sequence) { - for (val.sequence) |item| { - if (item != .string) { - continue; - } - try list.append(item.string); - } - } - } - return list.toOwnedSlice(); - } - - pub fn format(self: Mapping, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { - _ = fmt; - _ = options; - - try writer.writeAll("{ "); - for (self.items) |it| { - try std.fmt.format(writer, "{s}: ", .{it.key}); - try std.fmt.format(writer, "{}, ", .{it.value}); - } - try writer.writeAll("}"); - } -}; - -pub const Token = c.yaml_event_t; -pub const TokenList = []const Token; - -// -// - -pub fn parse(alloc: std.mem.Allocator, input: string) !Document { - var parser: c.yaml_parser_t = undefined; - _ = c.yaml_parser_initialize(&parser); - defer c.yaml_parser_delete(&parser); - - const lines = try u.split(alloc, input, "\n"); - - _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len); - - var all_events = std.ArrayList(Token).init(alloc); - var event: Token = undefined; - while (true) { - const p = c.yaml_parser_parse(&parser, &event); - if (p == 0) { - break; - } - - const et = event.type; - try all_events.append(event); - c.yaml_event_delete(&event); - - if (et == c.YAML_STREAM_END_EVENT) { - break; - } - } - - var p = Parser{ - .alloc = alloc, - .tokens = all_events.items, - .lines = lines, - .index = 0, - }; - const stream = try p.parse(); - return stream.docs[0]; -} - -pub const Parser = struct { - alloc: std.mem.Allocator, - tokens: TokenList, - lines: []const string, - index: usize, - - pub fn parse(self: *Parser) !Stream { - const item = try parse_item(self, null); - return item.stream; - } - - fn next(self: *Parser) ?Token { - if (self.index >= self.tokens.len) { - return null; - } - defer self.index += 1; - return self.tokens[self.index]; - } -}; - -pub const Error = - std.mem.Allocator.Error || - error{YamlUnexpectedToken}; - -fn parse_item(p: *Parser, start: ?Token) Error!Item { - const tok = start orelse p.next(); - return switch (tok.?.type) { - c.YAML_STREAM_START_EVENT => Item{ .stream = try parse_stream(p) }, - c.YAML_DOCUMENT_START_EVENT => Item{ .document = try parse_document(p) }, - c.YAML_MAPPING_START_EVENT => Item{ .mapping = try parse_mapping(p) }, - c.YAML_SEQUENCE_START_EVENT => Item{ .sequence = try parse_sequence(p) }, - c.YAML_SCALAR_EVENT => Item{ .string = get_event_string(tok.?, p.lines) }, - else => unreachable, - }; -} - -fn parse_stream(p: *Parser) Error!Stream { - var res = std.ArrayList(Document).init(p.alloc); - defer res.deinit(); - - while (true) { - const tok = p.next(); - if (tok.?.type == c.YAML_STREAM_END_EVENT) { - return Stream{ .docs = res.toOwnedSlice() }; - } - if (tok.?.type != c.YAML_DOCUMENT_START_EVENT) { - return error.YamlUnexpectedToken; - } - const item = try parse_item(p, tok); - try res.append(item.document); - } -} - -fn parse_document(p: *Parser) Error!Document { - const tok = p.next(); - if (tok.?.type != c.YAML_MAPPING_START_EVENT) { - return error.YamlUnexpectedToken; - } - const item = try parse_item(p, tok); - - if (p.next().?.type != c.YAML_DOCUMENT_END_EVENT) { - return error.YamlUnexpectedToken; - } - return Document{ .mapping = item.mapping }; -} - -fn parse_mapping(p: *Parser) Error!Mapping { - var res = std.ArrayList(Key).init(p.alloc); - defer res.deinit(); - - while (true) { - const tok = p.next(); - if (tok.?.type == c.YAML_MAPPING_END_EVENT) { - return Mapping{ .items = res.toOwnedSlice() }; - } - if (tok.?.type != c.YAML_SCALAR_EVENT) { - return error.YamlUnexpectedToken; - } - try res.append(Key{ - .key = get_event_string(tok.?, p.lines), - .value = try parse_value(p), - }); - } -} - -fn parse_value(p: *Parser) Error!Value { - const item = try parse_item(p, null); - return switch (item) { - .mapping => |x| Value{ .mapping = x }, - .sequence => |x| Value{ .sequence = x }, - .string => |x| Value{ .string = x }, - else => unreachable, - }; -} - -fn parse_sequence(p: *Parser) Error!Sequence { - var res = std.ArrayList(Item).init(p.alloc); - defer res.deinit(); - - while (true) { - const tok = p.next(); - if (tok.?.type == c.YAML_SEQUENCE_END_EVENT) { - return res.toOwnedSlice(); - } - try res.append(try parse_item(p, tok)); - } -} - -fn get_event_string(event: Token, lines: []const string) string { - const sm = event.start_mark; - const em = event.end_mark; - return lines[sm.line][sm.column..em.column]; -} diff --git a/zig.mod b/zig.mod index 47e4e87b8fe14a7b5e640ae75dfcd7ed4687dd9a..fbadcc777fc35e54709775dfdb57b9c030622696 100644 --- a/zig.mod +++ b/zig.mod @@ -5,27 +5,7 @@ license: MIT description: A package manager for the Zig programming language. min_zig_version: 0.10.0-dev.3998+c25ce5bba dependencies: - - src: git https://github.com/yaml/libyaml tag-0.2.5 - id: 8mdbh0zuneb0i3hs5jby5je0heem1i6yxusl7c8y8qx68hqc - license: MIT - c_include_dirs: - - include - c_source_flags: - - -DYAML_VERSION_MAJOR=0 - - -DYAML_VERSION_MINOR=2 - - -DYAML_VERSION_PATCH=5 - - -DYAML_VERSION_STRING="0.2.5" - - -DYAML_DECLARE_STATIC=1 - c_source_files: - - src/api.c - - src/dumper.c - - src/emitter.c - - src/loader.c - - src/parser.c - - src/reader.c - - src/scanner.c - - src/writer.c - + - src: git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc # yaml - src: git https://github.com/nektro/zig-ansi - src: git https://github.com/ziglibs/known-folders - src: git https://github.com/nektro/zig-licenses diff --git a/zigmod.lock b/zigmod.lock index 74b806c363eee5112302a6f94c8d603cb5e8d01a..4fceaab5fca18c84e4a3d7104bc19e6885e45bd3 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,4 +1,5 @@ 2 +git https://gist.github.com/nektro/d468fea84f8217e4c26ee8fbeeea38cc commit-0911d65210fe14467fa73afd5b09d781a3995adc git https://github.com/MasterQ32/zig-uri commit-e879df3a236869f92298fbe2db3c25e6e84cfd4c git https://github.com/marlersoft/zigwin32 commit-209f07cc5861c7bd9c3010a37f32bf6244f9a158 git https://github.com/nektro/arqv-ini commit-1a9b2e90379895e197893b6e19c93bd213ad36e6 -- 2.54.0