Skip to content

Commit

Permalink
psig_recmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Jul 28, 2024
1 parent f25b0c8 commit 913886d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
8 changes: 6 additions & 2 deletions src/reason-parser/reason_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1966,8 +1966,12 @@ signature_item:
| item_attributes opt_LET_MODULE_REC_ident module_type_body(COLON)
and_module_rec_declaration*
{ let loc = mklocation $symbolstartpos $endpos($3) in
let _ext, letmodule = $2 in
Psig_recmodule (Ast_helper.Md.mk letmodule $3 ~attrs:$1 ~loc :: $4) }
let ext, letmodule = $2 in
wrap_sig_ext
~loc
(Psig_recmodule (Ast_helper.Md.mk letmodule $3 ~attrs:$1 ~loc :: $4))
ext
}
| item_attributes MODULE TYPE as_loc(ident)
{ let loc = mklocation $symbolstartpos $endpos in
Psig_modtype (Ast_helper.Mtd.mk $4 ~attrs:$1 ~loc)
Expand Down
80 changes: 42 additions & 38 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7212,6 +7212,7 @@ let printer = object(self:'self)
| Psig_value ({ pval_prim = [_]; _ } as vd) -> self#primitive_declaration ~extension vd
| Psig_value vd -> self#val_binding ~extension vd
| Psig_module pmd -> self#psig_module ~extension pmd
| Psig_recmodule pmd -> self#psig_recmodule ~extension pmd
| _ -> self#payload "%%" extension (PSig [item])
end
| _ -> self#signature_item' item
Expand Down Expand Up @@ -7263,6 +7264,45 @@ let printer = object(self:'self)
~layout:(self#attach_std_item_attrs stdAttrs @@ layout)
()
method psig_recmodule ?extension decls =
let items = List.mapi (fun i xx ->
let {stdAttrs; docAttrs} =
partitionAttributes ~partDoc:true xx.pmd_attributes
in
let letPattern =
makeList [
makeList ~postSpace:true [
atom (if i == 0
then
add_extension_sugar "module" extension ^ " rec"
else "and");
atom (moduleIdent xx.pmd_name)
];
atom ":"
]
in
let layout =
self#attach_std_item_attrs stdAttrs
(self#module_type ~space:true letPattern xx.pmd_type)
in
let layoutWithDocAttrs =
self#attachDocAttrsToLayout
~stdAttrs
~docAttrs
~loc:xx.pmd_name.loc
~layout
()
in
(extractLocModDecl xx, layoutWithDocAttrs)
) decls
in
makeNonIndentedBreakingList
(groupAndPrint
~xf:(fun (_, layout) -> layout)
~getLoc:(fun (loc, _) -> loc)
~comments:self#comments
items)
method signature_item' x : Layout.t =
let item: Layout.t =
match x.psig_desc with
Expand Down Expand Up @@ -7361,42 +7401,7 @@ let printer = object(self:'self)
~layout
()
| Psig_class_type l -> self#class_type_declaration_list l
| Psig_recmodule decls ->
let items = List.mapi (fun i xx ->
let {stdAttrs; docAttrs} =
partitionAttributes ~partDoc:true xx.pmd_attributes
in
let letPattern =
makeList [
makeList ~postSpace:true [
atom (if i == 0 then "module rec" else "and");
atom (moduleIdent xx.pmd_name)
];
atom ":"
]
in
let layout =
self#attach_std_item_attrs stdAttrs
(self#module_type ~space:true letPattern xx.pmd_type)
in
let layoutWithDocAttrs =
self#attachDocAttrsToLayout
~stdAttrs
~docAttrs
~loc:xx.pmd_name.loc
~layout
()
in
(extractLocModDecl xx, layoutWithDocAttrs)
) decls
in
makeNonIndentedBreakingList
(groupAndPrint
~xf:(fun (_, layout) -> layout)
~getLoc:(fun (loc, _) -> loc)
~comments:self#comments
items)
| Psig_recmodule decls -> self#psig_recmodule decls
| Psig_attribute a -> self#floating_attribute a
| Psig_extension (({loc}, _) as ext, attrs) ->
let {stdAttrs; docAttrs} =
Expand Down Expand Up @@ -7829,8 +7834,7 @@ let printer = object(self:'self)
self#moduleExpressionToFormattedApplicationItems
~prefix:"include"
moduleExpr
| Pstr_recmodule decls -> (* 3.07 *)
self#recmodule decls
| Pstr_recmodule decls -> self#recmodule decls
| Pstr_attribute a -> self#floating_attribute a
| Pstr_extension ((_extension, PStr []) as extension, attrs) ->
(* Extension with attributes and without PStr gets printed inline *)
Expand Down
2 changes: 2 additions & 0 deletions test/general-syntax-rei.t/input.rei
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ module%foo X: Y;

module%foo X = Y;

module%foo rec X: Y;

2 changes: 2 additions & 0 deletions test/general-syntax-rei.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ Format general interface syntax
module%foo X: Y;

module%foo X = Y;

module%foo rec X: Y;

0 comments on commit 913886d

Please sign in to comment.