mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
26 lines
792 B
OCaml
26 lines
792 B
OCaml
|
|
type code_pos = { line : int; col : int }
|
||
|
|
type lexer_error = { pos : code_pos; msg : string }
|
||
|
|
|
||
|
|
module LexerError = struct
|
||
|
|
type t = lexer_error
|
||
|
|
|
||
|
|
let make (pos : code_pos) (msg : string) : lexer_error =
|
||
|
|
(* let pos = { line; col } in *)
|
||
|
|
{ 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
|
||
|
|
end
|
||
|
|
|
||
|
|
type lox_error = LexerError of lexer_error list
|
||
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|