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 @@
44//!
55//! Parsing from individual Zoir nodes is also available:
66//! * `fromZoir`/`fromZoirAlloc`
7//! * `fromZoirNode`/`fromZoirNodeAlloc`
87//!
98//! To update an existing values, see the `updateFrom*` variants.
109//!
......@@ -293,21 +292,6 @@ pub const Diagnostics = struct {
293292///
294293/// An allocator is still required for temporary allocations made during parsing.
295294pub 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
311295 T: type,
312296 gpa: Allocator,
313297 source: [:0]const u8,
......@@ -379,7 +363,7 @@ fn parseSliceAlloc(
379363 defer if (diag == null) zoir.deinit(gpa);
380364
381365 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);
383367}
384368
385369/// Like `fromSlice`, but operates on `Zoir` instead of ZON source.
......@@ -387,17 +371,17 @@ pub fn fromZoir(
387371 T: type,
388372 ast: Ast,
389373 zoir: Zoir,
374 node: Zoir.Node.Index,
390375 diag: ?*Diagnostics,
391376 options: FromOptions,
392377) error{ParseZon}!T {
393378 comptime assert(!requiresAllocator(T));
394 var buf: [0]u8 = .{};
395 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
396379 return fromZoirAlloc(
397380 T,
398 failing_allocator.allocator(),
381 .failing,
399382 ast,
400383 zoir,
384 node,
401385 diag,
402386 options,
403387 ) catch |err| switch (err) {
......@@ -412,18 +396,18 @@ pub fn updateFromZoir(
412396 current: *T,
413397 ast: Ast,
414398 zoir: Zoir,
399 node: Zoir.Node.Index,
415400 diag: ?*Diagnostics,
416401 options: UpdateFromOptions,
417402) error{ParseZon}!void {
418403 comptime assert(!requiresAllocator(T));
419 var buf: [0]u8 = .{};
420 var failing_allocator = std.heap.FixedBufferAllocator.init(&buf);
421404 updateFromZoirAlloc(
422405 T,
423 failing_allocator.allocator(),
406 .failing,
424407 current,
425408 ast,
426409 zoir,
410 node,
427411 diag,
428412 options,
429413 ) catch |err| switch (err) {
......@@ -434,97 +418,6 @@ pub fn updateFromZoir(
434418
435419/// Like `fromSliceAlloc`, but operates on `Zoir` instead of ZON source.
436420pub 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(
528421 T: type,
529422 gpa: Allocator,
530423 ast: Ast,
......@@ -534,12 +427,12 @@ pub fn fromZoirNodeAlloc(
534427 options: FromOptions,
535428) error{ OutOfMemory, ParseZon }!T {
536429 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());
538431 return out;
539432}
540433
541/// Like `updateZoirNodeAlloc`, but the parse starts at `node` instead of root.
542pub fn updateFromZoirNodeAlloc(
434/// Like updateSliceAlloc`, but operates on `Zoir` instead of ZON source.
435pub fn updateFromZoirAlloc(
543436 T: type,
544437 gpa: Allocator,
545438 current: *T,
......@@ -549,10 +442,10 @@ pub fn updateFromZoirNodeAlloc(
549442 diag: ?*Diagnostics,
550443 options: UpdateFromOptions,
551444) 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());
553446}
554447
555fn parseZoirNodeAlloc(
448fn parseZoirAlloc(
556449 T: type,
557450 gpa: Allocator,
558451 out: *T,
......@@ -3760,12 +3653,7 @@ test "std.zon no alloc" {
37603653
37613654 try std.testing.expectEqual(
37623655 Nested{ 1, 2, .{ 3, 4 } },
3763 try fromZoir(Nested, ast, zoir, null, .{}),
3764 );
3765
3766 try std.testing.expectEqual(
3767 Nested{ 1, 2, .{ 3, 4 } },
3768 try fromZoirNode(Nested, ast, zoir, .root, null, .{}),
3656 try fromZoir(Nested, ast, zoir, .root, null, .{}),
37693657 );
37703658}
37713659
......@@ -3974,19 +3862,11 @@ test "std.zon variants" {
39743862 try std.testing.expectEqual(end, curr);
39753863
39763864 curr = start;
3977 try updateFromZoir(Struct, &curr, ast, zoir, null, .{});
3865 try updateFromZoir(Struct, &curr, ast, zoir, .root, null, .{});
39783866 try std.testing.expectEqual(end, curr);
39793867
39803868 curr = start;
3981 try updateFromZoirAlloc(Struct, gpa, &curr, ast, zoir, 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, .{});
3869 try updateFromZoirAlloc(Struct, gpa, &curr, ast, zoir, .root, null, .{});
39903870 try std.testing.expectEqual(end, curr);
39913871 }
39923872
......@@ -4006,19 +3886,11 @@ test "std.zon variants" {
40063886 try fromSliceAlloc(Struct, gpa, ".{ .a = 100, .b = 20 }", null, .{}),
40073887 );
40083888 try std.testing.expectEqual(
4009 try fromZoir(Struct, ast, zoir, 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, .{}),
3889 try fromZoir(Struct, ast, zoir, .root, null, .{}),
40183890 end,
40193891 );
40203892 try std.testing.expectEqual(
4021 try fromZoirNodeAlloc(Struct, gpa, ast, zoir, .root, null, .{}),
3893 try fromZoirAlloc(Struct, gpa, ast, zoir, .root, null, .{}),
40223894 end,
40233895 );
40243896 }