2024-08-02 00:10:48 +02:00
|
|
|
type code_pos = { line : int; col : int }
|
|
|
|
|
type lexer_error = { pos : code_pos; msg : string }
|
|
|
|
|
|
|
|
|
|
module LexerError = struct
|
|
|
|
|
type t = lexer_error
|
|
|
|
|
|
2024-08-12 16:31:28 +02:00
|
|
|
let make (pos : code_pos) (msg : string) : lexer_error = { pos; msg }
|
2024-08-02 00:10:48 +02:00
|
|
|
|
2024-08-27 18:42:18 +02:00
|
|
|
let show (e : lexer_error) =
|
|
|
|
|
Printf.sprintf "LexerError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
|
2024-08-02 00:10:48 +02:00
|
|
|
end
|
|
|
|
|
|
2024-08-12 16:31:28 +02:00
|
|
|
type parser_error = { pos : code_pos; msg : string }
|
|
|
|
|
|
|
|
|
|
module ParserError = struct
|
|
|
|
|
type t = parser_error
|
|
|
|
|
|
|
|
|
|
let make (pos : code_pos) (msg : string) : parser_error = { pos; msg }
|
|
|
|
|
|
2024-08-27 18:42:18 +02:00
|
|
|
let show (e : parser_error) =
|
|
|
|
|
Printf.sprintf "ParserError at line %d, column %d: %s" e.pos.line e.pos.col e.msg
|
2024-08-12 16:31:28 +02:00
|
|
|
end
|
|
|
|
|
|
2024-08-27 17:15:35 +02:00
|
|
|
(* type runtime_error = { pos : code_pos; msg : string; type_ : runtime_error_type } *)
|
|
|
|
|
type runtime_error = Error of { pos : code_pos; msg : string } | Break | Continue
|
2024-08-17 13:32:59 +02:00
|
|
|
|
2024-08-25 02:12:51 +02:00
|
|
|
module RuntimeError = struct
|
2024-08-17 13:32:59 +02:00
|
|
|
type t = parser_error
|
|
|
|
|
|
2024-08-27 17:15:35 +02:00
|
|
|
let make (pos : code_pos) (msg : string) : runtime_error = Error { pos; msg }
|
|
|
|
|
let break () : runtime_error = Break
|
|
|
|
|
let continue () : runtime_error = Continue
|
2024-08-17 13:32:59 +02:00
|
|
|
|
2024-08-27 18:42:18 +02:00
|
|
|
let show (e : runtime_error) =
|
2024-08-27 17:15:35 +02:00
|
|
|
match e with
|
|
|
|
|
| Error { pos; msg } ->
|
2024-08-27 18:42:18 +02:00
|
|
|
Printf.sprintf "RuntimeError at line %d, column %d: %s" pos.line pos.col msg
|
2024-08-27 17:15:35 +02:00
|
|
|
| Break | Continue -> assert false
|
2024-08-17 13:32:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
type lox_error =
|
|
|
|
|
| LexerError of lexer_error list
|
|
|
|
|
| ParserError of parser_error list
|
2024-08-25 02:12:51 +02:00
|
|
|
| RuntimeError of runtime_error
|
2024-08-02 00:10:48 +02:00
|
|
|
|
|
|
|
|
let print_error (e : lox_error) =
|
|
|
|
|
match e with
|
|
|
|
|
| LexerError es ->
|
|
|
|
|
let num_errors = List.length es in
|
2024-08-25 02:12:51 +02:00
|
|
|
assert (num_errors <> 0);
|
2024-08-27 18:42:18 +02:00
|
|
|
Printf.fprintf stderr "found %d %s:\n" num_errors
|
2024-08-02 00:10:48 +02:00
|
|
|
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
2024-08-27 18:42:18 +02:00
|
|
|
List.iter (fun e -> LexerError.show e |> Printf.fprintf stderr "%s\n") es
|
2024-08-12 16:31:28 +02:00
|
|
|
| ParserError es ->
|
|
|
|
|
let num_errors = List.length es in
|
2024-08-25 02:12:51 +02:00
|
|
|
assert (num_errors <> 0);
|
2024-08-27 18:42:18 +02:00
|
|
|
Printf.fprintf stderr "found %d %s:\n" num_errors
|
2024-08-12 16:31:28 +02:00
|
|
|
(if num_errors = 1 then "ParserError" else "ParserErrors");
|
2024-08-27 18:42:18 +02:00
|
|
|
List.iter (fun e -> ParserError.show e |> Printf.fprintf stderr "%s\n") es
|
|
|
|
|
| RuntimeError e -> RuntimeError.show e |> Printf.fprintf stderr "%s\n"
|
2024-08-02 00:10:48 +02:00
|
|
|
|
|
|
|
|
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|
2024-08-12 16:31:28 +02:00
|
|
|
let of_parser_error e = Result.map_error (fun e -> ParserError e) e
|
2024-08-25 02:12:51 +02:00
|
|
|
let of_runtimer_error e = Result.map_error (fun e -> RuntimeError e) e
|