nice-data/examples/stream.zig
torque 01f98f9aff
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.
2023-09-27 23:44:06 -07:00

32 lines
923 B
Zig

const std = @import("std");
const nice = @import("nice");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) return;
const document: nice.Document = doc: {
const file = try std.fs.cwd().openFile(args[1], .{});
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);
if (len == 0) break;
try parser.feed(buf[0..len]);
}
break :doc try parser.finish();
};
defer document.deinit();
document.printDebug();
}