Skip to content

Commit

Permalink
Merge pull request #498 from v-gb/push-ooxmlqlytnkz
Browse files Browse the repository at this point in the history
make Ast_builder.{e,p}list able to build lists such as `a :: b :: c`
  • Loading branch information
NathanReb authored Jun 21, 2024
2 parents 0a842ea + d49d996 commit d894187
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
26 changes: 20 additions & 6 deletions src/ast_builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

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

0 comments on commit d894187

Please sign in to comment.