authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 02:35:47 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 02:35:47 -07:00
log02fb7045dc16c791436f7f4127c8a072e7acdff8
tree7a7ff04f4d52a8d08b9a48303e404115120a6790
parent92dd6f67bbb52f060d5ac20719142b0211854290

stop using intrusive_parser.Mixin


1 files changed, 32 insertions(+), 34 deletions(-)

json.zig+32-34
......@@ -17,7 +17,7 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
1717 defer p.deinit();
1818
1919 const root = try parseElementPrecise(alloc, &p, @TypeOf(inreader).Error || Error);
20 if (p.avail() > 0) return error.MalformedJson;
20 if (p.parser.avail() > 0) return error.MalformedJson;
2121 const data = try p.parser.data.toOwnedSlice(alloc);
2222
2323 return .{
......@@ -52,9 +52,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
5252 const t = tracer.trace(@src(), "", .{});
5353 defer t.end();
5454
55 if (try p.eat("null")) |_| return @enumFromInt(1);
56 if (try p.eat("true")) |_| return @enumFromInt(2);
57 if (try p.eat("false")) |_| return @enumFromInt(3);
55 if (try p.parser.eat("null")) |_| return @enumFromInt(1);
56 if (try p.parser.eat("true")) |_| return @enumFromInt(2);
57 if (try p.parser.eat("false")) |_| return @enumFromInt(3);
5858 if (try parseNumber(alloc, p)) |v| return v;
5959 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));
6060 if (try parseArray(alloc, p)) |v| return v;
......@@ -70,7 +70,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
7070 defer p.depth -= 1;
7171 if (p.depth > p.maximum_depth) return error.MalformedJson;
7272
73 _ = try p.eatByte('{') orelse return null;
73 _ = try p.parser.eatByte('{') orelse return null;
7474 try parseWs(p);
7575
7676 var sfa = std.heap.stackFallback(std.heap.page_size_min, alloc);
......@@ -78,24 +78,24 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
7878 var members = ObjectHashMap{};
7979 defer members.deinit(alloc_local);
8080
81 if (try p.eatByte('}')) |_| {
81 if (try p.parser.eatByte('}')) |_| {
8282 return .empty_object;
8383 }
8484 while (true) {
8585 const key = try parseString(alloc, p) orelse return error.MalformedJson;
8686 try parseWs(p);
87 _ = try p.eatByte(':') orelse return error.MalformedJson;
87 _ = try p.parser.eatByte(':') orelse return error.MalformedJson;
8888 try parseWs(p);
8989 const value = try parseValue(alloc, p);
9090 try members.put(alloc_local, key, value);
9191 try parseWs(p);
92 _ = try p.eatByte(',') orelse {
93 _ = try p.eatByte('}') orelse return error.MalformedJson;
92 _ = try p.parser.eatByte(',') orelse {
93 _ = try p.parser.eatByte('}') orelse return error.MalformedJson;
9494 break;
9595 };
9696 try parseWs(p);
9797 if (!p.support_trailing_commas) continue;
98 if (try p.eatByte('}')) |_| break;
98 if (try p.parser.eatByte('}')) |_| break;
9999 }
100100 if (members.entries.len == 0) {
101101 return .empty_object;
......@@ -111,7 +111,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
111111 defer p.depth -= 1;
112112 if (p.depth > p.maximum_depth) return error.MalformedJson;
113113
114 _ = try p.eatByte('[') orelse return null;
114 _ = try p.parser.eatByte('[') orelse return null;
115115 try parseWs(p);
116116
117117 var sfa = std.heap.stackFallback(std.heap.page_size_min, alloc);
......@@ -119,20 +119,20 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
119119 var elements = std.ArrayListUnmanaged(ValueIndex){};
120120 defer elements.deinit(alloc_local);
121121
122 if (try p.eatByte(']')) |_| {
122 if (try p.parser.eatByte(']')) |_| {
123123 return .empty_array;
124124 }
125125 while (true) {
126126 const elem = try parseValue(alloc, p);
127127 try elements.append(alloc_local, elem);
128128 try parseWs(p);
129 _ = try p.eatByte(',') orelse {
130 _ = try p.eatByte(']') orelse return error.MalformedJson;
129 _ = try p.parser.eatByte(',') orelse {
130 _ = try p.parser.eatByte(']') orelse return error.MalformedJson;
131131 break;
132132 };
133133 try parseWs(p);
134134 if (!p.support_trailing_commas) continue;
135 if (try p.eatByte(']')) |_| break;
135 if (try p.parser.eatByte(']')) |_| break;
136136 }
137137 if (elements.items.len == 0) {
138138 return .empty_array;
......@@ -148,10 +148,10 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
148148 var characters = std.ArrayList(u8).init(stack_fallback.get());
149149 defer characters.deinit();
150150
151 _ = try p.eatByte('"') orelse return null;
151 _ = try p.parser.eatByte('"') orelse return null;
152152
153153 while (true) {
154 const c = try p.shift();
154 const c = try p.parser.shift();
155155 if (c == '"') {
156156 break;
157157 }
......@@ -162,7 +162,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
162162 try characters.appendSlice(b);
163163 continue;
164164 }
165 switch (try p.shift()) {
165 switch (try p.parser.shift()) {
166166 inline 0x22, 0x5C, 0x2F => |d| try characters.append(d),
167167 'b' => try characters.append(0x8),
168168 'f' => try characters.append(0xC),
......@@ -171,7 +171,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
171171 't' => try characters.append(0x9),
172172 'u' => {
173173 var o: u32 = 0;
174 var d = try p.shiftBytesN(4);
174 var d = try p.parser.shiftBytesN(4);
175175 if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.MalformedJson;
176176 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));
177177 for (d, 0..) |e, i| o += (e * std.math.pow(u32, 2, @intCast(i)));
......@@ -195,14 +195,14 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
195195 var characters = std.ArrayList(u8).init(stack_fallback.get());
196196 defer characters.deinit();
197197
198 if (try p.eatByte('-')) |c| {
198 if (try p.parser.eatByte('-')) |c| {
199199 try characters.append(c);
200200 }
201 if (try p.eatByte('0')) |c| {
201 if (try p.parser.eatByte('0')) |c| {
202202 try characters.append(c);
203 if (try p.eatRange('1', '9')) |_| return error.MalformedJson;
203 if (try p.parser.eatRange('1', '9')) |_| return error.MalformedJson;
204204 }
205 while (try p.eatRange('0', '9')) |d| {
205 while (try p.parser.eatRange('0', '9')) |d| {
206206 try characters.append(d);
207207 }
208208 if (characters.items.len == 0) {
......@@ -211,19 +211,19 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
211211 if (characters.items.len == 1 and characters.items[0] == '-') {
212212 return error.MalformedJson;
213213 }
214 if (try p.eatByte('.')) |c| {
214 if (try p.parser.eatByte('.')) |c| {
215215 try characters.append(c);
216216 const l = characters.items.len;
217 while (try p.eatRange('0', '9')) |d| {
217 while (try p.parser.eatRange('0', '9')) |d| {
218218 try characters.append(d);
219219 }
220220 if (characters.items.len == l) return error.MalformedJson;
221221 }
222 if (try p.eatAnyScalar("eE")) |_| {
222 if (try p.parser.eatAnyScalar("eE")) |_| {
223223 try characters.append('e');
224 try characters.append(try p.eatAnyScalar("+-") orelse '+');
224 try characters.append(try p.parser.eatAnyScalar("+-") orelse '+');
225225 const l = characters.items.len;
226 while (try p.eatRange('0', '9')) |d| {
226 while (try p.parser.eatRange('0', '9')) |d| {
227227 try characters.append(d);
228228 }
229229 if (characters.items.len == l) return error.MalformedJson;
......@@ -237,10 +237,10 @@ fn parseWs(p: *Parser) !void {
237237 defer t.end();
238238
239239 while (true) {
240 if (try p.eatByte(0x20)) |_| continue; // space
241 if (try p.eatByte(0x0A)) |_| continue; // NL
242 if (try p.eatByte(0x0D)) |_| continue; // CR
243 if (try p.eatByte(0x09)) |_| continue; // TAB
240 if (try p.parser.eatByte(0x20)) |_| continue; // space
241 if (try p.parser.eatByte(0x0A)) |_| continue; // NL
242 if (try p.parser.eatByte(0x0D)) |_| continue; // CR
243 if (try p.parser.eatByte(0x09)) |_| continue; // TAB
244244 break;
245245 }
246246}
......@@ -282,8 +282,6 @@ pub const Parser = struct {
282282 maximum_depth: u16,
283283 };
284284
285 pub usingnamespace intrusive_parser.Mixin(@This());
286
287285 // tag(u8) + len(u32) + member_keys(N * u32) + member_values(N * u32)
288286 pub fn addObject(p: *Parser, members: *const ObjectHashMap) !ValueIndex {
289287 const t = tracer.trace(@src(), "({d})", .{members.entries.len});