Split up template parsing and execution
This commit is contained in:
parent
3b03764be7
commit
7191ad8f27
2 changed files with 130 additions and 45 deletions
|
@ -4,63 +4,141 @@ pub fn main() !void {
|
||||||
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{ .community = .{ .name = "abcd" } });
|
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 {
|
pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
|
||||||
@setEvalBranchQuota(@intCast(u32, template.len * 6));
|
@setEvalBranchQuota(@intCast(u32, template.len * 6));
|
||||||
comptime var iter = TokenIter{ .text = template };
|
comptime var iter = TokenIter{ .text = template };
|
||||||
comptime var state: State = .text;
|
|
||||||
inline while (comptime iter.next()) |token| {
|
inline while (comptime iter.next()) |token| {
|
||||||
|
if (logging) @compileLog(token);
|
||||||
switch (token) {
|
switch (token) {
|
||||||
.text => |text| switch (state) {
|
.text => |text| try writer.writeAll(text),
|
||||||
.text => try writer.writeAll(text),
|
.open_bracket => {
|
||||||
else => @compileError("Text token not allowed in state " ++ @tagName(state) ++ text),
|
const next = comptime iter.peek() orelse @compileError("Unexpected end of template");
|
||||||
},
|
if (next == .open_bracket) {
|
||||||
.open_bracket => switch (state) {
|
|
||||||
.text => state = .template_start,
|
|
||||||
.template_start => {
|
|
||||||
try writer.writeByte('{');
|
try writer.writeByte('{');
|
||||||
state = .text;
|
_ = comptime iter.next();
|
||||||
},
|
} else {
|
||||||
else => @compileError(""),
|
const result = comptime parseStatement(iter);
|
||||||
|
try executeStatement(writer, result.statement, args);
|
||||||
|
iter = result.new_iter;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.close_bracket => switch (state) {
|
.close_bracket => {
|
||||||
.text => state = .text_close_bracket,
|
const next = comptime iter.next() orelse @compileError("Unexpected end of template");
|
||||||
.text_close_bracket => {
|
if (comptime next == .close_bracket) try writer.writeByte('}') else @compileError("Unpaired close bracket, did you mean \"}}\"?");
|
||||||
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(""),
|
|
||||||
},
|
},
|
||||||
|
.whitespace => |wsp| try writer.writeAll(wsp),
|
||||||
|
.period => try writer.writeByte('.'),
|
||||||
|
.pound => try writer.writeByte('#'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn argDeref(writer: anytype, comptime iter: *TokenIter, arg: anytype) !void {
|
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype) !void {
|
||||||
inline while (comptime iter.peek()) |token| {
|
switch (stmt) {
|
||||||
switch (token) {
|
.expression => |expr| switch (expr) {
|
||||||
.period => {},
|
.arg_deref => |fields| try argDeref(writer, fields, args),
|
||||||
.text => |text| {
|
},
|
||||||
_ = comptime iter.next();
|
else => @compileError("TODO"),
|
||||||
return argDeref(writer, iter, @field(arg, text));
|
|
||||||
},
|
|
||||||
else => return try writer.writeAll(arg),
|
|
||||||
}
|
|
||||||
_ = comptime iter.next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
const State = enum {
|
||||||
text,
|
text,
|
||||||
text_close_bracket,
|
text_close_bracket,
|
||||||
|
@ -68,12 +146,17 @@ const State = enum {
|
||||||
template,
|
template,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Keyword = enum {
|
||||||
|
@"for",
|
||||||
|
};
|
||||||
|
|
||||||
const Token = union(enum) {
|
const Token = union(enum) {
|
||||||
text: []const u8,
|
text: []const u8,
|
||||||
open_bracket: void,
|
open_bracket: void,
|
||||||
close_bracket: void,
|
close_bracket: void,
|
||||||
period: void,
|
period: void,
|
||||||
whitespace: []const u8,
|
whitespace: []const u8,
|
||||||
|
pound: void,
|
||||||
};
|
};
|
||||||
|
|
||||||
const TokenIter = struct {
|
const TokenIter = struct {
|
||||||
|
@ -96,6 +179,7 @@ const TokenIter = struct {
|
||||||
'{' => return .{ .open_bracket = {} },
|
'{' => return .{ .open_bracket = {} },
|
||||||
'}' => return .{ .close_bracket = {} },
|
'}' => return .{ .close_bracket = {} },
|
||||||
'.' => return .{ .period = {} },
|
'.' => return .{ .period = {} },
|
||||||
|
'#' => return .{ .pound = {} },
|
||||||
' ', '\t', '\n', '\r' => {
|
' ', '\t', '\n', '\r' => {
|
||||||
var idx: usize = 0;
|
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) {}
|
||||||
|
@ -105,7 +189,7 @@ const TokenIter = struct {
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
var idx: usize = 0;
|
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;
|
self.start += idx - 1;
|
||||||
return .{ .text = remaining[0..idx] };
|
return .{ .text = remaining[0..idx] };
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>{ .community.name }</title>
|
<title>{.community.name}</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -9,10 +9,11 @@
|
||||||
<h2> {{ REAL BRACKETS }} </h2>
|
<h2> {{ REAL BRACKETS }} </h2>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
|
{{#for .community}}
|
||||||
{{#for args.notes |$note, $i|}}
|
{{#for args.notes |$note, $i|}}
|
||||||
<h3>Note no. {{$i}}</h3>
|
<h3>Note no. {{$i}}</h3>
|
||||||
{{#template note_display ($note)}}
|
{{#template note_display ($note)}}
|
||||||
{{/for}}
|
{{#end}}
|
||||||
</section>
|
</section>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue