2016-11-13 17:05:55 +00:00
|
|
|
open Import
|
|
|
|
|
2017-12-12 10:16:17 +00:00
|
|
|
type t = Usexp.Loc.t =
|
2016-11-03 16:44:09 +00:00
|
|
|
{ start : Lexing.position
|
|
|
|
; stop : Lexing.position
|
|
|
|
}
|
2016-11-13 11:13:47 +00:00
|
|
|
|
|
|
|
let of_lexbuf lb =
|
2016-11-13 11:27:31 +00:00
|
|
|
{ start = Lexing.lexeme_start_p lb
|
|
|
|
; stop = Lexing.lexeme_end_p lb
|
2016-11-13 11:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exception Error of t * string
|
|
|
|
|
2018-02-23 09:17:37 +00:00
|
|
|
let exnf t fmt =
|
|
|
|
Format.pp_print_as err_ppf 7 ""; (* "Error: " *)
|
|
|
|
kerrf fmt ~f:(fun s -> Error (t, s))
|
|
|
|
|
2016-11-13 11:13:47 +00:00
|
|
|
let fail t fmt =
|
2018-02-06 14:39:03 +00:00
|
|
|
Format.pp_print_as err_ppf 7 ""; (* "Error: " *)
|
|
|
|
kerrf fmt ~f:(fun s ->
|
|
|
|
raise (Error (t, s)))
|
2016-11-13 11:13:47 +00:00
|
|
|
|
|
|
|
let fail_lex lb fmt =
|
|
|
|
fail (of_lexbuf lb) fmt
|
2016-12-02 13:54:32 +00:00
|
|
|
|
2017-09-10 00:25:08 +00:00
|
|
|
let fail_opt t fmt =
|
|
|
|
match t with
|
|
|
|
| None -> die fmt
|
|
|
|
| Some t -> fail t fmt
|
|
|
|
|
2016-12-02 13:54:32 +00:00
|
|
|
let in_file fn =
|
|
|
|
let pos : Lexing.position =
|
|
|
|
{ pos_fname = fn
|
|
|
|
; pos_lnum = 1
|
|
|
|
; pos_cnum = 0
|
|
|
|
; pos_bol = 0
|
|
|
|
}
|
|
|
|
in
|
|
|
|
{ start = pos
|
|
|
|
; stop = pos
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:54:46 +00:00
|
|
|
let of_pos (fname, lnum, cnum, enum) =
|
|
|
|
let pos : Lexing.position =
|
|
|
|
{ pos_fname = fname
|
|
|
|
; pos_lnum = lnum
|
|
|
|
; pos_cnum = cnum
|
|
|
|
; pos_bol = 0
|
|
|
|
}
|
|
|
|
in
|
|
|
|
{ start = pos
|
|
|
|
; stop = { pos with pos_cnum = enum }
|
|
|
|
}
|
|
|
|
|
2017-02-25 17:53:39 +00:00
|
|
|
let none = in_file "<none>"
|
2017-05-18 15:50:53 +00:00
|
|
|
|
|
|
|
let print ppf { start; stop } =
|
|
|
|
let start_c = start.pos_cnum - start.pos_bol in
|
|
|
|
let stop_c = stop.pos_cnum - start.pos_bol in
|
|
|
|
Format.fprintf ppf
|
|
|
|
"@{<loc>File \"%s\", line %d, characters %d-%d:@}@\n"
|
|
|
|
start.pos_fname start.pos_lnum start_c stop_c
|
|
|
|
|
|
|
|
let warn t fmt =
|
2018-02-13 18:31:21 +00:00
|
|
|
Errors.kerrf ~f:print_to_console
|
|
|
|
("%a@{<warning>Warning@}: " ^^ fmt ^^ "@.") print t
|
2018-02-20 11:46:10 +00:00
|
|
|
|
|
|
|
let to_file_colon_line t =
|
|
|
|
sprintf "%s:%d" t.start.pos_fname t.start.pos_lnum
|
2018-02-20 16:44:25 +00:00
|
|
|
|
|
|
|
let pp_file_colon_line ppf t =
|
|
|
|
Format.pp_print_string ppf (to_file_colon_line t)
|