authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-15 11:30:24 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-15 11:30:24 -07:00
log7bcf9a17e412c0a50776b6e3c7a9f05ada377078
tree3e3f45a3197771a4518d85f434f1414b8fe5e317
parentb70ad0161e9cad2176fe26d9a39a20eb8391b102

put DoOptions into a struct


1 files changed, 36 insertions(+), 25 deletions(-)

src/lib.zig+36-25
......@@ -22,13 +22,16 @@ pub fn parse(comptime input: string) astgen.Value {
2222
2323pub fn compile(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype) !void {
2424 try writer.writeAll("<!DOCTYPE html>\n");
25 try do(Ctx, alloc, writer, value, data, data, 0, false);
25 try do(Ctx, alloc, writer, value, data, data, .{
26 .indent = 0,
27 .flag1 = false,
28 });
2629 try writer.writeAll("\n");
2730}
2831
2932pub const Writer = std.ArrayList(u8).Writer;
3033
31inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
34inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, opts: DoOptions) anyerror!void {
3235 switch (comptime value) {
3336 .element => |v| {
3437 const hastext = for (v.children) |x| {
......@@ -38,7 +41,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
3841 }
3942 } else false;
4043
41 if (flag1) for (range(indent)) |_| try writer.writeAll(" ");
44 if (opts.flag1) for (range(opts.indent)) |_| try writer.writeAll(" ");
4245 try writer.writeAll("<");
4346 try writer.writeAll(v.name);
4447
......@@ -47,7 +50,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
4750 .string => try writer.print(" {s}=\"{}\"", .{ it.key, std.zig.fmtEscapes(it.value.string[1 .. it.value.string.len - 1]) }),
4851 .body => {
4952 try writer.print(" {s}=\"", .{it.key});
50 try do(Ctx, alloc, writer, astgen.Value{ .body = it.value.body }, data, ctx, indent, flag1);
53 try do(Ctx, alloc, writer, astgen.Value{ .body = it.value.body }, data, ctx, opts);
5154 try writer.print("\"", .{});
5255 },
5356 }
......@@ -64,11 +67,14 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
6467
6568 if (!hastext) try writer.writeAll("\n");
6669 inline for (v.children) |it| {
67 try do(Ctx, alloc, writer, it, data, ctx, indent + 1, !hastext);
70 try do(Ctx, alloc, writer, it, data, ctx, .{
71 .indent = opts.indent + 1,
72 .flag1 = !hastext,
73 });
6874 }
69 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");
75 if (!hastext) for (range(opts.indent)) |_| try writer.writeAll(" ");
7076 try writer.print("</{s}>", .{v.name});
71 if (flag1) try writer.writeAll("\n");
77 if (opts.flag1) try writer.writeAll("\n");
7278 }
7379 },
7480 .string => |v| {
......@@ -111,25 +117,25 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
111117 switch (v.name) {
112118 .each => {
113119 comptime assertEqual(v.args.len, 1);
114 for (x) |item| try do(Ctx, alloc, writer, body, item, ctx, indent, flag1);
120 for (x) |item| try do(Ctx, alloc, writer, body, item, ctx, opts);
115121 },
116122 .@"if" => {
117123 comptime assertEqual(v.args.len, 1);
118124 if (comptime std.meta.trait.isIndexable(T)) {
119 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x.len > 0);
125 try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, x.len > 0);
120126 return;
121127 }
122128 switch (comptime TI) {
123 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x),
124 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x),
129 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, x),
130 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, opts, x),
125131 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #if block", .{@typeName(T)})),
126132 }
127133 },
128134 .ifnot => {
129135 comptime assertEqual(v.args.len, 1);
130136 switch (comptime TI) {
131 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
132 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
137 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, !x),
138 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, opts, !x),
133139 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #ifnot block", .{@typeName(T)})),
134140 }
135141 },
......@@ -137,23 +143,23 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
137143 comptime assertEqual(v.args.len, 2);
138144 const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx);
139145 if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) {
140 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
146 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, std.mem.eql(u8, @tagName(x), y));
141147 }
142 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x == y);
148 try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, x == y);
143149 },
144150 .ifnotequal => {
145151 comptime assertEqual(v.args.len, 2);
146152 const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx);
147153 if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) {
148 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
154 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, std.mem.eql(u8, @tagName(x), y));
149155 }
150 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x != y);
156 try doif(Ctx, alloc, writer, body, bottom, data, ctx, opts, x != y);
151157 },
152158 }
153159 },
154160 .body => |v| {
155161 inline for (v) |val| {
156 try do(Ctx, alloc, writer, val, data, ctx, indent, flag1);
162 try do(Ctx, alloc, writer, val, data, ctx, opts);
157163 }
158164 },
159165 .function => |v| {
......@@ -173,7 +179,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
173179 }
174180 const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"}, .raw = v.raw } };
175181 try @call(.auto, func, args);
176 try do(Ctx, alloc, writer, repvalue, try list.toOwnedSlice(), ctx, indent, flag1);
182 try do(Ctx, alloc, writer, repvalue, try list.toOwnedSlice(), ctx, opts);
177183 return;
178184 }
179185 @compileError("pek: unknown custom function: " ++ v.name);
......@@ -182,6 +188,11 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
182188 }
183189}
184190
191pub const DoOptions = struct {
192 indent: usize,
193 flag1: bool,
194};
195
185196fn search(comptime args: []const string, ctx: anytype) FieldSearch(@TypeOf(ctx), args) {
186197 if (args.len == 0) return ctx;
187198 if (args[0][0] == '"') return std.mem.trim(u8, args[0], "\"");
......@@ -289,18 +300,18 @@ fn contains(haystack: []const string, needle: string) bool {
289300 return false;
290301}
291302
292fn doif(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool) anyerror!void {
303fn doif(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, opts: DoOptions, flag2: bool) anyerror!void {
293304 if (flag2) {
294 try do(Ctx, alloc, writer, top, data, ctx, indent, flag1);
305 try do(Ctx, alloc, writer, top, data, ctx, opts);
295306 } else {
296 try do(Ctx, alloc, writer, bottom, data, ctx, indent, flag1);
307 try do(Ctx, alloc, writer, bottom, data, ctx, opts);
297308 }
298309}
299310
300fn docap(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: anytype) anyerror!void {
311fn docap(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, opts: DoOptions, flag2: anytype) anyerror!void {
301312 if (flag2) |_| {
302 try do(Ctx, alloc, writer, top, data, ctx, indent, flag1);
313 try do(Ctx, alloc, writer, top, data, ctx, opts);
303314 } else {
304 try do(Ctx, alloc, writer, bottom, data, ctx, indent, flag1);
315 try do(Ctx, alloc, writer, bottom, data, ctx, opts);
305316 }
306317}