From b0864b29d86846de08a0a07d57b3a9a768d46a2f Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Wed, 7 Dec 2022 17:10:53 -0800 Subject: [PATCH] Return closing block from parseTemplate --- src/template/lib.zig | 33 +++++++++++++++++++++------------ src/template/test.tmp.html | 3 +-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/template/lib.zig b/src/template/lib.zig index f1e141d..68ee360 100644 --- a/src/template/lib.zig +++ b/src/template/lib.zig @@ -18,7 +18,7 @@ pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !v const tokens = comptime parseTemplateTokens(ControlTokenIter{ .text = template }); const tmpl = comptime parseTemplate(tokens, 0, .root); - try executeTemplate(writer, tmpl.item, args, .{}); + try executeTemplate(writer, tmpl.items, args, .{}); } fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void { @@ -130,17 +130,23 @@ const TemplateType = enum { if_block, }; +const TemplateParseResult = struct { + new_idx: usize, + items: []const TemplateItem, + closing_block: ?ControlBlock, +}; + fn parseTemplate( comptime tokens: []const TemplateToken, comptime start: usize, comptime template_type: TemplateType, -) ParseResult(usize, []const TemplateItem) { +) TemplateParseResult { comptime { var i: usize = start; var current_text: []const u8 = ""; var items: []const TemplateItem = &.{}; - while (i < tokens.len) : (i += 1) { + const closing_block: ?ControlBlock = while (i < tokens.len) : (i += 1) { switch (tokens[i]) { .text => |text| current_text = current_text ++ text, .whitespace => |wsp| { @@ -163,12 +169,12 @@ fn parseTemplate( items = items ++ [_]TemplateItem{.{ .statement = .{ .@"if" = .{ - .subtemplate = subtemplate.item, + .subtemplate = subtemplate.items, .header = header, }, }, }}; - i = subtemplate.new_iter; + i = subtemplate.new_idx; }, .for_header => |header| { if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1; @@ -176,19 +182,19 @@ fn parseTemplate( items = items ++ [_]TemplateItem{.{ .statement = .{ .@"for" = .{ - .subtemplate = subtemplate.item, + .subtemplate = subtemplate.items, .header = header, }, }, }}; - i = subtemplate.new_iter; + i = subtemplate.new_idx; }, .end_for => if (template_type == .for_block) - break + break cb else @compileError("Unexpected /for tag"), .end_if => if (template_type == .if_block) - break + break cb else @compileError("Unexpected /if tag"), } @@ -200,13 +206,16 @@ fn parseTemplate( } }, } - } else if (template_type != .root) @compileError("End tag not found"); + } else null; if (current_text.len != 0) items = items ++ [_]TemplateItem{.{ .text = current_text }}; + if (template_type != .root and closing_block == null) @compileError("End tag not found"); + return .{ - .new_iter = i, - .item = items, + .new_idx = i, + .items = items, + .closing_block = closing_block, }; } } diff --git a/src/template/test.tmp.html b/src/template/test.tmp.html index 72435f4..772be18 100644 --- a/src/template/test.tmp.html +++ b/src/template/test.tmp.html @@ -14,8 +14,7 @@ {$b}: {= /for =} {= /for} - {#if .qux}qux!{/if=} - {#if .quxx}quxx!{/if} + {#if .qux}qux!{#elif .quxx}quxx!{/if=}