@@ -2937,7 +2937,6 @@ const Fmt = struct {
29372937};
29382938
29392939pub fn cmdFmt (gpa : * Allocator , args : []const []const u8 ) ! void {
2940- const stderr_file = io .getStdErr ();
29412940 var color : Color = .auto ;
29422941 var stdin_flag : bool = false ;
29432942 var check_flag : bool = false ;
@@ -2992,7 +2991,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
29922991 defer tree .deinit (gpa );
29932992
29942993 for (tree .errors ) | parse_error | {
2995- try printErrMsgToFile (gpa , parse_error , tree , "<stdin>" , stderr_file , color );
2994+ try printErrMsgToStdErr (gpa , parse_error , tree , "<stdin>" , color );
29962995 }
29972996 if (tree .errors .len != 0 ) {
29982997 process .exit (1 );
@@ -3136,7 +3135,7 @@ fn fmtPathFile(
31363135 defer tree .deinit (fmt .gpa );
31373136
31383137 for (tree .errors ) | parse_error | {
3139- try printErrMsgToFile (fmt .gpa , parse_error , tree , file_path , std . io . getStdErr () , fmt .color );
3138+ try printErrMsgToStdErr (fmt .gpa , parse_error , tree , file_path , fmt .color );
31403139 }
31413140 if (tree .errors .len != 0 ) {
31423141 fmt .any_error = true ;
@@ -3166,52 +3165,41 @@ fn fmtPathFile(
31663165 }
31673166}
31683167
3169- fn printErrMsgToFile (
3168+ fn printErrMsgToStdErr (
31703169 gpa : * mem.Allocator ,
31713170 parse_error : ast.Error ,
31723171 tree : ast.Tree ,
31733172 path : []const u8 ,
3174- file : fs.File ,
31753173 color : Color ,
31763174) ! void {
3177- const color_on = switch (color ) {
3178- .auto = > file .isTty (),
3179- .on = > true ,
3180- .off = > false ,
3181- };
31823175 const lok_token = parse_error .token ;
3183-
3184- const token_starts = tree .tokens .items (.start );
3185- const token_tags = tree .tokens .items (.tag );
3186- const first_token_start = token_starts [lok_token ];
31873176 const start_loc = tree .tokenLocation (0 , lok_token );
3177+ const source_line = tree .source [start_loc .line_start .. start_loc .line_end ];
31883178
31893179 var text_buf = std .ArrayList (u8 ).init (gpa );
31903180 defer text_buf .deinit ();
31913181 const writer = text_buf .writer ();
31923182 try tree .renderError (parse_error , writer );
31933183 const text = text_buf .items ;
31943184
3195- const stream = file .writer ();
3196- try stream .print ("{s}:{d}:{d}: error: {s}\n " , .{ path , start_loc .line + 1 , start_loc .column + 1 , text });
3185+ const message : Compilation.AllErrors.Message = .{
3186+ .src = .{
3187+ .src_path = path ,
3188+ .msg = text ,
3189+ .byte_offset = @intCast (u32 , start_loc .line_start ),
3190+ .line = @intCast (u32 , start_loc .line ),
3191+ .column = @intCast (u32 , start_loc .column ),
3192+ .source_line = source_line ,
3193+ },
3194+ };
31973195
3198- if (! color_on ) return ;
3196+ const ttyconf : std.debug.TTY.Config = switch (color ) {
3197+ .auto = > std .debug .detectTTYConfig (),
3198+ .on = > .escape_codes ,
3199+ .off = > .no_color ,
3200+ };
31993201
3200- // Print \r and \t as one space each so that column counts line up
3201- for (tree .source [start_loc .line_start .. start_loc .line_end ]) | byte | {
3202- try stream .writeByte (switch (byte ) {
3203- '\r ' , '\t ' = > ' ' ,
3204- else = > byte ,
3205- });
3206- }
3207- try stream .writeByte ('\n ' );
3208- try stream .writeByteNTimes (' ' , start_loc .column );
3209- if (token_tags [lok_token ].lexeme ()) | lexeme | {
3210- try stream .writeByteNTimes ('~' , lexeme .len );
3211- try stream .writeByte ('\n ' );
3212- } else {
3213- try stream .writeAll ("^\n " );
3214- }
3202+ message .renderToStdErr (ttyconf );
32153203}
32163204
32173205pub const info_zen =
@@ -3726,7 +3714,7 @@ pub fn cmdAstCheck(
37263714 defer file .tree .deinit (gpa );
37273715
37283716 for (file .tree .errors ) | parse_error | {
3729- try printErrMsgToFile (gpa , parse_error , file .tree , file .sub_file_path , io . getStdErr () , color );
3717+ try printErrMsgToStdErr (gpa , parse_error , file .tree , file .sub_file_path , color );
37303718 }
37313719 if (file .tree .errors .len != 0 ) {
37323720 process .exit (1 );
@@ -3847,7 +3835,7 @@ pub fn cmdChangelist(
38473835 defer file .tree .deinit (gpa );
38483836
38493837 for (file .tree .errors ) | parse_error | {
3850- try printErrMsgToFile (gpa , parse_error , file .tree , old_source_file , io . getStdErr () , .auto );
3838+ try printErrMsgToStdErr (gpa , parse_error , file .tree , old_source_file , .auto );
38513839 }
38523840 if (file .tree .errors .len != 0 ) {
38533841 process .exit (1 );
@@ -3884,7 +3872,7 @@ pub fn cmdChangelist(
38843872 defer new_tree .deinit (gpa );
38853873
38863874 for (new_tree .errors ) | parse_error | {
3887- try printErrMsgToFile (gpa , parse_error , new_tree , new_source_file , io . getStdErr () , .auto );
3875+ try printErrMsgToStdErr (gpa , parse_error , new_tree , new_source_file , .auto );
38883876 }
38893877 if (new_tree .errors .len != 0 ) {
38903878 process .exit (1 );
0 commit comments