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;...@@ -3,10 +3,10 @@ const string = []const u8;
3const extras = @import("extras");3const extras = @import("extras");
4const tracer = @import("tracer");4const tracer = @import("tracer");
55
6const Error = std.mem.Allocator.Error;6const Error = std.mem.Allocator.Error || error{ MalformedJson, EndOfStream };
7const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);7const 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 {
10 const t = tracer.trace(@src(), "", .{});10 const t = tracer.trace(@src(), "", .{});
11 defer t.end();11 defer t.end();
1212
...@@ -28,8 +28,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:...@@ -28,8 +28,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
28 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);28 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);
29 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);29 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);
3030
31 const root = try parseElement(alloc, &p);31 const root = parseElement(alloc, &p) catch |err| return @errorCast(err);
32 if (p.avail() > 0) return error.JsonExpectedTODO;32 if (p.avail() > 0) return error.MalformedJson;
33 const data = try p.extras.toOwnedSlice(alloc);33 const data = try p.extras.toOwnedSlice(alloc);
3434
35 return .{35 return .{
...@@ -62,7 +62,7 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {...@@ -62,7 +62,7 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex {
62 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));62 if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v));
63 if (try parseArray(alloc, p)) |v| return v;63 if (try parseArray(alloc, p)) |v| return v;
64 if (try parseObject(alloc, p)) |v| return v;64 if (try parseObject(alloc, p)) |v| return v;
65 return error.JsonExpectedValue;65 return error.MalformedJson;
66}66}
6767
68fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {68fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
...@@ -71,7 +71,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -71,7 +71,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
7171
72 p.depth += 1;72 p.depth += 1;
73 defer p.depth -= 1;73 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
76 _ = try p.eatByte('{') orelse return null;76 _ = try p.eatByte('{') orelse return null;
77 try parseWs(p);77 try parseWs(p);
...@@ -85,15 +85,15 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -85,15 +85,15 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
85 return .empty_object;85 return .empty_object;
86 }86 }
87 while (true) {87 while (true) {
88 const key = try parseString(alloc, p) orelse return error.JsonExpectedObjectKey;88 const key = try parseString(alloc, p) orelse return error.MalformedJson;
89 try parseWs(p);89 try parseWs(p);
90 _ = try p.eatByte(':') orelse return error.JsonExpectedObjectColon;90 _ = try p.eatByte(':') orelse return error.MalformedJson;
91 try parseWs(p);91 try parseWs(p);
92 const value = try parseValue(alloc, p);92 const value = try parseValue(alloc, p);
93 try members.put(alloc_local, key, value);93 try members.put(alloc_local, key, value);
94 try parseWs(p);94 try parseWs(p);
95 _ = try p.eatByte(',') orelse {95 _ = try p.eatByte(',') orelse {
96 _ = try p.eatByte('}') orelse return error.JsonExpectedTODO;96 _ = try p.eatByte('}') orelse return error.MalformedJson;
97 break;97 break;
98 };98 };
99 try parseWs(p);99 try parseWs(p);
...@@ -112,7 +112,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -112,7 +112,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
112112
113 p.depth += 1;113 p.depth += 1;
114 defer p.depth -= 1;114 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
117 _ = try p.eatByte('[') orelse return null;117 _ = try p.eatByte('[') orelse return null;
118 try parseWs(p);118 try parseWs(p);
...@@ -130,7 +130,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -130,7 +130,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
130 try elements.append(alloc_local, elem);130 try elements.append(alloc_local, elem);
131 try parseWs(p);131 try parseWs(p);
132 _ = try p.eatByte(',') orelse {132 _ = try p.eatByte(',') orelse {
133 _ = try p.eatByte(']') orelse return error.JsonExpectedTODO;133 _ = try p.eatByte(']') orelse return error.MalformedJson;
134 break;134 break;
135 };135 };
136 try parseWs(p);136 try parseWs(p);
...@@ -159,7 +159,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -159,7 +159,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
159 break;159 break;
160 }160 }
161 if (c != '\\') {161 if (c != '\\') {
162 if (c < 0x20) return error.JsonExpectedTODO;162 if (c < 0x20) return error.MalformedJson;
163 const l = std.unicode.utf8CodepointSequenceLength(c) catch unreachable;163 const l = std.unicode.utf8CodepointSequenceLength(c) catch unreachable;
164 const b = p.temp.items[p.idx - l ..][0..l];164 const b = p.temp.items[p.idx - l ..][0..l];
165 try characters.appendSlice(b);165 try characters.appendSlice(b);
...@@ -175,16 +175,16 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {...@@ -175,16 +175,16 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex {
175 'u' => {175 'u' => {
176 var o: u32 = 0;176 var o: u32 = 0;
177 var d = try p.shiftBytesN(4);177 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;
179 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));179 d = @bitCast(@byteSwap(@as(u32, @bitCast(d))));
180 for (d, 0..) |e, i| o += (e * std.math.pow(u32, 2, @intCast(i)));180 for (d, 0..) |e, i| o += (e * std.math.pow(u32, 2, @intCast(i)));
181 //181 //
182 if (o > std.math.maxInt(u21)) return error.JsonExpectedTODO;182 if (o > std.math.maxInt(u21)) return error.MalformedJson;
183 var b: [4]u8 = undefined;183 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;
185 try characters.appendSlice(b[0..l]);185 try characters.appendSlice(b[0..l]);
186 },186 },
187 else => return error.JsonExpectedTODO,187 else => return error.MalformedJson,
188 }188 }
189 }189 }
190 return try p.addStr(alloc, characters.items);190 return try p.addStr(alloc, characters.items);
...@@ -203,7 +203,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -203,7 +203,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
203 }203 }
204 if (try p.eatByte('0')) |c| {204 if (try p.eatByte('0')) |c| {
205 try characters.append(c);205 try characters.append(c);
206 if (try p.eatRange('1', '9')) |_| return error.JsonExpectedTODO;206 if (try p.eatRange('1', '9')) |_| return error.MalformedJson;
207 }207 }
208 while (try p.eatRange('0', '9')) |d| {208 while (try p.eatRange('0', '9')) |d| {
209 try characters.append(d);209 try characters.append(d);
...@@ -212,7 +212,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -212,7 +212,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
212 return null;212 return null;
213 }213 }
214 if (characters.items.len == 1 and characters.items[0] == '-') {214 if (characters.items.len == 1 and characters.items[0] == '-') {
215 return error.JsonExpectedTODO;215 return error.MalformedJson;
216 }216 }
217 if (try p.eatByte('.')) |c| {217 if (try p.eatByte('.')) |c| {
218 try characters.append(c);218 try characters.append(c);
...@@ -220,7 +220,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -220,7 +220,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
220 while (try p.eatRange('0', '9')) |d| {220 while (try p.eatRange('0', '9')) |d| {
221 try characters.append(d);221 try characters.append(d);
222 }222 }
223 if (characters.items.len == l) return error.JsonExpectedTODO;223 if (characters.items.len == l) return error.MalformedJson;
224 }224 }
225 if (try p.eatAnyScalar("eE")) |_| {225 if (try p.eatAnyScalar("eE")) |_| {
226 try characters.append('e');226 try characters.append('e');
...@@ -229,7 +229,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -229,7 +229,7 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
229 while (try p.eatRange('0', '9')) |d| {229 while (try p.eatRange('0', '9')) |d| {
230 try characters.append(d);230 try characters.append(d);
231 }231 }
232 if (characters.items.len == l) return error.JsonExpectedTODO;232 if (characters.items.len == l) return error.MalformedJson;
233 }233 }
234234
235 return try p.addNumber(alloc, characters.items);235 return try p.addNumber(alloc, characters.items);
...@@ -370,7 +370,7 @@ const Parser = struct {...@@ -370,7 +370,7 @@ const Parser = struct {
370370
371 const r = p.extras.items.len;371 const r = p.extras.items.len;
372 const l = members.entries.len;372 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;
374 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4 * 2));374 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4 * 2));
375 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.object));375 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.object));
376 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));376 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));
...@@ -386,7 +386,7 @@ const Parser = struct {...@@ -386,7 +386,7 @@ const Parser = struct {
386386
387 const r = p.extras.items.len;387 const r = p.extras.items.len;
388 const l = items.len;388 const l = items.len;
389 if (l > std.math.maxInt(u32)) return error.JsonExpectedTODO;389 if (l > std.math.maxInt(u32)) return error.MalformedJson;
390 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4));390 try p.extras.ensureUnusedCapacity(alloc, 1 + 4 + (l * 4));
391 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.array));391 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.array));
392 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));392 p.extras.appendSliceAssumeCapacity(&std.mem.toBytes(@as(u32, @intCast(l))));