Add fields for whitespace stripping

This commit is contained in:
jaina heartles 2022-11-17 23:52:07 -08:00
parent c9af99d08f
commit 22277beffc

View file

@ -26,7 +26,7 @@ fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args:
}; };
} }
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void { fn executeStatement(writer: anytype, comptime stmt: ExecutableStatement, args: anytype, captures: anytype) !void {
switch (stmt) { switch (stmt) {
.expression => |expr| { .expression => |expr| {
const val = evaluateExpression(expr, args, captures); const val = evaluateExpression(expr, args, captures);
@ -144,12 +144,13 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
} }
const result = parseExpressionOrStatement(iter, true); const result = parseExpressionOrStatement(iter, true);
iter = result.new_iter; iter = result.new_iter;
if (result.item == .end_for) { const stmt = result.item.executable;
if (stmt == .end_for) {
if (template_type == .for_block) break :parse_loop else @compileError("Unexpected end statement"); if (template_type == .for_block) break :parse_loop else @compileError("Unexpected end statement");
} else if (result.item == .end_if) { } else if (stmt == .end_if) {
if (template_type == .if_block) break :parse_loop else @compileError("Unexpected end statement"); if (template_type == .if_block) break :parse_loop else @compileError("Unexpected end statement");
} }
items = items ++ [_]TemplateItem{.{ .statement = result.item }}; items = items ++ [_]TemplateItem{.{ .statement = stmt }};
} }
}, },
.close_bracket => { .close_bracket => {
@ -181,7 +182,7 @@ fn parseExpressionOrStatement(
) ParseResult(if (as_statement) Statement else Expression) { ) ParseResult(if (as_statement) Statement else Expression) {
comptime { comptime {
var iter = tokens; var iter = tokens;
var stmt: Statement = while (iter.next()) |token| switch (token) { var stmt: ExecutableStatement = while (iter.next()) |token| switch (token) {
.whitespace => {}, .whitespace => {},
.pound => { .pound => {
if (!as_statement) @compileError("Unexpected Token"); if (!as_statement) @compileError("Unexpected Token");
@ -196,14 +197,22 @@ fn parseExpressionOrStatement(
// statemnt already finished so just return // statemnt already finished so just return
return .{ return .{
.new_iter = result.new_iter, .new_iter = result.new_iter,
.item = .{ .for_loop = result.item }, .item = .{
.executable = .{ .for_loop = result.item },
.strip_before = false,
.strip_after = false,
},
}; };
}, },
.@"if" => { .@"if" => {
const result = parseIfStatement(iter); const result = parseIfStatement(iter);
return .{ return .{
.new_iter = result.new_iter, .new_iter = result.new_iter,
.item = .{ .if_statement = result.item }, .item = .{
.executable = .{ .if_statement = result.item },
.strip_before = false,
.strip_after = false,
},
}; };
}, },
@ -241,7 +250,11 @@ fn parseExpressionOrStatement(
.whitespace => {}, .whitespace => {},
.close_bracket => return .{ .close_bracket => return .{
.new_iter = iter, .new_iter = iter,
.item = stmt, .item = .{
.executable = stmt,
.strip_before = true,
.strip_after = true,
},
}, },
else => { else => {
@compileLog(iter.row); @compileLog(iter.row);
@ -358,7 +371,7 @@ fn ParseResult(comptime T: type) type {
const TemplateItem = union(enum) { const TemplateItem = union(enum) {
text: []const u8, text: []const u8,
statement: Statement, statement: ExecutableStatement,
}; };
const Expression = union(enum) { const Expression = union(enum) {
@ -377,7 +390,8 @@ const IfStatement = struct {
condition: Expression, condition: Expression,
}; };
const Statement = union(enum) { const ExecutableStatement =
union(enum) {
expression: Expression, expression: Expression,
for_loop: ForLoop, for_loop: ForLoop,
end_for: void, end_for: void,
@ -385,6 +399,12 @@ const Statement = union(enum) {
end_if: void, end_if: void,
}; };
const Statement = struct {
executable: ExecutableStatement,
strip_before: bool,
strip_after: bool,
};
const Keyword = enum { const Keyword = enum {
@"for", @"for",
@"if", @"if",