Combine Expressions and statements
This commit is contained in:
parent
8ebe77541f
commit
61493dc797
1 changed files with 23 additions and 33 deletions
|
@ -99,7 +99,7 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
|
|||
items = items ++ [_]TemplateItem{.{ .text = current_text }};
|
||||
current_text = "";
|
||||
}
|
||||
const result = parseStatement(iter);
|
||||
const result = parseExpressionOrStatement(iter, true);
|
||||
iter = result.new_iter;
|
||||
if (result.item == .end_for) {
|
||||
if (template_type == .subtemplate) break :parse_loop else @compileError("Unexpected end statement");
|
||||
|
@ -129,12 +129,16 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
|
|||
}
|
||||
}
|
||||
|
||||
fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
|
||||
fn parseExpressionOrStatement(
|
||||
comptime tokens: TokenIter,
|
||||
comptime as_statement: bool,
|
||||
) ParseResult(if (as_statement) Statement else Expression) {
|
||||
comptime {
|
||||
var iter = tokens;
|
||||
var stmt: Statement = while (iter.next()) |token| switch (token) {
|
||||
.whitespace => {},
|
||||
.pound => {
|
||||
if (!as_statement) @compileError("Unexpected Token");
|
||||
const next = iter.next() orelse @compileError("Unexpected end of template");
|
||||
if (next != .text) @compileError("Expected keyword following '#' character");
|
||||
const text = next.text;
|
||||
|
@ -164,23 +168,25 @@ fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
|
|||
iter = names.new_iter;
|
||||
break .{ .expression = .{ .capture_deref = names.item } };
|
||||
},
|
||||
else => @compileError(""),
|
||||
else => if (as_statement) @compileError("TODO") else break,
|
||||
};
|
||||
|
||||
// search for end of statement
|
||||
while (iter.next()) |token| switch (token) {
|
||||
.whitespace => {},
|
||||
.close_bracket => return .{
|
||||
.new_iter = iter,
|
||||
.item = stmt,
|
||||
},
|
||||
else => {
|
||||
@compileLog(iter.row);
|
||||
@compileError("TODO" ++ @tagName(token));
|
||||
},
|
||||
};
|
||||
if (as_statement) {
|
||||
// search for end of statement
|
||||
while (iter.next()) |token| switch (token) {
|
||||
.whitespace => {},
|
||||
.close_bracket => return .{
|
||||
.new_iter = iter,
|
||||
.item = stmt,
|
||||
},
|
||||
else => {
|
||||
@compileLog(iter.row);
|
||||
@compileError("TODO" ++ @tagName(token));
|
||||
},
|
||||
};
|
||||
|
||||
@compileError("Unexpected end of template");
|
||||
@compileError("Unexpected end of template");
|
||||
} else return .{ .new_iter = iter, .item = stmt.expression };
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +214,7 @@ fn endStatement(comptime tokens: TokenIter) TokenIter {
|
|||
|
||||
fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
|
||||
comptime {
|
||||
const iterable = parseExpression(tokens);
|
||||
const iterable = parseExpressionOrStatement(tokens, false);
|
||||
var iter = iterable.new_iter;
|
||||
|
||||
iter = skipWhitespace(iter);
|
||||
|
@ -237,22 +243,6 @@ fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
|
|||
}
|
||||
}
|
||||
|
||||
fn parseExpression(comptime tokens: TokenIter) ParseResult(Expression) {
|
||||
comptime {
|
||||
var iter = tokens;
|
||||
while (iter.next()) |token| switch (token) {
|
||||
.whitespace => {},
|
||||
.period => {
|
||||
const names = parseDeref(iter);
|
||||
return .{ .new_iter = names.new_iter, .item = .{ .arg_deref = names.item } };
|
||||
},
|
||||
else => @compileError("Expected Expression"),
|
||||
};
|
||||
|
||||
@compileError("Unexpected end of template");
|
||||
}
|
||||
}
|
||||
|
||||
fn parseDeref(comptime tokens: TokenIter) ParseResult([]const []const u8) {
|
||||
comptime {
|
||||
var iter = tokens;
|
||||
|
|
Loading…
Reference in a new issue