std.diag
std.diag: detailed errors without error payloads. An error value is only a name; a function that needs to say more (where, and what exactly) takes `diags: *mut Diagnostics`, adds a `Diagnostic` for each problem, and returns its error. A parser can add several and still return a result. The caller reads them after `catch` and turns each byte offset into a line and column with `lineCol`.
Diagnostic
pub struct Diagnostic {
code: u32
start: usize
end: usize
message: []u8
}
One problem: a code that the caller defines, the bytes `start..end` of the input that it is about, and a message (a copy owned by the `Diagnostics`).
LineCol
pub struct LineCol {
line: usize
col: usize
}
A position in a text: line and column, both from 1 (the column counts bytes).
Diagnostics
pub struct Diagnostics {
list: List(Diagnostic)
alloc: Allocator
pub fn init(alloc: Allocator) Diagnostics
pub fn deinit(self: *mut Self)
pub fn add(self: *mut Self, code: u32, start: usize, end: usize, message: []u8) AllocError!void
pub fn len(self: *Self) usize
pub fn items(self: *Self) []Diagnostic
pub fn clear(self: *mut Self)
}
The diagnostics of an operation, in the order they were added.
pub fn init(alloc: Allocator) Diagnostics-
No diagnostics; nothing is allocated until the first `add`.
pub fn deinit(self: *mut Self)-
Frees the diagnostics and their messages.
pub fn add(self: *mut Self, code: u32, start: usize, end: usize, message: []u8) AllocError!void-
Adds a diagnostic; `message` is copied.
pub fn len(self: *Self) usize-
The number of diagnostics.
pub fn items(self: *Self) []Diagnostic-
The diagnostics, in order; valid until the next `add` or `clear`.
pub fn clear(self: *mut Self)-
Removes every diagnostic (and frees its message).
lineCol
pub fn lineCol(text: []u8, offset: usize) LineCol
The line and column of the byte at `offset` in `text` (`offset` may be `text.len`, the end).