Skip to content

Commit

Permalink
Modify the _ function_param types
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Jan 22, 2024
1 parent 07c78ee commit b026492
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 41 deletions.
48 changes: 30 additions & 18 deletions lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1220,12 +1220,18 @@ end = struct
let check_bindings l =
List.exists l ~f:(fun {pvb_pat; _} -> check_subpat pvb_pat)
in
let check_function_param param =
let check_param_val (_, _, p) = p == pat in
let check_expr_function_param param =
match param.pparam_desc with
| `Param_val (_, _, p) -> p == pat
| `Param_newtype _ -> false
| Param_val x -> check_param_val x
| Param_newtype _ -> false
in
let check_class_function_param param =
check_param_val param.pparam_desc
in
let check_class_function_params =
List.exists ~f:check_class_function_param
in
let check_function_params l = List.exists l ~f:check_function_param in
match ctx with
| Pld (PPat (p1, _)) -> assert (p1 == pat)
| Pld _ -> assert false
Expand Down Expand Up @@ -1283,9 +1289,9 @@ end = struct
| {pc_lhs; _} when pc_lhs == pat -> true
| _ -> false ) )
| Pexp_for (p, _, _, _, _) -> assert (p == pat)
| Pexp_fun (p, _) -> assert (check_function_param p) )
| Fpe ctx -> assert (check_function_param ctx)
| Fpc ctx -> assert (check_function_param ctx)
| Pexp_fun (p, _) -> assert (check_expr_function_param p) )
| Fpe ctx -> assert (check_expr_function_param ctx)
| Fpc ctx -> assert (check_class_function_param ctx)
| Vc _ -> assert false
| Lb x -> assert (x.pvb_pat == pat)
| Bo x -> assert (x.pbop_pat == pat)
Expand All @@ -1294,7 +1300,7 @@ end = struct
| Cl ctx ->
assert (
match ctx.pcl_desc with
| Pcl_fun (p, _) -> check_function_params p
| Pcl_fun (p, _) -> check_class_function_params p
| Pcl_constr _ -> false
| Pcl_structure {pcstr_self; _} ->
Option.exists ~f:(fun self_ -> self_ == pat) pcstr_self
Expand Down Expand Up @@ -1336,12 +1342,18 @@ end = struct
| PStr [{pstr_desc= Pstr_eval (e, _); _}] -> e == exp
| _ -> false
in
let check_function_param param =
let check_param_val (_, e, _) = Option.exists e ~f:(fun x -> x == exp) in
let check_expr_function_param param =
match param.pparam_desc with
| `Param_val (_, e, _) -> Option.exists e ~f:(fun x -> x == exp)
| `Param_newtype _ -> false
| Param_val x -> check_param_val x
| Param_newtype _ -> false
in
let check_class_function_param param =
check_param_val param.pparam_desc
in
let check_class_function_params =
List.exists ~f:check_class_function_param
in
let check_function_params l = List.exists l ~f:check_function_param in
match ctx with
| Pld (PPat (_, Some e1)) -> assert (e1 == exp)
| Pld _ -> assert false
Expand Down Expand Up @@ -1371,7 +1383,7 @@ end = struct
| {pc_rhs; _} when pc_rhs == exp -> true
| _ -> false ) )
| Pexp_fun (param, body) ->
assert (check_function_param param || body == exp)
assert (check_expr_function_param param || body == exp)
| Pexp_indexop_access {pia_lhs; pia_kind= Builtin idx; pia_rhs; _} ->
assert (
pia_lhs == exp || idx == exp
Expand Down Expand Up @@ -1420,8 +1432,8 @@ end = struct
| Pexp_for (_, e1, e2, _, e3) ->
assert (e1 == exp || e2 == exp || e3 == exp)
| Pexp_override e1N -> assert (List.exists e1N ~f:snd_f) )
| Fpe ctx -> assert (check_function_param ctx)
| Fpc ctx -> assert (check_function_param ctx)
| Fpe ctx -> assert (check_expr_function_param ctx)
| Fpc ctx -> assert (check_class_function_param ctx)
| Vc _ -> assert false
| Lb x -> assert (x.pvb_expr == exp)
| Bo x -> assert (x.pbop_exp == exp)
Expand All @@ -1444,7 +1456,7 @@ end = struct
| Cl ctx ->
let rec loop ctx =
match ctx.pcl_desc with
| Pcl_fun (param, e) -> check_function_params param || loop e
| Pcl_fun (param, e) -> check_class_function_params param || loop e
| Pcl_constr _ -> false
| Pcl_structure _ -> false
| Pcl_apply (_, l) -> List.exists l ~f:(fun (_, e) -> e == exp)
Expand Down Expand Up @@ -1889,8 +1901,8 @@ end = struct
| Ppat_cons _ -> true
| Ppat_construct _ | Ppat_record _ | Ppat_variant _ -> false
| _ -> true )
| Fpe {pparam_desc= `Param_val (_, _, _); _}, Ppat_cons _ -> true
| Fpc {pparam_desc= `Param_val (_, _, _); _}, Ppat_cons _ -> true
| Fpe {pparam_desc= Param_val (_, _, _); _}, Ppat_cons _ -> true
| Fpc {pparam_desc= _; _}, Ppat_cons _ -> true
| Pat {ppat_desc= Ppat_construct _; _}, Ppat_cons _ -> true
| _, Ppat_constraint (_, {ptyp_desc= Ptyp_poly _; _}) -> false
| ( Exp {pexp_desc= Pexp_letop _; _}
Expand Down
6 changes: 3 additions & 3 deletions lib/Extended_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,18 @@ module Parse = struct
{p with pexp_desc= Pexp_pack (name, Some pt)}
| { pexp_desc=
Pexp_fun
({pparam_desc= `Param_newtype types1; pparam_loc= loc1}, e1)
({pparam_desc= Param_newtype types1; pparam_loc= loc1}, e1)
; pexp_attributes= []
; _ } as e ->
let e =
match (expr m e1).pexp_desc with
| Pexp_fun
({pparam_desc= `Param_newtype types2; pparam_loc= loc2}, e2)
({pparam_desc= Param_newtype types2; pparam_loc= loc2}, e2)
->
{ e with
pexp_desc=
Pexp_fun
( { pparam_desc= `Param_newtype (types1 @ types2)
( { pparam_desc= Param_newtype (types1 @ types2)
; pparam_loc= {loc1 with loc_end= loc2.loc_end} }
, e2 ) }
| _ -> e
Expand Down
9 changes: 4 additions & 5 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ and type_constr_and_body c xbody =
let exp_ctx =
let pat = Ast_helper.Pat.any () in
let param =
{ pparam_desc= `Param_val (Nolabel, None, pat)
{ pparam_desc= Param_val (Nolabel, None, pat)
; pparam_loc= pat.ppat_loc }
in
Exp Ast_helper.(Exp.fun_ param exp)
Expand Down Expand Up @@ -1342,13 +1342,12 @@ and fmt_expr_fun_arg c fp =
Cmts.fmt c fp.pparam_loc
@@
match fp.pparam_desc with
| `Param_val x -> fmt_param_val c ctx x
| `Param_newtype x -> fmt_param_newtype c x
| Param_val x -> fmt_param_val c ctx x
| Param_newtype x -> fmt_param_newtype c x

and fmt_class_fun_arg c fp =
let ctx = Fpc fp in
Cmts.fmt c fp.pparam_loc
@@ match fp.pparam_desc with `Param_val x -> fmt_param_val c ctx x
Cmts.fmt c fp.pparam_loc @@ fmt_param_val c ctx fp.pparam_desc

and fmt_expr_fun_args c args = list args "@;" (fmt_expr_fun_arg c)

Expand Down
2 changes: 1 addition & 1 deletion lib/Sugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ module Let_binding = struct
[Extended_ast]. *)
let pat = Ast_helper.Pat.any () in
let param =
{ pparam_desc= `Param_val (Nolabel, None, pat)
{ pparam_desc= Param_val (Nolabel, None, pat)
; pparam_loc= pat.ppat_loc }
in
Exp (Ast_helper.Exp.fun_ param exp)
Expand Down
7 changes: 3 additions & 4 deletions vendor/parser-extended/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,10 @@ module FP = struct
List.map (map_loc sub) ty

let map_expr sub = function
| `Param_val x -> `Param_val (map_param_val sub x)
| `Param_newtype x -> `Param_newtype (map_param_newtype sub x)
| Param_val x -> Param_val (map_param_val sub x)
| Param_newtype x -> Param_newtype (map_param_newtype sub x)

let map_class sub = function
| `Param_val x -> `Param_val (map_param_val sub x)
let map_class sub x = map_param_val sub x

let map sub f { pparam_loc; pparam_desc } =
let pparam_loc = sub.location sub pparam_loc in
Expand Down
6 changes: 3 additions & 3 deletions vendor/parser-extended/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -2601,13 +2601,13 @@ param_newtype:
;
expr_fun_param:
mkfunparam(
param_val { `Param_val $1 }
| param_newtype { `Param_newtype $1 }
param_val { Param_val $1 }
| param_newtype { Param_newtype $1 }
) { $1 }
;
class_fun_param:
mkfunparam (
param_val { `Param_val $1 }
param_val { $1 }
) { $1 }
;
fun_def:
Expand Down
9 changes: 6 additions & 3 deletions vendor/parser-extended/parsetree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,13 @@ and 'a function_param =
pparam_desc : 'a;
}

and expr_function_param =
[ `Param_val of param_val | `Param_newtype of param_newtype ] function_param
and param_val_or_newtype =
| Param_val of param_val
| Param_newtype of param_newtype

and class_function_param = [ `Param_val of param_val ] function_param
and expr_function_param = param_val_or_newtype function_param

and class_function_param = param_val function_param

and type_constraint =
| Pconstraint of core_type
Expand Down
7 changes: 3 additions & 4 deletions vendor/parser-extended/printast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,12 @@ and param_newtype i ppf ty =
and expr_function_param i ppf { pparam_desc = desc; pparam_loc = loc } =
line i ppf "function_param %a\n" fmt_location loc;
match desc with
| `Param_val x -> param_val i ppf x
| `Param_newtype x -> param_newtype i ppf x
| Param_val x -> param_val i ppf x
| Param_newtype x -> param_newtype i ppf x

and class_function_param i ppf { pparam_desc = desc; pparam_loc = loc } =
line i ppf "function_param %a\n" fmt_location loc;
match desc with
| `Param_val x -> param_val i ppf x
param_val i ppf desc

and type_constraint i ppf constraint_ =
match constraint_ with
Expand Down

0 comments on commit b026492

Please sign in to comment.