|
10 | 10 | module Doc = Res_doc
|
11 | 11 | module Token = Res_token
|
12 | 12 |
|
| 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 | + |
13 | 35 | (* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
|
14 | 36 | let isArityIdent ident =
|
15 | 37 | if String.length ident >= 6 then
|
@@ -57,13 +79,17 @@ let printIdentLike ~allowUident txt =
|
57 | 79 | | NormalIdent -> Doc.text txt
|
58 | 80 |
|
59 | 81 | 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 |
67 | 93 |
|
68 | 94 | (* ReScript doesn't have parenthesized identifiers.
|
69 | 95 | * We don't support custom operators. *)
|
@@ -317,8 +343,34 @@ let printPolyVarIdent txt =
|
317 | 343 | )
|
318 | 344 | | Otyp_arrow _ as typ ->
|
319 | 345 | 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 | + ] |
322 | 374 |
|
323 | 375 | and printOutArrowType ~uncurried typ =
|
324 | 376 | let (typArgs, typ) = collectArrowArgs typ [] in
|
@@ -572,7 +624,10 @@ let printPolyVarIdent txt =
|
572 | 624 | Doc.text " =";
|
573 | 625 | Doc.line;
|
574 | 626 | 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) |
576 | 631 | )
|
577 | 632 | ]
|
578 | 633 | )
|
@@ -611,10 +666,10 @@ let printPolyVarIdent txt =
|
611 | 666 | match outRecStatus with
|
612 | 667 | | Orec_not -> "module "
|
613 | 668 | | Orec_first -> "module rec "
|
614 |
| - | Orec_next -> "and" |
| 669 | + | Orec_next -> "and " |
615 | 670 | );
|
616 | 671 | Doc.text modName;
|
617 |
| - Doc.text " = "; |
| 672 | + Doc.text ": "; |
618 | 673 | printOutModuleTypeDoc outModType;
|
619 | 674 | ]
|
620 | 675 | )
|
|
0 commit comments