From 382f87abac469db7cbc4968ca03e02cc1af5f6ee Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Mon, 14 Jun 2021 22:09:07 +0600 Subject: Rewrite printErrMsgToFile to use Message struct from Compilation --- src/main.zig | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 512828bd77..2adecd9542 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3227,17 +3227,9 @@ fn printErrMsgToFile( file: fs.File, color: Color, ) !void { - const color_on = switch (color) { - .auto => file.isTty(), - .on => true, - .off => false, - }; const lok_token = parse_error.token; - - const token_starts = tree.tokens.items(.start); - const token_tags = tree.tokens.items(.tag); - const first_token_start = token_starts[lok_token]; const start_loc = tree.tokenLocation(0, lok_token); + const source_line = tree.source[start_loc.line_start..start_loc.line_end]; var text_buf = std.ArrayList(u8).init(gpa); defer text_buf.deinit(); @@ -3245,26 +3237,24 @@ fn printErrMsgToFile( try tree.renderError(parse_error, writer); const text = text_buf.items; - const stream = file.writer(); - try stream.print("{s}:{d}:{d}: error: {s}\n", .{ path, start_loc.line + 1, start_loc.column + 1, text }); + const message: Compilation.AllErrors.Message = .{ + .src = .{ + .src_path = path, + .msg = text, + .byte_offset = @intCast(u32, start_loc.line_start), + .line = @intCast(u32, start_loc.line), + .column = @intCast(u32, start_loc.column), + .source_line = source_line, + }, + }; - if (!color_on) return; + const ttyconf: std.debug.TTY.Config = switch (color) { + .auto => std.debug.detectTTYConfig(), + .on => .escape_codes, + .off => .no_color, + }; - // Print \r and \t as one space each so that column counts line up - for (tree.source[start_loc.line_start..start_loc.line_end]) |byte| { - try stream.writeByte(switch (byte) { - '\r', '\t' => ' ', - else => byte, - }); - } - try stream.writeByte('\n'); - try stream.writeByteNTimes(' ', start_loc.column); - if (token_tags[lok_token].lexeme()) |lexeme| { - try stream.writeByteNTimes('~', lexeme.len); - try stream.writeByte('\n'); - } else { - try stream.writeAll("^\n"); - } + message.renderToStdErr(ttyconf); } pub const info_zen = -- cgit v1.2.3