@@ -1261,6 +1261,74 @@ module Layouts = struct
1261
1261
| _ -> failwith " Malformed [kind_abbrev] in structure"
1262
1262
end
1263
1263
1264
+ module Instances = struct
1265
+ type instance =
1266
+ { head : string ;
1267
+ args : (string * instance ) list
1268
+ }
1269
+
1270
+ type module_expr = Imod_instance of instance
1271
+
1272
+ let feature : Feature.t = Language_extension Instances
1273
+
1274
+ let module_expr_of_string ~loc str =
1275
+ Ast_helper.Mod. ident ~loc { txt = Lident str; loc }
1276
+
1277
+ let rec module_expr_of_instance ~loc { head; args } =
1278
+ let head = module_expr_of_string ~loc head in
1279
+ match args with
1280
+ | [] -> head
1281
+ | _ ->
1282
+ let args =
1283
+ List. concat_map
1284
+ (fun (param , value ) ->
1285
+ let param = module_expr_of_string ~loc param in
1286
+ let value = module_expr_of_instance ~loc value in
1287
+ [param; value])
1288
+ args
1289
+ in
1290
+ List. fold_left (Ast_helper.Mod. apply ~loc ) head args
1291
+
1292
+ let module_expr_of ~loc = function
1293
+ | Imod_instance instance ->
1294
+ Module_expr. make_entire_jane_syntax ~loc feature (fun () ->
1295
+ module_expr_of_instance ~loc instance)
1296
+
1297
+ let head_of_ident (lid : Longident.t Location.loc ) =
1298
+ match lid with
1299
+ | { txt = Lident s ; loc = _ } -> s
1300
+ | _ -> failwith " Malformed instance identifier"
1301
+
1302
+ let gather_args mexpr =
1303
+ let rec loop mexpr rev_acc =
1304
+ match mexpr.pmod_desc with
1305
+ | Pmod_apply (f , v ) -> (
1306
+ match f.pmod_desc with
1307
+ | Pmod_apply (f , n ) -> loop f ((n, v) :: rev_acc)
1308
+ | _ -> failwith " Malformed instance identifier" )
1309
+ | head -> head, List. rev rev_acc
1310
+ in
1311
+ loop mexpr []
1312
+
1313
+ let string_of_module_expr mexpr =
1314
+ match mexpr.pmod_desc with
1315
+ | Pmod_ident i -> head_of_ident i
1316
+ | _ -> failwith " Malformed instance identifier"
1317
+
1318
+ let rec instance_of_module_expr mexpr =
1319
+ match gather_args mexpr with
1320
+ | Pmod_ident i , args ->
1321
+ let head = head_of_ident i in
1322
+ let args = List. map instances_of_arg_pair args in
1323
+ { head; args }
1324
+ | _ -> failwith " Malformed instance identifier"
1325
+
1326
+ and instances_of_arg_pair (n , v ) =
1327
+ string_of_module_expr n, instance_of_module_expr v
1328
+
1329
+ let of_module_expr mexpr = Imod_instance (instance_of_module_expr mexpr)
1330
+ end
1331
+
1264
1332
(* *****************************************************************************)
1265
1333
(* * The interface to our novel syntax, which we export *)
1266
1334
@@ -1391,6 +1459,18 @@ module Module_type = struct
1391
1459
{ mty with pmty_attributes = mty.pmty_attributes @ attrs }
1392
1460
end
1393
1461
1462
+ module Module_expr = struct
1463
+ type t = Emod_instance of Instances .module_expr
1464
+
1465
+ let of_ast_internal (feat : Feature.t ) sigi =
1466
+ match feat with
1467
+ | Language_extension Instances ->
1468
+ Some (Emod_instance (Instances. of_module_expr sigi))
1469
+ | _ -> None
1470
+
1471
+ let of_ast = Module_expr. make_of_ast ~of_ast_internal
1472
+ end
1473
+
1394
1474
module Signature_item = struct
1395
1475
type t = Jsig_layout of Layouts .signature_item
1396
1476
0 commit comments