Skip to content

Commit 139938e

Browse files
committed
Sync latest outcome printer from the syntax repo.
1 parent d4e2932 commit 139938e

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

analysis/src/vendor/res_outcome_printer/res_outcome_printer.ml

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
module Doc = Res_doc
1111
module Token = Res_token
1212

13+
let rec unsafe_for_all_range s ~start ~finish p =
14+
start > finish ||
15+
p (String.unsafe_get s start) &&
16+
unsafe_for_all_range s ~start:(start + 1) ~finish p
17+
18+
let for_all_from s start p =
19+
let len = String.length s in
20+
unsafe_for_all_range s ~start ~finish:(len - 1) p
21+
22+
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
23+
let isValidNumericPolyvarNumber (x : string) =
24+
let len = String.length x in
25+
len > 0 && (
26+
let a = Char.code (String.unsafe_get x 0) in
27+
a <= 57 &&
28+
(if len > 1 then
29+
a > 48 &&
30+
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
31+
else
32+
a >= 48 )
33+
)
34+
1335
(* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
1436
let isArityIdent ident =
1537
if String.length ident >= 6 then
@@ -57,13 +79,17 @@ let printIdentLike ~allowUident txt =
5779
| NormalIdent -> Doc.text txt
5880

5981
let printPolyVarIdent txt =
60-
match classifyIdentContent ~allowUident:true txt with
61-
| ExoticIdent -> Doc.concat [
62-
Doc.text "\"";
63-
Doc.text txt;
64-
Doc.text"\""
65-
]
66-
| NormalIdent -> Doc.text txt
82+
(* numeric poly-vars don't need quotes: #644 *)
83+
if isValidNumericPolyvarNumber txt then
84+
Doc.text txt
85+
else
86+
match classifyIdentContent ~allowUident:true txt with
87+
| ExoticIdent -> Doc.concat [
88+
Doc.text "\"";
89+
Doc.text txt;
90+
Doc.text"\""
91+
]
92+
| NormalIdent -> Doc.text txt
6793

6894
(* ReScript doesn't have parenthesized identifiers.
6995
* We don't support custom operators. *)
@@ -317,8 +343,34 @@ let printPolyVarIdent txt =
317343
)
318344
| Otyp_arrow _ as typ ->
319345
printOutArrowType ~uncurried:false typ
320-
| Otyp_module (_modName, _stringList, _outTypes) ->
321-
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+
]
322374

323375
and printOutArrowType ~uncurried typ =
324376
let (typArgs, typ) = collectArrowArgs typ [] in
@@ -572,7 +624,10 @@ let printPolyVarIdent txt =
572624
Doc.text " =";
573625
Doc.line;
574626
Doc.group (
575-
Doc.join ~sep:Doc.line (List.map (fun prim -> Doc.text ("\"" ^ prim ^ "\"")) primitives)
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
629+
(* not display those garbage '\132' is a magic number for marshal *)
630+
Doc.text ("\"" ^ prim ^ "\"")) primitives)
576631
)
577632
]
578633
)
@@ -611,10 +666,10 @@ let printPolyVarIdent txt =
611666
match outRecStatus with
612667
| Orec_not -> "module "
613668
| Orec_first -> "module rec "
614-
| Orec_next -> "and"
669+
| Orec_next -> "and "
615670
);
616671
Doc.text modName;
617-
Doc.text " = ";
672+
Doc.text ": ";
618673
printOutModuleTypeDoc outModType;
619674
]
620675
)

0 commit comments

Comments
 (0)