Skip to content

Commit 1ed8132

Browse files
Implement printing of Otyp_module in outcome printer. (rescript-lang#402)
Fixes rescript-lang/syntax#398
1 parent b7e0177 commit 1ed8132

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

syntax/src/res_outcome_printer.ml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,34 @@ let printPolyVarIdent txt =
343343
)
344344
| Otyp_arrow _ as typ ->
345345
printOutArrowType ~uncurried:false typ
346-
| Otyp_module (_modName, _stringList, _outTypes) ->
347-
Doc.nil
346+
| Otyp_module (modName, stringList, outTypes) ->
347+
let packageTypeDoc = match (stringList, outTypes) with
348+
| [], [] -> Doc.nil
349+
| labels, types ->
350+
let i = ref 0 in
351+
let package = Doc.join ~sep:Doc.line (List.map2 (fun lbl typ ->
352+
Doc.concat [
353+
Doc.text (if i.contents > 0 then "and " else "with ");
354+
Doc.text lbl;
355+
Doc.text " = ";
356+
printOutTypeDoc typ;
357+
]
358+
) labels types)
359+
in
360+
Doc.indent (
361+
Doc.concat [
362+
Doc.line;
363+
package
364+
]
365+
)
366+
in
367+
Doc.concat [
368+
Doc.text "module";
369+
Doc.lparen;
370+
Doc.text modName;
371+
packageTypeDoc;
372+
Doc.rparen;
373+
]
348374

349375
and printOutArrowType ~uncurried typ =
350376
let (typArgs, typ) = collectArrowArgs typ [] in
@@ -598,8 +624,8 @@ let printPolyVarIdent txt =
598624
Doc.text " =";
599625
Doc.line;
600626
Doc.group (
601-
Doc.join ~sep:Doc.line (List.map (fun prim ->
602-
let prim = if prim <> "" && (prim.[0] [@doesNotRaise]) = '\132' then "#rescript-external" else prim in
627+
Doc.join ~sep:Doc.line (List.map (fun prim ->
628+
let prim = if prim <> "" && (prim.[0] [@doesNotRaise]) = '\132' then "#rescript-external" else prim in
603629
(* not display those garbage '\132' is a magic number for marshal *)
604630
Doc.text ("\"" ^ prim ^ "\"")) primitives)
605631
)

syntax/tests/oprint/expected/oprint.res.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,11 @@ type dotdotObjectCoordinate<'a> = 'a
440440
}
441441
type permissions = [#644 | #777]
442442
type numericPolyVarWithPayload = [#1(string) | #2(int, string)]
443-
let numericPolyVarMatch: [> #1(string) | #2(int, string)]
443+
let numericPolyVarMatch: [> #1(string) | #2(int, string)]
444+
let sort: (module(Set.S with elt = 'a), list<'a>) => list<'a>
445+
let make_set: (('a, 'a) => int) => module(Set.S with elt = 'a)
446+
type picture = string
447+
module type DEVICE = {
448+
let draw: picture => unit
449+
}
450+
let devices: Hashtbl.t<string, module(DEVICE)>

syntax/tests/oprint/oprint.res

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,22 @@ type permissions = [
294294
| #777 => #1("payload")
295295
| #644 => #2(42, "test")
296296
}
297+
298+
let sort = (type s, module(Set: Set.S with type elt = s), l) =>
299+
Set.elements(List.fold_right(Set.add, l, Set.empty))
300+
301+
let make_set = (type s, cmp) => {
302+
module S = Set.Make({
303+
type t = s
304+
let compare = cmp
305+
})
306+
module(S: Set.S with type elt = s)
307+
}
308+
309+
type picture = string
310+
311+
module type DEVICE = {
312+
let draw: picture => unit
313+
}
314+
315+
let devices: Hashtbl.t<string, module(DEVICE)> = Hashtbl.create(17)

0 commit comments

Comments
 (0)