Add loop captures

This commit is contained in:
jaina heartles 2022-11-15 23:53:29 -08:00
parent dcf777f5ea
commit 9d74bce266
2 changed files with 55 additions and 20 deletions

View file

@ -33,8 +33,10 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype) !v
.for_loop => |loop| { .for_loop => |loop| {
const fields = loop.iterable.arg_deref; const fields = loop.iterable.arg_deref;
const iterable = argDeref(args, fields); const iterable = argDeref(args, fields);
for (iterable) |str| { const subtemplate = loop.subtemplate;
try writer.writeAll(str); try writer.writeAll("capture on " ++ loop.capture ++ ": ");
for (iterable) |_| {
try executeTemplate(writer, subtemplate, args);
} }
}, },
else => @compileError("TODO"), else => @compileError("TODO"),
@ -102,6 +104,8 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
}, },
.period => current_text = current_text ++ ".", .period => current_text = current_text ++ ".",
.pound => current_text = current_text ++ "#", .pound => current_text = current_text ++ "#",
.pipe => current_text = current_text ++ "|",
.dollar => current_text = current_text ++ "$",
} }
} }
@ -166,22 +170,56 @@ fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
} }
} }
fn skipWhitespace(comptime tokens: TokenIter) TokenIter {
comptime {
var iter = tokens;
while (iter.peek()) |token| switch (token) {
.whitespace => _ = iter.next(),
else => break,
};
return iter;
}
}
fn endStatement(comptime tokens: TokenIter) TokenIter {
comptime {
var iter = skipWhitespace(tokens);
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .close_bracket) @compileError("Unexpected token");
return iter;
}
}
fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) { fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
comptime { comptime {
const iterable = parseExpression(tokens); const iterable = parseExpression(tokens);
var iter = iterable.new_iter; var iter = iterable.new_iter;
while (iter.next()) |token| switch (token) {
.whitespace => {}, iter = skipWhitespace(iter);
.close_bracket => break, {
else => @compileError("Unexpected token"), 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;
}; };
{
const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .pipe) @compileError("Unexpected token");
}
iter = endStatement(iter);
const subtemplate = parseTemplate(iter, .subtemplate); const subtemplate = parseTemplate(iter, .subtemplate);
return .{ .new_iter = subtemplate.new_iter, .item = .{ return .{ .new_iter = subtemplate.new_iter, .item = .{ .iterable = iterable.item, .subtemplate = subtemplate.item, .capture = capture } };
.iterable = iterable.item,
.subtemplate = subtemplate.item,
} };
} }
} }
@ -247,6 +285,7 @@ const Expression = union(enum) {
const ForLoop = struct { const ForLoop = struct {
subtemplate: []const TemplateItem, subtemplate: []const TemplateItem,
iterable: Expression, iterable: Expression,
capture: []const u8,
}; };
const Statement = union(enum) { const Statement = union(enum) {
@ -255,13 +294,6 @@ const Statement = union(enum) {
end_for: void, end_for: void,
}; };
const State = enum {
text,
text_close_bracket,
template_start,
template,
};
const Keyword = enum { const Keyword = enum {
@"for", @"for",
end_for, end_for,
@ -274,6 +306,8 @@ const Token = union(enum) {
period: void, period: void,
whitespace: []const u8, whitespace: []const u8,
pound: void, pound: void,
pipe: void,
dollar: void,
}; };
const TokenIter = struct { const TokenIter = struct {
@ -299,6 +333,8 @@ const TokenIter = struct {
'}' => return .{ .close_bracket = {} }, '}' => return .{ .close_bracket = {} },
'.' => return .{ .period = {} }, '.' => return .{ .period = {} },
'#' => return .{ .pound = {} }, '#' => return .{ .pound = {} },
'|' => return .{ .pipe = {} },
'$' => return .{ .dollar = {} },
' ', '\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) {}
@ -310,7 +346,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] };

View file

@ -9,8 +9,7 @@
<h2> {{ REAL BRACKETS }} </h2> <h2> {{ REAL BRACKETS }} </h2>
<section> <section>
{#for .foo} {#for .foo |$f|}a testing thing {#end_for}
{#end_for}
{{#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)}}