print errors to stderr instead stdout

This commit is contained in:
Moritz Gmeiner 2024-08-27 18:42:18 +02:00
commit e2fcb0f6ef
3 changed files with 22 additions and 25 deletions

View file

@ -6,8 +6,8 @@ module LexerError = struct
let make (pos : code_pos) (msg : string) : lexer_error = { pos; msg }
let print (e : lexer_error) =
Printf.printf "LexerError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
let show (e : lexer_error) =
Printf.sprintf "LexerError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
end
type parser_error = { pos : code_pos; msg : string }
@ -17,8 +17,8 @@ module ParserError = struct
let make (pos : code_pos) (msg : string) : parser_error = { pos; msg }
let print (e : parser_error) =
Printf.printf "ParserError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
let show (e : parser_error) =
Printf.sprintf "ParserError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
end
(* type runtime_error = { pos : code_pos; msg : string; type_ : runtime_error_type } *)
@ -31,10 +31,10 @@ module RuntimeError = struct
let break () : runtime_error = Break
let continue () : runtime_error = Continue
let print (e : runtime_error) =
let show (e : runtime_error) =
match e with
| Error { pos; msg } ->
Printf.printf "RuntimeError at line %d, column %d: %s\n" pos.line pos.col msg
Printf.sprintf "RuntimeError at line %d, column %d: %s" pos.line pos.col msg
| Break | Continue -> assert false
end
@ -48,16 +48,16 @@ let print_error (e : lox_error) =
| LexerError es ->
let num_errors = List.length es in
assert (num_errors <> 0);
Printf.printf "found %d %s:\n" num_errors
Printf.fprintf stderr "found %d %s:\n" num_errors
(if num_errors = 1 then "LexerError" else "LexerErrors");
List.iter LexerError.print es
List.iter (fun e -> LexerError.show e |> Printf.fprintf stderr "%s\n") es
| ParserError es ->
let num_errors = List.length es in
assert (num_errors <> 0);
Printf.printf "found %d %s:\n" num_errors
Printf.fprintf stderr "found %d %s:\n" num_errors
(if num_errors = 1 then "ParserError" else "ParserErrors");
List.iter ParserError.print es
| RuntimeError e -> RuntimeError.print e
List.iter (fun e -> ParserError.show e |> Printf.fprintf stderr "%s\n") es
| RuntimeError e -> RuntimeError.show e |> Printf.fprintf stderr "%s\n"
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
let of_parser_error e = Result.map_error (fun e -> ParserError e) e