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