| 1 |
open Ppxlib |
| 2 |
module Filename = Stdlib.Filename |
| 3 |
|
| 4 |
let dirname = ref None |
| 5 |
let set_dirname dn = dirname := dn |
| 6 |
|
| 7 |
let () = |
| 8 |
Driver.add_arg |
| 9 |
"-dirname" |
| 10 |
(String (fun s -> dirname := Some s)) |
| 11 |
~doc:"<dir> Name of the current directory relative to the root of the project" |
| 12 |
;; |
| 13 |
|
| 14 |
let chop_dot_slash_prefix ~fname = |
| 15 |
match Base.String.chop_prefix ~prefix:"./" fname with |
| 16 |
| Some fname -> fname |
| 17 |
| None -> fname |
| 18 |
;; |
| 19 |
|
| 20 |
let expand_filename fname = |
| 21 |
match Filename.is_relative fname, !dirname with |
| 22 |
| true, Some dirname -> |
| 23 |
|
| 24 |
Filename.concat dirname (chop_dot_slash_prefix ~fname) |
| 25 |
| _ -> fname |
| 26 |
;; |
| 27 |
|
| 28 |
let lift_position ~loc = |
| 29 |
let loc = { loc with loc_ghost = true } in |
| 30 |
let (module Builder) = Ast_builder.make loc in |
| 31 |
let open Builder in |
| 32 |
let pos = loc.Location.loc_start in |
| 33 |
let id = Located.lident in |
| 34 |
pexp_record |
| 35 |
[ id "Ppx_here_lib.pos_fname", estring (expand_filename pos.Lexing.pos_fname) |
| 36 |
; id "pos_lnum", eint pos.Lexing.pos_lnum |
| 37 |
; id "pos_cnum", eint pos.Lexing.pos_cnum |
| 38 |
; id "pos_bol", eint pos.Lexing.pos_bol |
| 39 |
] |
| 40 |
None |
| 41 |
;; |
| 42 |
|
| 43 |
let lift_position_as_string ~(loc : Location.t) = |
| 44 |
let { Lexing.pos_fname; pos_lnum; pos_cnum; pos_bol } = loc.loc_start in |
| 45 |
Ast_builder.Default.estring |
| 46 |
~loc |
| 47 |
(Printf.sprintf "%s:%d:%d" (expand_filename pos_fname) pos_lnum (pos_cnum - pos_bol)) |
| 48 |
;; |