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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
open Gospel
open Ortac_core
open Ppxlib
type config_under_construction = {
sut_core_type' : Ppxlib.core_type option;
init_sut' : Ppxlib.expression option;
gen_mod' : Ppxlib.structure option;
pp_mod' : Ppxlib.structure option;
ty_mod' : Ppxlib.structure option;
cleanup' : Ppxlib.structure_item option;
}
let config_under_construction =
{
sut_core_type' = None;
init_sut' = None;
gen_mod' = None;
pp_mod' = None;
ty_mod' = None;
cleanup' = None;
}
type t = {
context : Context.t;
sut_core_type : Ppxlib.core_type;
init_sut : Ppxlib.expression;
init_sut_txt : string;
gen_mod : Ppxlib.structure option;
pp_mod : Ppxlib.structure option;
ty_mod : Ppxlib.structure option;
cleanup : Ppxlib.structure_item option;
module_prefix : string option;
submodule : string option;
}
let mk_config context module_prefix submodule cfg_uc =
let open Reserr in
let* sut_core_type =
of_option
~default:(Incomplete_configuration_module `Sut, Location.none)
cfg_uc.sut_core_type'
and* init_sut =
of_option
~default:(Incomplete_configuration_module `Init_sut, Location.none)
cfg_uc.init_sut'
in
let init_sut_txt = Fmt.str "%a" Pprintast.expression init_sut
and gen_mod = cfg_uc.gen_mod'
and pp_mod = cfg_uc.pp_mod'
and ty_mod = cfg_uc.ty_mod'
and cleanup = cfg_uc.cleanup' in
ok
{
context;
sut_core_type;
init_sut;
init_sut_txt;
gen_mod;
pp_mod;
ty_mod;
cleanup;
module_prefix;
submodule;
}
let get_sut_type_name config =
let open Ppxlib in
match config.sut_core_type.ptyp_desc with
| Ppxlib.Ptyp_constr (lid, _) -> lid.txt
| _ -> failwith "unreachable case in get_sut_type_name"
let get_sut_type_name_str config =
Ppxlib.Longident.last_exn (get_sut_type_name config)
let is_sut config ty =
let sut_type_name = get_sut_type_name config in
let open Ppxlib in
match ty.ptyp_desc with
| Ptyp_constr (lid, _) -> lid.txt = sut_type_name
| _ -> false
let does_return_sut config ty =
let rec aux ty =
match ty.ptyp_desc with
| Ptyp_arrow (_, _, r) -> aux r
| Ptyp_constr _ -> is_sut config ty
| _ -> false
in
aux ty
let dump ppf t =
Fmt.(
pf ppf "sut_core_type: %a; init_sut: %a@." Ppxlib_ast.Pprintast.expression
t.init_sut Ppxlib_ast.Pprintast.core_type t.sut_core_type)
let rec acceptable_type_parameter param =
let open Ppxlib in
let open Reserr in
let str = Fmt.str "%a" Ppxlib_ast.Pprintast.core_type param in
match param.ptyp_desc with
| Ptyp_constr (_, cts) ->
let* _ = promote_map acceptable_type_parameter cts in
ok ()
| Ptyp_tuple args ->
let* _ = promote_map acceptable_type_parameter args in
ok ()
| Ptyp_var _ | Ptyp_any ->
error (Type_parameter_not_instantiated str, Location.none)
| _ -> error (Type_not_supported_for_sut_parameter str, Location.none)
let core_type_is_a_well_formed_sut (core_type : Ppxlib.core_type) =
let open Ppxlib in
let open Reserr in
match core_type.ptyp_desc with
| Ptyp_constr (_lid, cts) ->
let* _ = promote_map acceptable_type_parameter cts in
ok ()
| _ ->
let str = Fmt.str "%a" Ppxlib_ast.Pprintast.core_type core_type in
error (Sut_type_not_supported str, Location.none)
let value_bindings cfg_uc =
let open Reserr in
let aux cfg_uc vb =
let open Ppxlib in
match vb.pvb_pat.ppat_desc with
| Ppat_var s when String.equal "init_sut" s.txt ->
let init_sut' = Some vb.pvb_expr in
ok { cfg_uc with init_sut' }
| Ppat_var s when String.equal "cleanup" s.txt ->
let cleanup' =
Option.some @@ Ortac_core.Builder.pstr_value Nonrecursive [ vb ]
in
ok { cfg_uc with cleanup' }
| _ -> ok cfg_uc
in
fold_left aux cfg_uc
let type_declarations cfg_uc =
let open Reserr in
let aux cfg_uc (td : type_declaration) =
let open Ppxlib in
if String.equal "sut" td.ptype_name.txt then
let* manifest =
of_option
~default:
( Sut_type_not_supported (Fmt.str "%a" Pprintast.type_declaration td),
td.ptype_loc )
td.ptype_manifest
in
let* () = core_type_is_a_well_formed_sut manifest in
ok { cfg_uc with sut_core_type' = Some manifest }
else ok cfg_uc
in
fold_left aux cfg_uc
let module_binding cfg_uc (mb : Ppxlib.module_binding) =
let open Reserr in
let get_structure name mb =
match mb.pmb_expr.pmod_desc with
| Pmod_structure structure
| Pmod_constraint ({ pmod_desc = Pmod_structure structure; _ }, _) ->
ok structure
| _ -> error (Not_a_structure name, Location.none)
in
match mb.pmb_name.txt with
| Some name when String.equal "Gen" name ->
let* content = get_structure name mb in
ok { cfg_uc with gen_mod' = Some content }
| Some name when String.equal "Pp" name ->
let* content = get_structure name mb in
ok { cfg_uc with pp_mod' = Some content }
| Some name when String.equal "Ty" name ->
let* content = get_structure name mb in
ok { cfg_uc with ty_mod' = Some content }
| _ -> ok cfg_uc
let scan_config cfg_uc config_mod =
let open Reserr in
let* ic =
try ok @@ open_in config_mod
with _ -> error (No_configuration_file config_mod, Location.none)
in
let lb = Lexing.from_channel ic in
let () = Lexing.set_filename lb config_mod in
let* ast =
try ok @@ Ppxlib.Parse.implementation lb
with _ ->
error
( Syntax_error_in_config_module config_mod,
Location.
{
loc_start = lb.lex_start_p;
loc_end = lb.lex_curr_p;
loc_ghost = false;
} )
in
close_in ic;
let aux cfg_uc (str : structure_item) =
match str.pstr_desc with
| Pstr_eval (_, _) -> ok cfg_uc
| Pstr_value (_, xs) -> value_bindings cfg_uc xs
| Pstr_primitive _ -> ok cfg_uc
| Pstr_type (_, xs) -> type_declarations cfg_uc xs
| Pstr_typext _ -> ok cfg_uc
| Pstr_exception _ -> ok cfg_uc
| Pstr_module mb -> module_binding cfg_uc mb
| Pstr_recmodule _ -> ok cfg_uc
| Pstr_modtype _ -> ok cfg_uc
| Pstr_open _ -> ok cfg_uc
| Pstr_class _ -> ok cfg_uc
| Pstr_class_type _ -> ok cfg_uc
| Pstr_include _ -> ok cfg_uc
| Pstr_attribute _ -> ok cfg_uc
| Pstr_extension (_, _) -> ok cfg_uc
in
fold_left aux cfg_uc ast
let init gospel config_module module_prefix submodule =
let open Reserr in
try
let module_name = Utils.module_name_of_path gospel in
Parser_frontend.parse_ocaml_gospel gospel |> Utils.type_check [] gospel
|> fun (env, sigs) ->
assert (List.length env = 1);
let namespace = List.hd env in
let context = Context.init module_name namespace in
let add ctx s =
match s.Tast.sig_desc with
| Sig_val ({ vd_name; vd_spec = Some { sp_pure = true; _ }; _ }, _) ->
let ls = Context.get_ls ctx [ vd_name.id_str ] in
Context.add_function ls vd_name.id_str ctx
| Sig_function { fun_ls; fun_def = Some _; _ } ->
Context.add_function fun_ls fun_ls.ls_name.id_str ctx
| _ -> ctx
in
let context = List.fold_left add context sigs in
let* config =
scan_config config_under_construction config_module
>>= mk_config context module_prefix submodule
in
ok (sigs, config)
with Gospel.Warnings.Error (l, k) ->
error (Ortac_core.Warnings.GospelError k, l)