Debian Codegraph @

src/ppx_sexp_value.ml

raw symbols SCIP
1 open Base
2 open Ppxlib
3 open Ast_builder.Default
4
5 let omit_nil =
6 Attribute.declare
7 "sexp_value.sexp.omit_nil"
8 Attribute.Context.core_type
9 Ast_pattern.(pstr nil)
10 ()
11 ;;
12
13 let option =
14 Attribute.declare
15 "sexp_value.sexp.option"
16 Attribute.Context.core_type
17 Ast_pattern.(pstr nil)
18 ()
19 ;;
20
21 let sexp_atom ~loc x = [%expr Ppx_sexp_conv_lib.Sexp.Atom [%e x]]
22 let sexp_list ~loc x = [%expr Ppx_sexp_conv_lib.Sexp.List [%e x]]
23
24 let rec list_and_tail_of_ast_list rev_el e =
25 match e.pexp_desc with
26 | Pexp_construct
27 ({ txt = Lident "::"; _ }, Some { pexp_desc = Pexp_tuple [ hd; tl ]; _ }) ->
28 list_and_tail_of_ast_list (hd :: rev_el) tl
29 | Pexp_construct ({ txt = Lident "[]"; _ }, None) -> List.rev rev_el, None
30 | _ -> List.rev rev_el, Some e
31 ;;
32
33 let sexp_of_constant ~loc const =
34 let f typ =
35 eapply
36 ~loc
37 (evar ~loc ("Ppx_sexp_conv_lib.Conv.sexp_of_" ^ typ))
38 [ pexp_constant ~loc const ]
39 in
40 match const with
41 | Pconst_integer _ -> f "int"
42 | Pconst_char _ -> f "char"
43 | Pconst_string _ -> f "string"
44 | Pconst_float _ -> f "float"
45 ;;
46
47 type omittable_sexp =
48 | Present of expression
49 | Optional of (Location.t * string) * expression * (expression -> expression)
50 (* In [Optional (_, e, k)], [e] is an ast whose values have type ['a option], and [k] is
51 a function from ast of type ['a] to ast of type [Sexp.t]. The None case should not be
52 displayed, and the [a] in the Some case should be displayed by calling [k] on it. *)
53 | Omit_nil of Location.t * expression * (expression -> expression)
54 (* In [Omit_nil (_, e, k)], [e] is an ast of type [Sexp.t], and [k] if a function
55 ast of type [Sexp.t] and returns an other [Sexp.t].
56 When [e] is [List []], it should be not displayed. Otherwise [e] should be
57 displayed by calling [k] on it. *)
58
59 let wrap_sexp_if_present omittable_sexp ~f =
60 match omittable_sexp with
61 | Optional (loc, e, k) -> Optional (loc, e, fun e -> f (k e))
62 | Present e -> Present (f e)
63 | Omit_nil (loc, e, k) -> Omit_nil (loc, e, fun e -> f (k e))
64 ;;
65
66 let sexp_of_constraint ~loc expr ctyp =
67 match ctyp with
68 | [%type: [%t? ty] option] when Option.is_some (Attribute.get option ctyp) ->
69 let sexp_of = Ppx_sexp_conv_expander.Sexp_of.core_type ty in
70 Optional ((loc, "[@sexp.optional]"), expr, fun expr -> eapply ~loc sexp_of [ expr ])
71 | _ ->
72 let expr =
73 let sexp_of = Ppx_sexp_conv_expander.Sexp_of.core_type ctyp in
74 eapply ~loc sexp_of [ expr ]
75 in
76 (match Attribute.get omit_nil ctyp with
77 | Some () -> Omit_nil (loc, expr, Fn.id)
78 | None -> Present expr)
79 ;;
80
81 let rec sexp_of_expr expr =
82 match omittable_sexp_of_expr expr with
83 | Present v -> v
84 | Optional ((loc, s), _, _) ->
85 Location.raise_errorf ~loc "ppx_sexp_value: cannot handle %s in this context" s
86 | Omit_nil (loc, _, _) ->
87 Location.raise_errorf ~loc "ppx_sexp_value: cannot handle [@omit_nil] in this context"
88
89 and omittable_sexp_of_expr expr =
90 let loc = { expr.pexp_loc with loc_ghost = true } in
91 wrap_sexp_if_present
92 ~f:(fun new_expr -> { new_expr with pexp_attributes = expr.pexp_attributes })
93 (match expr.pexp_desc with
94 | Pexp_ifthenelse (e1, e2, e3) ->
95 Present
96 { expr with
97 pexp_desc =
98 Pexp_ifthenelse
99 ( e1
100 , sexp_of_expr e2
101 , match e3 with
102 | None -> None
103 | Some e -> Some (sexp_of_expr e) )
104 }
105 | Pexp_constraint (expr, ctyp) -> sexp_of_constraint ~loc expr ctyp
106 | Pexp_construct ({ txt = Lident "[]"; _ }, None)
107 | Pexp_construct
108 ({ txt = Lident "::"; _ }, Some { pexp_desc = Pexp_tuple [ _; _ ]; _ }) ->
109 let el, tl = list_and_tail_of_ast_list [] expr in
110 let el = List.map el ~f:omittable_sexp_of_expr in
111 let tl =
112 match tl with
113 | None -> [%expr []]
114 | Some e ->
115 [%expr
116 match [%e sexp_of_expr e] with
117 | Ppx_sexp_conv_lib.Sexp.List l -> l
118 | Ppx_sexp_conv_lib.Sexp.Atom _ as sexp -> [ sexp ]]
119 in
120 Present (sexp_of_omittable_sexp_list loc el ~tl)
121 | Pexp_constant const -> Present (sexp_of_constant ~loc const)
122 | Pexp_extension ({ txt = "here"; _ }, PStr []) ->
123 Present (sexp_atom ~loc (Ppx_here_expander.lift_position_as_string ~loc))
124 | Pexp_extension ({ txt = "string"; _ }, _) -> Present (sexp_atom ~loc expr)
125 | Pexp_construct ({ txt = Lident "()"; _ }, None) ->
126 Present (sexp_list ~loc (elist ~loc []))
127 | Pexp_construct ({ txt = Lident constr; _ }, None) | Pexp_variant (constr, None) ->
128 Present (sexp_atom ~loc (estring ~loc constr))
129 | Pexp_construct ({ txt = Lident constr; _ }, Some arg)
130 | Pexp_variant (constr, Some arg) ->
131 let k hole =
132 sexp_list ~loc (elist ~loc [ sexp_atom ~loc (estring ~loc constr); hole ])
133 in
134 wrap_sexp_if_present (omittable_sexp_of_expr arg) ~f:k
135 | Pexp_tuple el ->
136 let el = List.map el ~f:omittable_sexp_of_expr in
137 Present (sexp_of_omittable_sexp_list loc el ~tl:(elist ~loc []))
138 | Pexp_record (fields, None) -> Present (sexp_of_record ~loc fields)
139 | Pexp_apply
140 ( { pexp_desc = Pexp_ident { txt = Lident "~~"; _ }; _ }
141 , [ (Nolabel, { pexp_desc = Pexp_constraint (expr, ctyp); _ }) ] ) ->
142 let expr_str = Pprintast.string_of_expression expr in
143 let k hole =
144 sexp_list ~loc (elist ~loc [ sexp_atom ~loc (estring ~loc expr_str); hole ])
145 in
146 wrap_sexp_if_present (sexp_of_constraint ~loc expr ctyp) ~f:k
147 | _ ->
148 Location.raise_errorf
149 ~loc
150 "ppx_sexp_value: don't know how to handle this construct")
151
152 and sexp_of_omittable_sexp_list loc el ~tl =
153 let l =
154 List.fold_left (List.rev el) ~init:tl ~f:(fun acc e ->
155 match e with
156 | Present e -> [%expr [%e e] :: [%e acc]]
157 | Optional (_, v_opt, k) ->
158 (* We match simultaneously on the head and tail in the generated code to avoid
159 changing their respective typing environments. *)
160 [%expr
161 match [%e v_opt], [%e acc] with
162 | None, tl -> tl
163 | Some v, tl -> [%e k [%expr v]] :: tl]
164 | Omit_nil (_, e, k) ->
165 [%expr
166 match [%e e], [%e acc] with
167 | Ppx_sexp_conv_lib.Sexp.List [], tl -> tl
168 | v, tl -> [%e k [%expr v]] :: tl])
169 in
170 sexp_list ~loc l
171
172 and sexp_of_record ~loc fields =
173 sexp_of_omittable_sexp_list
174 loc
175 ~tl:(elist ~loc [])
176 (List.map fields ~f:(fun (id, e) ->
177 let e =
178 match e.pexp_desc with
179 | Pexp_constraint (e', c)
180 when Location.compare_pos id.loc.loc_start e.pexp_loc.loc_start = 0
181 && Location.compare e.pexp_loc e'.pexp_loc = 0 ->
182 (* { foo : int } *)
183 { e with pexp_desc = Pexp_constraint ({ e' with pexp_loc = id.loc }, c) }
184 | _ -> e
185 in
186 let loc = { id.loc with loc_end = e.pexp_loc.loc_end; loc_ghost = true } in
187 let name = String.concat ~sep:"." (Longident.flatten_exn id.txt) in
188 let k hole =
189 sexp_list
190 ~loc
191 (elist
192 ~loc
193 [ sexp_atom ~loc (estring ~loc:{ id.loc with loc_ghost = true } name)
194 ; hole
195 ])
196 in
197 wrap_sexp_if_present (omittable_sexp_of_expr e) ~f:k))
198 ;;
199
200 let () =
201 Driver.register_transformation
202 "sexp_value"
203 ~extensions:
204 [ Extension.declare
205 "sexp"
206 Extension.Context.expression
207 Ast_pattern.(pstr (pstr_eval __ nil ^:: nil))
208 (fun ~loc:_ ~path:_ e -> sexp_of_expr e)
209 ; Extension.declare
210 "lazy_sexp"
211 Extension.Context.expression
212 Ast_pattern.(pstr (pstr_eval __ nil ^:: nil))
213 (fun ~loc ~path:_ e -> [%expr lazy [%e sexp_of_expr e]])
214 ]
215 ;;