Compare commits

...

6 Commits

Author SHA1 Message Date
f431f7aa1c build: update for zig-0.13 2024-06-18 12:48:32 -07:00
a9f2298132 fix useless var
Unlike lists, this isn't mutated for maps because there's no
intermediate builder object being used for maps (lists are assembled
with an ArrayList, after which they are converted to plain slices).
2024-05-12 18:49:22 -07:00
2b643c5221 build: update for zig-0.12.0 2024-05-12 18:45:27 -07:00
27667032e1 make naming more consistent 2024-03-08 23:46:43 -08:00
259c22999c example: output JSON
this works a lot better than the very bad dump function I had before.
The output isn't pretty printed but can easily be made so by piping it
to jq, for example.
2024-03-08 10:50:21 -08:00
ab0202a5f7 yaml.Value: add basic JSON emitting
This is quite useful for checking yaml document structure.
2024-03-08 10:49:29 -08:00
4 changed files with 27 additions and 34 deletions

View File

@@ -8,11 +8,9 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const yaml_zig = b.addModule("libyaml", .{ const yaml_zig = b.addModule("zaye", .{
.source_file = .{ .path = "src/yaml.zig" }, .root_source_file = b.path("src/zaye.zig"),
}); });
// yaml_zig.addIncludePath(.{ .path = b.getInstallPath(.header, "") });
// _ = yaml_zig;
const libyaml = libyaml_build.libyamlLib(b, .{ const libyaml = libyaml_build.libyamlLib(b, .{
.name = "libyaml", .name = "libyaml",
@@ -20,16 +18,17 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
yaml_zig.linkLibrary(libyaml);
const example_step = b.step("example", "build example"); const example_step = b.step("example", "build example");
const ex_exe = b.addExecutable(.{ const ex_exe = b.addExecutable(.{
.name = "yamltest", .name = "yamltest",
.root_source_file = .{ .path = "example/main.zig" }, .root_source_file = b.path("example/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
ex_exe.linkLibrary(libyaml); ex_exe.root_module.addImport("yaml", yaml_zig);
ex_exe.addModule("yaml", yaml_zig);
const install = b.addInstallArtifact(ex_exe, .{}); const install = b.addInstallArtifact(ex_exe, .{});
example_step.dependOn(&install.step); example_step.dependOn(&install.step);

View File

@@ -27,27 +27,5 @@ pub fn main() !void {
}; };
defer doc.deinit(); defer doc.deinit();
std.debug.print("\n-----\n\n", .{}); try std.json.stringify(doc.root, .{}, std.io.getStdOut().writer());
dump(doc.root);
}
fn dump(val: yaml.Value) void {
switch (val) {
.scalar => |str| std.debug.print("scalar: {s}\n", .{str}),
.list => |list| {
std.debug.print("list: \n", .{});
for (list) |item| dump(item);
std.debug.print("end list\n", .{});
},
.map => |map| {
std.debug.print("map: \n", .{});
var iter = map.iterator();
while (iter.next()) |entry| {
std.debug.print("key: {s}\n", .{entry.key_ptr.*});
dump(entry.value_ptr.*);
}
std.debug.print("end map\n", .{});
},
}
} }

View File

@@ -5,7 +5,7 @@ const std = @import("std");
const LibyamlOptions = struct { const LibyamlOptions = struct {
name: []const u8, name: []const u8,
target: std.zig.CrossTarget, target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode, optimize: std.builtin.OptimizeMode,
}; };
@@ -22,8 +22,8 @@ pub fn libyamlLib(
const cflags = [_][]const u8{}; const cflags = [_][]const u8{};
lib.linkLibC(); lib.linkLibC();
lib.addIncludePath(.{ .path = include_prefix }); lib.addIncludePath(b.path(include_prefix));
lib.addCSourceFiles(&sources, &cflags); lib.addCSourceFiles(.{ .files = &sources, .flags = &cflags });
lib.defineCMacro("YAML_VERSION_MAJOR", "0"); lib.defineCMacro("YAML_VERSION_MAJOR", "0");
lib.defineCMacro("YAML_VERSION_MINOR", "2"); lib.defineCMacro("YAML_VERSION_MINOR", "2");
lib.defineCMacro("YAML_VERSION_PATCH", "5"); lib.defineCMacro("YAML_VERSION_PATCH", "5");

View File

@@ -266,7 +266,7 @@ pub const Value = union(enum) {
} }
fn endMap(self: *Builder, diag: *ParseDiagnostic) !void { fn endMap(self: *Builder, diag: *ParseDiagnostic) !void {
var top = self.container_stack.pop(); const top = self.container_stack.pop();
if (top != .map) { if (top != .map) {
diag.setMessage("map ended when a map was not the top container"); diag.setMessage("map ended when a map was not the top container");
@@ -297,4 +297,20 @@ pub const Value = union(enum) {
} }
} }
}; };
pub fn jsonStringify(value: @This(), jws: anytype) !void {
switch (value) {
.scalar => |scalar| try jws.write(scalar),
.list => |list| try jws.write(list),
.map => |map| {
try jws.beginObject();
var it = map.iterator();
while (it.next()) |entry| {
try jws.objectField(entry.key_ptr.*);
try jws.write(entry.value_ptr.*);
}
try jws.endObject();
},
}
}
}; };