parser: start the arduous journey of hooking up diagnostics

The errors in the line buffer and tokenizer now have diagnostics. The
line number is trivial to keep track of due to the line buffer, but
the column index requires quite a bit of juggling, as we pass
successively trimmed down buffers to the internals of the parser.
There will probably be some column index counting problems in the
future. Also, handling the diagnostics is a bit awkward, since it's a
mandatory out-parameter of the parse functions now. The user must
provide a valid diagnostics object that survives for the life of the
parser.
This commit is contained in:
2023-09-27 23:44:06 -07:00
parent 3258e7fdb5
commit 01f98f9aff
7 changed files with 185 additions and 45 deletions

View File

@@ -15,7 +15,16 @@ pub fn main() !void {
var needfree = true;
defer if (needfree) allocator.free(data);
const document = try nice.parseBuffer(allocator, data, .{});
var diagnostics = nice.Diagnostics{};
const document = nice.parseBuffer(allocator, data, &diagnostics, .{}) catch |err| {
std.debug.print("{s}:{d} col:{d}: {s}\n", .{
args[1],
diagnostics.row,
diagnostics.line_offset,
diagnostics.message,
});
return err;
};
defer document.deinit();
// free data memory to ensure that the parsed document is not holding

View File

@@ -16,6 +16,7 @@ pub fn main() !void {
defer file.close();
var parser = try nice.StreamParser.init(allocator, .{});
defer parser.deinit();
errdefer parser.parse_state.document.deinit();
while (true) {
var buf = [_]u8{0} ** 1024;
const len = try file.read(&buf);