authorgravatar for mason@anthropicstudios.comMason Remaley <mason@anthropicstudios.com> 2025-02-21 21:55:36-08:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-04-02 06:08:04+01:00
log3b2fec17a4dfe034c28f528d003bf09b5e44c2b6
tree0cf63d06f4353619c176fffe9774b7bad678e63a
parent1b62a22268117340ee7a17f019df01cd39ec1421
signaturelock-open Commit is signed but in an unrecognized format.

std.zon: populate `Zoir.Node.Index` values with corresponding ZOIR node

This allows using `std.zon` to parse schemas which are not directly representable in the Zig type system; for instance, `build.zig.zon`.

1 files changed, 28 insertions(+), 0 deletions(-)

lib/std/zon/parse.zig+28
...@@ -436,6 +436,10 @@ const Parser = struct {...@@ -436,6 +436,10 @@ const Parser = struct {
436 T: type,436 T: type,
437 node: Zoir.Node.Index,437 node: Zoir.Node.Index,
438 ) error{ ParseZon, OutOfMemory, WrongType }!T {438 ) error{ ParseZon, OutOfMemory, WrongType }!T {
439 if (T == Zoir.Node.Index) {
440 return node;
441 }
442
439 switch (@typeInfo(T)) {443 switch (@typeInfo(T)) {
440 .optional => |optional| if (node.get(self.zoir) == .null) {444 .optional => |optional| if (node.get(self.zoir) == .null) {
441 return null;445 return null;
...@@ -3441,3 +3445,27 @@ test "std.zon add pointers" {...@@ -3441,3 +3445,27 @@ test "std.zon add pointers" {
3441 try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});3445 try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});
3442 }3446 }
3443}3447}
3448
3449test "std.zon stop on node" {
3450 const gpa = std.testing.allocator;
3451
3452 {
3453 const Vec2 = struct {
3454 x: Zoir.Node.Index,
3455 y: f32,
3456 };
3457
3458 var status: Status = .{};
3459 defer status.deinit(gpa);
3460 const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &status, .{});
3461 try std.testing.expectEqual(result.y, 2.5);
3462 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(status.zoir.?));
3463 }
3464
3465 {
3466 var status: Status = .{};
3467 defer status.deinit(gpa);
3468 const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &status, .{});
3469 try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(status.zoir.?));
3470 }
3471}