| ... | ... | @@ -127,7 +127,7 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V |
| 127 | 127 | .block => |v| { |
| 128 | 128 | const body = astgen.Value{ .body = v.body }; |
| 129 | 129 | const bottom = astgen.Value{ .body = v.bttm }; |
| 130 | | const x = if (comptime std.mem.eql(u8, v.args[0][0], "this")) search(v.args[0][1..], data) else search(v.args[0], ctx); |
| 130 | const x = resolveArg(v.args[0], data, ctx); |
| 131 | 131 | const T = @TypeOf(x); |
| 132 | 132 | const TI = @typeInfo(T); |
| 133 | 133 | switch (v.name) { |
| ... | ... | @@ -167,7 +167,7 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V |
| 167 | 167 | }, |
| 168 | 168 | .ifequal => { |
| 169 | 169 | comptime assertEqual(v.args.len, 2); |
| 170 | | const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx); |
| 170 | const y = resolveArg(v.args[1], data, ctx); |
| 171 | 171 | if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) { |
| 172 | 172 | return try doif(alloc, writer, body, bottom, data, ctx, opts, std.mem.eql(u8, @tagName(x), y)); |
| 173 | 173 | } |
| ... | ... | @@ -175,7 +175,7 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V |
| 175 | 175 | }, |
| 176 | 176 | .ifnotequal => { |
| 177 | 177 | comptime assertEqual(v.args.len, 2); |
| 178 | | const y = if (comptime std.mem.eql(u8, v.args[1][0], "this")) search(v.args[1][1..], data) else search(v.args[1], ctx); |
| 178 | const y = resolveArg(v.args[1], data, ctx); |
| 179 | 179 | if (@typeInfo(@TypeOf(x)) == .Enum and comptime std.meta.trait.isZigString(@TypeOf(y))) { |
| 180 | 180 | return try doif(alloc, writer, body, bottom, data, ctx, opts, std.mem.eql(u8, @tagName(x), y)); |
| 181 | 181 | } |
| ... | ... | @@ -201,7 +201,7 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V |
| 201 | 201 | args.@"1" = list.writer(); |
| 202 | 202 | inline for (v.args, 0..) |arg, i| { |
| 203 | 203 | const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2}); |
| 204 | | @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx); |
| 204 | @field(args, field_name) = resolveArg(arg, data, ctx); |
| 205 | 205 | } |
| 206 | 206 | const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"}, .raw = v.raw } }; |
| 207 | 207 | try @call(.auto, func, args); |
| ... | ... | @@ -223,7 +223,7 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V |
| 223 | 223 | }; |
| 224 | 224 | inline for (v.args, 0..) |arg, i| { |
| 225 | 225 | const field_name = comptime std.fmt.comptimePrint("{d}", .{i}); |
| 226 | | @field(args[3], field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx); |
| 226 | @field(args[3], field_name) = resolveArg(arg, data, ctx); |
| 227 | 227 | } |
| 228 | 228 | const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"}, .raw = v.raw } }; |
| 229 | 229 | try @call(.auto, func, args); |
| ... | ... | @@ -248,6 +248,20 @@ pub const DoOptions = struct { |
| 248 | 248 | flag1: bool, |
| 249 | 249 | }; |
| 250 | 250 | |
| 251 | fn resolveArg(comptime arg: astgen.Arg, data: anytype, ctx: anytype) ResolveArg(arg, @TypeOf(data), @TypeOf(ctx)) { |
| 252 | return switch (arg) { |
| 253 | .plain => |av| av[1 .. av.len - 1], |
| 254 | .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) search(av[1..], data) else search(av, ctx), |
| 255 | }; |
| 256 | } |
| 257 | |
| 258 | fn ResolveArg(comptime arg: astgen.Arg, comptime This: type, comptime Ctx: type) type { |
| 259 | return switch (arg) { |
| 260 | .plain => string, |
| 261 | .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) FieldSearch(This, av[1..]) else FieldSearch(Ctx, av), |
| 262 | }; |
| 263 | } |
| 264 | |
| 251 | 265 | fn search(comptime args: []const string, ctx: anytype) FieldSearch(@TypeOf(ctx), args) { |
| 252 | 266 | if (args.len == 0) return ctx; |
| 253 | 267 | if (args[0][0] == '"') return std.mem.trim(u8, args[0], "\""); |