diff --git a/src/astgen.zig b/src/astgen.zig index 6db25aab09fe86451290821502a8d3f43ca95579..eb037cc46fae302ee7cc212aea21dd8527586ca4 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -28,7 +28,7 @@ pub const Attr = struct { pub const Block = struct { name: Type, - args: []const string, + args: []const []const string, body: []const Value, pub const Type = enum { @@ -42,7 +42,7 @@ pub const Block = struct { pub const Fn = struct { name: string, - args: []const string, + args: []const []const string, }; // @@ -131,7 +131,7 @@ const Parser = struct { if (self.tryEatSymbol("#")) { const w = self.eat(.word); if (std.meta.stringToEnum(Block.Type, w)) |name| { - const args = self.doReplacement(); + const args = self.doArgs(); var children: []const Value = &.{}; while (!self.tryEatSymbol("/")) { children = children ++ &[_]Value{self.doValue()}; @@ -146,7 +146,7 @@ const Parser = struct { } return Value{ .function = .{ .name = w, - .args = self.doReplacement(), + .args = self.doArgs(), } }; } return Value{ .replacement = self.doReplacement() }; @@ -154,6 +154,21 @@ const Parser = struct { return Value{ .element = self.doElement() }; } + pub fn doArgs(comptime self: *Parser) []const []const string { + var ret: []const []const string = &.{}; + var temp: []const string = &.{self.eat(.word)}; + while (!self.tryEatSymbol("}")) { + if (self.tryEatSymbol(".")) { + temp = temp ++ &[_]string{self.eat(.word)}; + } else { + ret = ret ++ &[_][]const string{temp}; + temp = &.{self.eat(.word)}; + } + } + ret = ret ++ &[_][]const string{temp}; + return ret; + } + pub fn doReplacement(comptime self: *Parser) []const string { var ret: []const string = &.{}; ret = ret ++ &[_]string{self.eat(.word)}; diff --git a/src/lib.zig b/src/lib.zig index 973303423833a13ac97f16570e79306fb8f2ddfb..0ec266f6366f93e019e8f9cbbc5e068e48fb6782 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -89,10 +89,10 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype @compileError("pek: compile: unsupported type: " ++ @typeName(TO)); }, .block => |v| { - const x = comptime search(data, v.args); - switch (v.name) { .each => { + comptime assertEqual(v.args.len, 1); + const x = comptime search(data, v.args[0]); inline for (x) |item| { inline for (v.body) |val| { try do(writer, val, item, ctx, indent, flag1); @@ -121,3 +121,7 @@ fn entityLookupBefore(in: []const u8) ?htmlentities.Entity { } return null; } + +fn assertEqual(comptime a: usize, comptime b: usize) void { + if (a != b) @compileError(std.fmt.comptimePrint("{d} != {d}", .{ a, b })); +}