| ... | ... | @@ -50,8 +50,7 @@ pub const Error = union(enum) { |
| 50 | 50 | switch (self.err) { |
| 51 | 51 | .zoir => |err| { |
| 52 | 52 | if (self.index >= err.note_count) return null; |
| 53 | | const zoir = self.diag.zoir.?; |
| 54 | | const note = err.getNotes(zoir)[self.index]; |
| 53 | const note = err.getNotes(self.diag.zoir)[self.index]; |
| 55 | 54 | self.index += 1; |
| 56 | 55 | return .{ .zoir = note }; |
| 57 | 56 | }, |
| ... | ... | @@ -82,16 +81,15 @@ pub const Error = union(enum) { |
| 82 | 81 | |
| 83 | 82 | pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter(Note.formatMessage) { |
| 84 | 83 | return .{ .data = switch (self) { |
| 85 | | .zoir => |note| note.msg.get(diag.zoir.?), |
| 84 | .zoir => |note| note.msg.get(diag.zoir), |
| 86 | 85 | .type_check => |note| note.msg, |
| 87 | 86 | } }; |
| 88 | 87 | } |
| 89 | 88 | |
| 90 | 89 | pub fn getLocation(self: Note, diag: *const Diagnostics) Ast.Location { |
| 91 | | const ast = diag.ast.?; |
| 92 | 90 | switch (self) { |
| 93 | | .zoir => |note| return zoirErrorLocation(ast, note.token, note.node_or_offset), |
| 94 | | .type_check => |note| return ast.tokenLocation(note.offset, note.token), |
| 91 | .zoir => |note| return zoirErrorLocation(diag.ast, note.token, note.node_or_offset), |
| 92 | .type_check => |note| return diag.ast.tokenLocation(note.offset, note.token), |
| 95 | 93 | } |
| 96 | 94 | } |
| 97 | 95 | }; |
| ... | ... | @@ -101,16 +99,14 @@ pub const Error = union(enum) { |
| 101 | 99 | diag: *const Diagnostics, |
| 102 | 100 | |
| 103 | 101 | pub fn next(self: *@This()) ?Error { |
| 104 | | const zoir = self.diag.zoir orelse return null; |
| 105 | | |
| 106 | | if (self.index < zoir.compile_errors.len) { |
| 107 | | const result: Error = .{ .zoir = zoir.compile_errors[self.index] }; |
| 102 | if (self.index < self.diag.zoir.compile_errors.len) { |
| 103 | const result: Error = .{ .zoir = self.diag.zoir.compile_errors[self.index] }; |
| 108 | 104 | self.index += 1; |
| 109 | 105 | return result; |
| 110 | 106 | } |
| 111 | 107 | |
| 112 | 108 | if (self.diag.type_check) |err| { |
| 113 | | if (self.index == zoir.compile_errors.len) { |
| 109 | if (self.index == self.diag.zoir.compile_errors.len) { |
| 114 | 110 | const result: Error = .{ .type_check = err }; |
| 115 | 111 | self.index += 1; |
| 116 | 112 | return result; |
| ... | ... | @@ -168,7 +164,7 @@ pub const Error = union(enum) { |
| 168 | 164 | _ = f; |
| 169 | 165 | _ = options; |
| 170 | 166 | switch (self.err) { |
| 171 | | .zoir => |err| try writer.writeAll(err.msg.get(self.diag.zoir.?)), |
| 167 | .zoir => |err| try writer.writeAll(err.msg.get(self.diag.zoir)), |
| 172 | 168 | .type_check => |tc| try writer.writeAll(tc.message), |
| 173 | 169 | } |
| 174 | 170 | } |
| ... | ... | @@ -181,14 +177,13 @@ pub const Error = union(enum) { |
| 181 | 177 | } |
| 182 | 178 | |
| 183 | 179 | pub fn getLocation(self: @This(), diag: *const Diagnostics) Ast.Location { |
| 184 | | const ast = diag.ast.?; |
| 185 | 180 | return switch (self) { |
| 186 | 181 | .zoir => |err| return zoirErrorLocation( |
| 187 | | diag.ast.?, |
| 182 | diag.ast, |
| 188 | 183 | err.token, |
| 189 | 184 | err.node_or_offset, |
| 190 | 185 | ), |
| 191 | | .type_check => |err| return ast.tokenLocation(err.offset, err.token), |
| 186 | .type_check => |err| return diag.ast.tokenLocation(err.offset, err.token), |
| 192 | 187 | }; |
| 193 | 188 | } |
| 194 | 189 | |
| ... | ... | @@ -211,19 +206,33 @@ pub const Error = union(enum) { |
| 211 | 206 | |
| 212 | 207 | /// Information about the success or failure of a parse. |
| 213 | 208 | pub const Diagnostics = struct { |
| 214 | | ast: ?Ast = null, |
| 215 | | zoir: ?Zoir = null, |
| 209 | ast: Ast = .{ |
| 210 | .source = "", |
| 211 | .tokens = .empty, |
| 212 | .nodes = .empty, |
| 213 | .extra_data = &.{}, |
| 214 | .mode = .zon, |
| 215 | .errors = &.{}, |
| 216 | }, |
| 217 | zoir: Zoir = .{ |
| 218 | .nodes = .empty, |
| 219 | .extra = &.{}, |
| 220 | .limbs = &.{}, |
| 221 | .string_bytes = &.{}, |
| 222 | .compile_errors = &.{}, |
| 223 | .error_notes = &.{}, |
| 224 | }, |
| 216 | 225 | type_check: ?Error.TypeCheckFailure = null, |
| 217 | 226 | |
| 218 | 227 | fn assertEmpty(self: Diagnostics) void { |
| 219 | | assert(self.ast == null); |
| 220 | | assert(self.zoir == null); |
| 228 | assert(self.ast.tokens.len == 0); |
| 229 | assert(self.zoir.nodes.len == 0); |
| 221 | 230 | assert(self.type_check == null); |
| 222 | 231 | } |
| 223 | 232 | |
| 224 | 233 | pub fn deinit(self: *Diagnostics, gpa: Allocator) void { |
| 225 | | if (self.ast) |*ast| ast.deinit(gpa); |
| 226 | | if (self.zoir) |*zoir| zoir.deinit(gpa); |
| 234 | self.ast.deinit(gpa); |
| 235 | self.zoir.deinit(gpa); |
| 227 | 236 | if (self.type_check) |tc| tc.deinit(gpa); |
| 228 | 237 | self.* = undefined; |
| 229 | 238 | } |
| ... | ... | @@ -3459,13 +3468,13 @@ test "std.zon stop on node" { |
| 3459 | 3468 | defer diag.deinit(gpa); |
| 3460 | 3469 | const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &diag, .{}); |
| 3461 | 3470 | try std.testing.expectEqual(result.y, 2.5); |
| 3462 | | try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(diag.zoir.?)); |
| 3471 | try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(diag.zoir)); |
| 3463 | 3472 | } |
| 3464 | 3473 | |
| 3465 | 3474 | { |
| 3466 | 3475 | var diag: Diagnostics = .{}; |
| 3467 | 3476 | defer diag.deinit(gpa); |
| 3468 | 3477 | const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &diag, .{}); |
| 3469 | | try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(diag.zoir.?)); |
| 3478 | try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(diag.zoir)); |
| 3470 | 3479 | } |
| 3471 | 3480 | } |