fediglam/src/template/lib.zig

1364 lines
47 KiB
Zig
Raw Normal View History

2022-11-16 03:10:16 +00:00
const std = @import("std");
pub fn main() !void {
2022-12-10 09:21:39 +00:00
const Enum = enum { foo, bar, baz };
try execute(
std.io.getStdOut().writer(),
2022-12-08 11:21:18 +00:00
.{ .test_tmpl = "{.x} {%context_foo}" },
@embedFile("./test.tmp.html"),
.{
2022-12-10 10:44:54 +00:00
.community = .{ .name = "<abcd>" },
2022-12-14 08:46:24 +00:00
.foo = &[_][]const u8{ "5", "4", "3", "2", "1" },
.baz = &[_][]const []const u8{
&.{ "5", "4", "3", "2", "1" },
&.{ "5", "4", "3", "2", "1" },
},
2022-12-14 08:46:24 +00:00
.start = 1,
.end = 3,
.bar = .{ .x = "x" },
.qux = false,
.quxx = true,
2022-12-10 08:02:04 +00:00
.quxx2 = true,
.maybe_foo = @as(?[]const u8, "foo"),
.maybe_bar = @as(?[]const u8, null),
2022-12-10 08:32:24 +00:00
.snap = Enum.bar,
2022-12-10 09:21:39 +00:00
.crackle = union(Enum) {
foo: []const u8,
bar: []const u8,
baz: []const u8,
}{ .foo = "abcd" },
2022-12-08 10:58:18 +00:00
.x = "y",
2022-11-16 08:33:06 +00:00
},
2022-12-08 07:47:07 +00:00
.{
.context_foo = "foo",
},
);
2022-11-16 03:10:16 +00:00
}
2022-12-08 07:47:07 +00:00
pub fn execute(
writer: anytype,
comptime other_templates: anytype,
comptime template: []const u8,
args: anytype,
context: anytype,
) !void {
2022-12-13 09:33:22 +00:00
@setEvalBranchQuota(@intCast(u32, template.len * 12));
2022-11-18 11:24:25 +00:00
const tokens = comptime parseTemplateTokens(ControlTokenIter{ .text = template });
const tmpl = comptime parseTemplate(tokens, 0, .root);
2022-12-08 07:47:07 +00:00
try executeTemplate(writer, other_templates, tmpl.items, args, .{}, context);
2022-11-16 06:35:27 +00:00
}
2022-12-08 07:47:07 +00:00
fn executeTemplate(
writer: anytype,
comptime templates: anytype,
comptime items: []const TemplateItem,
args: anytype,
captures: anytype,
context: anytype,
) !void {
2022-11-18 11:24:25 +00:00
inline for (items) |it| {
switch (it) {
.text => |text| try writer.writeAll(text),
2022-12-08 07:47:07 +00:00
.statement => |stmt| try executeStatement(writer, templates, stmt, args, captures, context),
2022-11-18 11:24:25 +00:00
}
}
2022-11-16 03:10:16 +00:00
}
2022-12-08 07:47:07 +00:00
fn executeStatement(
writer: anytype,
comptime templates: anytype,
comptime stmt: Statement,
args: anytype,
captures: anytype,
context: anytype,
) !void {
switch (stmt) {
2022-11-16 08:33:06 +00:00
.expression => |expr| {
const val = try evaluateExpression(expr, args, captures, context);
2022-11-16 08:33:06 +00:00
try print(writer, val);
},
2022-11-18 11:24:25 +00:00
.@"for" => |loop| {
const iterable = try evaluateExpression(loop.header.iterable, args, captures, context);
2022-11-16 07:53:29 +00:00
const subtemplate = loop.subtemplate;
//std.log.debug("{any}", .{subtemplate});
2022-12-14 10:35:23 +00:00
for (iterable) |v, i| {
const with_item_capture = addCapture(captures, loop.header.item_capture, v);
const with_idx_capture = if (comptime loop.header.idx_capture) |name| addCapture(with_item_capture, name, i) else with_item_capture;
2022-11-16 08:01:45 +00:00
try executeTemplate(
writer,
templates,
2022-11-16 08:01:45 +00:00
subtemplate,
args,
2022-12-14 10:35:23 +00:00
with_idx_capture,
2022-12-08 07:47:07 +00:00
context,
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 = try evaluateExpression(if_stmt.header.condition, args, captures, context);
2022-11-18 07:39:32 +00:00
const subtemplate = if_stmt.subtemplate;
var was_true: bool = false;
if (if_stmt.header.capture) |capture| {
if (condition) |val| {
was_true = true;
try executeTemplate(
writer,
templates,
subtemplate,
args,
addCapture(captures, capture, val),
context,
);
}
2022-12-08 01:30:01 +00:00
} else {
if (condition) {
was_true = true;
try executeTemplate(
writer,
templates,
subtemplate,
args,
captures,
context,
);
}
}
if (!was_true) {
if (if_stmt.else_branch) |branch| switch (branch) {
2022-12-08 07:47:07 +00:00
.@"else" => |subtmpl| try executeTemplate(writer, templates, subtmpl, args, captures, context),
.elif => |elif| try executeStatement(writer, templates, .{ .@"if" = elif.* }, args, captures, context),
};
2022-12-08 01:30:01 +00:00
}
2022-11-18 07:39:32 +00:00
},
2022-12-10 09:21:39 +00:00
.@"switch" => |switch_stmt| {
const expr = try evaluateExpression(switch_stmt.expression, args, captures, context);
2022-12-10 09:21:39 +00:00
const exhaustive = switch_stmt.cases.len == std.meta.fields(@TypeOf(expr)).len;
if (exhaustive and switch_stmt.else_branch != null) @compileError("Unused else branch in switch");
if (!exhaustive and switch_stmt.else_branch == null) @compileError("Not all switch cases covered");
2022-12-10 09:21:39 +00:00
var found = false;
inline for (switch_stmt.cases) |case| {
if (std.meta.isTag(expr, case.header.tag)) {
found = true;
if (case.header.capture) |capture| {
try executeTemplate(
writer,
templates,
case.subtemplate,
args,
addCapture(captures, capture, @field(expr, case.header.tag)),
context,
);
} else {
try executeTemplate(
writer,
templates,
case.subtemplate,
args,
captures,
context,
);
}
}
} else if (!found) if (switch_stmt.else_branch) |subtemplate| {
try executeTemplate(
writer,
templates,
subtemplate,
args,
captures,
context,
);
};
},
2022-12-08 07:24:32 +00:00
.call_template => |call| {
const new_template = @field(templates, call.template_name);
2022-12-08 07:47:07 +00:00
try execute(
writer,
templates,
new_template,
try evaluateExpression(call.args, args, captures, context),
2022-12-08 07:47:07 +00:00
context,
);
2022-12-08 07:24:32 +00:00
},
2022-12-12 06:50:44 +00:00
.format => |fmt| {
try std.fmt.format(
writer,
"{" ++ fmt.format ++ "}",
.{try evaluateExpression(fmt.value, args, captures, context)},
2022-12-12 06:50:44 +00:00
);
},
2022-11-18 11:24:25 +00:00
//else => @compileError("TODO"),
}
}
2022-12-10 10:44:54 +00:00
fn htmlEscape(writer: anytype, str: []const u8) !void {
for (str) |ch| switch (ch) {
'<' => try writer.writeAll("&lt;"),
'>' => try writer.writeAll("&gt;"),
'"' => try writer.writeAll("&quot;"),
'&' => try writer.writeAll("&amp;"),
else => try writer.writeByte(ch),
};
}
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);
2022-12-12 06:50:44 +00:00
if (T == void) return;
2022-12-10 10:44:54 +00:00
if (comptime std.meta.trait.isZigString(T)) return htmlEscape(writer, arg);
2022-11-19 11:13:05 +00:00
if (comptime std.meta.trait.isNumber(T)) return std.fmt.format(writer, "{}", .{arg});
2022-12-12 11:40:51 +00:00
try std.fmt.format(writer, "{}", .{arg});
2022-11-16 07:39:36 +00:00
}
2022-12-14 09:29:00 +00:00
const ExpressionError = error{ IndexOutOfBounds, NullOptional };
2022-12-14 10:57:27 +00:00
fn Deref(comptime T: type, comptime field: []const u8) type {
if (std.meta.trait.isIndexable(T) and std.mem.eql(u8, field, "len")) return usize;
switch (@typeInfo(T)) {
.Pointer => return Deref(std.meta.Child(T), field),
.Struct => |info| for (info.fields) |f| {
if (std.mem.eql(u8, field, f.name)) return f.field_type;
} else @compileError("Field " ++ field ++ " does not exist on type " ++ @typeName(T)),
else => @compileError("Cannot retrieve field " ++ field ++ " from type " ++ @typeName(T)),
}
}
2022-12-08 07:47:07 +00:00
fn EvaluateExpression(
comptime expression: Expression,
comptime Args: type,
comptime Captures: type,
comptime Context: type,
) type {
2022-11-16 08:33:06 +00:00
return switch (expression) {
2022-12-14 09:29:00 +00:00
.args => Args,
.captures => Captures,
.context => Context,
.deref => |expr| {
const T = EvaluateExpression(expr.container, Args, Captures, Context);
2022-12-14 10:57:27 +00:00
return Deref(T, expr.field);
2022-12-14 09:29:00 +00:00
},
2022-12-10 08:02:04 +00:00
.equals => bool,
2022-12-10 08:32:24 +00:00
.builtin => |call| switch (call.*) {
.isTag => bool,
2022-12-14 08:46:24 +00:00
.slice => |sl| []const std.meta.Elem(EvaluateExpression(sl.iterable, Args, Captures, Context)),
2022-12-10 08:32:24 +00:00
},
2022-12-14 09:36:58 +00:00
.optional_unwrap => |expr| std.meta.Child(EvaluateExpression(expr.*, Args, Captures, Context)),
2022-12-14 10:08:07 +00:00
.int => isize,
2022-11-16 08:33:06 +00:00
};
}
fn evaluateExpression(
comptime expression: Expression,
args: anytype,
captures: anytype,
2022-12-08 07:47:07 +00:00
context: anytype,
) ExpressionError!EvaluateExpression(expression, @TypeOf(args), @TypeOf(captures), @TypeOf(context)) {
2022-11-16 08:33:06 +00:00
return switch (expression) {
2022-12-14 09:29:00 +00:00
.args => args,
.captures => captures,
.context => context,
.deref => |expr| {
return @field(
try evaluateExpression(expr.container, args, captures, context),
expr.field,
);
},
2022-12-10 08:02:04 +00:00
.equals => |eql| {
const lhs = try evaluateExpression(eql.lhs, args, captures, context);
const rhs = try evaluateExpression(eql.rhs, args, captures, context);
2022-12-10 08:02:04 +00:00
const T = @TypeOf(lhs, rhs);
if (comptime std.meta.trait.isZigString(T)) {
return std.mem.eql(u8, lhs, rhs);
} else if (comptime std.meta.trait.isContainer(T) and @hasDecl(T, "eql")) {
return T.eql(lhs, rhs);
2022-12-13 10:28:37 +00:00
} else return std.meta.eql(lhs, rhs);
2022-12-10 08:02:04 +00:00
},
2022-12-10 08:32:24 +00:00
.builtin => |call| switch (call.*) {
.isTag => |hdr| {
const val = try evaluateExpression(hdr.expression, args, captures, context);
2022-12-10 08:32:24 +00:00
return std.meta.isTag(val, hdr.tag);
},
2022-12-14 08:46:24 +00:00
.slice => |sl| {
const iterable = try evaluateExpression(sl.iterable, args, captures, context);
2022-12-14 10:08:07 +00:00
const start = std.math.cast(usize, try evaluateExpression(sl.start, args, captures, context)) orelse return error.IndexOutOfBounds;
const end = std.math.cast(usize, try evaluateExpression(sl.end, args, captures, context)) orelse return error.IndexOutOfBounds;
2022-12-14 08:46:24 +00:00
if (comptime std.meta.trait.is(.Array)(@TypeOf(iterable))) @compileError("Cannot slice an array, pass a slice or pointer to array instead");
if (start > iterable.len or end > iterable.len) return error.IndexOutOfBounds;
return iterable[start..end];
},
2022-12-10 08:32:24 +00:00
},
2022-12-14 09:36:58 +00:00
.optional_unwrap => |expr| {
const val = try evaluateExpression(expr.*, args, captures, context);
return val orelse error.NullOptional;
},
2022-12-14 10:08:07 +00:00
.int => |i| return i,
2022-11-16 08:33:06 +00:00
};
}
2022-11-16 08:18:14 +00:00
fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type {
2022-12-08 10:54:36 +00:00
if (std.mem.eql(u8, name, "_")) return Root;
2022-11-16 08:18:14 +00:00
var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{
.name = name,
.field_type = Val,
.default_value = null,
.is_comptime = false,
.alignment = @alignOf(Val),
}};
2022-12-14 10:57:27 +00:00
const Result = @Type(.{ .Struct = .{
2022-11-16 08:18:14 +00:00
.layout = .Auto,
.fields = fields,
.decls = &.{},
.is_tuple = false,
} });
2022-12-14 10:57:27 +00:00
return Result;
2022-11-16 08:18:14 +00:00
}
fn addCapture(root: anytype, comptime name: []const u8, val: anytype) AddCapture(@TypeOf(root), name, @TypeOf(val)) {
2022-12-08 10:54:36 +00:00
if (comptime std.mem.eql(u8, name, "_")) return root;
var result: AddCapture(@TypeOf(root), name, @TypeOf(val)) = undefined;
inline for (std.meta.fields(@TypeOf(root))) |f| {
@field(result, f.name) = @field(root, f.name);
}
2022-11-16 08:18:14 +00:00
@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-12-08 01:26:36 +00:00
if_else_block,
2022-12-10 09:21:39 +00:00
switch_block,
2022-11-16 07:16:46 +00:00
};
const TemplateParseResult = struct {
new_idx: usize,
items: []const TemplateItem,
closing_block: ?ControlBlock,
};
fn parseIfBlock(
comptime header: IfHeader,
comptime tokens: []const TemplateToken,
comptime start: usize,
) ParseResult(usize, If) {
const subtemplate = parseTemplate(tokens, start, .if_block);
switch (subtemplate.closing_block.?.block) {
.end_if => return .{
.new_iter = subtemplate.new_idx,
.item = If{
.header = header,
.subtemplate = subtemplate.items,
.else_branch = null,
},
},
.@"else" => {
const else_subtemplate = parseTemplate(tokens, subtemplate.new_idx + 1, .if_else_block);
return .{
.new_iter = else_subtemplate.new_idx,
.item = If{
.header = header,
.subtemplate = subtemplate.items,
.else_branch = .{ .@"else" = else_subtemplate.items },
},
};
},
.elif_header => |elif_header| {
const else_if = parseIfBlock(elif_header, tokens, subtemplate.new_idx + 1);
return .{
.new_iter = else_if.new_iter,
.item = If{
.header = header,
.subtemplate = subtemplate.items,
.else_branch = .{ .elif = &else_if.item },
},
};
},
else => unreachable,
}
}
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-12-10 09:21:39 +00:00
@setEvalBranchQuota(tokens.len * 100);
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;
}
if (i != 0 and tokens[i - 1] == .control_block) {
if (tokens[i - 1].control_block.strip_after) continue;
}
2022-11-18 11:24:25 +00:00
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| {
const parsed = parseIfBlock(header, tokens, i + 1);
i = parsed.new_iter;
2022-11-18 11:24:25 +00:00
items = items ++ [_]TemplateItem{.{
.statement = .{ .@"if" = parsed.item },
2022-11-18 11:24:25 +00:00
}};
},
.for_header => |header| {
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
},
2022-12-10 09:21:39 +00:00
.switch_header => |header| {
var cases: []const Case = &.{};
var else_branch: ?[]const TemplateItem = null;
var last_header: CaseHeader = header.first_case;
var is_else = false;
while (true) {
const case = parseTemplate(tokens, i + 1, .switch_block);
i = case.new_idx;
if (!is_else) {
cases = cases ++ [_]Case{.{
.header = last_header,
.subtemplate = case.items,
}};
} else {
else_branch = case.items;
}
switch (case.closing_block.?.block) {
.end_switch => break,
.@"else" => is_else = true,
.case_header => |case_header| last_header = case_header,
else => @compileError("Unexpected token"),
}
}
items = items ++ [_]TemplateItem{.{
.statement = .{
.@"switch" = .{
.expression = header.expression,
.cases = cases,
.else_branch = else_branch,
},
},
}};
},
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"),
2022-12-08 01:26:36 +00:00
.end_if => if (template_type == .if_block or template_type == .if_else_block)
break cb
2022-11-18 11:24:25 +00:00
else
@compileError("Unexpected /if tag"),
.elif_header => if (template_type == .if_block)
break cb
else
@compileError("Unexpected #elif tag"),
2022-12-10 09:21:39 +00:00
.@"else" => if (template_type == .if_block or template_type == .switch_block)
2022-12-08 01:26:36 +00:00
break cb
else
@compileError("Unexpected #else tag"),
2022-12-08 07:47:07 +00:00
.call_template => |call| items = items ++ [_]TemplateItem{.{
.statement = .{ .call_template = call },
}},
2022-12-12 06:50:44 +00:00
.format => |call| items = items ++ [_]TemplateItem{.{
.statement = .{ .format = call },
}},
2022-12-10 09:21:39 +00:00
.end_switch, .case_header => if (template_type == .switch_block)
break cb
else
@compileError("Unexpected /switch tag"),
2022-11-18 11:24:25 +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 }},
2022-12-14 10:08:07 +00:00
.number, .text => |text| items = items ++ [_]TemplateToken{.{ .text = text }},
2022-11-18 11:24:25 +00:00
.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 = "=" }},
2022-12-08 07:47:07 +00:00
.at => items = items ++ [_]TemplateToken{.{ .text = "@" }},
.comma => items = items ++ [_]TemplateToken{.{ .text = "," }},
2022-12-08 11:21:18 +00:00
.percent => items = items ++ [_]TemplateToken{.{ .text = "%" }},
2022-12-10 08:32:24 +00:00
.open_paren => items = items ++ [_]TemplateToken{.{ .text = "(" }},
.close_paren => items = items ++ [_]TemplateToken{.{ .text = ")" }},
2022-12-12 06:50:44 +00:00
.double_quote => items = items ++ [_]TemplateToken{.{ .text = "\"" }},
2022-12-13 09:33:22 +00:00
.question_mark => items = items ++ [_]TemplateToken{.{ .text = "?" }},
2022-11-18 11:24:25 +00:00
};
return items;
}
}
2022-12-14 10:08:07 +00:00
fn tryParseIdentifier(comptime tokens: ControlTokenIter) ?ParseResult(ControlTokenIter, []const u8) {
comptime {
var iter = skipWhitespace(tokens);
var ident: []const u8 = "";
var first: bool = true;
while (iter.next()) |token| switch (token) {
.number, .text => |text| {
if (first and token == .number) return null;
ident = ident ++ text;
first = false;
},
else => {
iter.putBack(token);
break;
},
};
if (first) return null;
return ParseResult(ControlTokenIter, []const u8){
.new_iter = iter,
.item = ident,
};
}
}
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;
2022-12-10 08:02:04 +00:00
var last_valid_iter: ?ControlTokenIter = null;
var expr: ?Expression = null;
2022-12-14 09:29:00 +00:00
while (iter.next()) |token| {
switch (token) {
.whitespace => {},
.period => {
iter = skipWhitespace(iter);
if (expr == null) {
expr = .{ .args = {} };
if (iter.peek()) |n| if (n == .text) iter.putBack(.{ .period = {} });
2022-12-14 10:08:07 +00:00
} else if (tryParseIdentifier(iter)) |ident| {
iter = ident.new_iter;
expr = .{
2022-12-14 09:29:00 +00:00
.deref = &.{
.container = expr.?,
2022-12-14 10:08:07 +00:00
.field = ident.item,
2022-12-14 09:36:58 +00:00
},
2022-12-14 10:08:07 +00:00
};
} else if (iter.peek()) |next| if (next == .question_mark) {
_ = iter.next();
expr = .{
2022-12-14 09:36:58 +00:00
.optional_unwrap = blk: {
const e = expr.?;
break :blk &e;
2022-12-14 09:29:00 +00:00
},
2022-12-14 10:08:07 +00:00
};
};
2022-12-10 08:02:04 +00:00
last_valid_iter = iter;
2022-12-14 09:29:00 +00:00
},
.dollar => {
if (expr != null) break;
iter = skipWhitespace(iter);
expr = .{ .captures = {} };
if (iter.peek()) |n| if (n == .text) iter.putBack(.{ .period = {} });
last_valid_iter = iter;
},
.percent => {
if (expr != null) break;
iter = skipWhitespace(iter);
expr = .{ .context = {} };
if (iter.peek()) |n| if (n == .text) iter.putBack(.{ .period = {} });
last_valid_iter = iter;
},
.equals => {
const next = iter.next() orelse break;
if (next == .equals) {
const lhs = expr orelse break;
const rhs = parseExpression(iter);
iter = rhs.new_iter;
expr = .{
.equals = &.{
.lhs = lhs,
.rhs = rhs.item,
},
};
last_valid_iter = iter;
} else break;
},
.at => {
if (expr != null) break;
const builtin = parseBuiltin(iter);
iter = builtin.new_iter;
expr = .{ .builtin = &builtin.item };
last_valid_iter = iter;
},
2022-12-14 10:08:07 +00:00
.number => |n| {
if (expr != null) break;
const num = std.fmt.parseInt(isize, n, 10) catch @compileError("Error parsing integer");
expr = .{ .int = num };
last_valid_iter = iter;
},
2022-12-14 09:29:00 +00:00
else => break,
}
}
2022-11-18 09:58:13 +00:00
return .{
2022-12-10 08:02:04 +00:00
.new_iter = last_valid_iter orelse @compileError("Invalid Expression"),
.item = expr orelse @compileError("Invalid Expression"),
2022-11-18 09:58:13 +00:00
};
}
}
2022-12-10 08:32:24 +00:00
fn expectToken(comptime token: ?ControlToken, comptime exp: std.meta.Tag(ControlToken)) void {
comptime {
if (token == null) @compileError("Unexpected End Of Template");
const token_tag = std.meta.activeTag(token.?);
if (token_tag != exp)
@compileError("Expected " ++ @tagName(exp) ++ ", got " ++ @tagName(token_tag));
}
}
fn parseBuiltin(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, BuiltinCall) {
comptime {
var iter = tokens;
const builtin = blk: {
const next = iter.next() orelse @compileError("Invalid Builtin");
if (next != .text) @compileError("Invalid Builtin");
break :blk std.meta.stringToEnum(Builtin, next.text) orelse @compileError("Invalid Builtin");
};
iter = skipWhitespace(iter);
expectToken(iter.next(), .open_paren);
iter = skipWhitespace(iter);
const call = switch (builtin) {
.isTag => blk: {
const expr = parseExpression(iter);
2022-12-14 08:46:24 +00:00
iter = skipWhitespace(expr.new_iter);
2022-12-10 08:32:24 +00:00
expectToken(iter.next(), .comma);
iter = skipWhitespace(iter);
const tag = iter.next();
expectToken(tag, .text);
break :blk .{
.isTag = .{
.tag = tag.?.text,
.expression = expr.item,
},
};
},
2022-12-14 08:46:24 +00:00
.slice => blk: {
const expr = parseExpression(iter);
iter = skipWhitespace(expr.new_iter);
expectToken(iter.next(), .comma);
iter = skipWhitespace(iter);
const start = parseExpression(iter);
iter = skipWhitespace(start.new_iter);
expectToken(iter.next(), .comma);
iter = skipWhitespace(iter);
const end = parseExpression(iter);
iter = skipWhitespace(end.new_iter);
break :blk .{
.slice = .{
.iterable = expr.item,
.start = start.item,
.end = end.item,
},
};
},
2022-12-10 08:32:24 +00:00
};
iter = skipWhitespace(iter);
expectToken(iter.next(), .close_paren);
return .{
.new_iter = iter,
.item = call,
};
}
}
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 };
},
2022-12-08 01:26:36 +00:00
.@"else" => break .{ .@"else" = {} },
.@"elif" => {
const result = parseIfHeader(iter);
iter = result.new_iter;
break .{ .elif_header = result.item };
},
2022-12-08 07:24:32 +00:00
.template => {
const result = parseCallTemplate(iter);
iter = result.new_iter;
break .{ .call_template = result.item };
},
2022-12-10 09:21:39 +00:00
.@"switch" => {
const result = parseSwitchHeader(iter);
iter = result.new_iter;
break .{ .switch_header = result.item };
},
.case => {
const result = parseCaseHeader(iter);
iter = result.new_iter;
break .{ .case_header = result.item };
},
2022-12-12 06:50:44 +00:00
.format => {
const result = parseFormat(iter);
iter = result.new_iter;
break .{ .format = result.item };
},
2022-11-18 11:24:25 +00:00
//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 = {} },
2022-12-10 09:21:39 +00:00
.@"switch" => break .{ .end_switch = {} },
2022-11-18 11:24:25 +00:00
}
},
2022-12-08 11:21:18 +00:00
.period, .dollar, .percent => {
2022-11-18 11:24:25 +00:00
iter.putBack(token);
const expr = parseExpression(iter);
iter = expr.new_iter;
break .{ .expression = expr.item };
},
2022-12-12 06:50:44 +00:00
else => @compileError("TODO " ++ @tagName(token)),
};
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);
2022-12-14 10:08:07 +00:00
@compileError("TODO " ++ @tagName(token));
2022-11-18 09:58:13 +00:00
},
};
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-12-14 10:35:23 +00:00
var iter = skipWhitespace(iterable.new_iter);
2022-11-16 07:53:29 +00:00
2022-12-14 10:35:23 +00:00
const captures = tryParseCapture(iter) orelse {
@compileLog(iter.row);
@compileError("Expected capture");
2022-11-16 07:16:46 +00:00
};
2022-12-14 10:35:23 +00:00
if (captures.item.len == 0 or captures.item.len > 2) @compileError("Expected 1 or 2 captures");
2022-11-16 07:16:46 +00:00
2022-11-18 11:24:25 +00:00
return .{
2022-12-14 10:35:23 +00:00
.new_iter = captures.new_iter,
2022-11-18 11:24:25 +00:00
.item = .{
.iterable = iterable.item,
2022-12-14 10:35:23 +00:00
.item_capture = captures.item[0],
.idx_capture = if (captures.item.len == 2) captures.item[1] else null,
2022-11-18 11:24:25 +00:00
},
};
2022-11-18 07:39:32 +00:00
}
}
fn tryParseCapture(comptime tokens: ControlTokenIter) ?ParseResult(ControlTokenIter, []const []const u8) {
comptime {
2022-12-14 10:35:23 +00:00
var iter = skipWhitespace(tokens);
if ((iter.next() orelse return null) != .pipe) return null;
var captures: []const []const u8 = &.{};
while (true) {
2022-12-08 11:21:18 +00:00
iter = skipWhitespace(iter);
if ((iter.next() orelse return null) != .dollar) return null;
iter = skipWhitespace(iter);
const name = switch (iter.next() orelse return null) {
.text => |text| text,
else => return null,
};
iter = skipWhitespace(iter);
captures = captures ++ &[_][]const u8{name};
switch (iter.next() orelse return null) {
.pipe => break,
.comma => {},
else => return null,
}
}
return .{
.new_iter = iter,
.item = captures,
};
}
}
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
const captures = tryParseCapture(iter);
if (captures) |cap| {
if (cap.item.len == 1) {
return .{
.new_iter = cap.new_iter,
.item = IfHeader{
.condition = condition.item,
.capture = cap.item[0],
},
};
} else @compileError("Only one capture allowed for if statements");
}
2022-11-18 11:24:25 +00:00
return .{
.new_iter = iter,
.item = .{
.condition = condition.item,
.capture = null,
2022-11-18 11:24:25 +00:00
},
};
2022-11-16 07:16:46 +00:00
}
}
2022-12-10 09:21:39 +00:00
fn parseCaseHeader(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, CaseHeader) {
comptime {
var iter = skipWhitespace(tokens);
const tag = iter.next();
expectToken(tag, .text);
const captures = tryParseCapture(iter);
if (captures) |cap| {
if (cap.item.len == 1) {
return .{
.new_iter = cap.new_iter,
.item = CaseHeader{
.tag = tag.?.text,
.capture = cap.item[0],
},
};
} else @compileError("Only one capture allowed for case statements");
}
return .{
.new_iter = iter,
.item = .{
.tag = tag.?.text,
.capture = null,
},
};
}
}
fn parseSwitchHeader(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, SwitchHeader) {
comptime {
const condition = parseExpression(tokens);
var iter = skipWhitespace(condition.new_iter);
const next = iter.next();
expectToken(next, .text);
if (!std.mem.eql(u8, next.?.text, "case")) @compileError("Expected case following switch condition");
iter = skipWhitespace(iter);
const first = parseCaseHeader(iter);
return .{
.new_iter = first.new_iter,
.item = .{
.expression = condition.item,
.first_case = first.item,
},
};
}
}
2022-12-08 07:24:32 +00:00
fn parseCallTemplate(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, CallTemplate) {
comptime {
var iter = tokens;
const template_name = while (iter.next()) |token| switch (token) {
.text => |t| break t,
.whitespace => {},
else => @compileError("Unexpected token"),
} else @compileError("Unexpected end of template");
const args = parseExpression(iter);
return .{
.new_iter = args.new_iter,
.item = .{
.template_name = template_name,
.args = args.item,
},
};
}
}
2022-12-12 06:50:44 +00:00
fn parseFormat(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, FormatStmt) {
comptime {
var iter = skipWhitespace(tokens);
expectToken(iter.next(), .double_quote);
var fmt_str: []const u8 = "";
while (true) switch (iter.next() orelse @compileError("Unexpected end of template")) {
2022-12-14 10:08:07 +00:00
.text, .number, .whitespace => |t| fmt_str = fmt_str ++ t,
2022-12-12 06:50:44 +00:00
.open_bracket => fmt_str = fmt_str ++ "{",
.close_bracket => fmt_str = fmt_str ++ "}",
.period => fmt_str = fmt_str ++ ".",
.pound => fmt_str = fmt_str ++ "#",
.pipe => fmt_str = fmt_str ++ "|",
.dollar => fmt_str = fmt_str ++ "$",
.slash => fmt_str = fmt_str ++ "/",
.equals => fmt_str = fmt_str ++ "=",
.at => fmt_str = fmt_str ++ "@",
.comma => fmt_str = fmt_str ++ ",",
.percent => fmt_str = fmt_str ++ "%",
.open_paren => fmt_str = fmt_str ++ "(",
.close_paren => fmt_str = fmt_str ++ ")",
2022-12-13 09:33:22 +00:00
.question_mark => fmt_str = fmt_str ++ "?",
2022-12-12 06:50:44 +00:00
.double_quote => break,
};
const expr = parseExpression(iter);
return .{
.new_iter = expr.new_iter,
.item = .{
.format = fmt_str,
.value = expr.item,
},
};
}
}
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
};
2022-12-14 09:29:00 +00:00
const DerefExpr = struct {
container: Expression,
2022-12-13 09:33:22 +00:00
field: []const u8,
};
2022-12-10 08:02:04 +00:00
const EqualsExpr = struct {
lhs: Expression,
rhs: Expression,
};
const Expression = union(enum) {
2022-12-14 09:29:00 +00:00
args: void,
captures: void,
context: void,
deref: *const DerefExpr,
2022-12-10 08:02:04 +00:00
equals: *const EqualsExpr,
2022-12-10 08:32:24 +00:00
builtin: *const BuiltinCall,
2022-12-14 09:29:00 +00:00
optional_unwrap: *const Expression,
2022-12-14 10:08:07 +00:00
int: isize,
};
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-12-14 10:35:23 +00:00
item_capture: []const u8,
idx_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,
else_branch: ?union(enum) {
@"else": []const TemplateItem,
elif: *const If,
},
2022-11-18 11:24:25 +00:00
};
2022-12-10 09:21:39 +00:00
const Case = struct {
header: CaseHeader,
subtemplate: []const TemplateItem,
};
const SwitchHeader = struct {
expression: Expression,
first_case: CaseHeader,
};
const CaseHeader = struct {
tag: []const u8,
capture: ?[]const u8,
};
const Switch = struct {
expression: Expression,
cases: []const Case,
else_branch: ?[]const TemplateItem,
};
2022-12-08 07:24:32 +00:00
const CallTemplate = struct {
template_name: []const u8,
args: Expression,
};
2022-11-18 11:24:25 +00:00
const IfHeader = struct {
2022-11-18 07:39:32 +00:00
condition: Expression,
capture: ?[]const u8,
2022-11-18 07:39:32 +00:00
};
2022-12-12 06:50:44 +00:00
const FormatStmt = struct {
format: []const u8,
value: 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-12-10 09:21:39 +00:00
@"switch": Switch,
2022-12-08 07:24:32 +00:00
call_template: CallTemplate,
2022-12-12 06:50:44 +00:00
format: FormatStmt,
};
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,
2022-12-08 01:26:36 +00:00
@"else": void,
elif_header: IfHeader,
2022-12-08 07:24:32 +00:00
call_template: CallTemplate,
2022-12-10 09:21:39 +00:00
switch_header: SwitchHeader,
case_header: CaseHeader,
end_switch: void,
2022-12-12 06:50:44 +00:00
format: FormatStmt,
2022-11-18 11:24:25 +00:00
};
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-12-08 01:26:36 +00:00
@"else",
@"elif",
2022-12-08 07:24:32 +00:00
@"template",
2022-12-10 09:21:39 +00:00
@"switch",
@"case",
2022-12-12 06:50:44 +00:00
@"format",
2022-11-18 07:23:02 +00:00
};
const EndKeyword = enum {
@"for",
2022-11-18 07:39:32 +00:00
@"if",
2022-12-10 09:21:39 +00:00
@"switch",
};
2022-12-10 08:32:24 +00:00
const Builtin = enum {
isTag,
2022-12-14 08:46:24 +00:00
slice,
2022-12-10 08:32:24 +00:00
};
const BuiltinCall = union(Builtin) {
isTag: struct {
tag: []const u8,
expression: Expression,
},
2022-12-14 08:46:24 +00:00
slice: struct {
iterable: Expression,
start: Expression,
end: Expression,
},
2022-12-10 08:32:24 +00:00
};
2022-11-18 10:05:13 +00:00
const ControlToken = union(enum) {
2022-11-16 03:10:16 +00:00
text: []const u8,
2022-12-14 10:08:07 +00:00
number: []const u8,
2022-11-16 03:10:16 +00:00
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-12-08 07:47:07 +00:00
at: void,
comma: void,
2022-12-08 11:21:18 +00:00
percent: void,
2022-12-10 08:32:24 +00:00
open_paren: void,
close_paren: void,
2022-12-12 06:50:44 +00:00
double_quote: void,
2022-12-13 09:33:22 +00:00
question_mark: 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-12-14 09:29:00 +00:00
peeked_tokens: [2]?ControlToken = [2]?ControlToken{ null, null },
peeked_token_count: usize = 0,
2022-11-16 03:10:16 +00:00
2022-11-16 07:16:46 +00:00
row: usize = 0,
2022-12-14 10:08:07 +00:00
fn isControlChar(ch: u8) bool {
return switch (ch) {
'{', '}', '.', '#', '|', '$', '/', '=', '@', ',', '%', '(', ')', '"', '?' => true,
else => false,
};
}
fn isTextChar(ch: u8) bool {
return !std.ascii.isWhitespace(ch) and !std.ascii.isDigit(ch) and !isControlChar(ch);
}
2022-11-18 10:05:13 +00:00
fn next(self: *ControlTokenIter) ?ControlToken {
2022-12-14 09:29:00 +00:00
if (self.peeked_token_count != 0) {
const t = self.peeked_tokens[self.peeked_token_count - 1].?;
self.peeked_tokens[self.peeked_token_count - 1] = null;
self.peeked_token_count -= 1;
return t;
2022-11-16 03:10:16 +00:00
}
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-12-08 07:47:07 +00:00
'@' => return .{ .at = {} },
',' => return .{ .comma = {} },
2022-12-08 11:21:18 +00:00
'%' => return .{ .percent = {} },
2022-12-10 08:32:24 +00:00
'(' => return .{ .open_paren = {} },
')' => return .{ .close_paren = {} },
2022-12-12 06:50:44 +00:00
'"' => return .{ .double_quote = {} },
2022-12-13 09:33:22 +00:00
'?' => return .{ .question_mark = {} },
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] };
},
2022-12-14 10:08:07 +00:00
'0'...'9' => {
var idx: usize = 0;
while (idx < remaining.len and std.ascii.isDigit(remaining[idx])) : (idx += 1) {}
self.start += idx - 1;
return .{ .number = remaining[0..idx] };
},
2022-11-16 03:10:16 +00:00
else => {
var idx: usize = 0;
2022-12-14 10:08:07 +00:00
while (idx < remaining.len and isTextChar(remaining[idx])) : (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();
2022-12-14 09:29:00 +00:00
if (token) |t| self.putBack(t);
2022-11-16 03:10:16 +00:00
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-12-14 09:29:00 +00:00
std.debug.assert(self.peeked_token_count < self.peeked_tokens.len);
self.peeked_tokens[self.peeked_token_count] = token;
self.peeked_token_count += 1;
2022-11-18 09:58:13 +00:00
}
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);
2022-12-08 07:47:07 +00:00
try execute(stream.writer(), .{}, tmpl, args, .{});
2022-12-03 05:49:17 +00:00
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");
2022-12-14 09:29:00 +00:00
try testCase("{#for .vals |$val|}{$val}{/for}", .{ .vals = [_]u8{ 1, 2, 3 } }, "123");
try testCase("{#if .val}1{#else}0{/if}", .{ .val = true }, "1");
try testCase("{#if .val}1{#else}0{/if}", .{ .val = false }, "0");
try testCase("{#if .val}1{#elif .foo}2{/if}", .{ .val = false, .foo = true }, "2");
try testCase("{#if .val}1{#elif .foo}2{/if}", .{ .val = false, .foo = false }, "");
try testCase("{#if .val}1{#elif .foo}2{#else}0{/if}", .{ .val = false, .foo = false }, "0");
try testCase("{#if .val}1{#elif .foo}2{#else}0{/if}", .{ .val = true, .foo = false }, "1");
try testCase("{#if .val}1{#elif .foo}2{#else}0{/if}", .{ .val = false, .foo = true }, "2");
try testCase("{#if .val}1{#elif .foo}2{#else}0{/if}", .{ .val = true, .foo = true }, "1");
2022-12-03 05:49:17 +00:00
}