From 4d6a43460c932c0630177b0aa7cf94a3f694a0c2 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Thu, 3 Aug 2023 20:13:15 +0200 Subject: [PATCH] Add locations for Pparam Signed-off-by: Paul-Elliot --- astlib/ast_502.ml | 22 ++++++++++++++-------- astlib/migrate_501_502.ml | 12 ++++++++---- astlib/migrate_502_501.ml | 11 +++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/astlib/ast_502.ml b/astlib/ast_502.ml index 2a3ca5c2c..7766f208b 100644 --- a/astlib/ast_502.ml +++ b/astlib/ast_502.ml @@ -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] @@ -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 }; ] ]} @@ -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 diff --git a/astlib/migrate_501_502.ml b/astlib/migrate_501_502.ml index 7ad0868ac..f1dece962 100644 --- a/astlib/migrate_501_502.ml +++ b/astlib/migrate_501_502.ml @@ -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) ) diff --git a/astlib/migrate_502_501.ml b/astlib/migrate_502_501.ml index d4f5ffb2d..abc8f9e08 100644 --- a/astlib/migrate_502_501.ml +++ b/astlib/migrate_502_501.ml @@ -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, @@ -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 = []; })