From 72df07652237e8d66765782a20c572f1b8915ccc Mon Sep 17 00:00:00 2001 From: Valentin Gatien-Baron Date: Mon, 10 Jun 2024 19:26:37 +0200 Subject: [PATCH] make Ast_builder.{e,p}list able to build lists such as `a :: b :: c` Signed-off-by: Valentin Gatien-Baron --- CHANGES.md | 3 +++ src/ast_builder.ml | 24 ++++++++++++++++-------- src/ast_builder_intf.ml | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e3458f2d9..756af5062 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,9 @@ unreleased ---------- +- Ast_builder.{e,p}list now take a `?tail:expression` parameter, so they + can build ASTs like `a :: b :: c` instead of only `[ a; b ]`. + - Fix `Longident.parse` so it also handles indexing operators such as `.!()`, `.%(;..)<-`, or `Vec.(.%())` (#494, @octachron) diff --git a/src/ast_builder.ml b/src/ast_builder.ml index 99a2e4992..980672ddf 100644 --- a/src/ast_builder.ml +++ b/src/ast_builder.ml @@ -127,21 +127,29 @@ module Default = struct let econstruct cd arg = pexp_construct ~loc:cd.pcd_loc (Located.map_lident cd.pcd_name) arg - let rec elist ~loc l = + let rec elist ~loc ?tail l = match l with - | [] -> pexp_construct ~loc (Located.mk ~loc (Longident.Lident "[]")) None + | [] -> ( + match tail with + | Some e -> e + | None -> + pexp_construct ~loc (Located.mk ~loc (Longident.Lident "[]")) None) | x :: l -> pexp_construct ~loc (Located.mk ~loc (Longident.Lident "::")) - (Some (pexp_tuple ~loc [ x; elist ~loc l ])) + (Some (pexp_tuple ~loc [ x; elist ~loc ?tail l ])) - let rec plist ~loc l = + let rec plist ~loc ?tail l = match l with - | [] -> ppat_construct ~loc (Located.mk ~loc (Longident.Lident "[]")) None + | [] -> ( + match tail with + | Some p -> p + | None -> + ppat_construct ~loc (Located.mk ~loc (Longident.Lident "[]")) None) | x :: l -> ppat_construct ~loc (Located.mk ~loc (Longident.Lident "::")) - (Some (ppat_tuple ~loc [ x; plist ~loc l ])) + (Some (ppat_tuple ~loc [ x; plist ~loc ?tail l ])) let unapplied_type_constr_conv_without_apply ~loc (ident : Longident.t) ~f = match ident with @@ -386,8 +394,8 @@ end) : S = struct let eapply e el = Default.eapply ~loc e el let eabstract ps e = Default.eabstract ~loc ps e let esequence el = Default.esequence ~loc el - let elist l = Default.elist ~loc l - let plist l = Default.plist ~loc l + let elist ?tail l = Default.elist ~loc ?tail l + let plist ?tail l = Default.plist ~loc ?tail l let type_constr_conv ident ~f args = Default.type_constr_conv ~loc ident ~f args diff --git a/src/ast_builder_intf.ml b/src/ast_builder_intf.ml index 652828d93..9bd42fefc 100644 --- a/src/ast_builder_intf.ml +++ b/src/ast_builder_intf.ml @@ -42,8 +42,8 @@ module type Additional_helpers = sig val pexp_tuple_opt : (expression list -> expression option) with_loc val pconstruct : constructor_declaration -> pattern option -> pattern val econstruct : constructor_declaration -> expression option -> expression - val elist : (expression list -> expression) with_loc - val plist : (pattern list -> pattern) with_loc + val elist : (?tail:expression -> expression list -> expression) with_loc + val plist : (?tail:pattern -> pattern list -> pattern) with_loc val pstr_value_list : loc:Location.t ->