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
|
|
|
|
|
|
|
|
let print (e : lexer_error) =
|
|
|
|
|
Printf.printf "LexerError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
|
|
let print (e : parser_error) =
|
|
|
|
|
Printf.printf "ParserError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
type lox_error = LexerError of lexer_error list | ParserError of parser_error list
|
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
|
|
|
|
|
assert (num_errors != 0);
|
|
|
|
|
Printf.printf "found %d %s:\n" num_errors
|
|
|
|
|
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
|
|
|
|
List.iter LexerError.print es
|
2024-08-12 16:31:28 +02:00
|
|
|
| ParserError es ->
|
|
|
|
|
let num_errors = List.length es in
|
|
|
|
|
assert (num_errors != 0);
|
|
|
|
|
Printf.printf "found %d %s:\n" num_errors
|
|
|
|
|
(if num_errors = 1 then "ParserError" else "ParserErrors");
|
|
|
|
|
List.iter ParserError.print es
|
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
|