Split up template parsing and execution

This commit is contained in:
jaina heartles 2022-11-15 22:07:45 -08:00
parent 3b03764be7
commit 7191ad8f27
2 changed files with 130 additions and 45 deletions

View file

@ -4,63 +4,141 @@ pub fn main() !void {
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{ .community = .{ .name = "abcd" } });
}
const logging = false;
pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
@setEvalBranchQuota(@intCast(u32, template.len * 6));
comptime var iter = TokenIter{ .text = template };
comptime var state: State = .text;
inline while (comptime iter.next()) |token| {
if (logging) @compileLog(token);
switch (token) {
.text => |text| switch (state) {
.text => try writer.writeAll(text),
else => @compileError("Text token not allowed in state " ++ @tagName(state) ++ text),
},
.open_bracket => switch (state) {
.text => state = .template_start,
.template_start => {
.text => |text| try writer.writeAll(text),
.open_bracket => {
const next = comptime iter.peek() orelse @compileError("Unexpected end of template");
if (next == .open_bracket) {
try writer.writeByte('{');
state = .text;
},
else => @compileError(""),
_ = comptime iter.next();
} else {
const result = comptime parseStatement(iter);
try executeStatement(writer, result.statement, args);
iter = result.new_iter;
}
},
.close_bracket => switch (state) {
.text => state = .text_close_bracket,
.text_close_bracket => {
try writer.writeByte('}');
state = .text;
},
.template_start, .template => state = .text,
//else => @compileError(""),
},
.whitespace => |wsp| switch (state) {
.text => try writer.writeAll(wsp),
else => {},
},
.period => switch (state) {
.text => try writer.writeByte('.'),
.template_start, .template => {
try argDeref(writer, &iter, args);
state = .template;
},
else => @compileError(""),
.close_bracket => {
const next = comptime iter.next() orelse @compileError("Unexpected end of template");
if (comptime next == .close_bracket) try writer.writeByte('}') else @compileError("Unpaired close bracket, did you mean \"}}\"?");
},
.whitespace => |wsp| try writer.writeAll(wsp),
.period => try writer.writeByte('.'),
.pound => try writer.writeByte('#'),
}
}
}
fn argDeref(writer: anytype, comptime iter: *TokenIter, arg: anytype) !void {
inline while (comptime iter.peek()) |token| {
switch (token) {
.period => {},
.text => |text| {
_ = comptime iter.next();
return argDeref(writer, iter, @field(arg, text));
},
else => return try writer.writeAll(arg),
}
_ = comptime iter.next();
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype) !void {
switch (stmt) {
.expression => |expr| switch (expr) {
.arg_deref => |fields| try argDeref(writer, fields, args),
},
else => @compileError("TODO"),
}
}
fn argDeref(writer: anytype, comptime names: []const []const u8, arg: anytype) !void {
if (names.len == 0) {
const T = @TypeOf(arg);
if (comptime std.meta.trait.isZigString(T)) return writer.writeAll(arg);
return std.fmt.format(writer, "{any}", .{arg});
}
return argDeref(writer, names[1..], @field(arg, names[0]));
}
fn parseStatement(comptime tokens: TokenIter) StatementResult {
comptime {
var iter = tokens;
while (iter.next()) |token| switch (token) {
.whitespace => {},
.pound => {
const next = iter.next() orelse @compileError("Unexpected end of template");
if (logging) @compileLog("keyword", next);
if (next != .text) @compileError("Expected keyword following '#' character");
const text = next.text;
const keyword = std.meta.stringToEnum(Keyword, text) orelse @compileError("Unknown keyword: " ++ text);
_ = keyword;
@panic("todo");
},
.period => {
const expr = parseArgDeref(iter);
iter = expr.new_iter;
while (iter.next()) |it| switch (it) {
.whitespace => {},
.close_bracket => break,
else => @compileError("TODO"),
};
return .{ .new_iter = iter, .statement = .{
.expression = expr.expression,
} };
},
else => @compileError(""),
};
@compileError("Unexpected end of template");
}
}
fn parseArgDeref(comptime tokens: TokenIter) ExpressionResult {
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,
.expression = .{ .arg_deref = fields },
} else @compileError("Unexpected token"),
}
_ = iter.next();
}
}
}
const Expression = union(enum) {
arg_deref: []const []const u8,
};
const ExpressionResult = struct {
new_iter: TokenIter,
expression: Expression,
};
const Statement = union(enum) {
expression: Expression,
for_loop: struct {
subtemplate: []const u8,
indexable: Expression,
iteration_capture: []const u8,
index_capture: ?[]const u8,
},
};
const StatementResult = struct {
new_iter: TokenIter,
statement: Statement,
};
const State = enum {
text,
text_close_bracket,
@ -68,12 +146,17 @@ const State = enum {
template,
};
const Keyword = enum {
@"for",
};
const Token = union(enum) {
text: []const u8,
open_bracket: void,
close_bracket: void,
period: void,
whitespace: []const u8,
pound: void,
};
const TokenIter = struct {
@ -96,6 +179,7 @@ const TokenIter = struct {
'{' => return .{ .open_bracket = {} },
'}' => return .{ .close_bracket = {} },
'.' => return .{ .period = {} },
'#' => return .{ .pound = {} },
' ', '\t', '\n', '\r' => {
var idx: usize = 0;
while (idx < remaining.len and std.mem.indexOfScalar(u8, " \t\n\r", remaining[idx]) != null) : (idx += 1) {}
@ -105,7 +189,7 @@ const TokenIter = struct {
},
else => {
var idx: usize = 0;
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}. \t\n\r", remaining[idx]) == null) : (idx += 1) {}
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}.# \t\n\r", remaining[idx]) == null) : (idx += 1) {}
self.start += idx - 1;
return .{ .text = remaining[0..idx] };

View file

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>{ .community.name }</title>
<title>{.community.name}</title>
</head>
<body>
@ -9,10 +9,11 @@
<h2> {{ REAL BRACKETS }} </h2>
<section>
{{#for .community}}
{{#for args.notes |$note, $i|}}
<h3>Note no. {{$i}}</h3>
{{#template note_display ($note)}}
{{/for}}
{{#end}}
</section>
</body>
</html>