From 6d2c08878ddc04f8ed306f97795b47d7294684ee Mon Sep 17 00:00:00 2001 From: torque Date: Sun, 22 Oct 2023 15:36:34 -0700 Subject: [PATCH] examples: add parsing to an object example --- build.zig | 1 + examples/reify.zig | 88 ++++++++++++++++++++++++++++++++++++++++++++ src/nice.zig | 1 + src/parser/value.zig | 12 +++--- 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 examples/reify.zig diff --git a/build.zig b/build.zig index bc8afcf..78beac1 100644 --- a/build.zig +++ b/build.zig @@ -26,6 +26,7 @@ const Example = struct { const examples = [_]Example{ .{ .name = "parse", .file = "examples/parse.zig" }, .{ .name = "stream", .file = "examples/stream.zig" }, + .{ .name = "reify", .file = "examples/reify.zig" }, }; pub fn add_examples(b: *std.build, options: ExampleOptions) void { diff --git a/examples/reify.zig b/examples/reify.zig new file mode 100644 index 0000000..7f46c2b --- /dev/null +++ b/examples/reify.zig @@ -0,0 +1,88 @@ +const std = @import("std"); + +const nice = @import("nice"); + +const Example = struct { + useful: bool, + number: i32, + string: []const u8, + longstring: []const u8, + tuple: struct { bool, i8 }, + enume: enum { first, second, third }, + taggart: union(enum) { first: []const u8, second: i32 }, + exist: ?bool, + again: ?bool, + array: [5]i16, + nested: [3]struct { index: usize, title: []const u8 }, +}; + +const source = + \\useful: true + \\number: 0x9001 + \\string: > salutations, earthen oblate spheroid + \\ + \\longstring: + \\ | If, at first, you don't think this string has + \\ + multiple lines, then perhaps you are the one who is + \\ # yeah, let's add a newline here + \\ > wrong. + \\ # and a trailing newline for good measure + \\ > + \\tuple: [ no, 127 ] + \\enume: third + \\taggart: + \\ first: string a thing + \\list: + \\ - I am a list item + \\exist: null + \\again: true + \\array: [ 1, 2, 3, 4, 5 ] + \\nested: + \\ - { index: 1, title: none } + \\ - { index: 2, title: such } + \\ - { index: 3, title: luck } + \\ +; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + var diagnostics = nice.Diagnostics{}; + var loaded = nice.parseBufferTo(Example, allocator, source, &diagnostics, .{}) catch |err| { + std.debug.print("row:{d} col:{d}: {s}\n", .{ + diagnostics.row, + diagnostics.line_offset, + diagnostics.message, + }); + return err; + }; + defer loaded.deinit(); + + std.debug.print("{s} {{\n", .{@typeName(Example)}); + std.debug.print(" useful: {}\n", .{loaded.value.useful}); + std.debug.print(" number: {d}\n", .{loaded.value.number}); + std.debug.print(" string: {s}\n", .{loaded.value.string}); + std.debug.print(" longstring: {s}\n", .{loaded.value.longstring}); + std.debug.print(" tuple: {{ {}, {d} }}\n", .{ loaded.value.tuple[0], loaded.value.tuple[1] }); + std.debug.print(" enume: {s}\n", .{@tagName(loaded.value.enume)}); + std.debug.print(" taggart: ", .{}); + switch (loaded.value.taggart) { + .first => |val| std.debug.print(".first = {s}\n", .{val}), + .second => |val| std.debug.print(".second = {d}\n", .{val}), + } + std.debug.print(" exist: {?}\n", .{loaded.value.exist}); + std.debug.print(" again: {?}\n", .{loaded.value.again}); + std.debug.print(" array: [ ", .{}); + for (loaded.value.array) |item| { + std.debug.print("{d}, ", .{item}); + } + std.debug.print("]\n", .{}); + std.debug.print(" nested: [\n", .{}); + for (loaded.value.nested) |item| { + std.debug.print(" {{ index: {d}, title: {s} }}\n", .{ item.index, item.title }); + } + std.debug.print(" ]\n", .{}); + std.debug.print("}}\n", .{}); +} diff --git a/src/nice.zig b/src/nice.zig index 44124f8..b0fd84f 100644 --- a/src/nice.zig +++ b/src/nice.zig @@ -65,6 +65,7 @@ pub const buffers = @import("./linebuffer.zig"); pub const tokenizer = @import("./tokenizer.zig"); pub const parser = @import("./parser.zig"); pub const parseBuffer = parser.parseBuffer; +pub const parseBufferTo = parser.parseBufferTo; pub const StreamParser = parser.StreamParser; pub const Document = parser.Document; pub const Value = parser.Value; diff --git a/src/parser/value.zig b/src/parser/value.zig index ecafd21..692594a 100644 --- a/src/parser/value.zig +++ b/src/parser/value.zig @@ -67,14 +67,14 @@ pub const Value = union(enum) { inline .scalar, .string => |str, tag| { if (tag == .string and !options.coerce_strings) return error.BadValue; if (options.case_insensitive_scalar_coersion) { - for (options.boolean_strings.truthy) |check| + for (options.boolean_scalars.truthy) |check| if (std.ascii.eqlIgnoreCase(str, check)) return true; - for (options.boolean_strings.falsy) |check| + for (options.boolean_scalars.falsy) |check| if (std.ascii.eqlIgnoreCase(str, check)) return false; } else { - for (options.boolean_strings.truthy) |check| + for (options.boolean_scalars.truthy) |check| if (std.mem.eql(u8, str, check)) return true; - for (options.boolean_strings.falsy) |check| + for (options.boolean_scalars.falsy) |check| if (std.mem.eql(u8, str, check)) return false; } @@ -260,10 +260,10 @@ pub const Value = union(enum) { inline .scalar, .string => |str, tag| { if (tag == .string and !options.coerce_strings) return error.BadValue; if (options.case_insensitive_scalar_coersion) { - for (options.null_strings) |check| + for (options.null_scalars) |check| if (std.ascii.eqlIgnoreCase(str, check)) return null; } else { - for (options.null_strings) |check| + for (options.null_scalars) |check| if (std.mem.eql(u8, str, check)) return null; }