authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:27 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-05-28 11:07:27 -07:00
log42bf4b50b14381d0526f3346d099536100f257af
tree53d68e24d30e59c645c82223b7b7c208115541ed
parent2ddcf31902337ca9452300c52201bdb49d591317

add tracer instrumentation


1 files changed, 39 insertions(+), 0 deletions(-)

json.zig+39
......@@ -36,6 +36,9 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype) anyerror
3636}
3737
3838fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
39 const t = tracer.trace(@src(), "", .{});
40 defer t.end();
41
3942 try parseWs(p);
4043 const v = try parseValue(alloc, p);
4144 parseWs(p) catch |err| switch (err) {
......@@ -46,6 +49,9 @@ fn parseElement(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
4649}
4750
4851fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
52 const t = tracer.trace(@src(), "", .{});
53 defer t.end();
54
4955 if (try p.eat("null")) |_| return @enumFromInt(1);
5056 if (try p.eat("true")) |_| return @enumFromInt(2);
5157 if (try p.eat("false")) |_| return @enumFromInt(3);
......@@ -57,6 +63,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
5763}
5864
5965fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
66 const t = tracer.trace(@src(), "", .{});
67 defer t.end();
68
6069 _ = try p.eatByte('{') orelse return null;
6170
6271 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);
......@@ -86,6 +95,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
8695}
8796
8897fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
98 const t = tracer.trace(@src(), "", .{});
99 defer t.end();
100
89101 _ = try p.eatByte('[') orelse return null;
90102
91103 var sfa = std.heap.stackFallback(std.mem.page_size, alloc);
......@@ -111,6 +123,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
111123}
112124
113125fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
126 const t = tracer.trace(@src(), "", .{});
127 defer t.end();
128
114129 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);
115130 var characters = std.ArrayList(u8).init(stack_fallback.get());
116131 defer characters.deinit();
......@@ -152,6 +167,9 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
152167}
153168
154169fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
170 const t = tracer.trace(@src(), "", .{});
171 defer t.end();
172
155173 var stack_fallback = std.heap.stackFallback(std.mem.page_size, alloc);
156174 var characters = std.ArrayList(u8).init(stack_fallback.get());
157175 defer characters.deinit();
......@@ -194,6 +212,9 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
194212}
195213
196214fn parseWs(p: *Parser) !void {
215 const t = tracer.trace(@src(), "", .{});
216 defer t.end();
217
197218 while (true) {
198219 if (try p.eatByte(0x20)) |_| continue; // space
199220 if (try p.eatByte(0x0A)) |_| continue; // NL
......@@ -257,6 +278,9 @@ const Parser = struct {
257278 }
258279
259280 pub fn eatByte(p: *Parser, test_c: u8) !?u8 {
281 const t = tracer.trace(@src(), " '{c}'", .{test_c});
282 defer t.end();
283
260284 try p.peekAmt(1);
261285 if (p.slice()[0] == test_c) {
262286 p.idx += 1;
......@@ -266,6 +290,9 @@ const Parser = struct {
266290 }
267291
268292 pub fn eatAnyScalar(p: *Parser, test_s: string) !?u8 {
293 const t = tracer.trace(@src(), " ({s})", .{test_s});
294 defer t.end();
295
269296 std.debug.assert(extras.matchesAll(u8, test_s, std.ascii.isASCII));
270297 try p.peekAmt(1);
271298 if (std.mem.indexOfScalar(u8, test_s, p.slice()[0])) |idx| {
......@@ -290,6 +317,9 @@ const Parser = struct {
290317 }
291318
292319 pub fn addObject(p: *Parser, alloc: std.mem.Allocator, members: *ObjectHashMap) !ValueIndex {
320 const t = tracer.trace(@src(), "({d})", .{members.entries.len});
321 defer t.end();
322
293323 const r = p.extras.items.len;
294324 const l = members.entries.len;
295325 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
......@@ -302,6 +332,9 @@ const Parser = struct {
302332 }
303333
304334 pub fn addArray(p: *Parser, alloc: std.mem.Allocator, items: []const ValueIndex) !ValueIndex {
335 const t = tracer.trace(@src(), "({d})", .{items.len});
336 defer t.end();
337
305338 const r = p.extras.items.len;
306339 const l = items.len;
307340 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
......@@ -313,6 +346,9 @@ const Parser = struct {
313346 }
314347
315348 pub fn addStr(p: *Parser, alloc: std.mem.Allocator, str: string) !StringIndex {
349 const t = tracer.trace(@src(), "({d})", .{str.len});
350 defer t.end();
351
316352 const adapter: Adapter = .{ .p = p };
317353 const res = try p.strings_map.getOrPutAdapted(alloc, str, adapter);
318354 if (res.found_existing) return res.value_ptr.*;
......@@ -345,6 +381,9 @@ const Parser = struct {
345381 };
346382
347383 pub fn addNumber(p: *Parser, alloc: std.mem.Allocator, v: []const u8) !ValueIndex {
384 const t = tracer.trace(@src(), "({s})", .{v});
385 defer t.end();
386
348387 const r = p.extras.items.len;
349388 const l = v.len;
350389 if (l > std.math.maxInt(u8)) return error.JsonExpectedTODO;