diff --git a/CHANGES.md b/CHANGES.md index e3458f2d..9d824842 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ unreleased ---------- +- new functions `Ast_builder.{e,p}list_v2` that take an extra `?tail:expression` + parameter compared to `Ast_builder.{e,p}list`, 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 99a2e499..dbdb7dd8 100644 --- a/src/ast_builder.ml +++ b/src/ast_builder.ml @@ -127,21 +127,33 @@ 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_v2 ~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_v2 ~loc ?tail l ])) - let rec plist ~loc l = + let elist ~loc l = elist_v2 ~loc l + + let rec plist_v2 ~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_v2 ~loc ?tail l ])) + + let plist ~loc l = plist_v2 ~loc l let unapplied_type_constr_conv_without_apply ~loc (ident : Longident.t) ~f = match ident with @@ -386,6 +398,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_v2 ?tail l = Default.elist_v2 ~loc ?tail l + let plist_v2 ?tail l = Default.plist_v2 ~loc ?tail l let elist l = Default.elist ~loc l let plist l = Default.plist ~loc l diff --git a/src/ast_builder_intf.ml b/src/ast_builder_intf.ml index 652828d9..e7be569e 100644 --- a/src/ast_builder_intf.ml +++ b/src/ast_builder_intf.ml @@ -42,7 +42,9 @@ 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_v2 : (?tail:expression -> expression list -> expression) with_loc val elist : (expression list -> expression) with_loc + val plist_v2 : (?tail:pattern -> pattern list -> pattern) with_loc val plist : (pattern list -> pattern) with_loc val pstr_value_list :