Parse template calls
This commit is contained in:
parent
9dabc237e0
commit
e072b045c6
2 changed files with 45 additions and 0 deletions
|
@ -8,6 +8,7 @@ pub fn main() !void {
|
||||||
&.{ "5", "4", "3", "2", "1" },
|
&.{ "5", "4", "3", "2", "1" },
|
||||||
&.{ "5", "4", "3", "2", "1" },
|
&.{ "5", "4", "3", "2", "1" },
|
||||||
},
|
},
|
||||||
|
.bar = .{ .x = "x" },
|
||||||
.qux = false,
|
.qux = false,
|
||||||
.quxx = true,
|
.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"),
|
//else => @compileError("TODO"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,6 +255,11 @@ fn parseTemplate(
|
||||||
break cb
|
break cb
|
||||||
else
|
else
|
||||||
@compileError("Unexpected #else tag"),
|
@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;
|
iter = result.new_iter;
|
||||||
break .{ .elif_header = result.item };
|
break .{ .elif_header = result.item };
|
||||||
},
|
},
|
||||||
|
.template => {
|
||||||
|
const result = parseCallTemplate(iter);
|
||||||
|
iter = result.new_iter;
|
||||||
|
break .{ .call_template = result.item };
|
||||||
|
},
|
||||||
|
|
||||||
//else => @compileError("TODO"),
|
//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 {
|
fn ParseResult(comptime It: type, comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
new_iter: It,
|
new_iter: It,
|
||||||
|
@ -565,6 +600,11 @@ const If = struct {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const CallTemplate = struct {
|
||||||
|
template_name: []const u8,
|
||||||
|
args: Expression,
|
||||||
|
};
|
||||||
|
|
||||||
const IfHeader = struct {
|
const IfHeader = struct {
|
||||||
condition: Expression,
|
condition: Expression,
|
||||||
};
|
};
|
||||||
|
@ -573,6 +613,7 @@ const Statement = union(enum) {
|
||||||
expression: Expression,
|
expression: Expression,
|
||||||
@"for": For,
|
@"for": For,
|
||||||
@"if": If,
|
@"if": If,
|
||||||
|
call_template: CallTemplate,
|
||||||
};
|
};
|
||||||
|
|
||||||
const ControlBlock = struct {
|
const ControlBlock = struct {
|
||||||
|
@ -584,6 +625,7 @@ const ControlBlock = struct {
|
||||||
end_if: void,
|
end_if: void,
|
||||||
@"else": void,
|
@"else": void,
|
||||||
elif_header: IfHeader,
|
elif_header: IfHeader,
|
||||||
|
call_template: CallTemplate,
|
||||||
};
|
};
|
||||||
block: Data,
|
block: Data,
|
||||||
strip_before: bool,
|
strip_before: bool,
|
||||||
|
@ -595,6 +637,7 @@ const Keyword = enum {
|
||||||
@"if",
|
@"if",
|
||||||
@"else",
|
@"else",
|
||||||
@"elif",
|
@"elif",
|
||||||
|
@"template",
|
||||||
};
|
};
|
||||||
|
|
||||||
const EndKeyword = enum {
|
const EndKeyword = enum {
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
{=#else=}
|
{=#else=}
|
||||||
neither
|
neither
|
||||||
{=/if}
|
{=/if}
|
||||||
|
|
||||||
|
{#template test_tmpl .bar}
|
||||||
</section>
|
</section>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue