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

View file

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