-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.zig
More file actions
90 lines (80 loc) · 2.41 KB
/
root.zig
File metadata and controls
90 lines (80 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const std = @import("std");
pub const Toml = union(enum) {
string : []const u8,
integer : i64,
float : f64,
boolean : bool,
datetime: DateTime,
array : Array,
table : Table,
const Self = @This();
pub const Error = error { TomlError } || std.mem.Allocator.Error;
pub const Parser = @import("plib").Parser(@import("gen").abnf);
pub const Ast = Parser.Ast;
pub const AstTag = Parser.Tag;
pub const Tag = @as(type, std.meta.Tag(Self));
pub const DateTime = @import("datetime.zig").DateTime;
pub const Array = std.ArrayListUnmanaged(Self);
pub const Table = std.StringHashMapUnmanaged(Self);
pub const Name = union(enum) {str: []const u8, num: usize};
pub const Path = std.ArrayListUnmanaged(Name);
pub usingnamespace @import("core.zig");
pub usingnamespace @import("formatter_flat.zig");
pub usingnamespace @import("formatter_json.zig");
pub usingnamespace @import("json_to_toml.zig");
};
test "edit" {
std.debug.print("\n", .{});
const allocator = std.testing.allocator;
const file_text =
\\# Top comment.
\\ # Top comment.
\\# Top comment.
\\
\\# [no-extraneous-groups-please]
\\
\\[group] # Comment
\\answer = 42 # Comment
\\# no-extraneous-keys-please = 999
\\# Inbetween comment.
\\more = [ # Comment
\\ # What about multiple # comments?
\\ # Can you handle it?
\\ #
\\ # Evil.
\\# Evil.
\\ 42, 42, # Comments within arrays are fun.
\\ # What about multiple # comments?
\\ # Can you handle it?
\\ #
\\ # Evil.
\\# Evil.
\\# ] Did I fool you?
\\] # Hopefully not.
\\
\\# Make sure the space between the datetime and "#" isn't lexed.
\\dt = 1979-05-27T07:32:12-07:00 # c
\\d = 1979-05-27 # Comment
\\
;
var edit = @import("formatter_edit.zig").init(allocator);
defer edit.deinit();
var toml = try Toml.parse(Toml, .{
.allocator = allocator,
.input = file_text,
.edit_formatter = &edit,
});
toml.table.getPtr("group").?.table.getPtr("answer").?.integer = 66;
try edit.setVal(toml);
std.debug.print("{}", .{&edit});
}
test "sentinel" {
std.debug.print("\n", .{});
const allocator = std.testing.allocator;
var toml = try Toml.parse(std.StringHashMapUnmanaged([:0]const u8), .{
.allocator = allocator,
.input = "a = \"fuck\"",
});
defer Toml.deinitAny(toml, allocator);
std.debug.print("{s}\n", .{toml.get("a").?});
}