diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 3d2606c5ec..dd26e9594c 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -375,28 +375,21 @@ fn printErrMsgToFile( const text = text_buf.toOwnedSlice(); const stream = &file.outStream().stream; - if (!color_on) { - try stream.print( - "{}:{}:{}: error: {}\n", - path, - start_loc.line + 1, - start_loc.column + 1, - text, - ); - return; - } + try stream.print( "{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, text); - try stream.print( - "{}:{}:{}: error: {}\n{}\n", - path, - start_loc.line + 1, - start_loc.column + 1, - text, - tree.source[start_loc.line_start..start_loc.line_end], - ); + if (!color_on) return; + + // 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); try stream.writeByteNTimes('~', last_token.end - first_token.start); - try stream.write("\n"); + try stream.writeByte('\n'); } export fn stage2_DepTokenizer_init(input: [*]const u8, len: usize) stage2_DepTokenizer { diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index f78e666779..a404eca629 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -8,6 +8,15 @@ test "zig fmt: change use to usingnamespace" { ); } +test "zig fmt: whitespace fixes" { + try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}", + \\test "" { + \\ const hi = x; + \\} + \\ + ); +} + test "zig fmt: while else err prong with no block" { try testCanonical( \\test "" { diff --git a/std/zig/tokenizer.zig b/std/zig/tokenizer.zig index ef171c0674..fb4827da86 100644 --- a/std/zig/tokenizer.zig +++ b/std/zig/tokenizer.zig @@ -301,10 +301,7 @@ pub const Tokenizer = struct { const c = self.buffer[self.index]; switch (state) { State.Start => switch (c) { - ' ' => { - result.start = self.index + 1; - }, - '\n' => { + ' ', '\n', '\t', '\r' => { result.start = self.index + 1; }, 'c' => {