package genprint

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ppx_genprint.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# 1 "ppx_genprint.cppo.ml"
open Ppxlib
open Caml

(*
extract from compiler call, add to tuple argument to 'print', use to find the cmt file.
        lid "include_dirs", make_list make_string !Clflags.include_dirs;
        lid "load_path",    make_list make_string (Load_path.get_paths ());

the cmt for current file with be stored alongside the object.
the load-path should be byte/native for dune.
difference with include-dirs? load-path includes 'include-dirs under 4.08

*)

let envar_set name = try ignore@@Sys.getenv name;true with _->false
let remove_extensions = envar_set "GENPRINT_REMOVE"

let count = ref 0
let cwd = Sys.getcwd()
let fullpath f=
  if Filename.is_relative f then
    Filename.concat cwd f
  else
    f

let getwords l =
  List.fold_left (fun msg (_,word) ->
      match word.pexp_desc with
      | Pexp_ident id ->
         let word'= Longident.name id.txt in
         (word' ^" "^ msg)
      | Pexp_construct({txt=Lident lid} ,_)->
         (lid ^" "^ msg)
      (* | Pexp_constant _-> *)
      | _ ->
         Location.raise_errorf ~loc:word.pexp_loc "this should be a word")
    "" l

let set_loadpath locopt =
  let loc = match locopt with
    | None-> Location.none
    | Some loc->loc
  in
  (* should be setup from a ppx.context *)
  
# 48 "ppx_genprint.cppo.ml"
  let lp = Ocaml_common.Load_path.get_paths() in
  
# 50 "ppx_genprint.cppo.ml"
  let lp = List.map fullpath lp in
  let expl = [%expr [%e Ast_builder.Default.(
                       (elist ~loc (List.map (estring ~loc) lp))
             )]]
  in
  [[%stri let __loadpath = [%e expl]]], []

(* put this value into every compiled unit once *)
let () =
  Driver.register_transformation
    ~enclose_impl:set_loadpath
    "genprint-loadpath" ~extensions:[]

(* cache mode can be detected by examining data as loaded
(* used internally *)
val ppx_mode: bool ref

let set_ppxmode locopt =
  let loc = match locopt with
    | None-> Location.none
    | Some loc->loc
  in
  [[%stri let _ = Genprint.ppx_mode:=true ]], []

(* put this value into every compiled unit once *)
let () =
  Driver.register_transformation
    ~enclose_impl:set_ppxmode
    "genprint-ppxmode" ~extensions:[]
*)


let expand ~loc ~path (e : expression) =
  incr count;
  (*free-form text before the value including capitalisation *)
  let words, e =
    match e.pexp_desc with
    |Pexp_apply(e,el)->
      begin
        match List.rev el with
        | [] -> assert false          (* would not be an apply without something *)
        | (_,v)::tl ->                (* the last arg is the expression to eval *)
           let msg = getwords @@ tl@[Nolabel,e] in
          msg,v
      end
    | _-> "",e
  in
  if remove_extensions then [%expr let _=[%e e] in ()] else
  [%expr [%e Ast_builder.Default.(
   pexp_apply ~loc (
    pexp_apply ~loc
      (pexp_ident ~loc (Located.mk ~loc (Longident.parse "Genprint.print") ))
      [Nolabel,estring ~loc words;
       Nolabel,pexp_tuple ~loc [ 
                   (eint ~loc !count);
                   (estring ~loc @@ Filename.basename path);
                   (pexp_ident ~loc (Located.mk ~loc (Longident.parse "__loadpath") ));
                 ]
      ])
    [Nolabel,e]
  )]]


let genprint =
  Extension.declare
    "pr"
    Extension.Context.expression
    Ast_pattern.(pstr ((pstr_eval __ nil) ^:: nil))
    expand


let () =
  Driver.register_transformation
    "genprint" ~extensions:[ genprint ]

let expand ~loc ~path (payload : payload) = 
  incr count;
  (* let open Ast_builder.Default in *)
  let words = match payload with
    |PStr [] -> ""
    |PStr [{pstr_desc=Pstr_eval(e,_)}]->
      begin match e.pexp_desc with
      |Pexp_apply(e,el)-> getwords @@ (List.rev el) @[Nolabel,e]
      | _-> getwords [Nolabel,e]
      end
    | _ -> Location.raise_errorf ~loc "improper syntax: empty or non-keyword word and Words"
  in
  if remove_extensions then [%expr fun x->x] else

  [%expr [%e Ast_builder.Default.(
    pexp_apply ~loc
      (pexp_ident ~loc (Located.mk ~loc (Longident.parse "Genprint.print_with_return") ))
      [Nolabel,estring ~loc words;
       Nolabel,pexp_tuple ~loc [
                   (eint ~loc !count);
                   (estring ~loc @@ Filename.basename path);
                   (pexp_ident ~loc (Located.mk ~loc (Longident.parse "__loadpath") ));
                 ]
      ]
  )]]

let genprint =
  Extension.declare
    "prr"
    Extension.Context.expression
    (* Ast_pattern.(pstr ((pstr_eval __ nil) ^:: nil)) *)
    Ast_pattern.( (( __ ) ))
    expand


let () =
  Driver.register_transformation
    "genprint-with-return" ~extensions:[ genprint ]

(* custom printer installation *)
let expand ~loc ~path (fn : expression) = 
  incr count;
  if remove_extensions then [%expr let _=[%e fn] in ()] else

  [%expr [%e Ast_builder.Default.(
    pexp_apply ~loc
      (pexp_ident ~loc (Located.mk ~loc (Longident.parse "Genprint.install_printer") ))
      [Nolabel,fn;
       Nolabel,pexp_tuple ~loc [
                   (eint ~loc !count);
                   (estring ~loc @@ Filename.basename path);
                   (pexp_ident ~loc (Located.mk ~loc (Longident.parse "__loadpath") ));
                 ]
      ]
  )]]

let genprint =
  Extension.declare
    "install_printer"
    Extension.Context.expression
    Ast_pattern.(pstr ((pstr_eval __ nil) ^:: nil))
    expand


let () =
  Driver.register_transformation
    "genprint-install-printer" ~extensions:[ genprint ]

let remove_printer ~loc ~path (fn : expression) = 
  incr count;
  if remove_extensions then [%expr let _=[%e fn] in ()] else

  [%expr [%e Ast_builder.Default.(
    pexp_apply ~loc
      (pexp_ident ~loc (Located.mk ~loc (Longident.parse "Genprint.remove_printer") ))
      [Nolabel,fn;
       Nolabel,pexp_tuple ~loc [
                   (eint ~loc !count);
                   (estring ~loc @@ Filename.basename path);
                   (pexp_ident ~loc (Located.mk ~loc (Longident.parse "__loadpath") ));
                 ]
      ]
  )]]

let genprint =
  Extension.declare
    "remove_printer"
    Extension.Context.expression
    Ast_pattern.(pstr ((pstr_eval __ nil) ^:: nil))
    remove_printer


let () =
  Driver.register_transformation
    "genprint-remove-printer" ~extensions:[ genprint ]


(*
let expand_ff ~loc ~path (lid : longident) (el : (arg_label * expression) list) =
...

let genprint_freeform =
    Extension.declare
      "prs"
      Extension.Context.expression
      (* pattern appearing in [%pr <sss> v] is the application of the ident to the value *)
      Ast_pattern.(pstr ((pstr_eval (pexp_apply (pexp_ident __) ( __)  ) nil ) ^:: nil))
      expand_ff
*)
OCaml

Innovation. Community. Security.