Parse template calls

This commit is contained in:
jaina heartles 2022-12-07 23:24:32 -08:00
parent 9dabc237e0
commit e072b045c6
2 changed files with 45 additions and 0 deletions

View File

@ -8,6 +8,7 @@ pub fn main() !void {
&.{ "5", "4", "3", "2", "1" },
&.{ "5", "4", "3", "2", "1" },
},
.bar = .{ .x = "x" },
.qux = false,
.quxx = true,
});
@ -61,6 +62,9 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
};
}
},
.call_template => |call| {
std.log.debug("calling template {s} with arg {any}", .{ call.template_name, call.args });
},
//else => @compileError("TODO"),
}
}
@ -251,6 +255,11 @@ fn parseTemplate(
break cb
else
@compileError("Unexpected #else tag"),
.call_template => |call| {
items = items ++ [_]TemplateItem{.{
.statement = .{ .call_template = call },
}};
},
}
},
}
@ -377,6 +386,11 @@ fn parseControlBlock(comptime tokens: ControlTokenIter) ParseResult(ControlToken
iter = result.new_iter;
break .{ .elif_header = result.item };
},
.template => {
const result = parseCallTemplate(iter);
iter = result.new_iter;
break .{ .call_template = result.item };
},
//else => @compileError("TODO"),
}
@ -529,6 +543,27 @@ fn parseDeref(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, [
}
}
fn parseCallTemplate(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIter, CallTemplate) {
comptime {
var iter = tokens;
const template_name = while (iter.next()) |token| switch (token) {
.text => |t| break t,
.whitespace => {},
else => @compileError("Unexpected token"),
} else @compileError("Unexpected end of template");
const args = parseExpression(iter);
return .{
.new_iter = args.new_iter,
.item = .{
.template_name = template_name,
.args = args.item,
},
};
}
}
fn ParseResult(comptime It: type, comptime T: type) type {
return struct {
new_iter: It,
@ -565,6 +600,11 @@ const If = struct {
},
};
const CallTemplate = struct {
template_name: []const u8,
args: Expression,
};
const IfHeader = struct {
condition: Expression,
};
@ -573,6 +613,7 @@ const Statement = union(enum) {
expression: Expression,
@"for": For,
@"if": If,
call_template: CallTemplate,
};
const ControlBlock = struct {
@ -584,6 +625,7 @@ const ControlBlock = struct {
end_if: void,
@"else": void,
elif_header: IfHeader,
call_template: CallTemplate,
};
block: Data,
strip_before: bool,
@ -595,6 +637,7 @@ const Keyword = enum {
@"if",
@"else",
@"elif",
@"template",
};
const EndKeyword = enum {

View File

@ -21,6 +21,8 @@
{=#else=}
neither
{=/if}
{#template test_tmpl .bar}
</section>
</body>
</html>