parent
f8a799abfb
commit
9dabc237e0
2 changed files with 87 additions and 32 deletions
|
@ -8,8 +8,8 @@ pub fn main() !void {
|
|||
&.{ "5", "4", "3", "2", "1" },
|
||||
&.{ "5", "4", "3", "2", "1" },
|
||||
},
|
||||
.qux = true,
|
||||
.quxx = false,
|
||||
.qux = false,
|
||||
.quxx = true,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,10 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
|
|||
if (condition) {
|
||||
try executeTemplate(writer, subtemplate, args, captures);
|
||||
} else {
|
||||
if (if_stmt.else_branch) |branch| try executeTemplate(writer, branch, args, captures);
|
||||
if (if_stmt.else_branch) |branch| switch (branch) {
|
||||
.@"else" => |subtmpl| try executeTemplate(writer, subtmpl, args, captures),
|
||||
.elif => |elif| try executeStatement(writer, .{ .@"if" = elif.* }, args, captures),
|
||||
};
|
||||
}
|
||||
},
|
||||
//else => @compileError("TODO"),
|
||||
|
@ -141,6 +144,48 @@ const TemplateParseResult = struct {
|
|||
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,
|
||||
}
|
||||
}
|
||||
|
||||
fn parseTemplate(
|
||||
comptime tokens: []const TemplateToken,
|
||||
comptime start: usize,
|
||||
|
@ -155,9 +200,12 @@ fn parseTemplate(
|
|||
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 != 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;
|
||||
}
|
||||
current_text = current_text ++ wsp;
|
||||
},
|
||||
.control_block => |cb| {
|
||||
|
@ -169,27 +217,13 @@ fn parseTemplate(
|
|||
switch (cb.block) {
|
||||
.expression => |expr| items = items ++ [_]TemplateItem{.{ .statement = .{ .expression = expr } }},
|
||||
.if_header => |header| {
|
||||
if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1;
|
||||
const subtemplate = parseTemplate(tokens, i + 1, .if_block);
|
||||
i = subtemplate.new_idx;
|
||||
const else_subtemplate: ?TemplateParseResult = if (subtemplate.closing_block.?.block == .@"else")
|
||||
parseTemplate(tokens, i + 1, .if_else_block)
|
||||
else
|
||||
null;
|
||||
if (else_subtemplate) |sub| i = sub.new_idx;
|
||||
const parsed = parseIfBlock(header, tokens, i + 1);
|
||||
i = parsed.new_iter;
|
||||
items = items ++ [_]TemplateItem{.{
|
||||
.statement = .{
|
||||
.@"if" = .{
|
||||
.subtemplate = subtemplate.items,
|
||||
.header = header,
|
||||
|
||||
.else_branch = if (else_subtemplate) |sub| sub.items else null,
|
||||
},
|
||||
},
|
||||
.statement = .{ .@"if" = parsed.item },
|
||||
}};
|
||||
},
|
||||
.for_header => |header| {
|
||||
if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1;
|
||||
const subtemplate = parseTemplate(tokens, i + 1, .for_block);
|
||||
items = items ++ [_]TemplateItem{.{
|
||||
.statement = .{
|
||||
|
@ -209,17 +243,15 @@ fn parseTemplate(
|
|||
break cb
|
||||
else
|
||||
@compileError("Unexpected /if tag"),
|
||||
.elif_header => if (template_type == .if_block)
|
||||
break cb
|
||||
else
|
||||
@compileError("Unexpected #elif tag"),
|
||||
.@"else" => if (template_type == .if_block)
|
||||
break cb
|
||||
else
|
||||
@compileError("Unexpected #else tag"),
|
||||
}
|
||||
|
||||
if (i != tokens.len - 1 and tokens[i] == .control_block) {
|
||||
if (tokens[i].control_block.strip_after and tokens[i + 1] == .whitespace) {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
} else null;
|
||||
|
@ -340,6 +372,11 @@ fn parseControlBlock(comptime tokens: ControlTokenIter) ParseResult(ControlToken
|
|||
break .{ .if_header = result.item };
|
||||
},
|
||||
.@"else" => break .{ .@"else" = {} },
|
||||
.@"elif" => {
|
||||
const result = parseIfHeader(iter);
|
||||
iter = result.new_iter;
|
||||
break .{ .elif_header = result.item };
|
||||
},
|
||||
|
||||
//else => @compileError("TODO"),
|
||||
}
|
||||
|
@ -522,7 +559,10 @@ const ForHeader = struct {
|
|||
const If = struct {
|
||||
subtemplate: []const TemplateItem,
|
||||
header: IfHeader,
|
||||
else_branch: ?[]const TemplateItem,
|
||||
else_branch: ?union(enum) {
|
||||
@"else": []const TemplateItem,
|
||||
elif: *const If,
|
||||
},
|
||||
};
|
||||
|
||||
const IfHeader = struct {
|
||||
|
@ -543,6 +583,7 @@ const ControlBlock = struct {
|
|||
if_header: IfHeader,
|
||||
end_if: void,
|
||||
@"else": void,
|
||||
elif_header: IfHeader,
|
||||
};
|
||||
block: Data,
|
||||
strip_before: bool,
|
||||
|
@ -553,6 +594,7 @@ const Keyword = enum {
|
|||
@"for",
|
||||
@"if",
|
||||
@"else",
|
||||
@"elif",
|
||||
};
|
||||
|
||||
const EndKeyword = enum {
|
||||
|
@ -646,4 +688,12 @@ test "template" {
|
|||
try testCase("{#if .val}1{/if}", .{ .val = true }, "1");
|
||||
try testCase("{#for .vals |$v|}{$v}{/for}", .{ .vals = [_]u8{ 1, 2, 3 } }, "123");
|
||||
try testCase("{#for .vals |$v|=} {$v} {=/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");
|
||||
}
|
||||
|
|
|
@ -14,8 +14,13 @@
|
|||
{$b}:
|
||||
{= /for =}
|
||||
{= /for}
|
||||
{#if .qux}qux{#else}!qux{/if}
|
||||
{#if .quxx}quxx{#else}!quxx{/if}
|
||||
{#if .qux=}
|
||||
qux
|
||||
{=#elif .quxx=}
|
||||
quxx
|
||||
{=#else=}
|
||||
neither
|
||||
{=/if}
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue