authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:56:10 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-14 13:56:10 -07:00
logce0478831d26f7bde9c49be4d377b02b5955499a
treecbf6fe054b0885f5448181bd1eba28dc8e440031
parenta40a27e182c022c67370b6af790e201baa4ae908

allow attribute values to accept a Body


2 files changed, 19 insertions(+), 5 deletions(-)

src/astgen.zig+10-3
......@@ -19,12 +19,12 @@ pub const Value = union(enum) {
1919pub const Element = struct {
2020 name: string,
2121 attrs: []const Attr,
22 children: []const Value,
22 children: Body,
2323};
2424
2525pub const Attr = struct {
2626 key: string,
27 value: string,
27 value: union(enum) { string: string, body: Body },
2828};
2929
3030pub const Block = struct {
......@@ -103,10 +103,17 @@ const Parser = struct {
103103 pub fn doAttr(comptime self: *Parser) Attr {
104104 const k = self.eat(.word);
105105 self.eatSymbol("=");
106 const body = self.doChildren();
107 if (body.len > 0) {
108 return Attr{
109 .key = k,
110 .value = .{ .body = body },
111 };
112 }
106113 const v = self.eat(.string);
107114 return Attr{
108115 .key = k,
109 .value = v,
116 .value = .{ .string = v },
110117 };
111118 }
112119
src/lib.zig+9-2
......@@ -34,8 +34,15 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
3434 try writer.writeAll("<");
3535 try writer.writeAll(v.name);
3636
37 for (v.attrs) |it| {
38 try writer.print(" {s}=\"{}\"", .{ it.key, std.zig.fmtEscapes(it.value[1 .. it.value.len - 1]) });
37 inline for (v.attrs) |it| {
38 switch (comptime it.value) {
39 .string => try writer.print(" {s}=\"{}\"", .{ it.key, std.zig.fmtEscapes(it.value.string[1 .. it.value.string.len - 1]) }),
40 .body => {
41 try writer.print(" {s}=\"", .{it.key});
42 try do(alloc, writer, astgen.Value{ .body = it.value.body }, data, ctx, indent, flag1);
43 try writer.print("\"", .{});
44 },
45 }
3946 }
4047
4148 if (v.children.len == 0) {