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) {...@@ -19,12 +19,12 @@ pub const Value = union(enum) {
19pub const Element = struct {19pub const Element = struct {
20 name: string,20 name: string,
21 attrs: []const Attr,21 attrs: []const Attr,
22 children: []const Value,22 children: Body,
23};23};
2424
25pub const Attr = struct {25pub const Attr = struct {
26 key: string,26 key: string,
27 value: string,27 value: union(enum) { string: string, body: Body },
28};28};
2929
30pub const Block = struct {30pub const Block = struct {
...@@ -103,10 +103,17 @@ const Parser = struct {...@@ -103,10 +103,17 @@ const Parser = struct {
103 pub fn doAttr(comptime self: *Parser) Attr {103 pub fn doAttr(comptime self: *Parser) Attr {
104 const k = self.eat(.word);104 const k = self.eat(.word);
105 self.eatSymbol("=");105 self.eatSymbol("=");
106 const body = self.doChildren();
107 if (body.len > 0) {
108 return Attr{
109 .key = k,
110 .value = .{ .body = body },
111 };
112 }
106 const v = self.eat(.string);113 const v = self.eat(.string);
107 return Attr{114 return Attr{
108 .key = k,115 .key = k,
109 .value = v,116 .value = .{ .string = v },
110 };117 };
111 }118 }
112119
src/lib.zig+9-2
...@@ -34,8 +34,15 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype...@@ -34,8 +34,15 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
34 try writer.writeAll("<");34 try writer.writeAll("<");
35 try writer.writeAll(v.name);35 try writer.writeAll(v.name);
3636
37 for (v.attrs) |it| {37 inline for (v.attrs) |it| {
38 try writer.print(" {s}=\"{}\"", .{ it.key, std.zig.fmtEscapes(it.value[1 .. it.value.len - 1]) });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 }
39 }46 }
4047
41 if (v.children.len == 0) {48 if (v.children.len == 0) {