| ... | ... | @@ -4,7 +4,6 @@ |
| 4 | 4 | //! |
| 5 | 5 | //! Parsing from individual Zoir nodes is also available: |
| 6 | 6 | //! * `fromZoir`/`fromZoirAlloc` |
| 7 | | //! * `fromZoirNode`/`fromZoirNodeAlloc` |
| 8 | 7 | //! |
| 9 | 8 | //! To update an existing values, see the `updateFrom*` variants. |
| 10 | 9 | //! |
| ... | ... | @@ -293,21 +292,6 @@ pub const Diagnostics = struct { |
| 293 | 292 | /// |
| 294 | 293 | /// An allocator is still required for temporary allocations made during parsing. |
| 295 | 294 | pub 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 | 295 | T: type, |
| 312 | 296 | gpa: Allocator, |
| 313 | 297 | source: [:0]const u8, |
| ... | ... | @@ -379,7 +363,7 @@ fn parseSliceAlloc( |
| 379 | 363 | defer if (diag == null) zoir.deinit(gpa); |
| 380 | 364 | |
| 381 | 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 | } |
| 384 | 368 | |
| 385 | 369 | /// Like `fromSlice`, but operates on `Zoir` instead of ZON source. |
| ... | ... | @@ -387,17 +371,17 @@ pub fn fromZoir( |
| 387 | 371 | T: type, |
| 388 | 372 | ast: Ast, |
| 389 | 373 | zoir: Zoir, |
| 374 | node: Zoir.Node.Index, |
| 390 | 375 | diag: ?*Diagnostics, |
| 391 | 376 | options: FromOptions, |
| 392 | 377 | ) error{ParseZon}!T { |
| 393 | 378 | comptime assert(!requiresAllocator(T)); |
| 394 | | var buf: [0]u8 = .{}; |
| 395 | | var failing_allocator = std.heap.FixedBufferAllocator.init(&buf); |
| 396 | 379 | return fromZoirAlloc( |
| 397 | 380 | T, |
| 398 | | failing_allocator.allocator(), |
| 381 | .failing, |
| 399 | 382 | ast, |
| 400 | 383 | zoir, |
| 384 | node, |
| 401 | 385 | diag, |
| 402 | 386 | options, |
| 403 | 387 | ) catch |err| switch (err) { |
| ... | ... | @@ -412,18 +396,18 @@ pub fn updateFromZoir( |
| 412 | 396 | current: *T, |
| 413 | 397 | ast: Ast, |
| 414 | 398 | zoir: Zoir, |
| 399 | node: Zoir.Node.Index, |
| 415 | 400 | diag: ?*Diagnostics, |
| 416 | 401 | options: UpdateFromOptions, |
| 417 | 402 | ) error{ParseZon}!void { |
| 418 | 403 | comptime assert(!requiresAllocator(T)); |
| 419 | | var buf: [0]u8 = .{}; |
| 420 | | var failing_allocator = std.heap.FixedBufferAllocator.init(&buf); |
| 421 | 404 | updateFromZoirAlloc( |
| 422 | 405 | T, |
| 423 | | failing_allocator.allocator(), |
| 406 | .failing, |
| 424 | 407 | current, |
| 425 | 408 | ast, |
| 426 | 409 | zoir, |
| 410 | node, |
| 427 | 411 | diag, |
| 428 | 412 | options, |
| 429 | 413 | ) catch |err| switch (err) { |
| ... | ... | @@ -434,97 +418,6 @@ pub fn updateFromZoir( |
| 434 | 418 | |
| 435 | 419 | /// Like `fromSliceAlloc`, but operates on `Zoir` instead of ZON source. |
| 436 | 420 | pub 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. |
| 448 | | pub 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 | | |
| 460 | | fn 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. |
| 473 | | pub 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. |
| 499 | | pub 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. |
| 527 | | pub fn fromZoirNodeAlloc( |
| 528 | 421 | T: type, |
| 529 | 422 | gpa: Allocator, |
| 530 | 423 | ast: Ast, |
| ... | ... | @@ -534,12 +427,12 @@ pub fn fromZoirNodeAlloc( |
| 534 | 427 | options: FromOptions, |
| 535 | 428 | ) error{ OutOfMemory, ParseZon }!T { |
| 536 | 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 | 431 | return out; |
| 539 | 432 | } |
| 540 | 433 | |
| 541 | | /// Like `updateZoirNodeAlloc`, but the parse starts at `node` instead of root. |
| 542 | | pub fn updateFromZoirNodeAlloc( |
| 434 | /// Like updateSliceAlloc`, but operates on `Zoir` instead of ZON source. |
| 435 | pub fn updateFromZoirAlloc( |
| 543 | 436 | T: type, |
| 544 | 437 | gpa: Allocator, |
| 545 | 438 | current: *T, |
| ... | ... | @@ -549,10 +442,10 @@ pub fn updateFromZoirNodeAlloc( |
| 549 | 442 | diag: ?*Diagnostics, |
| 550 | 443 | options: UpdateFromOptions, |
| 551 | 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 | } |
| 554 | 447 | |
| 555 | | fn parseZoirNodeAlloc( |
| 448 | fn parseZoirAlloc( |
| 556 | 449 | T: type, |
| 557 | 450 | gpa: Allocator, |
| 558 | 451 | out: *T, |
| ... | ... | @@ -3760,12 +3653,7 @@ test "std.zon no alloc" { |
| 3760 | 3653 | |
| 3761 | 3654 | try std.testing.expectEqual( |
| 3762 | 3655 | 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, .{}), |
| 3769 | 3657 | ); |
| 3770 | 3658 | } |
| 3771 | 3659 | |
| ... | ... | @@ -3974,19 +3862,11 @@ test "std.zon variants" { |
| 3974 | 3862 | try std.testing.expectEqual(end, curr); |
| 3975 | 3863 | |
| 3976 | 3864 | curr = start; |
| 3977 | | try updateFromZoir(Struct, &curr, ast, zoir, null, .{}); |
| 3865 | try updateFromZoir(Struct, &curr, ast, zoir, .root, null, .{}); |
| 3978 | 3866 | try std.testing.expectEqual(end, curr); |
| 3979 | 3867 | |
| 3980 | 3868 | 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, .{}); |
| 3990 | 3870 | try std.testing.expectEqual(end, curr); |
| 3991 | 3871 | } |
| 3992 | 3872 | |
| ... | ... | @@ -4006,19 +3886,11 @@ test "std.zon variants" { |
| 4006 | 3886 | try fromSliceAlloc(Struct, gpa, ".{ .a = 100, .b = 20 }", null, .{}), |
| 4007 | 3887 | ); |
| 4008 | 3888 | 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, .{}), |
| 4018 | 3890 | end, |
| 4019 | 3891 | ); |
| 4020 | 3892 | try std.testing.expectEqual( |
| 4021 | | try fromZoirNodeAlloc(Struct, gpa, ast, zoir, .root, null, .{}), |
| 3893 | try fromZoirAlloc(Struct, gpa, ast, zoir, .root, null, .{}), |
| 4022 | 3894 | end, |
| 4023 | 3895 | ); |
| 4024 | 3896 | } |