| 1 |
open Ast_helper |
| 2 |
open Longident |
| 3 |
|
| 4 |
let place = ref None |
| 5 |
|
| 6 |
let file = ref None |
| 7 |
|
| 8 |
let read_file () = |
| 9 |
match !file with |
| 10 |
| None -> "<none>" |
| 11 |
| Some s -> |
| 12 |
let ic = open_in s in |
| 13 |
begin match input_line ic with |
| 14 |
| exception End_of_file -> |
| 15 |
close_in ic; |
| 16 |
"<none>" |
| 17 |
| s -> |
| 18 |
close_in ic; |
| 19 |
s |
| 20 |
end |
| 21 |
|
| 22 |
let impl str = |
| 23 |
let arg = |
| 24 |
match !place with |
| 25 |
| None -> Exp.ident (Location.mknoloc (Lident "__MODULE__")) |
| 26 |
| Some s -> Exp.constant (Const.string (Printf.sprintf "%s (%s)" s (read_file ()))) |
| 27 |
in |
| 28 |
Str.eval |
| 29 |
(Exp.apply (Exp.ident (Location.mknoloc (Ldot (Lident "Hello", "hello")))) |
| 30 |
[Nolabel, arg]) :: str |
| 31 |
|
| 32 |
open Ppxlib |
| 33 |
|
| 34 |
let () = |
| 35 |
Driver.add_arg "-place" (Arg.String (fun s -> place := Some s)) |
| 36 |
~doc:"PLACE where to say hello from"; |
| 37 |
Driver.add_arg "-file" (Arg.String (fun s -> file := Some s)) |
| 38 |
~doc:"Add info from file" |
| 39 |
|
| 40 |
let () = |
| 41 |
Driver.register_transformation_using_ocaml_current_ast ~impl "hello" |