Skip to content

Commit

Permalink
Add locations for Pparam
Browse files Browse the repository at this point in the history
Signed-off-by: Paul-Elliot <[email protected]>
  • Loading branch information
panglesd committed Aug 3, 2023
1 parent ff95254 commit 4d6a434
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
22 changes: 14 additions & 8 deletions astlib/ast_502.ml
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ module Parsetree = struct
pbop_loc : Location.t;
}

and function_param (*IF_CURRENT = Parsetree.function_param *) =
and function_param_desc (*IF_CURRENT = Parsetree.function_param_desc *) =
| Pparam_val of arg_label * expression option * pattern
(** [Pparam_val (lbl, exp0, P)] represents the parameter:
- [P]
Expand All @@ -475,17 +475,18 @@ module Parsetree = struct
Note: If [E0] is provided, only
{{!Asttypes.arg_label.Optional}[Optional]} is allowed.
*)
| Pparam_newtype of string loc * Location.t
(** [Pparam_newtype (x, loc)] represents the parameter [(type x)].
[x] carries the location of the identifier, whereas [loc] is
the location of the [(type x)] as a whole.
| Pparam_newtype of string loc
(** [Pparam_newtype x] represents the parameter [(type x)].
[x] carries the location of the identifier, whereas the [pparam_loc]
on the enclosing [function_param] node is the location of the [(type x)]
as a whole.
Multiple parameters [(type a b c)] are represented as multiple
[Pparam_newtype] nodes, let's say:
{[ [ Pparam_newtype (a, loc1);
Pparam_newtype (b, loc2);
Pparam_newtype (c, loc3);
{[ [ { pparam_kind = Pparam_newtype a; pparam_loc = loc1 };
{ pparam_kind = Pparam_newtype b; pparam_loc = loc2 };
{ pparam_kind = Pparam_newtype c; pparam_loc = loc3 };
]
]}
Expand All @@ -495,6 +496,11 @@ module Parsetree = struct
variables [a], [b], and [c] in the source code.
*)

and function_param (*IF_CURRENT = Parsetree.function_param *) =
{ pparam_loc : Location.t;
pparam_desc : function_param_desc;
}

and function_body (*IF_CURRENT = Parsetree.function_body *) =
| Pfunction_body of expression
| Pfunction_cases of case list * Location.t * attributes
Expand Down
12 changes: 8 additions & 4 deletions astlib/migrate_501_502.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ and copy_expression_desc loc :
| Ast_501.Parsetree.Pexp_fun (arg_label, opt_expr, pat, expr) ->
Ast_502.Parsetree.Pexp_function
( [
Pparam_val
( copy_arg_label arg_label,
Option.map copy_expression opt_expr,
copy_pattern pat );
{
pparam_desc =
Pparam_val
( copy_arg_label arg_label,
Option.map copy_expression opt_expr,
copy_pattern pat );
pparam_loc = loc;
};
],
None,
Ast_502.Parsetree.Pfunction_body (copy_expression expr) )
Expand Down
11 changes: 7 additions & 4 deletions astlib/migrate_502_501.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ and copy_expression_desc :
List.fold_right
(fun param expr ->
match param with
| Ast_502.Parsetree.Pparam_val (lbl, exp0, p) ->
| {
Ast_502.Parsetree.pparam_desc = Pparam_val (lbl, exp0, p);
pparam_loc;
} ->
let pexp_desc =
Ast_501.Parsetree.Pexp_fun
( copy_arg_label lbl,
Expand All @@ -122,17 +125,17 @@ and copy_expression_desc :
in
{
Ast_501.Parsetree.pexp_desc;
pexp_loc = expr.pexp_loc;
pexp_loc = pparam_loc;
pexp_loc_stack = [];
pexp_attributes = [];
}
| Pparam_newtype (x, loc) ->
| { pparam_desc = Pparam_newtype x; pparam_loc } ->
let pexp_desc =
Ast_501.Parsetree.Pexp_newtype (copy_loc (fun x -> x) x, expr)
in
{
Ast_501.Parsetree.pexp_desc;
pexp_loc = loc;
pexp_loc = pparam_loc;
pexp_loc_stack = [];
pexp_attributes = [];
})
Expand Down

0 comments on commit 4d6a434

Please sign in to comment.