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:...@@ -17,7 +17,7 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
17 defer p.deinit();17 defer p.deinit();
1818
19 const root = try parseElementPrecise(alloc, &p, @TypeOf(inreader).Error || Error);19 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;
21 const data = try p.parser.data.toOwnedSlice(alloc);21 const data = try p.parser.data.toOwnedSlice(alloc);
2222
23 return .{23 return .{
...@@ -52,9 +52,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {...@@ -52,9 +52,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
52 const t = tracer.trace(@src(), "", .{});52 const t = tracer.trace(@src(), "", .{});
53 defer t.end();53 defer t.end();
5454
55 if (try p.eat("null")) |_| return @enumFromInt(1);55 if (try p.parser.eat("null")) |_| return @enumFromInt(1);
56 if (try p.eat("true")) |_| return @enumFromInt(2);56 if (try p.parser.eat("true")) |_| return @enumFromInt(2);
57 if (try p.eat("false")) |_| return @enumFromInt(3);57 if (try p.parser.eat("false")) |_| return @enumFromInt(3);
58 if (try parseNumber(alloc, p)) |v| return v;58 if (try parseNumber(alloc, p)) |v| return v;
59 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));59 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));
60 if (try parseArray(alloc, p)) |v| return v;60 if (try parseArray(alloc, p)) |v| return v;
...@@ -70,7 +70,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -70,7 +70,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
70 defer p.depth -= 1;70 defer p.depth -= 1;
71 if (p.depth > p.maximum_depth) return error.MalformedJson;71 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;
74 try parseWs(p);74 try parseWs(p);
7575
76 var sfa = std.heap.stackFallback(std.heap.page_size_min, alloc);76 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 {...@@ -78,24 +78,24 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
78 var members = ObjectHashMap{};78 var members = ObjectHashMap{};
79 defer members.deinit(alloc_local);79 defer members.deinit(alloc_local);
8080
81 if (try p.eatByte('}')) |_| {81 if (try p.parser.eatByte('}')) |_| {
82 return .empty_object;82 return .empty_object;
83 }83 }
84 while (true) {84 while (true) {
85 const key = try parseString(alloc, p) orelse return error.MalformedJson;85 const key = try parseString(alloc, p) orelse return error.MalformedJson;
86 try parseWs(p);86 try parseWs(p);
87 _ = try p.eatByte(':') orelse return error.MalformedJson;87 _ = try p.parser.eatByte(':') orelse return error.MalformedJson;
88 try parseWs(p);88 try parseWs(p);
89 const value = try parseValue(alloc, p);89 const value = try parseValue(alloc, p);
90 try members.put(alloc_local, key, value);90 try members.put(alloc_local, key, value);
91 try parseWs(p);91 try parseWs(p);
92 _ = try p.eatByte(',') orelse {92 _ = try p.parser.eatByte(',') orelse {
93 _ = try p.eatByte('}') orelse return error.MalformedJson;93 _ = try p.parser.eatByte('}') orelse return error.MalformedJson;
94 break;94 break;
95 };95 };
96 try parseWs(p);96 try parseWs(p);
97 if (!p.support_trailing_commas) continue;97 if (!p.support_trailing_commas) continue;
98 if (try p.eatByte('}')) |_| break;98 if (try p.parser.eatByte('}')) |_| break;
99 }99 }
100 if (members.entries.len == 0) {100 if (members.entries.len == 0) {
101 return .empty_object;101 return .empty_object;
...@@ -111,7 +111,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -111,7 +111,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
111 defer p.depth -= 1;111 defer p.depth -= 1;
112 if (p.depth > p.maximum_depth) return error.MalformedJson;112 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;
115 try parseWs(p);115 try parseWs(p);
116116
117 var sfa = std.heap.stackFallback(std.heap.page_size_min, alloc);117 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 {...@@ -119,20 +119,20 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
119 var elements = std.ArrayListUnmanaged(ValueIndex){};119 var elements = std.ArrayListUnmanaged(ValueIndex){};
120 defer elements.deinit(alloc_local);120 defer elements.deinit(alloc_local);
121121
122 if (try p.eatByte(']')) |_| {122 if (try p.parser.eatByte(']')) |_| {
123 return .empty_array;123 return .empty_array;
124 }124 }
125 while (true) {125 while (true) {
126 const elem = try parseValue(alloc, p);126 const elem = try parseValue(alloc, p);
127 try elements.append(alloc_local, elem);127 try elements.append(alloc_local, elem);
128 try parseWs(p);128 try parseWs(p);
129 _ = try p.eatByte(',') orelse {129 _ = try p.parser.eatByte(',') orelse {
130 _ = try p.eatByte(']') orelse return error.MalformedJson;130 _ = try p.parser.eatByte(']') orelse return error.MalformedJson;
131 break;131 break;
132 };132 };
133 try parseWs(p);133 try parseWs(p);
134 if (!p.support_trailing_commas) continue;134 if (!p.support_trailing_commas) continue;
135 if (try p.eatByte(']')) |_| break;135 if (try p.parser.eatByte(']')) |_| break;
136 }136 }
137 if (elements.items.len == 0) {137 if (elements.items.len == 0) {
138 return .empty_array;138 return .empty_array;
...@@ -148,10 +148,10 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -148,10 +148,10 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
148 var characters = std.ArrayList(u8).init(stack_fallback.get());148 var characters = std.ArrayList(u8).init(stack_fallback.get());
149 defer characters.deinit();149 defer characters.deinit();
150150
151 _ = try p.eatByte('"') orelse return null;151 _ = try p.parser.eatByte('"') orelse return null;
152152
153 while (true) {153 while (true) {
154 const c = try p.shift();154 const c = try p.parser.shift();
155 if (c == '"') {155 if (c == '"') {
156 break;156 break;
157 }157 }
...@@ -162,7 +162,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -162,7 +162,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
162 try characters.appendSlice(b);162 try characters.appendSlice(b);
163 continue;163 continue;
164 }164 }
165 switch (try p.shift()) {165 switch (try p.parser.shift()) {
166 inline 0x22, 0x5C, 0x2F => |d| try characters.append(d),166 inline 0x22, 0x5C, 0x2F => |d| try characters.append(d),
167 'b' => try characters.append(0x8),167 'b' => try characters.append(0x8),
168 'f' => try characters.append(0xC),168 'f' => try characters.append(0xC),
...@@ -171,7 +171,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -171,7 +171,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
171 't' => try characters.append(0x9),171 't' => try characters.append(0x9),
172 'u' => {172 'u' => {
173 var o: u32 = 0;173 var o: u32 = 0;
174 var d = try p.shiftBytesN(4);174 var d = try p.parser.shiftBytesN(4);
175 if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.MalformedJson;175 if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.MalformedJson;
176 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));176 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));
177 for (d, 0..) |e, i| o += (e * std.math.pow(u32, 2, @intCast(i)));177 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 {...@@ -195,14 +195,14 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
195 var characters = std.ArrayList(u8).init(stack_fallback.get());195 var characters = std.ArrayList(u8).init(stack_fallback.get());
196 defer characters.deinit();196 defer characters.deinit();
197197
198 if (try p.eatByte('-')) |c| {198 if (try p.parser.eatByte('-')) |c| {
199 try characters.append(c);199 try characters.append(c);
200 }200 }
201 if (try p.eatByte('0')) |c| {201 if (try p.parser.eatByte('0')) |c| {
202 try characters.append(c);202 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;
204 }204 }
205 while (try p.eatRange('0', '9')) |d| {205 while (try p.parser.eatRange('0', '9')) |d| {
206 try characters.append(d);206 try characters.append(d);
207 }207 }
208 if (characters.items.len == 0) {208 if (characters.items.len == 0) {
...@@ -211,19 +211,19 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -211,19 +211,19 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
211 if (characters.items.len == 1 and characters.items[0] == '-') {211 if (characters.items.len == 1 and characters.items[0] == '-') {
212 return error.MalformedJson;212 return error.MalformedJson;
213 }213 }
214 if (try p.eatByte('.')) |c| {214 if (try p.parser.eatByte('.')) |c| {
215 try characters.append(c);215 try characters.append(c);
216 const l = characters.items.len;216 const l = characters.items.len;
217 while (try p.eatRange('0', '9')) |d| {217 while (try p.parser.eatRange('0', '9')) |d| {
218 try characters.append(d);218 try characters.append(d);
219 }219 }
220 if (characters.items.len == l) return error.MalformedJson;220 if (characters.items.len == l) return error.MalformedJson;
221 }221 }
222 if (try p.eatAnyScalar("eE")) |_| {222 if (try p.parser.eatAnyScalar("eE")) |_| {
223 try characters.append('e');223 try characters.append('e');
224 try characters.append(try p.eatAnyScalar("+-") orelse '+');224 try characters.append(try p.parser.eatAnyScalar("+-") orelse '+');
225 const l = characters.items.len;225 const l = characters.items.len;
226 while (try p.eatRange('0', '9')) |d| {226 while (try p.parser.eatRange('0', '9')) |d| {
227 try characters.append(d);227 try characters.append(d);
228 }228 }
229 if (characters.items.len == l) return error.MalformedJson;229 if (characters.items.len == l) return error.MalformedJson;
...@@ -237,10 +237,10 @@ fn parseWs(p: *Parser) !void {...@@ -237,10 +237,10 @@ fn parseWs(p: *Parser) !void {
237 defer t.end();237 defer t.end();
238238
239 while (true) {239 while (true) {
240 if (try p.eatByte(0x20)) |_| continue; // space240 if (try p.parser.eatByte(0x20)) |_| continue; // space
241 if (try p.eatByte(0x0A)) |_| continue; // NL241 if (try p.parser.eatByte(0x0A)) |_| continue; // NL
242 if (try p.eatByte(0x0D)) |_| continue; // CR242 if (try p.parser.eatByte(0x0D)) |_| continue; // CR
243 if (try p.eatByte(0x09)) |_| continue; // TAB243 if (try p.parser.eatByte(0x09)) |_| continue; // TAB
244 break;244 break;
245 }245 }
246}246}
...@@ -282,8 +282,6 @@ pub const Parser = struct {...@@ -282,8 +282,6 @@ pub const Parser = struct {
282 maximum_depth: u16,282 maximum_depth: u16,
283 };283 };
284284
285 pub usingnamespace intrusive_parser.Mixin(@This());
286
287 // tag(u8) + len(u32) + member_keys(N * u32) + member_values(N * u32)285 // tag(u8) + len(u32) + member_keys(N * u32) + member_values(N * u32)
288 pub fn addObject(p: *Parser, members: *const ObjectHashMap) !ValueIndex {286 pub fn addObject(p: *Parser, members: *const ObjectHashMap) !ValueIndex {
289 const t = tracer.trace(@src(), "({d})", .{members.entries.len});287 const t = tracer.trace(@src(), "({d})", .{members.entries.len});