| ... | ... | @@ -476,6 +476,8 @@ const Parser = struct { |
| 476 | 476 | } |
| 477 | 477 | }; |
| 478 | 478 | |
| 479 | pub threadlocal var doc: ?*const Document = null; |
| 480 | |
| 479 | 481 | pub const Document = struct { |
| 480 | 482 | extras: []const u8, |
| 481 | 483 | root: ValueIndex, |
| ... | ... | @@ -483,6 +485,22 @@ pub const Document = struct { |
| 483 | 485 | pub fn deinit(this: *const Document, alloc: std.mem.Allocator) void { |
| 484 | 486 | alloc.free(this.extras); |
| 485 | 487 | } |
| 488 | |
| 489 | pub fn acquire(this: *const Document) void { |
| 490 | std.debug.assert(doc == null); |
| 491 | doc = this; |
| 492 | } |
| 493 | |
| 494 | pub fn release(this: *const Document) void { |
| 495 | std.debug.assert(doc == this); |
| 496 | doc = null; |
| 497 | } |
| 498 | |
| 499 | pub fn format(this: *const Document, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 500 | _ = fmt; |
| 501 | _ = options; |
| 502 | return std.fmt.format(writer, "{}", .{this.root}); |
| 503 | } |
| 486 | 504 | }; |
| 487 | 505 | |
| 488 | 506 | pub const ValueIndex = enum(u32) { |
| ... | ... | @@ -494,6 +512,47 @@ pub const ValueIndex = enum(u32) { |
| 494 | 512 | empty_array = 9, |
| 495 | 513 | empty_object = 14, |
| 496 | 514 | _, |
| 515 | |
| 516 | pub fn format(this: ValueIndex, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 517 | _ = fmt; |
| 518 | _ = options; |
| 519 | return std.fmt.format(writer, "{}", .{this.v()}); |
| 520 | } |
| 521 | |
| 522 | pub fn v(this: ValueIndex) Value { |
| 523 | std.debug.assert(this != .zero); |
| 524 | return switch (@as(Value.Tag, @enumFromInt(doc.?.extras[@intFromEnum(this)]))) { |
| 525 | .zero => .zero, |
| 526 | .null => .null, |
| 527 | .true => .true, |
| 528 | .false => .false, |
| 529 | inline .object, .array, .string, .number => |t| @unionInit(Value, @tagName(t), @enumFromInt(@intFromEnum(this))), |
| 530 | }; |
| 531 | } |
| 532 | |
| 533 | pub fn string(this: ValueIndex) []const u8 { |
| 534 | return this.v().string.to(); |
| 535 | } |
| 536 | |
| 537 | pub fn array(this: ValueIndex) []align(1) const ValueIndex { |
| 538 | return this.v().array.to(); |
| 539 | } |
| 540 | |
| 541 | pub fn object(this: ValueIndex) ObjectIndex { |
| 542 | return this.v().object; |
| 543 | } |
| 544 | |
| 545 | pub fn number(this: ValueIndex) NumberIndex { |
| 546 | return this.v().number; |
| 547 | } |
| 548 | |
| 549 | pub fn boolean(this: ValueIndex) bool { |
| 550 | return switch (this) { |
| 551 | .true => true, |
| 552 | .false => false, |
| 553 | else => unreachable, |
| 554 | }; |
| 555 | } |
| 497 | 556 | }; |
| 498 | 557 | |
| 499 | 558 | pub const Value = union(enum(u8)) { |
| ... | ... | @@ -507,20 +566,146 @@ pub const Value = union(enum(u8)) { |
| 507 | 566 | number: NumberIndex, |
| 508 | 567 | |
| 509 | 568 | const Tag = std.meta.Tag(@This()); |
| 569 | |
| 570 | pub fn format(this: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 571 | _ = options; |
| 572 | _ = fmt; |
| 573 | return switch (this) { |
| 574 | .zero => unreachable, |
| 575 | .null => std.fmt.format(writer, "null", .{}), |
| 576 | .true => std.fmt.format(writer, "true", .{}), |
| 577 | .false => std.fmt.format(writer, "false", .{}), |
| 578 | inline .object, .array, .string, .number => |t| std.fmt.format(writer, "{}", .{t}), |
| 579 | }; |
| 580 | } |
| 510 | 581 | }; |
| 511 | 582 | |
| 512 | 583 | pub const StringIndex = enum(u32) { |
| 513 | 584 | _, |
| 585 | |
| 586 | pub fn format(this: StringIndex, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 587 | _ = options; |
| 588 | _ = fmt; |
| 589 | try writer.writeAll("\""); |
| 590 | try writer.writeAll(this.to()); |
| 591 | try writer.writeAll("\""); |
| 592 | } |
| 593 | |
| 594 | pub fn to(this: StringIndex) []const u8 { |
| 595 | var d = doc.?.extras.ptr[@intFromEnum(this)..]; |
| 596 | std.debug.assert(@as(Value.Tag, @enumFromInt(d[0])) == .string); |
| 597 | const len: u32 = @bitCast(d[1..5].*); |
| 598 | return d[5..][0..len]; |
| 599 | } |
| 514 | 600 | }; |
| 515 | 601 | |
| 516 | 602 | pub const ArrayIndex = enum(u32) { |
| 517 | 603 | _, |
| 604 | |
| 605 | pub fn format(this: ArrayIndex, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 606 | _ = options; |
| 607 | _ = fmt; |
| 608 | |
| 609 | const items = this.to(); |
| 610 | try writer.writeAll("["); |
| 611 | for (items, 0..) |item, i| { |
| 612 | if (i > 0) try writer.writeAll(","); |
| 613 | try writer.print("{}", .{item}); |
| 614 | } |
| 615 | try writer.writeAll("]"); |
| 616 | } |
| 617 | |
| 618 | pub fn to(this: ArrayIndex) []align(1) const ValueIndex { |
| 619 | var d = doc.?.extras.ptr[@intFromEnum(this)..]; |
| 620 | std.debug.assert(@as(Value.Tag, @enumFromInt(d[0])) == .array); |
| 621 | const len: u32 = @bitCast(d[1..5].*); |
| 622 | const e: [*]align(1) const ValueIndex = @ptrCast(d[5..]); |
| 623 | return e[0..len]; |
| 624 | } |
| 518 | 625 | }; |
| 519 | 626 | |
| 520 | 627 | pub const ObjectIndex = enum(u32) { |
| 521 | 628 | _, |
| 629 | |
| 630 | pub fn format(this: ObjectIndex, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 631 | _ = options; |
| 632 | _ = fmt; |
| 633 | const keys, const values = this.to(); |
| 634 | try writer.writeAll("{"); |
| 635 | for (keys, values, 0..) |k, v, i| { |
| 636 | if (i > 0) try writer.writeAll(","); |
| 637 | try writer.print("{}", .{k}); |
| 638 | try writer.writeAll(":"); |
| 639 | try writer.print("{}", .{v}); |
| 640 | } |
| 641 | try writer.writeAll("}"); |
| 642 | } |
| 643 | |
| 644 | pub fn to(this: ObjectIndex) struct { []align(1) const StringIndex, []align(1) const ValueIndex } { |
| 645 | var d = doc.?.extras.ptr[@intFromEnum(this)..]; |
| 646 | std.debug.assert(@as(Value.Tag, @enumFromInt(d[0])) == .object); |
| 647 | const len: u32 = @bitCast(d[1..5].*); |
| 648 | const k: [*]align(1) const StringIndex = @ptrCast(d[5..]); |
| 649 | const v: [*]align(1) const ValueIndex = @ptrCast(k + len); |
| 650 | return .{ k[0..len], v[0..len] }; |
| 651 | } |
| 652 | |
| 653 | pub fn get(this: ObjectIndex, needle: []const u8, comptime tag: Value.Tag) ?ValueIndex { |
| 654 | const keys, const values = this.to(); |
| 655 | for (keys, values) |k, v| { |
| 656 | if (std.mem.eql(u8, needle, k.to())) { |
| 657 | if (v.v() == tag) { |
| 658 | return v; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | return null; |
| 663 | } |
| 664 | |
| 665 | pub fn getO(this: ObjectIndex, needle: []const u8) ?ObjectIndex { |
| 666 | return if (this.get(needle, .object)) |v| v.object() else null; |
| 667 | } |
| 668 | |
| 669 | pub fn getA(this: ObjectIndex, needle: []const u8) ?[]align(1) const ValueIndex { |
| 670 | return if (this.get(needle, .array)) |v| v.array() else null; |
| 671 | } |
| 672 | |
| 673 | pub fn getS(this: ObjectIndex, needle: []const u8) ?[]const u8 { |
| 674 | return if (this.get(needle, .string)) |v| v.string() else null; |
| 675 | } |
| 676 | |
| 677 | pub fn getN(this: ObjectIndex, needle: []const u8) ?NumberIndex { |
| 678 | return if (this.get(needle, .number)) |v| v.number() else null; |
| 679 | } |
| 680 | |
| 681 | pub fn getB(this: ObjectIndex, needle: []const u8) ?bool { |
| 682 | if (this.get(needle, .true)) |v| return v.boolean(); |
| 683 | if (this.get(needle, .false)) |v| return v.boolean(); |
| 684 | return null; |
| 685 | } |
| 522 | 686 | }; |
| 523 | 687 | |
| 524 | 688 | pub const NumberIndex = enum(u32) { |
| 525 | 689 | _, |
| 690 | |
| 691 | pub fn format(this: NumberIndex, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 692 | _ = options; |
| 693 | _ = fmt; |
| 694 | try writer.writeAll(this.to()); |
| 695 | } |
| 696 | |
| 697 | pub fn to(this: NumberIndex) []const u8 { |
| 698 | var d = doc.?.extras.ptr[@intFromEnum(this)..]; |
| 699 | std.debug.assert(@as(Value.Tag, @enumFromInt(d[0])) == .number); |
| 700 | const len: u32 = @bitCast(d[1..5].*); |
| 701 | return d[5..][0..len]; |
| 702 | } |
| 703 | |
| 704 | pub fn get(this: NumberIndex, comptime T: type) T { |
| 705 | return switch (@typeInfo(T)) { |
| 706 | .Int => std.fmt.parseInt(T, this.to(), 10) catch unreachable, |
| 707 | .Float => std.fmt.parseFloat(T, this.to()) catch unreachable, |
| 708 | else => @compileError("not a number type"), |
| 709 | }; |
| 710 | } |
| 526 | 711 | }; |