examples: add parsing to an object example
This commit is contained in:
parent
cca7d61666
commit
6d2c08878d
@ -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 {
|
||||
|
88
examples/reify.zig
Normal file
88
examples/reify.zig
Normal file
@ -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", .{});
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user