fediglam/src/template/lib.zig

630 lines
21 KiB
Zig
Raw Normal View History

2022-11-16 03:10:16 +00:00
const std = @import("std");
pub fn main() !void {
2022-11-16 07:39:36 +00:00
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{
.community = .{ .name = "abcd" },
.foo = [_][]const u8{ "5", "4", "3", "2", "1" },
2022-11-16 08:33:06 +00:00
.baz = [_][]const []const u8{
&.{ "5", "4", "3", "2", "1" },
&.{ "5", "4", "3", "2", "1" },
},
2022-11-18 07:39:32 +00:00
.qux = true,
.quxx = false,
2022-11-16 07:39:36 +00:00
});
2022-11-16 03:10:16 +00:00
}
pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
2022-11-18 07:39:32 +00:00
@setEvalBranchQuota(@intCast(u32, template.len * 8));
2022-11-18 11:24:25 +00:00
const tokens = comptime parseTemplateTokens(ControlTokenIter{ .text = template });
const tmpl = comptime parseTemplate(tokens, 0, .root);
try executeTemplate(writer, tmpl.items, args, .{});
2022-11-16 06:35:27 +00:00
}
2022-11-16 08:01:45 +00:00
fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void {
2022-11-18 11:24:25 +00:00
inline for (items) |it| {
switch (it) {
.text => |text| try writer.writeAll(text),
.statement => |stmt| try executeStatement(writer, stmt, args, captures),
}
}
2022-11-16 03:10:16 +00:00
}
2022-11-18 09:59:27 +00:00
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
switch (stmt) {
2022-11-16 08:33:06 +00:00
.expression => |expr| {
const val = evaluateExpression(expr, args, captures);
try print(writer, val);
},
2022-11-18 11:24:25 +00:00
.@"for" => |loop| {
const iterable = evaluateExpression(loop.header.iterable, args, captures);
2022-11-16 07:53:29 +00:00
const subtemplate = loop.subtemplate;
//std.log.debug("{any}", .{subtemplate});
2022-11-16 08:18:14 +00:00
for (iterable) |v| {
2022-11-16 08:01:45 +00:00
try executeTemplate(
writer,
subtemplate,
args,
2022-11-18 11:24:25 +00:00
addCapture(captures, loop.header.capture, v),
2022-11-16 08:01:45 +00:00
);
2022-11-16 07:39:36 +00:00
}
2022-11-16 07:16:46 +00:00
},
2022-11-18 11:24:25 +00:00
.@"if" => |if_stmt| {
const condition = evaluateExpression(if_stmt.header.condition, args, captures);
2022-11-18 07:39:32 +00:00
const subtemplate = if_stmt.subtemplate;
if (condition) try executeTemplate(writer, subtemplate, args, captures);
},
2022-11-18 11:24:25 +00:00
//else => @compileError("TODO"),
}
}
2022-11-16 07:39:36 +00:00
fn print(writer: anytype, arg: anytype) !void {
2022-11-19 11:13:05 +00:00
const T = @TypeOf(arg);
if (comptime std.meta.trait.isZigString(T)) return writer.writeAll(arg);
if (comptime std.meta.trait.isNumber(T)) return std.fmt.format(writer, "{}", .{arg});
2022-11-16 08:33:06 +00:00
@compileLog(@TypeOf(arg));
2022-11-16 07:39:36 +00:00
@compileError("TODO");
}
2022-11-16 08:01:45 +00:00
fn Deref(comptime T: type, comptime names: []const []const u8) type {
2022-11-16 07:39:36 +00:00
if (names.len == 0) return T;
// Compiler segfaults when I use std.meta to get this info so we search it manually
const field = for (@typeInfo(T).Struct.fields) |f| {
if (std.mem.eql(u8, f.name, names[0])) break f;
} else @compileError("Unknown field " ++ names[0] ++ " in type " ++ @typeName(T));
2022-11-16 08:01:45 +00:00
return Deref(field.field_type, names[1..]);
2022-11-16 07:39:36 +00:00
}
2022-11-16 08:01:45 +00:00
fn deref(arg: anytype, comptime names: []const []const u8) Deref(@TypeOf(arg), names) {
2022-11-16 07:39:36 +00:00
if (names.len == 0) return arg;
2022-11-16 08:01:45 +00:00
return deref(@field(arg, names[0]), names[1..]);
}
2022-11-16 08:33:06 +00:00
fn EvaluateExpression(comptime expression: Expression, comptime Args: type, comptime Captures: type) type {
return switch (expression) {
.arg_deref => |names| Deref(Args, names),
.capture_deref => |names| Deref(Captures, names),
};
}
fn evaluateExpression(
comptime expression: Expression,
args: anytype,
captures: anytype,
) EvaluateExpression(expression, @TypeOf(args), @TypeOf(captures)) {
return switch (expression) {
.arg_deref => |names| deref(args, names),
.capture_deref => |names| deref(captures, names),
};
}
2022-11-16 08:18:14 +00:00
fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type {
var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{
.name = name,
.field_type = Val,
.default_value = null,
.is_comptime = false,
.alignment = @alignOf(Val),
}};
return @Type(.{ .Struct = .{
.layout = .Auto,
.fields = fields,
.decls = &.{},
.is_tuple = false,
} });
}
fn addCapture(root: anytype, comptime name: []const u8, val: anytype) AddCapture(@TypeOf(root), name, @TypeOf(val)) {
var result = std.mem.zeroInit(AddCapture(@TypeOf(root), name, @TypeOf(val)), root);
@field(result, name) = val;
return result;
}
2022-11-16 07:16:46 +00:00
const TemplateType = enum {
root,
2022-11-18 07:39:32 +00:00
for_block,
if_block,
2022-11-16 07:16:46 +00:00
};
const TemplateParseResult = struct {
new_idx: usize,
items: []const TemplateItem,
closing_block: ?ControlBlock,
};
2022-11-18 11:24:25 +00:00
fn parseTemplate(
comptime tokens: []const TemplateToken,
comptime start: usize,
comptime template_type: TemplateType,
) TemplateParseResult {
2022-11-16 06:35:27 +00:00
comptime {
2022-11-18 11:24:25 +00:00
var i: usize = start;
2022-11-16 06:35:27 +00:00
var current_text: []const u8 = "";
2022-11-18 11:24:25 +00:00
var items: []const TemplateItem = &.{};
2022-11-16 06:35:27 +00:00
const closing_block: ?ControlBlock = while (i < tokens.len) : (i += 1) {
2022-11-18 11:24:25 +00:00
switch (tokens[i]) {
.text => |text| current_text = current_text ++ text,
.whitespace => |wsp| {
if (i != tokens.len - 1 and tokens[i + 1] == .control_block)
if (tokens[i + 1].control_block.strip_before)
continue;
current_text = current_text ++ wsp;
},
.control_block => |cb| {
if (current_text.len != 0) {
items = items ++ [_]TemplateItem{.{ .text = current_text }};
current_text = "";
}
switch (cb.block) {
.expression => |expr| items = items ++ [_]TemplateItem{.{ .statement = .{ .expression = expr } }},
.if_header => |header| {
if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1;
2022-11-18 11:24:25 +00:00
const subtemplate = parseTemplate(tokens, i + 1, .if_block);
items = items ++ [_]TemplateItem{.{
.statement = .{
.@"if" = .{
.subtemplate = subtemplate.items,
2022-11-18 11:24:25 +00:00
.header = header,
},
},
}};
i = subtemplate.new_idx;
2022-11-18 11:24:25 +00:00
},
.for_header => |header| {
if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1;
2022-11-18 11:24:25 +00:00
const subtemplate = parseTemplate(tokens, i + 1, .for_block);
items = items ++ [_]TemplateItem{.{
.statement = .{
.@"for" = .{
.subtemplate = subtemplate.items,
2022-11-18 11:24:25 +00:00
.header = header,
},
},
}};
i = subtemplate.new_idx;
2022-11-18 11:24:25 +00:00
},
.end_for => if (template_type == .for_block)
break cb
2022-11-18 11:24:25 +00:00
else
@compileError("Unexpected /for tag"),
.end_if => if (template_type == .if_block)
break cb
2022-11-18 11:24:25 +00:00
else
@compileError("Unexpected /if tag"),
}
if (i != tokens.len - 1 and tokens[i] == .control_block) {
if (tokens[i].control_block.strip_after and tokens[i + 1] == .whitespace) {
i += 1;
2022-11-16 07:16:46 +00:00
}
2022-11-16 06:35:27 +00:00
}
},
}
} else null;
2022-11-16 06:35:27 +00:00
2022-11-18 11:24:25 +00:00
if (current_text.len != 0) items = items ++ [_]TemplateItem{.{ .text = current_text }};
2022-11-16 06:35:27 +00:00
if (template_type != .root and closing_block == null) @compileError("End tag not found");
2022-11-16 07:16:46 +00:00
return .{
.new_idx = i,
.items = items,
.closing_block = closing_block,
2022-11-16 07:16:46 +00:00
};
2022-11-16 06:35:27 +00:00
}
}
2022-11-18 11:24:25 +00:00
const TemplateToken = union(enum) {
text: []const u8,
whitespace: []const u8,
control_block: ControlBlock,
};
fn parseTemplateTokens(comptime tokens: ControlTokenIter) []const TemplateToken {
comptime {
var iter = tokens;
var items: []const TemplateToken = &.{};
while (iter.next()) |token| switch (token) {
.whitespace => |wsp| items = items ++ [_]TemplateToken{.{ .whitespace = wsp }},
.text => |text| items = items ++ [_]TemplateToken{.{ .text = text }},
.open_bracket => {
const next = iter.next() orelse @compileError("Unexpected end of template");
if (next == .open_bracket) {
items = items ++ [_]TemplateToken{.{ .text = "{" }};
} else {
iter.putBack(next);
const result = parseControlBlock(iter);
iter = result.new_iter;
items = items ++ [_]TemplateToken{.{ .control_block = result.item }};
}
},
.close_bracket => {
const next = iter.next() orelse @compileError("Unexpected end of template");
if (next == .close_bracket)
items = items ++ [_]TemplateToken{.{ .text = "}" }}
else
@compileError("Unpaired close bracket, did you mean \"}}\"?");
},
.period => items = items ++ [_]TemplateToken{.{ .text = "." }},
.pound => items = items ++ [_]TemplateToken{.{ .text = "#" }},
.pipe => items = items ++ [_]TemplateToken{.{ .text = "|" }},
.dollar => items = items ++ [_]TemplateToken{.{ .text = "$" }},
.slash => items = items ++ [_]TemplateToken{.{ .text = "/" }},
.equals => items = items ++ [_]TemplateToken{.{ .text = "=" }},
};
return items;
}
}
2022-11-18 10:05:13 +00:00
fn parseExpression(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, Expression) {
2022-11-18 09:58:13 +00:00
comptime {
var iter = tokens;
var expr: Expression = while (iter.next()) |token| switch (token) {
.whitespace => {},
.period => {
const names = parseDeref(iter);
iter = names.new_iter;
break .{ .arg_deref = names.item };
},
.dollar => {
const names = parseDeref(iter);
iter = names.new_iter;
break .{ .capture_deref = names.item };
},
else => @compileError("TODO"),
};
return .{
.new_iter = iter,
.item = expr,
};
}
}
2022-11-18 10:05:13 +00:00
fn parseControlBlock(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, ControlBlock) {
comptime {
var iter = tokens;
2022-11-18 08:09:10 +00:00
2022-11-18 11:24:25 +00:00
const strip_before = if (iter.next()) |first| blk: {
if (first == .equals) {
break :blk true;
2022-11-18 08:09:10 +00:00
}
2022-11-18 11:24:25 +00:00
iter.putBack(first);
break :blk false;
} else @compileError("Unexpected end of template");
var stmt: ControlBlock.Data = while (iter.next()) |token| switch (token) {
.equals => @compileError("Unexpected '='"),
.whitespace => {},
.pound => {
const next = iter.next() orelse @compileError("Unexpected end of template");
if (next != .text) @compileError("Expected keyword following '#' character");
const text = next.text;
const keyword = std.meta.stringToEnum(Keyword, text) orelse @compileError("Unknown keyword: " ++ text);
switch (keyword) {
.@"for" => {
const result = parseForHeader(iter);
iter = result.new_iter;
break .{ .for_header = result.item };
},
.@"if" => {
const result = parseIfHeader(iter);
iter = result.new_iter;
break .{ .if_header = result.item };
},
//else => @compileError("TODO"),
}
},
.slash => {
const next = iter.next() orelse @compileError("Unexpected end of template");
if (next != .text) @compileError("Expected keyword following '/' character");
const text = next.text;
const keyword = std.meta.stringToEnum(EndKeyword, text) orelse @compileError("Unknown keyword: " ++ text);
switch (keyword) {
.@"for" => break .{ .end_for = {} },
.@"if" => break .{ .end_if = {} },
}
},
.period, .dollar => {
iter.putBack(token);
const expr = parseExpression(iter);
iter = expr.new_iter;
break .{ .expression = expr.item };
},
else => @compileError("TODO"),
};
2022-11-18 09:58:13 +00:00
// search for end of statement
var strip_after: bool = false;
while (iter.next()) |token| switch (token) {
.whitespace => {},
.equals => {
if (iter.peek()) |t| {
if (t == .close_bracket) {
strip_after = true;
continue;
2022-11-18 08:09:10 +00:00
}
2022-11-18 09:58:13 +00:00
}
@compileError("Unexpected '='");
},
.close_bracket => return .{
.new_iter = iter,
.item = .{
2022-11-18 11:24:25 +00:00
.block = stmt,
2022-11-18 09:58:13 +00:00
.strip_before = strip_before,
.strip_after = strip_after,
2022-11-16 08:06:53 +00:00
},
2022-11-18 09:58:13 +00:00
},
else => {
@compileLog(iter.row);
@compileError("TODO" ++ @tagName(token));
},
};
2022-11-16 07:16:46 +00:00
2022-11-18 09:58:13 +00:00
@compileError("Unexpected end of template");
2022-11-16 07:16:46 +00:00
}
}
2022-11-18 10:05:13 +00:00
fn skipWhitespace(comptime tokens: ControlTokenIter) ControlTokenIter {
2022-11-16 07:53:29 +00:00
comptime {
var iter = tokens;
while (iter.peek()) |token| switch (token) {
.whitespace => _ = iter.next(),
else => break,
};
return iter;
}
}
2022-11-18 10:05:13 +00:00
fn endControlBlock(comptime tokens: ControlTokenIter) ControlTokenIter {
2022-11-16 07:53:29 +00:00
comptime {
var iter = skipWhitespace(tokens);
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .close_bracket) @compileError("Unexpected token");
return iter;
}
}
2022-11-18 11:24:25 +00:00
fn parseForHeader(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, ForHeader) {
2022-11-16 07:16:46 +00:00
comptime {
2022-11-18 09:58:13 +00:00
const iterable = parseExpression(tokens);
2022-11-16 07:39:36 +00:00
var iter = iterable.new_iter;
2022-11-16 07:53:29 +00:00
iter = skipWhitespace(iter);
{
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .pipe) @compileError("Unexpected token");
}
{
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .dollar) @compileError("Unexpected token");
}
const capture = blk: {
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .text) @compileError("Unexpected token");
break :blk token.text;
2022-11-16 07:16:46 +00:00
};
2022-11-16 07:53:29 +00:00
{
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .pipe) @compileError("Unexpected token");
}
2022-11-16 07:16:46 +00:00
2022-11-18 11:24:25 +00:00
return .{
.new_iter = iter,
.item = .{
.iterable = iterable.item,
.capture = capture,
},
};
2022-11-18 07:39:32 +00:00
}
}
2022-11-18 11:24:25 +00:00
fn parseIfHeader(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, IfHeader) {
2022-11-18 07:39:32 +00:00
comptime {
2022-11-18 09:58:13 +00:00
const condition = parseExpression(tokens);
2022-11-18 11:24:25 +00:00
var iter = condition.new_iter;
2022-11-18 07:39:32 +00:00
2022-11-18 11:24:25 +00:00
return .{
.new_iter = iter,
.item = .{
.condition = condition.item,
},
};
2022-11-16 07:16:46 +00:00
}
}
2022-11-18 10:05:13 +00:00
fn parseDeref(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, []const []const u8) {
comptime {
var iter = tokens;
var fields: []const []const u8 = &.{};
var wants = .text;
while (iter.peek()) |token| {
switch (token) {
.whitespace => {},
.text => |text| {
if (wants != .text) @compileError("Unexpected token \"" ++ text ++ "\"");
fields = fields ++ [1][]const u8{text};
wants = .period;
},
.period => {
if (wants != .period) @compileError("Unexpected token \".\"");
wants = .text;
},
else => if (wants == .period) return .{
.new_iter = iter,
2022-11-16 06:11:16 +00:00
.item = fields,
} else @compileError("Unexpected token"),
}
_ = iter.next();
2022-11-16 03:10:16 +00:00
}
}
}
2022-11-18 10:02:24 +00:00
fn ParseResult(comptime It: type, comptime T: type) type {
2022-11-16 06:11:16 +00:00
return struct {
2022-11-18 10:02:24 +00:00
new_iter: It,
2022-11-16 06:11:16 +00:00
item: T,
};
}
2022-11-16 06:35:27 +00:00
const TemplateItem = union(enum) {
text: []const u8,
2022-11-18 09:59:27 +00:00
statement: Statement,
2022-11-16 06:35:27 +00:00
};
const Expression = union(enum) {
arg_deref: []const []const u8,
2022-11-16 08:01:45 +00:00
capture_deref: []const []const u8,
};
2022-11-18 11:24:25 +00:00
const For = struct {
2022-11-16 07:16:46 +00:00
subtemplate: []const TemplateItem,
2022-11-18 11:24:25 +00:00
header: ForHeader,
};
const ForHeader = struct {
2022-11-16 07:39:36 +00:00
iterable: Expression,
2022-11-16 07:53:29 +00:00
capture: []const u8,
2022-11-16 07:16:46 +00:00
};
2022-11-18 11:24:25 +00:00
const If = struct {
2022-11-18 07:39:32 +00:00
subtemplate: []const TemplateItem,
2022-11-18 11:24:25 +00:00
header: IfHeader,
};
const IfHeader = struct {
2022-11-18 07:39:32 +00:00
condition: Expression,
};
2022-11-18 09:59:27 +00:00
const Statement = union(enum) {
expression: Expression,
2022-11-18 11:24:25 +00:00
@"for": For,
@"if": If,
};
2022-11-18 09:59:27 +00:00
const ControlBlock = struct {
2022-11-18 11:24:25 +00:00
const Data = union(enum) {
expression: Expression,
for_header: ForHeader,
end_for: void,
if_header: IfHeader,
end_if: void,
};
block: Data,
2022-11-18 07:52:07 +00:00
strip_before: bool,
strip_after: bool,
};
const Keyword = enum {
@"for",
2022-11-18 07:39:32 +00:00
@"if",
2022-11-18 07:23:02 +00:00
};
const EndKeyword = enum {
@"for",
2022-11-18 07:39:32 +00:00
@"if",
};
2022-11-18 10:05:13 +00:00
const ControlToken = union(enum) {
2022-11-16 03:10:16 +00:00
text: []const u8,
open_bracket: void,
close_bracket: void,
period: void,
whitespace: []const u8,
pound: void,
2022-11-16 07:53:29 +00:00
pipe: void,
dollar: void,
2022-11-18 07:23:02 +00:00
slash: void,
2022-11-18 08:09:10 +00:00
equals: void,
2022-11-16 03:10:16 +00:00
};
2022-11-18 10:05:13 +00:00
const ControlTokenIter = struct {
2022-11-16 03:10:16 +00:00
start: usize = 0,
text: []const u8,
2022-11-18 10:05:13 +00:00
peeked_token: ?ControlToken = null,
2022-11-16 03:10:16 +00:00
2022-11-16 07:16:46 +00:00
row: usize = 0,
2022-11-18 10:05:13 +00:00
fn next(self: *ControlTokenIter) ?ControlToken {
2022-11-16 03:10:16 +00:00
if (self.peeked_token) |token| {
self.peeked_token = null;
return token;
}
const remaining = self.text[self.start..];
if (remaining.len == 0) return null;
const ch = remaining[0];
self.start += 1;
switch (ch) {
'{' => return .{ .open_bracket = {} },
'}' => return .{ .close_bracket = {} },
'.' => return .{ .period = {} },
'#' => return .{ .pound = {} },
2022-11-16 07:53:29 +00:00
'|' => return .{ .pipe = {} },
'$' => return .{ .dollar = {} },
2022-11-18 07:23:02 +00:00
'/' => return .{ .slash = {} },
2022-11-18 08:09:10 +00:00
'=' => return .{ .equals = {} },
2022-11-16 03:10:16 +00:00
' ', '\t', '\n', '\r' => {
var idx: usize = 0;
while (idx < remaining.len and std.mem.indexOfScalar(u8, " \t\n\r", remaining[idx]) != null) : (idx += 1) {}
2022-11-16 07:16:46 +00:00
const newline_count = std.mem.count(u8, remaining[0..idx], "\n");
self.row += newline_count;
2022-11-16 03:10:16 +00:00
self.start += idx - 1;
return .{ .whitespace = remaining[0..idx] };
},
else => {
var idx: usize = 0;
2022-11-18 08:09:10 +00:00
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}.#|$/= \t\n\r", remaining[idx]) == null) : (idx += 1) {}
2022-11-16 03:10:16 +00:00
self.start += idx - 1;
return .{ .text = remaining[0..idx] };
},
}
}
2022-11-18 10:05:13 +00:00
fn peek(self: *ControlTokenIter) ?ControlToken {
2022-11-16 03:10:16 +00:00
const token = self.next();
self.peeked_token = token;
return token;
}
2022-11-18 09:58:13 +00:00
2022-11-18 10:05:13 +00:00
fn putBack(self: *ControlTokenIter, token: ControlToken) void {
2022-11-18 09:58:13 +00:00
std.debug.assert(self.peeked_token == null);
self.peeked_token = token;
}
2022-11-16 03:10:16 +00:00
};
2022-12-03 05:49:17 +00:00
test "template" {
const testCase = struct {
fn case(comptime tmpl: []const u8, args: anytype, expected: []const u8) !void {
var stream = std.io.changeDetectionStream(expected, std.io.null_writer);
try execute(stream.writer(), tmpl, args);
try std.testing.expect(!stream.changeDetected());
}
}.case;
try testCase("", .{}, "");
try testCase("abcd", .{}, "abcd");
try testCase("{.val}", .{ .val = 3 }, "3");
try testCase("{#if .val}1{/if}", .{ .val = true }, "1");
try testCase("{#for .vals |$v|}{$v}{/for}", .{ .vals = [_]u8{ 1, 2, 3 } }, "123");
try testCase("{#for .vals |$v|=} {$v} {=/for}", .{ .vals = [_]u8{ 1, 2, 3 } }, "123");
}