feat: serialize lex errors into json
This commit is contained in:
parent
bcb2ddf6c2
commit
d61063b279
23
src/cli.zig
23
src/cli.zig
@ -31,11 +31,24 @@ pub fn tokenize_to_json() !void {
|
|||||||
defer tokens.deinit();
|
defer tokens.deinit();
|
||||||
|
|
||||||
// serialize & print json to stdout
|
// serialize & print json to stdout
|
||||||
const json_out = try std.json.stringifyAlloc(alloc, .{
|
var json_arrl = std.ArrayList(u8).init(alloc);
|
||||||
.errors = error_array.items,
|
defer json_arrl.deinit();
|
||||||
}, .{});
|
var json_writer = json_arrl.writer();
|
||||||
defer alloc.free(json_out);
|
|
||||||
try stdout.print("{s}", .{json_out});
|
try json_writer.writeAll("{\"errors\":[");
|
||||||
|
|
||||||
|
const errors_len = error_array.items.len - 1;
|
||||||
|
for (error_array.items, 0..) |err, idx| {
|
||||||
|
try err.write_json(alloc, json_writer);
|
||||||
|
|
||||||
|
// write a comma only if there are items left
|
||||||
|
if (idx < errors_len) {
|
||||||
|
try json_writer.writeAll(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try json_writer.writeAll("]}");
|
||||||
|
|
||||||
|
try stdout.print("{s}", .{json_arrl.items});
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
|
||||||
// the end
|
// the end
|
||||||
|
Loading…
Reference in New Issue
Block a user