Parse else blocks

This commit is contained in:
jaina heartles 2022-12-07 17:26:36 -08:00
parent b0864b29d8
commit bb297a4f6e
2 changed files with 17 additions and 3 deletions

View File

@ -128,6 +128,7 @@ const TemplateType = enum {
root,
for_block,
if_block,
if_else_block,
};
const TemplateParseResult = struct {
@ -166,6 +167,12 @@ fn parseTemplate(
.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;
items = items ++ [_]TemplateItem{.{
.statement = .{
.@"if" = .{
@ -174,7 +181,6 @@ fn parseTemplate(
},
},
}};
i = subtemplate.new_idx;
},
.for_header => |header| {
if (i != tokens.len - 1 and tokens[i + 1] == .whitespace and cb.strip_after) i += 1;
@ -193,10 +199,14 @@ fn parseTemplate(
break cb
else
@compileError("Unexpected /for tag"),
.end_if => if (template_type == .if_block)
.end_if => if (template_type == .if_block or template_type == .if_else_block)
break cb
else
@compileError("Unexpected /if tag"),
.@"else" => if (template_type == .if_block)
break cb
else
@compileError("Unexpected #else tag"),
}
if (i != tokens.len - 1 and tokens[i] == .control_block) {
@ -323,6 +333,7 @@ fn parseControlBlock(comptime tokens: ControlTokenIter) ParseResult(ControlToken
iter = result.new_iter;
break .{ .if_header = result.item };
},
.@"else" => break .{ .@"else" = {} },
//else => @compileError("TODO"),
}
@ -524,6 +535,7 @@ const ControlBlock = struct {
end_for: void,
if_header: IfHeader,
end_if: void,
@"else": void,
};
block: Data,
strip_before: bool,
@ -533,6 +545,7 @@ const ControlBlock = struct {
const Keyword = enum {
@"for",
@"if",
@"else",
};
const EndKeyword = enum {

View File

@ -14,7 +14,8 @@
{$b}:
{= /for =}
{= /for}
{#if .qux}qux!{#elif .quxx}quxx!{/if=}
{#if .qux}qux!{#else}!qux!{/if=}
{#if .quxx}quxx!{#else}!quxx!{/if}
</section>
</body>
</html>