authorgravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-07-18 14:26:42-07:00
committergravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-09-26 12:35:38-07:00
log9e87e807ab65449845d12eaa3d98e2792194354d
tree8984517d90026b3968bde50c0a39ab81504d1d5b
parent09e69d88ed0e3b8cf3c65aec4559614a835021a9

Simplify by requiring node when parsing ZOIR


1 files changed, 17 insertions(+), 145 deletions(-)

lib/std/zon/parse.zig+17-145
...@@ -4,7 +4,6 @@...@@ -4,7 +4,6 @@
4//!4//!
5//! Parsing from individual Zoir nodes is also available:5//! Parsing from individual Zoir nodes is also available:
6//! * `fromZoir`/`fromZoirAlloc`6//! * `fromZoir`/`fromZoirAlloc`
7//! * `fromZoirNode`/`fromZoirNodeAlloc`
8//!7//!
9//! To update an existing values, see the `updateFrom*` variants.8//! To update an existing values, see the `updateFrom*` variants.
10//!9//!
...@@ -293,21 +292,6 @@ pub const Diagnostics = struct {...@@ -293,21 +292,6 @@ pub const Diagnostics = struct {
293///292///
294/// An allocator is still required for temporary allocations made during parsing.293/// An allocator is still required for temporary allocations made during parsing.
295pub fn fromSlice(294pub fn fromSlice(
296 /// The type to deserialize into. Only the following types are supported, unsupported types will
297 /// result in a compiler error:
298 ///
299 /// * Optionals of supported types
300 /// * Booleans
301 /// * Integers except for `comptime_int`
302 /// * Floats except for `comptime_float`
303 /// * Enums
304 /// * Single item pointers to supported types
305 /// * Slices of supported types
306 /// * Arrays of supported types
307 /// * Vectors of supported types
308 /// * Structures of supported types
309 /// * Tuples of supported types
310 /// * Unions
311 T: type,295 T: type,
312 gpa: Allocator,296 gpa: Allocator,
313 source: [:0]const u8,297 source: [:0]const u8,
...@@ -379,7 +363,7 @@ fn parseSliceAlloc(...@@ -379,7 +363,7 @@ fn parseSliceAlloc(
379 defer if (diag == null) zoir.deinit(gpa);363 defer if (diag == null) zoir.deinit(gpa);
380364
381 if (diag) |s| s.* = .{};365 if (diag) |s| s.* = .{};
382 try parseZoirAlloc(T, gpa, current, ast, zoir, diag, options);366 try parseZoirAlloc(T, gpa, current, ast, zoir, .root, diag, options);
383}367}
384368
385/// Like `fromSlice`, but operates on `Zoir` instead of ZON source.369/// Like `fromSlice`, but operates on `Zoir` instead of ZON source.
...@@ -387,17 +371,17 @@ pub fn fromZoir(...@@ -387,17 +371,17 @@ pub fn fromZoir(
387 T: type,371 T: type,
388 ast: Ast,372 ast: Ast,
389 zoir: Zoir,373 zoir: Zoir,
374 node: Zoir.Node.Index,
390 diag: ?*Diagnostics,375 diag: ?*Diagnostics,
391 options: FromOptions,376 options: FromOptions,
392) error{ParseZon}!T {377) error{ParseZon}!T {
393 comptime assert(!requiresAllocator(T));378 comptime assert(!requiresAllocator(T));
394 var buf: [0]u8 = .{};
395 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
396 return fromZoirAlloc(379 return fromZoirAlloc(
397 T,380 T,
398 failing_allocator.allocator(),381 .failing,
399 ast,382 ast,
400 zoir,383 zoir,
384 node,
401 diag,385 diag,
402 options,386 options,
403 ) catch |err| switch (err) {387 ) catch |err| switch (err) {
...@@ -412,18 +396,18 @@ pub fn updateFromZoir(...@@ -412,18 +396,18 @@ pub fn updateFromZoir(
412 current: *T,396 current: *T,
413 ast: Ast,397 ast: Ast,
414 zoir: Zoir,398 zoir: Zoir,
399 node: Zoir.Node.Index,
415 diag: ?*Diagnostics,400 diag: ?*Diagnostics,
416 options: UpdateFromOptions,401 options: UpdateFromOptions,
417) error{ParseZon}!void {402) error{ParseZon}!void {
418 comptime assert(!requiresAllocator(T));403 comptime assert(!requiresAllocator(T));
419 var buf: [0]u8 = .{};
420 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
421 updateFromZoirAlloc(404 updateFromZoirAlloc(
422 T,405 T,
423 failing_allocator.allocator(),406 .failing,
424 current,407 current,
425 ast,408 ast,
426 zoir,409 zoir,
410 node,
427 diag,411 diag,
428 options,412 options,
429 ) catch |err| switch (err) {413 ) catch |err| switch (err) {
...@@ -434,97 +418,6 @@ pub fn updateFromZoir(...@@ -434,97 +418,6 @@ pub fn updateFromZoir(
434418
435/// Like `fromSliceAlloc`, but operates on `Zoir` instead of ZON source.419/// Like `fromSliceAlloc`, but operates on `Zoir` instead of ZON source.
436pub fn fromZoirAlloc(420pub fn fromZoirAlloc(
437 T: type,
438 gpa: Allocator,
439 ast: Ast,
440 zoir: Zoir,
441 diag: ?*Diagnostics,
442 options: FromOptions,
443) error{ OutOfMemory, ParseZon }!T {
444 return fromZoirNodeAlloc(T, gpa, ast, zoir, .root, diag, options);
445}
446
447/// Like updateSliceAlloc`, but operates on `Zoir` instead of ZON source.
448pub fn updateFromZoirAlloc(
449 T: type,
450 gpa: Allocator,
451 current: *T,
452 ast: Ast,
453 zoir: Zoir,
454 diag: ?*Diagnostics,
455 options: UpdateFromOptions,
456) error{ OutOfMemory, ParseZon }!void {
457 try parseZoirAlloc(T, gpa, current, ast, zoir, diag, options.toParserOptions());
458}
459
460fn parseZoirAlloc(
461 T: type,
462 gpa: Allocator,
463 current: *T,
464 ast: Ast,
465 zoir: Zoir,
466 diag: ?*Diagnostics,
467 options: Parser.Options,
468) error{ OutOfMemory, ParseZon }!void {
469 try parseZoirNodeAlloc(T, gpa, current, ast, zoir, .root, diag, options);
470}
471
472/// Like `fromZoir`, but the parse starts at `node` instead of root.
473pub fn fromZoirNode(
474 T: type,
475 ast: Ast,
476 zoir: Zoir,
477 node: Zoir.Node.Index,
478 diag: ?*Diagnostics,
479 options: FromOptions,
480) error{ParseZon}!T {
481 comptime assert(!requiresAllocator(T));
482 var buf: [0]u8 = .{};
483 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
484 return fromZoirNodeAlloc(
485 T,
486 failing_allocator.allocator(),
487 ast,
488 zoir,
489 node,
490 diag,
491 options,
492 ) catch |err| switch (err) {
493 error.OutOfMemory => unreachable, // Checked by comptime assertion above
494 else => |e| return e,
495 };
496}
497
498/// Like `updateZoir`, but the parse starts at `node` instead of root.
499pub fn updateFromZoirNode(
500 T: type,
501 current: *T,
502 ast: Ast,
503 zoir: Zoir,
504 node: Zoir.Node.Index,
505 diag: ?*Diagnostics,
506 options: UpdateFromOptions,
507) error{ParseZon}!void {
508 comptime assert(!requiresAllocator(T));
509 var buf: [0]u8 = .{};
510 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
511 updateFromZoirNodeAlloc(
512 T,
513 failing_allocator.allocator(),
514 current,
515 ast,
516 zoir,
517 node,
518 diag,
519 options,
520 ) catch |err| switch (err) {
521 error.OutOfMemory => unreachable, // Checked by comptime assertion above
522 else => |e| return e,
523 };
524}
525
526/// Like `fromZoirAlloc`, but the parse starts at `node` instead of root.
527pub fn fromZoirNodeAlloc(
528 T: type,421 T: type,
529 gpa: Allocator,422 gpa: Allocator,
530 ast: Ast,423 ast: Ast,
...@@ -534,12 +427,12 @@ pub fn fromZoirNodeAlloc(...@@ -534,12 +427,12 @@ pub fn fromZoirNodeAlloc(
534 options: FromOptions,427 options: FromOptions,
535) error{ OutOfMemory, ParseZon }!T {428) error{ OutOfMemory, ParseZon }!T {
536 var out: T = undefined;429 var out: T = undefined;
537 try parseZoirNodeAlloc(T, gpa, &out, ast, zoir, node, diag, options.toParserOptions());430 try parseZoirAlloc(T, gpa, &out, ast, zoir, node, diag, options.toParserOptions());
538 return out;431 return out;
539}432}
540433
541/// Like `updateZoirNodeAlloc`, but the parse starts at `node` instead of root.434/// Like updateSliceAlloc`, but operates on `Zoir` instead of ZON source.
542pub fn updateFromZoirNodeAlloc(435pub fn updateFromZoirAlloc(
543 T: type,436 T: type,
544 gpa: Allocator,437 gpa: Allocator,
545 current: *T,438 current: *T,
...@@ -549,10 +442,10 @@ pub fn updateFromZoirNodeAlloc(...@@ -549,10 +442,10 @@ pub fn updateFromZoirNodeAlloc(
549 diag: ?*Diagnostics,442 diag: ?*Diagnostics,
550 options: UpdateFromOptions,443 options: UpdateFromOptions,
551) error{ OutOfMemory, ParseZon }!void {444) error{ OutOfMemory, ParseZon }!void {
552 return parseZoirNodeAlloc(T, gpa, current, ast, zoir, node, diag, options.toParserOptions());445 return parseZoirAlloc(T, gpa, current, ast, zoir, node, diag, options.toParserOptions());
553}446}
554447
555fn parseZoirNodeAlloc(448fn parseZoirAlloc(
556 T: type,449 T: type,
557 gpa: Allocator,450 gpa: Allocator,
558 out: *T,451 out: *T,
...@@ -3760,12 +3653,7 @@ test "std.zon no alloc" {...@@ -3760,12 +3653,7 @@ test "std.zon no alloc" {
37603653
3761 try std.testing.expectEqual(3654 try std.testing.expectEqual(
3762 Nested{ 1, 2, .{ 3, 4 } },3655 Nested{ 1, 2, .{ 3, 4 } },
3763 try fromZoir(Nested, ast, zoir, null, .{}),3656 try fromZoir(Nested, ast, zoir, .root, null, .{}),
3764 );
3765
3766 try std.testing.expectEqual(
3767 Nested{ 1, 2, .{ 3, 4 } },
3768 try fromZoirNode(Nested, ast, zoir, .root, null, .{}),
3769 );3657 );
3770}3658}
37713659
...@@ -3974,19 +3862,11 @@ test "std.zon variants" {...@@ -3974,19 +3862,11 @@ test "std.zon variants" {
3974 try std.testing.expectEqual(end, curr);3862 try std.testing.expectEqual(end, curr);
39753863
3976 curr = start;3864 curr = start;
3977 try updateFromZoir(Struct, &curr, ast, zoir, null, .{});3865 try updateFromZoir(Struct, &curr, ast, zoir, .root, null, .{});
3978 try std.testing.expectEqual(end, curr);3866 try std.testing.expectEqual(end, curr);
39793867
3980 curr = start;3868 curr = start;
3981 try updateFromZoirAlloc(Struct, gpa, &curr, ast, zoir, null, .{});3869 try updateFromZoirAlloc(Struct, gpa, &curr, ast, zoir, .root, null, .{});
3982 try std.testing.expectEqual(end, curr);
3983
3984 curr = start;
3985 try updateFromZoirNode(Struct, &curr, ast, zoir, .root, null, .{});
3986 try std.testing.expectEqual(end, curr);
3987
3988 curr = start;
3989 try updateFromZoirNodeAlloc(Struct, gpa, &curr, ast, zoir, .root, null, .{});
3990 try std.testing.expectEqual(end, curr);3870 try std.testing.expectEqual(end, curr);
3991 }3871 }
39923872
...@@ -4006,19 +3886,11 @@ test "std.zon variants" {...@@ -4006,19 +3886,11 @@ test "std.zon variants" {
4006 try fromSliceAlloc(Struct, gpa, ".{ .a = 100, .b = 20 }", null, .{}),3886 try fromSliceAlloc(Struct, gpa, ".{ .a = 100, .b = 20 }", null, .{}),
4007 );3887 );
4008 try std.testing.expectEqual(3888 try std.testing.expectEqual(
4009 try fromZoir(Struct, ast, zoir, null, .{}),3889 try fromZoir(Struct, ast, zoir, .root, null, .{}),
4010 end,
4011 );
4012 try std.testing.expectEqual(
4013 try fromZoirAlloc(Struct, gpa, ast, zoir, null, .{}),
4014 end,
4015 );
4016 try std.testing.expectEqual(
4017 try fromZoirNode(Struct, ast, zoir, .root, null, .{}),
4018 end,3890 end,
4019 );3891 );
4020 try std.testing.expectEqual(3892 try std.testing.expectEqual(
4021 try fromZoirNodeAlloc(Struct, gpa, ast, zoir, .root, null, .{}),3893 try fromZoirAlloc(Struct, gpa, ast, zoir, .root, null, .{}),
4022 end,3894 end,
4023 );3895 );
4024 }3896 }