diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 3d2606c5ecf6fac2581b6ba38eb6f29abcbb3dd2..dd26e9594c9fe79fc1292e63a555cdd17b0edcab 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); + + 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.print( - "{}:{}:{}: error: {}\n{}\n", - path, - start_loc.line + 1, - start_loc.column + 1, - text, - tree.source[start_loc.line_start..start_loc.line_end], - ); + 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 f78e666779d8f71b310e18c4164b04d95011af99..a404eca62955c10741b6d0ce7b365987e4fa74fa 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 ef171c0674578083084a36a74c0f8f940b996356..fb4827da86f707a95e2b2ad18d110dd197b97d7e 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' => {