authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-24 20:46:10 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-24 20:46:10 -08:00
logd387b9f94b384254a1979e74f42554287a7545a2
treec72b7999af33f84c0f3bb8c63b96d3a2ab26e0d4
parentc771db2448322a7d51ebe41cc4d48454f62c475c

add a context param to allow consumer to control where functions are sourced from


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

src/lib.zig+25-25
......@@ -19,13 +19,13 @@ pub fn parse(comptime input: []const u8) astgen.Value {
1919 return astgen.Value{ .element = astgen.do(tokenize.do(input, &.{ '[', '=', ']', '(', ')', '{', '}', '#', '/', '.', '<', '>' })) };
2020}
2121
22pub fn compile(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype) !void {
22pub fn compile(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype) !void {
2323 try writer.writeAll("<!DOCTYPE html>\n");
24 try do(alloc, writer, value, data, data, 0, false);
24 try do(Ctx, alloc, writer, value, data, data, 0, false);
2525 try writer.writeAll("\n");
2626}
2727
28fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
28fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
2929 switch (comptime value) {
3030 .element => |v| {
3131 const hastext = for (v.children) |x| {
......@@ -41,7 +41,7 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
4141 .string => try writer.print(" {s}=\"{}\"", .{ it.key, std.zig.fmtEscapes(it.value.string[1 .. it.value.string.len - 1]) }),
4242 .body => {
4343 try writer.print(" {s}=\"", .{it.key});
44 try do(alloc, writer, astgen.Value{ .body = it.value.body }, data, ctx, indent, flag1);
44 try do(Ctx, alloc, writer, astgen.Value{ .body = it.value.body }, data, ctx, indent, flag1);
4545 try writer.print("\"", .{});
4646 },
4747 }
......@@ -58,7 +58,7 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
5858
5959 if (!hastext) try writer.writeAll("\n");
6060 inline for (v.children) |it| {
61 try do(alloc, writer, it, data, ctx, indent + 1, !hastext);
61 try do(Ctx, alloc, writer, it, data, ctx, indent + 1, !hastext);
6262 }
6363 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");
6464 try writer.print("</{s}>", .{v.name});
......@@ -106,25 +106,25 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
106106 switch (v.name) {
107107 .each => {
108108 comptime assertEqual(v.args.len, 1);
109 for (x) |item| try do(alloc, writer, body, item, ctx, indent, flag1);
109 for (x) |item| try do(Ctx, alloc, writer, body, item, ctx, indent, flag1);
110110 },
111111 .@"if" => {
112112 comptime assertEqual(v.args.len, 1);
113113 if (comptime std.meta.trait.isIndexable(T)) {
114 try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x.len > 0);
114 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x.len > 0);
115115 return;
116116 }
117117 switch (comptime TI) {
118 .Bool => try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x),
119 .Optional => try docap(alloc, writer, body, bottom, data, ctx, indent, flag1, x),
118 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x),
119 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x),
120120 else => @compileError(comptime std.fmt.comptimePrint("pek: unable to use '{s}' in an #if block", .{@typeName(T)})),
121121 }
122122 },
123123 .ifnot => {
124124 comptime assertEqual(v.args.len, 1);
125125 switch (comptime TI) {
126 .Bool => try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
127 .Optional => try docap(alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
126 .Bool => try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
127 .Optional => try docap(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, !x),
128128 else => @compileError(comptime std.fmt.comptimePrint("pek: unable to use '{s}' in an #ifnot block", .{@typeName(T)})),
129129 }
130130 },
......@@ -132,31 +132,31 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
132132 comptime assertEqual(v.args.len, 2);
133133 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);
134134 if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) {
135 return try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
135 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
136136 }
137 try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x == y);
137 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x == y);
138138 },
139139 .ifnotequal => {
140140 comptime assertEqual(v.args.len, 2);
141141 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);
142142 if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) {
143 return try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
143 return try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, std.mem.eql(u8, @tagName(x), y));
144144 }
145 try doif(alloc, writer, body, bottom, data, ctx, indent, flag1, x != y);
145 try doif(Ctx, alloc, writer, body, bottom, data, ctx, indent, flag1, x != y);
146146 },
147147 }
148148 },
149149 .body => |v| {
150150 inline for (v) |val| {
151 try do(alloc, writer, val, data, ctx, indent, flag1);
151 try do(Ctx, alloc, writer, val, data, ctx, indent, flag1);
152152 }
153153 },
154154 .function => |v| {
155155 var arena = std.heap.ArenaAllocator.init(alloc);
156156 defer arena.deinit();
157157
158 if (@hasDecl(root, "pek_" ++ v.name)) {
159 const func = @field(root, "pek_" ++ v.name);
158 if (@hasDecl(Ctx, "pek_" ++ v.name)) {
159 const func = @field(Ctx, "pek_" ++ v.name);
160160 var list = std.ArrayList(u8).init(arena.allocator());
161161 errdefer list.deinit();
162162 var args: FnArgsTuple(func) = undefined;
......@@ -168,7 +168,7 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
168168 }
169169 const repvalue = astgen.Value{ .replacement = &.{"this"} };
170170 try @call(.{}, func, args);
171 try do(alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);
171 try do(Ctx, alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);
172172 return;
173173 }
174174 @compileError("pek: unknown custom function: " ++ v.name);
......@@ -255,19 +255,19 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool {
255255 return false;
256256}
257257
258fn doif(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 {
258fn 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 {
259259 if (flag2) {
260 try do(alloc, writer, top, data, ctx, indent, flag1);
260 try do(Ctx, alloc, writer, top, data, ctx, indent, flag1);
261261 } else {
262 try do(alloc, writer, bottom, data, ctx, indent, flag1);
262 try do(Ctx, alloc, writer, bottom, data, ctx, indent, flag1);
263263 }
264264}
265265
266fn docap(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 {
266fn 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 {
267267 if (flag2) |_| {
268 try do(alloc, writer, top, data, ctx, indent, flag1);
268 try do(Ctx, alloc, writer, top, data, ctx, indent, flag1);
269269 } else {
270 try do(alloc, writer, bottom, data, ctx, indent, flag1);
270 try do(Ctx, alloc, writer, bottom, data, ctx, indent, flag1);
271271 }
272272}
273273