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