mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
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`.
This commit is contained in:
parent
1b62a22268
commit
3b2fec17a4
@ -436,6 +436,10 @@ const Parser = struct {
|
|||||||
T: type,
|
T: type,
|
||||||
node: Zoir.Node.Index,
|
node: Zoir.Node.Index,
|
||||||
) error{ ParseZon, OutOfMemory, WrongType }!T {
|
) error{ ParseZon, OutOfMemory, WrongType }!T {
|
||||||
|
if (T == Zoir.Node.Index) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
switch (@typeInfo(T)) {
|
switch (@typeInfo(T)) {
|
||||||
.optional => |optional| if (node.get(self.zoir) == .null) {
|
.optional => |optional| if (node.get(self.zoir) == .null) {
|
||||||
return null;
|
return null;
|
||||||
@ -3441,3 +3445,27 @@ test "std.zon add pointers" {
|
|||||||
try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});
|
try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "std.zon stop on node" {
|
||||||
|
const gpa = std.testing.allocator;
|
||||||
|
|
||||||
|
{
|
||||||
|
const Vec2 = struct {
|
||||||
|
x: Zoir.Node.Index,
|
||||||
|
y: f32,
|
||||||
|
};
|
||||||
|
|
||||||
|
var status: Status = .{};
|
||||||
|
defer status.deinit(gpa);
|
||||||
|
const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &status, .{});
|
||||||
|
try std.testing.expectEqual(result.y, 2.5);
|
||||||
|
try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(status.zoir.?));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var status: Status = .{};
|
||||||
|
defer status.deinit(gpa);
|
||||||
|
const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &status, .{});
|
||||||
|
try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(status.zoir.?));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user