authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-02 04:45:19 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-02 04:45:19 -07:00
log7a7ecc3115b2e7be5e63fa56461ed10aa252b811
treef38324c8b863c7f1076e7e6250cbaee258dbfab2
parent83e30f7206ff25c236d6a33a12f1e7dc2efe15e8

cleanup error set


1 files changed, 22 insertions(+), 22 deletions(-)

json.zig+22-22
......@@ -3,10 +3,10 @@ const string = []const u8;
33const extras = @import("extras");
44const tracer = @import("tracer");
55
6const Error = std.mem.Allocator.Error;
6const Error = std.mem.Allocator.Error || error{ MalformedJson, EndOfStream };
77const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);
88
9pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) anyerror!Document {
9pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) (@TypeOf(inreader).Error || Error)!Document {
1010 const t = tracer.trace(@src(), "", .{});
1111 defer t.end();
1212
......@@ -28,8 +28,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
2828 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);
2929 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);
3030
31 const root = try parseElement(alloc, &p);
32 if (p.avail() > 0) return error.JsonExpectedTODO;
31 const root = parseElement(alloc, &p) catch |err| return @errorCast(err);
32 if (p.avail() > 0) return error.MalformedJson;
3333 const data = try p.extras.toOwnedSlice(alloc);
3434
3535 return .{
......@@ -62,7 +62,7 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
6262 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));
6363 if (try parseArray(alloc, p)) |v| return v;
6464 if (try parseObject(alloc, p)) |v| return v;
65 return error.JsonExpectedValue;
65 return error.MalformedJson;
6666}
6767
6868fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
......@@ -71,7 +71,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
7171
7272 p.depth += 1;
7373 defer p.depth -= 1;
74 if (p.depth > p.maximum_depth) return error.JsonExpectedTODO;
74 if (p.depth > p.maximum_depth) return error.MalformedJson;
7575
7676 _ = try p.eatByte('{') orelse return null;
7777 try parseWs(p);
......@@ -85,15 +85,15 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
8585 return .empty_object;
8686 }
8787 while (true) {
88 const key = try parseString(alloc, p) orelse return error.JsonExpectedObjectKey;
88 const key = try parseString(alloc, p) orelse return error.MalformedJson;
8989 try parseWs(p);
90 _ = try p.eatByte(':') orelse return error.JsonExpectedObjectColon;
90 _ = try p.eatByte(':') orelse return error.MalformedJson;
9191 try parseWs(p);
9292 const value = try parseValue(alloc, p);
9393 try members.put(alloc_local, key, value);
9494 try parseWs(p);
9595 _ = try p.eatByte(',') orelse {
96 _ = try p.eatByte('}') orelse return error.JsonExpectedTODO;
96 _ = try p.eatByte('}') orelse return error.MalformedJson;
9797 break;
9898 };
9999 try parseWs(p);
......@@ -112,7 +112,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
112112
113113 p.depth += 1;
114114 defer p.depth -= 1;
115 if (p.depth > p.maximum_depth) return error.JsonExpectedTODO;
115 if (p.depth > p.maximum_depth) return error.MalformedJson;
116116
117117 _ = try p.eatByte('[') orelse return null;
118118 try parseWs(p);
......@@ -130,7 +130,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
130130 try elements.append(alloc_local, elem);
131131 try parseWs(p);
132132 _ = try p.eatByte(',') orelse {
133 _ = try p.eatByte(']') orelse return error.JsonExpectedTODO;
133 _ = try p.eatByte(']') orelse return error.MalformedJson;
134134 break;
135135 };
136136 try parseWs(p);
......@@ -159,7 +159,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
159159 break;
160160 }
161161 if (c != '\\') {
162 if (c < 0x20) return error.JsonExpectedTODO;
162 if (c < 0x20) return error.MalformedJson;
163163 const l = std.unicode.utf8CodepointSequenceLength(c) catch unreachable;
164164 const b = p.temp.items[p.idx - l ..][0..l];
165165 try characters.appendSlice(b);
......@@ -175,16 +175,16 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
175175 'u' => {
176176 var o: u32 = 0;
177177 var d = try p.shiftBytesN(4);
178 if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.JsonExpectedTODO;
178 if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.MalformedJson;
179179 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));
180180 for (d, 0..) |e, i| o += (e * std.math.pow(u32, 2, @intCast(i)));
181181 //
182 if (o > std.math.maxInt(u21)) return error.JsonExpectedTODO;
182 if (o > std.math.maxInt(u21)) return error.MalformedJson;
183183 var b: [4]u8 = undefined;
184 const l = std.unicode.utf8Encode(@intCast(o), &b) catch return error.JsonExpectedTODO;
184 const l = std.unicode.utf8Encode(@intCast(o), &b) catch return error.MalformedJson;
185185 try characters.appendSlice(b[0..l]);
186186 },
187 else => return error.JsonExpectedTODO,
187 else => return error.MalformedJson,
188188 }
189189 }
190190 return try p.addStr(alloc, characters.items);
......@@ -203,7 +203,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
203203 }
204204 if (try p.eatByte('0')) |c| {
205205 try characters.append(c);
206 if (try p.eatRange('1', '9')) |_| return error.JsonExpectedTODO;
206 if (try p.eatRange('1', '9')) |_| return error.MalformedJson;
207207 }
208208 while (try p.eatRange('0', '9')) |d| {
209209 try characters.append(d);
......@@ -212,7 +212,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
212212 return null;
213213 }
214214 if (characters.items.len == 1 and characters.items[0] == '-') {
215 return error.JsonExpectedTODO;
215 return error.MalformedJson;
216216 }
217217 if (try p.eatByte('.')) |c| {
218218 try characters.append(c);
......@@ -220,7 +220,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
220220 while (try p.eatRange('0', '9')) |d| {
221221 try characters.append(d);
222222 }
223 if (characters.items.len == l) return error.JsonExpectedTODO;
223 if (characters.items.len == l) return error.MalformedJson;
224224 }
225225 if (try p.eatAnyScalar("eE")) |_| {
226226 try characters.append('e');
......@@ -229,7 +229,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
229229 while (try p.eatRange('0', '9')) |d| {
230230 try characters.append(d);
231231 }
232 if (characters.items.len == l) return error.JsonExpectedTODO;
232 if (characters.items.len == l) return error.MalformedJson;
233233 }
234234
235235 return try p.addNumber(alloc, characters.items);
......@@ -370,7 +370,7 @@ const Parser = struct {
370370
371371 const r = p.extras.items.len;
372372 const l = members.entries.len;
373 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
373 if (l > std.math.maxInt(u32)) return error.MalformedJson;
374374 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4 * 2));
375375 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.object));
376376 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));
......@@ -386,7 +386,7 @@ const Parser = struct {
386386
387387 const r = p.extras.items.len;
388388 const l = items.len;
389 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;
389 if (l > std.math.maxInt(u32)) return error.MalformedJson;
390390 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4));
391391 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.array));
392392 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));