2022-11-16 03:10:16 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2022-11-16 07:39:36 +00:00
|
|
|
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{
|
|
|
|
.community = .{ .name = "abcd" },
|
|
|
|
.foo = [_][]const u8{ "5", "4", "3", "2", "1" },
|
2022-11-16 08:33:06 +00:00
|
|
|
.baz = [_][]const []const u8{
|
|
|
|
&.{ "5", "4", "3", "2", "1" },
|
|
|
|
&.{ "5", "4", "3", "2", "1" },
|
|
|
|
},
|
2022-11-18 07:39:32 +00:00
|
|
|
.qux = true,
|
|
|
|
.quxx = false,
|
2022-11-16 07:39:36 +00:00
|
|
|
});
|
2022-11-16 03:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
|
2022-11-18 07:39:32 +00:00
|
|
|
@setEvalBranchQuota(@intCast(u32, template.len * 8));
|
2022-11-16 07:16:46 +00:00
|
|
|
const tmpl = comptime parseTemplate(TokenIter{ .text = template }, .root);
|
2022-11-16 08:33:06 +00:00
|
|
|
try executeTemplate(writer, tmpl.item, args, .{});
|
2022-11-16 06:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void {
|
2022-11-16 06:35:27 +00:00
|
|
|
inline for (items) |it| switch (it) {
|
|
|
|
.text => |text| try writer.writeAll(text),
|
2022-11-16 08:01:45 +00:00
|
|
|
.statement => |stmt| try executeStatement(writer, stmt, args, captures),
|
2022-11-16 06:35:27 +00:00
|
|
|
};
|
2022-11-16 03:10:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
|
2022-11-16 06:07:45 +00:00
|
|
|
switch (stmt) {
|
2022-11-16 08:33:06 +00:00
|
|
|
.expression => |expr| {
|
|
|
|
const val = evaluateExpression(expr, args, captures);
|
|
|
|
try print(writer, val);
|
2022-11-16 06:07:45 +00:00
|
|
|
},
|
2022-11-16 07:39:36 +00:00
|
|
|
.for_loop => |loop| {
|
2022-11-16 08:33:06 +00:00
|
|
|
const iterable = evaluateExpression(loop.iterable, args, captures);
|
2022-11-16 07:53:29 +00:00
|
|
|
const subtemplate = loop.subtemplate;
|
2022-11-16 08:18:14 +00:00
|
|
|
for (iterable) |v| {
|
2022-11-16 08:01:45 +00:00
|
|
|
try executeTemplate(
|
|
|
|
writer,
|
|
|
|
subtemplate,
|
|
|
|
args,
|
2022-11-16 08:18:14 +00:00
|
|
|
addCapture(captures, loop.capture, v),
|
2022-11-16 08:01:45 +00:00
|
|
|
);
|
2022-11-16 07:39:36 +00:00
|
|
|
}
|
2022-11-16 07:16:46 +00:00
|
|
|
},
|
2022-11-18 07:39:32 +00:00
|
|
|
.if_statement => |if_stmt| {
|
|
|
|
const condition = evaluateExpression(if_stmt.condition, args, captures);
|
|
|
|
const subtemplate = if_stmt.subtemplate;
|
|
|
|
if (condition) try executeTemplate(writer, subtemplate, args, captures);
|
|
|
|
},
|
2022-11-16 06:07:45 +00:00
|
|
|
else => @compileError("TODO"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:39:36 +00:00
|
|
|
fn print(writer: anytype, arg: anytype) !void {
|
|
|
|
if (comptime std.meta.trait.isZigString(@TypeOf(arg))) return writer.writeAll(arg);
|
2022-11-16 08:33:06 +00:00
|
|
|
@compileLog(@TypeOf(arg));
|
2022-11-16 07:39:36 +00:00
|
|
|
|
|
|
|
@compileError("TODO");
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
fn Deref(comptime T: type, comptime names: []const []const u8) type {
|
2022-11-16 07:39:36 +00:00
|
|
|
if (names.len == 0) return T;
|
|
|
|
|
|
|
|
// Compiler segfaults when I use std.meta to get this info so we search it manually
|
|
|
|
const field = for (@typeInfo(T).Struct.fields) |f| {
|
|
|
|
if (std.mem.eql(u8, f.name, names[0])) break f;
|
|
|
|
} else @compileError("Unknown field " ++ names[0] ++ " in type " ++ @typeName(T));
|
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
return Deref(field.field_type, names[1..]);
|
2022-11-16 07:39:36 +00:00
|
|
|
}
|
2022-11-16 06:07:45 +00:00
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
fn deref(arg: anytype, comptime names: []const []const u8) Deref(@TypeOf(arg), names) {
|
2022-11-16 07:39:36 +00:00
|
|
|
if (names.len == 0) return arg;
|
2022-11-16 08:01:45 +00:00
|
|
|
return deref(@field(arg, names[0]), names[1..]);
|
2022-11-16 06:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 08:33:06 +00:00
|
|
|
fn EvaluateExpression(comptime expression: Expression, comptime Args: type, comptime Captures: type) type {
|
|
|
|
return switch (expression) {
|
|
|
|
.arg_deref => |names| Deref(Args, names),
|
|
|
|
.capture_deref => |names| Deref(Captures, names),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn evaluateExpression(
|
|
|
|
comptime expression: Expression,
|
|
|
|
args: anytype,
|
|
|
|
captures: anytype,
|
|
|
|
) EvaluateExpression(expression, @TypeOf(args), @TypeOf(captures)) {
|
|
|
|
return switch (expression) {
|
|
|
|
.arg_deref => |names| deref(args, names),
|
|
|
|
.capture_deref => |names| deref(captures, names),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:18:14 +00:00
|
|
|
fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type {
|
|
|
|
var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{
|
|
|
|
.name = name,
|
|
|
|
.field_type = Val,
|
|
|
|
.default_value = null,
|
|
|
|
.is_comptime = false,
|
|
|
|
.alignment = @alignOf(Val),
|
|
|
|
}};
|
|
|
|
|
|
|
|
return @Type(.{ .Struct = .{
|
|
|
|
.layout = .Auto,
|
|
|
|
.fields = fields,
|
|
|
|
.decls = &.{},
|
|
|
|
.is_tuple = false,
|
|
|
|
} });
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addCapture(root: anytype, comptime name: []const u8, val: anytype) AddCapture(@TypeOf(root), name, @TypeOf(val)) {
|
|
|
|
var result = std.mem.zeroInit(AddCapture(@TypeOf(root), name, @TypeOf(val)), root);
|
|
|
|
@field(result, name) = val;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
const TemplateType = enum {
|
|
|
|
root,
|
2022-11-18 07:39:32 +00:00
|
|
|
for_block,
|
|
|
|
if_block,
|
2022-11-16 07:16:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateType) ParseResult([]const TemplateItem) {
|
2022-11-16 06:35:27 +00:00
|
|
|
comptime {
|
2022-11-16 07:16:46 +00:00
|
|
|
var iter = tokens;
|
2022-11-16 06:35:27 +00:00
|
|
|
var items: []const TemplateItem = &.{};
|
|
|
|
var current_text: []const u8 = "";
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
parse_loop: while (iter.next()) |token| {
|
2022-11-16 06:35:27 +00:00
|
|
|
switch (token) {
|
|
|
|
.whitespace, .text => |text| current_text = current_text ++ text,
|
|
|
|
.open_bracket => {
|
|
|
|
const next = iter.peek() orelse @compileError("Unexpected end of template");
|
|
|
|
if (next == .open_bracket) {
|
|
|
|
current_text = current_text ++ "{";
|
|
|
|
_ = iter.next();
|
|
|
|
} else {
|
|
|
|
if (current_text.len != 0) {
|
|
|
|
items = items ++ [_]TemplateItem{.{ .text = current_text }};
|
|
|
|
current_text = "";
|
|
|
|
}
|
2022-11-16 08:06:53 +00:00
|
|
|
const result = parseExpressionOrStatement(iter, true);
|
2022-11-16 06:35:27 +00:00
|
|
|
iter = result.new_iter;
|
2022-11-16 07:16:46 +00:00
|
|
|
if (result.item == .end_for) {
|
2022-11-18 07:39:32 +00:00
|
|
|
if (template_type == .for_block) break :parse_loop else @compileError("Unexpected end statement");
|
|
|
|
} else if (result.item == .end_if) {
|
|
|
|
if (template_type == .if_block) break :parse_loop else @compileError("Unexpected end statement");
|
2022-11-16 07:16:46 +00:00
|
|
|
}
|
2022-11-16 06:35:27 +00:00
|
|
|
items = items ++ [_]TemplateItem{.{ .statement = result.item }};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.close_bracket => {
|
|
|
|
const next = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (next == .close_bracket) current_text = current_text ++ "}" else @compileError("Unpaired close bracket, did you mean \"}}\"?");
|
|
|
|
},
|
|
|
|
.period => current_text = current_text ++ ".",
|
|
|
|
.pound => current_text = current_text ++ "#",
|
2022-11-16 07:53:29 +00:00
|
|
|
.pipe => current_text = current_text ++ "|",
|
|
|
|
.dollar => current_text = current_text ++ "$",
|
2022-11-18 07:23:02 +00:00
|
|
|
.slash => current_text = current_text ++ "/",
|
2022-11-16 06:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_text.len != 0) {
|
|
|
|
items = items ++ [_]TemplateItem{.{ .text = current_text }};
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
return .{
|
|
|
|
.new_iter = iter,
|
|
|
|
.item = items,
|
|
|
|
};
|
2022-11-16 06:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:06:53 +00:00
|
|
|
fn parseExpressionOrStatement(
|
|
|
|
comptime tokens: TokenIter,
|
|
|
|
comptime as_statement: bool,
|
|
|
|
) ParseResult(if (as_statement) Statement else Expression) {
|
2022-11-16 06:07:45 +00:00
|
|
|
comptime {
|
|
|
|
var iter = tokens;
|
2022-11-16 07:16:46 +00:00
|
|
|
var stmt: Statement = while (iter.next()) |token| switch (token) {
|
2022-11-16 06:07:45 +00:00
|
|
|
.whitespace => {},
|
|
|
|
.pound => {
|
2022-11-16 08:06:53 +00:00
|
|
|
if (!as_statement) @compileError("Unexpected Token");
|
2022-11-16 06:07:45 +00:00
|
|
|
const next = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (next != .text) @compileError("Expected keyword following '#' character");
|
|
|
|
const text = next.text;
|
|
|
|
const keyword = std.meta.stringToEnum(Keyword, text) orelse @compileError("Unknown keyword: " ++ text);
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
switch (keyword) {
|
|
|
|
.@"for" => {
|
|
|
|
const result = parseForLoop(iter);
|
|
|
|
// statemnt already finished so just return
|
|
|
|
return .{
|
|
|
|
.new_iter = result.new_iter,
|
|
|
|
.item = .{ .for_loop = result.item },
|
|
|
|
};
|
|
|
|
},
|
2022-11-18 07:39:32 +00:00
|
|
|
.@"if" => {
|
|
|
|
const result = parseIfStatement(iter);
|
|
|
|
return .{
|
|
|
|
.new_iter = result.new_iter,
|
|
|
|
.item = .{ .if_statement = result.item },
|
|
|
|
};
|
|
|
|
},
|
2022-11-16 07:16:46 +00:00
|
|
|
|
|
|
|
//else => @compileError("TODO"),
|
|
|
|
}
|
2022-11-16 06:07:45 +00:00
|
|
|
},
|
2022-11-18 07:23:02 +00:00
|
|
|
.slash => {
|
|
|
|
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;
|
|
|
|
const keyword = std.meta.stringToEnum(EndKeyword, text) orelse @compileError("Unknown keyword: " ++ text);
|
|
|
|
|
|
|
|
switch (keyword) {
|
|
|
|
.@"for" => break .{ .end_for = {} },
|
2022-11-18 07:39:32 +00:00
|
|
|
.@"if" => break .{ .end_if = {} },
|
2022-11-18 07:23:02 +00:00
|
|
|
}
|
|
|
|
},
|
2022-11-16 06:07:45 +00:00
|
|
|
.period => {
|
2022-11-16 08:01:45 +00:00
|
|
|
const names = parseDeref(iter);
|
|
|
|
iter = names.new_iter;
|
|
|
|
break .{ .expression = .{ .arg_deref = names.item } };
|
|
|
|
},
|
|
|
|
.dollar => {
|
|
|
|
const names = parseDeref(iter);
|
|
|
|
iter = names.new_iter;
|
|
|
|
break .{ .expression = .{ .capture_deref = names.item } };
|
2022-11-16 03:10:16 +00:00
|
|
|
},
|
2022-11-16 08:06:53 +00:00
|
|
|
else => if (as_statement) @compileError("TODO") else break,
|
2022-11-16 06:07:45 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 08:06:53 +00:00
|
|
|
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));
|
|
|
|
},
|
|
|
|
};
|
2022-11-16 07:16:46 +00:00
|
|
|
|
2022-11-16 08:06:53 +00:00
|
|
|
@compileError("Unexpected end of template");
|
|
|
|
} else return .{ .new_iter = iter, .item = stmt.expression };
|
2022-11-16 07:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:53:29 +00:00
|
|
|
fn skipWhitespace(comptime tokens: TokenIter) TokenIter {
|
|
|
|
comptime {
|
|
|
|
var iter = tokens;
|
|
|
|
while (iter.peek()) |token| switch (token) {
|
|
|
|
.whitespace => _ = iter.next(),
|
|
|
|
else => break,
|
|
|
|
};
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn endStatement(comptime tokens: TokenIter) TokenIter {
|
|
|
|
comptime {
|
|
|
|
var iter = skipWhitespace(tokens);
|
|
|
|
|
|
|
|
const token = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (token != .close_bracket) @compileError("Unexpected token");
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
|
|
|
|
comptime {
|
2022-11-16 08:06:53 +00:00
|
|
|
const iterable = parseExpressionOrStatement(tokens, false);
|
2022-11-16 07:39:36 +00:00
|
|
|
var iter = iterable.new_iter;
|
2022-11-16 07:53:29 +00:00
|
|
|
|
|
|
|
iter = skipWhitespace(iter);
|
|
|
|
{
|
|
|
|
const token = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (token != .pipe) @compileError("Unexpected token");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const token = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (token != .dollar) @compileError("Unexpected token");
|
|
|
|
}
|
|
|
|
const capture = blk: {
|
|
|
|
const token = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (token != .text) @compileError("Unexpected token");
|
|
|
|
break :blk token.text;
|
2022-11-16 07:16:46 +00:00
|
|
|
};
|
2022-11-16 07:53:29 +00:00
|
|
|
{
|
|
|
|
const token = iter.next() orelse @compileError("Unexpected end of template");
|
|
|
|
if (token != .pipe) @compileError("Unexpected token");
|
|
|
|
}
|
|
|
|
iter = endStatement(iter);
|
2022-11-16 07:16:46 +00:00
|
|
|
|
2022-11-18 07:39:32 +00:00
|
|
|
const subtemplate = parseTemplate(iter, .for_block);
|
|
|
|
|
|
|
|
return .{ .new_iter = subtemplate.new_iter, .item = .{
|
|
|
|
.iterable = iterable.item,
|
|
|
|
.subtemplate = subtemplate.item,
|
|
|
|
.capture = capture,
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parseIfStatement(comptime tokens: TokenIter) ParseResult(IfStatement) {
|
|
|
|
comptime {
|
|
|
|
const condition = parseExpressionOrStatement(tokens, false);
|
|
|
|
var iter = endStatement(condition.new_iter);
|
|
|
|
|
|
|
|
const subtemplate = parseTemplate(iter, .if_block);
|
2022-11-16 07:16:46 +00:00
|
|
|
|
2022-11-18 07:39:32 +00:00
|
|
|
return .{ .new_iter = subtemplate.new_iter, .item = .{ .condition = condition.item, .subtemplate = subtemplate.item } };
|
2022-11-16 07:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:01:45 +00:00
|
|
|
fn parseDeref(comptime tokens: TokenIter) ParseResult([]const []const u8) {
|
2022-11-16 06:07:45 +00:00
|
|
|
comptime {
|
|
|
|
var iter = tokens;
|
|
|
|
var fields: []const []const u8 = &.{};
|
|
|
|
var wants = .text;
|
|
|
|
while (iter.peek()) |token| {
|
|
|
|
switch (token) {
|
|
|
|
.whitespace => {},
|
|
|
|
.text => |text| {
|
|
|
|
if (wants != .text) @compileError("Unexpected token \"" ++ text ++ "\"");
|
|
|
|
fields = fields ++ [1][]const u8{text};
|
|
|
|
wants = .period;
|
|
|
|
},
|
|
|
|
.period => {
|
|
|
|
if (wants != .period) @compileError("Unexpected token \".\"");
|
|
|
|
wants = .text;
|
|
|
|
},
|
|
|
|
else => if (wants == .period) return .{
|
|
|
|
.new_iter = iter,
|
2022-11-16 06:11:16 +00:00
|
|
|
.item = fields,
|
2022-11-16 06:07:45 +00:00
|
|
|
} else @compileError("Unexpected token"),
|
|
|
|
}
|
|
|
|
_ = iter.next();
|
2022-11-16 03:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 06:11:16 +00:00
|
|
|
fn ParseResult(comptime T: type) type {
|
|
|
|
return struct {
|
|
|
|
new_iter: TokenIter,
|
|
|
|
item: T,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-16 06:35:27 +00:00
|
|
|
const TemplateItem = union(enum) {
|
|
|
|
text: []const u8,
|
|
|
|
statement: Statement,
|
|
|
|
};
|
|
|
|
|
2022-11-16 06:07:45 +00:00
|
|
|
const Expression = union(enum) {
|
|
|
|
arg_deref: []const []const u8,
|
2022-11-16 08:01:45 +00:00
|
|
|
capture_deref: []const []const u8,
|
2022-11-16 06:07:45 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
const ForLoop = struct {
|
|
|
|
subtemplate: []const TemplateItem,
|
2022-11-16 07:39:36 +00:00
|
|
|
iterable: Expression,
|
2022-11-16 07:53:29 +00:00
|
|
|
capture: []const u8,
|
2022-11-16 07:16:46 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 07:39:32 +00:00
|
|
|
const IfStatement = struct {
|
|
|
|
subtemplate: []const TemplateItem,
|
|
|
|
condition: Expression,
|
|
|
|
};
|
|
|
|
|
2022-11-16 06:07:45 +00:00
|
|
|
const Statement = union(enum) {
|
|
|
|
expression: Expression,
|
2022-11-16 07:16:46 +00:00
|
|
|
for_loop: ForLoop,
|
|
|
|
end_for: void,
|
2022-11-18 07:39:32 +00:00
|
|
|
if_statement: IfStatement,
|
|
|
|
end_if: void,
|
2022-11-16 06:07:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Keyword = enum {
|
|
|
|
@"for",
|
2022-11-18 07:39:32 +00:00
|
|
|
@"if",
|
2022-11-18 07:23:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const EndKeyword = enum {
|
|
|
|
@"for",
|
2022-11-18 07:39:32 +00:00
|
|
|
@"if",
|
2022-11-16 06:07:45 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 03:10:16 +00:00
|
|
|
const Token = union(enum) {
|
|
|
|
text: []const u8,
|
|
|
|
open_bracket: void,
|
|
|
|
close_bracket: void,
|
|
|
|
period: void,
|
|
|
|
whitespace: []const u8,
|
2022-11-16 06:07:45 +00:00
|
|
|
pound: void,
|
2022-11-16 07:53:29 +00:00
|
|
|
pipe: void,
|
|
|
|
dollar: void,
|
2022-11-18 07:23:02 +00:00
|
|
|
slash: void,
|
2022-11-16 03:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const TokenIter = struct {
|
|
|
|
start: usize = 0,
|
|
|
|
text: []const u8,
|
|
|
|
peeked_token: ?Token = null,
|
|
|
|
|
2022-11-16 07:16:46 +00:00
|
|
|
row: usize = 0,
|
|
|
|
|
2022-11-16 03:10:16 +00:00
|
|
|
fn next(self: *TokenIter) ?Token {
|
|
|
|
if (self.peeked_token) |token| {
|
|
|
|
self.peeked_token = null;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
const remaining = self.text[self.start..];
|
|
|
|
if (remaining.len == 0) return null;
|
|
|
|
|
|
|
|
const ch = remaining[0];
|
|
|
|
self.start += 1;
|
|
|
|
switch (ch) {
|
|
|
|
'{' => return .{ .open_bracket = {} },
|
|
|
|
'}' => return .{ .close_bracket = {} },
|
|
|
|
'.' => return .{ .period = {} },
|
2022-11-16 06:07:45 +00:00
|
|
|
'#' => return .{ .pound = {} },
|
2022-11-16 07:53:29 +00:00
|
|
|
'|' => return .{ .pipe = {} },
|
|
|
|
'$' => return .{ .dollar = {} },
|
2022-11-18 07:23:02 +00:00
|
|
|
'/' => return .{ .slash = {} },
|
2022-11-16 03:10:16 +00:00
|
|
|
' ', '\t', '\n', '\r' => {
|
|
|
|
var idx: usize = 0;
|
|
|
|
while (idx < remaining.len and std.mem.indexOfScalar(u8, " \t\n\r", remaining[idx]) != null) : (idx += 1) {}
|
2022-11-16 07:16:46 +00:00
|
|
|
const newline_count = std.mem.count(u8, remaining[0..idx], "\n");
|
|
|
|
self.row += newline_count;
|
2022-11-16 03:10:16 +00:00
|
|
|
|
|
|
|
self.start += idx - 1;
|
|
|
|
return .{ .whitespace = remaining[0..idx] };
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
var idx: usize = 0;
|
2022-11-18 07:23:02 +00:00
|
|
|
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}.#|$/ \t\n\r", remaining[idx]) == null) : (idx += 1) {}
|
2022-11-16 03:10:16 +00:00
|
|
|
|
|
|
|
self.start += idx - 1;
|
|
|
|
return .{ .text = remaining[0..idx] };
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek(self: *TokenIter) ?Token {
|
|
|
|
const token = self.next();
|
|
|
|
self.peeked_token = token;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
};
|