Skip to content

Commit

Permalink
make Ast_builder.{e,p}list able to build lists such as a :: b :: c
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Gatien-Baron <[email protected]>
  • Loading branch information
v-gb committed Jun 11, 2024
1 parent a0d7c8e commit c638da1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
24 changes: 16 additions & 8 deletions src/ast_builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ast_builder_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit c638da1

Please sign in to comment.