| ... | ... | @@ -17,7 +17,7 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: |
| 17 | 17 | defer p.deinit(); |
| 18 | 18 | |
| 19 | 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 | 21 | const data = try p.parser.data.toOwnedSlice(alloc); |
| 22 | 22 | |
| 23 | 23 | return .{ |
| ... | ... | @@ -52,9 +52,9 @@ fn parseValue(alloc: std.mem.Allocator, p: *Parser) anyerror!ValueIndex { |
| 52 | 52 | const t = tracer.trace(@src(), "", .{}); |
| 53 | 53 | defer t.end(); |
| 54 | 54 | |
| 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); |
| 58 | 58 | if (try parseNumber(alloc, p)) |v| return v; |
| 59 | 59 | if (try parseString(alloc, p)) |v| return @enumFromInt(@intFromEnum(v)); |
| 60 | 60 | if (try parseArray(alloc, p)) |v| return v; |
| ... | ... | @@ -70,7 +70,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex { |
| 70 | 70 | defer p.depth -= 1; |
| 71 | 71 | if (p.depth > p.maximum_depth) return error.MalformedJson; |
| 72 | 72 | |
| 73 | | _ = try p.eatByte('{') orelse return null; |
| 73 | _ = try p.parser.eatByte('{') orelse return null; |
| 74 | 74 | try parseWs(p); |
| 75 | 75 | |
| 76 | 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 | 78 | var members = ObjectHashMap{}; |
| 79 | 79 | defer members.deinit(alloc_local); |
| 80 | 80 | |
| 81 | | if (try p.eatByte('}')) |_| { |
| 81 | if (try p.parser.eatByte('}')) |_| { |
| 82 | 82 | return .empty_object; |
| 83 | 83 | } |
| 84 | 84 | while (true) { |
| 85 | 85 | const key = try parseString(alloc, p) orelse return error.MalformedJson; |
| 86 | 86 | try parseWs(p); |
| 87 | | _ = try p.eatByte(':') orelse return error.MalformedJson; |
| 87 | _ = try p.parser.eatByte(':') orelse return error.MalformedJson; |
| 88 | 88 | try parseWs(p); |
| 89 | 89 | const value = try parseValue(alloc, p); |
| 90 | 90 | try members.put(alloc_local, key, value); |
| 91 | 91 | 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; |
| 94 | 94 | break; |
| 95 | 95 | }; |
| 96 | 96 | try parseWs(p); |
| 97 | 97 | if (!p.support_trailing_commas) continue; |
| 98 | | if (try p.eatByte('}')) |_| break; |
| 98 | if (try p.parser.eatByte('}')) |_| break; |
| 99 | 99 | } |
| 100 | 100 | if (members.entries.len == 0) { |
| 101 | 101 | return .empty_object; |
| ... | ... | @@ -111,7 +111,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex { |
| 111 | 111 | defer p.depth -= 1; |
| 112 | 112 | if (p.depth > p.maximum_depth) return error.MalformedJson; |
| 113 | 113 | |
| 114 | | _ = try p.eatByte('[') orelse return null; |
| 114 | _ = try p.parser.eatByte('[') orelse return null; |
| 115 | 115 | try parseWs(p); |
| 116 | 116 | |
| 117 | 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 | 119 | var elements = std.ArrayListUnmanaged(ValueIndex){}; |
| 120 | 120 | defer elements.deinit(alloc_local); |
| 121 | 121 | |
| 122 | | if (try p.eatByte(']')) |_| { |
| 122 | if (try p.parser.eatByte(']')) |_| { |
| 123 | 123 | return .empty_array; |
| 124 | 124 | } |
| 125 | 125 | while (true) { |
| 126 | 126 | const elem = try parseValue(alloc, p); |
| 127 | 127 | try elements.append(alloc_local, elem); |
| 128 | 128 | 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; |
| 131 | 131 | break; |
| 132 | 132 | }; |
| 133 | 133 | try parseWs(p); |
| 134 | 134 | if (!p.support_trailing_commas) continue; |
| 135 | | if (try p.eatByte(']')) |_| break; |
| 135 | if (try p.parser.eatByte(']')) |_| break; |
| 136 | 136 | } |
| 137 | 137 | if (elements.items.len == 0) { |
| 138 | 138 | return .empty_array; |
| ... | ... | @@ -148,10 +148,10 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex { |
| 148 | 148 | var characters = std.ArrayList(u8).init(stack_fallback.get()); |
| 149 | 149 | defer characters.deinit(); |
| 150 | 150 | |
| 151 | | _ = try p.eatByte('"') orelse return null; |
| 151 | _ = try p.parser.eatByte('"') orelse return null; |
| 152 | 152 | |
| 153 | 153 | while (true) { |
| 154 | | const c = try p.shift(); |
| 154 | const c = try p.parser.shift(); |
| 155 | 155 | if (c == '"') { |
| 156 | 156 | break; |
| 157 | 157 | } |
| ... | ... | @@ -162,7 +162,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex { |
| 162 | 162 | try characters.appendSlice(b); |
| 163 | 163 | continue; |
| 164 | 164 | } |
| 165 | | switch (try p.shift()) { |
| 165 | switch (try p.parser.shift()) { |
| 166 | 166 | inline 0x22, 0x5C, 0x2F => |d| try characters.append(d), |
| 167 | 167 | 'b' => try characters.append(0x8), |
| 168 | 168 | 'f' => try characters.append(0xC), |
| ... | ... | @@ -171,7 +171,7 @@ fn parseString(alloc: std.mem.Allocator, p: *Parser) anyerror!?StringIndex { |
| 171 | 171 | 't' => try characters.append(0x9), |
| 172 | 172 | 'u' => { |
| 173 | 173 | var o: u32 = 0; |
| 174 | | var d = try p.shiftBytesN(4); |
| 174 | var d = try p.parser.shiftBytesN(4); |
| 175 | 175 | if (!extras.matchesAll(u8, &d, std.ascii.isHex)) return error.MalformedJson; |
| 176 | 176 | d = @bitCast(@byteSwap(@as(u32, @bitCast(d)))); |
| 177 | 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 | 195 | var characters = std.ArrayList(u8).init(stack_fallback.get()); |
| 196 | 196 | defer characters.deinit(); |
| 197 | 197 | |
| 198 | | if (try p.eatByte('-')) |c| { |
| 198 | if (try p.parser.eatByte('-')) |c| { |
| 199 | 199 | try characters.append(c); |
| 200 | 200 | } |
| 201 | | if (try p.eatByte('0')) |c| { |
| 201 | if (try p.parser.eatByte('0')) |c| { |
| 202 | 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 | 206 | try characters.append(d); |
| 207 | 207 | } |
| 208 | 208 | if (characters.items.len == 0) { |
| ... | ... | @@ -211,19 +211,19 @@ fn parseNumber(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex { |
| 211 | 211 | if (characters.items.len == 1 and characters.items[0] == '-') { |
| 212 | 212 | return error.MalformedJson; |
| 213 | 213 | } |
| 214 | | if (try p.eatByte('.')) |c| { |
| 214 | if (try p.parser.eatByte('.')) |c| { |
| 215 | 215 | try characters.append(c); |
| 216 | 216 | const l = characters.items.len; |
| 217 | | while (try p.eatRange('0', '9')) |d| { |
| 217 | while (try p.parser.eatRange('0', '9')) |d| { |
| 218 | 218 | try characters.append(d); |
| 219 | 219 | } |
| 220 | 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 | 223 | try characters.append('e'); |
| 224 | | try characters.append(try p.eatAnyScalar("+-") orelse '+'); |
| 224 | try characters.append(try p.parser.eatAnyScalar("+-") orelse '+'); |
| 225 | 225 | const l = characters.items.len; |
| 226 | | while (try p.eatRange('0', '9')) |d| { |
| 226 | while (try p.parser.eatRange('0', '9')) |d| { |
| 227 | 227 | try characters.append(d); |
| 228 | 228 | } |
| 229 | 229 | if (characters.items.len == l) return error.MalformedJson; |
| ... | ... | @@ -237,10 +237,10 @@ fn parseWs(p: *Parser) !void { |
| 237 | 237 | defer t.end(); |
| 238 | 238 | |
| 239 | 239 | 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 |
| 244 | 244 | break; |
| 245 | 245 | } |
| 246 | 246 | } |
| ... | ... | @@ -282,8 +282,6 @@ pub const Parser = struct { |
| 282 | 282 | maximum_depth: u16, |
| 283 | 283 | }; |
| 284 | 284 | |
| 285 | | pub usingnamespace intrusive_parser.Mixin(@This()); |
| 286 | | |
| 287 | 285 | // tag(u8) + len(u32) + member_keys(N * u32) + member_values(N * u32) |
| 288 | 286 | pub fn addObject(p: *Parser, members: *const ObjectHashMap) !ValueIndex { |
| 289 | 287 | const t = tracer.trace(@src(), "({d})", .{members.entries.len}); |