|
8 | 8 | * In general it represent messages to show results or errors to the user. *)
|
9 | 9 |
|
10 | 10 | module Doc = Res_doc
|
11 |
| -module Token = Res_token |
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 |
| - && |
27 |
| - let a = Char.code (String.unsafe_get x 0) in |
28 |
| - a <= 57 |
29 |
| - && |
30 |
| - if len > 1 then |
31 |
| - a > 48 |
32 |
| - && for_all_from x 1 (function |
33 |
| - | '0' .. '9' -> true |
34 |
| - | _ -> false) |
35 |
| - else a >= 48 |
36 |
| - |
37 |
| -type identifierStyle = ExoticIdent | NormalIdent |
38 |
| - |
39 |
| -let classifyIdentContent ~allowUident txt = |
40 |
| - let len = String.length txt in |
41 |
| - let rec go i = |
42 |
| - if i == len then NormalIdent |
43 |
| - else |
44 |
| - let c = String.unsafe_get txt i in |
45 |
| - if |
46 |
| - i == 0 |
47 |
| - && not |
48 |
| - ((allowUident && c >= 'A' && c <= 'Z') |
49 |
| - || (c >= 'a' && c <= 'z') |
50 |
| - || c = '_') |
51 |
| - then ExoticIdent |
52 |
| - else if |
53 |
| - not |
54 |
| - ((c >= 'a' && c <= 'z') |
55 |
| - || (c >= 'A' && c <= 'Z') |
56 |
| - || c = '\'' || c = '_' |
57 |
| - || (c >= '0' && c <= '9')) |
58 |
| - then ExoticIdent |
59 |
| - else go (i + 1) |
60 |
| - in |
61 |
| - if Token.isKeywordTxt txt then ExoticIdent else go 0 |
62 |
| - |
63 |
| -let printIdentLike ~allowUident txt = |
64 |
| - match classifyIdentContent ~allowUident txt with |
65 |
| - | ExoticIdent -> Doc.concat [Doc.text "\\\""; Doc.text txt; Doc.text "\""] |
66 |
| - | NormalIdent -> Doc.text txt |
67 |
| - |
68 |
| -let printPolyVarIdent txt = |
69 |
| - (* numeric poly-vars don't need quotes: #644 *) |
70 |
| - if isValidNumericPolyvarNumber txt then Doc.text txt |
71 |
| - else |
72 |
| - match classifyIdentContent ~allowUident:true txt with |
73 |
| - | ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""] |
74 |
| - | NormalIdent -> Doc.text txt |
| 11 | +module Printer = Res_printer |
75 | 12 |
|
76 | 13 | (* ReScript doesn't have parenthesized identifiers.
|
77 | 14 | * We don't support custom operators. *)
|
@@ -119,7 +56,7 @@ let escapeStringContents s =
|
119 | 56 |
|
120 | 57 | let rec printOutIdentDoc ?(allowUident = true) (ident : Outcometree.out_ident) =
|
121 | 58 | match ident with
|
122 |
| - | Oide_ident s -> printIdentLike ~allowUident s |
| 59 | + | Oide_ident s -> Printer.printIdentLike ~allowUident s |
123 | 60 | | Oide_dot (ident, s) ->
|
124 | 61 | Doc.concat [printOutIdentDoc ident; Doc.dot; Doc.text s]
|
125 | 62 | | Oide_apply (call, arg) ->
|
@@ -189,7 +126,8 @@ let rec printOutTypeDoc (outType : Outcometree.out_type) =
|
189 | 126 | Doc.space;
|
190 | 127 | Doc.join ~sep:Doc.space
|
191 | 128 | (List.map
|
192 |
| - (fun lbl -> printIdentLike ~allowUident:true lbl) |
| 129 | + (fun lbl -> |
| 130 | + Printer.printIdentLike ~allowUident:true lbl) |
193 | 131 | tags);
|
194 | 132 | ]));
|
195 | 133 | Doc.softLine;
|
@@ -400,7 +338,7 @@ and printOutVariant variant =
|
400 | 338 | (Doc.concat
|
401 | 339 | [
|
402 | 340 | Doc.text "#";
|
403 |
| - printPolyVarIdent name; |
| 341 | + Printer.printPolyVarIdent name; |
404 | 342 | (match types with
|
405 | 343 | | [] -> Doc.nil
|
406 | 344 | | types ->
|
@@ -530,7 +468,7 @@ and printRecordDeclRowDoc (name, mut, opt, arg) =
|
530 | 468 | (Doc.concat
|
531 | 469 | [
|
532 | 470 | (if mut then Doc.text "mutable " else Doc.nil);
|
533 |
| - printIdentLike ~allowUident:false name; |
| 471 | + Printer.printIdentLike ~allowUident:false name; |
534 | 472 | (if opt then Doc.text "?" else Doc.nil);
|
535 | 473 | Doc.text ": ";
|
536 | 474 | printOutTypeDoc arg;
|
@@ -733,7 +671,9 @@ let rec printOutSigItemDoc ?(printNameAsIs = false)
|
733 | 671 | attrs;
|
734 | 672 | kw;
|
735 | 673 | (if printNameAsIs then Doc.text outTypeDecl.otype_name
|
736 |
| - else printIdentLike ~allowUident:false outTypeDecl.otype_name); |
| 674 | + else |
| 675 | + Printer.printIdentLike ~allowUident:false |
| 676 | + outTypeDecl.otype_name); |
737 | 677 | typeParams;
|
738 | 678 | kind;
|
739 | 679 | ]);
|
@@ -865,7 +805,7 @@ and printOutExtensionConstructorDoc
|
865 | 805 | (Doc.concat
|
866 | 806 | [
|
867 | 807 | Doc.text "type ";
|
868 |
| - printIdentLike ~allowUident:false outExt.oext_type_name; |
| 808 | + Printer.printIdentLike ~allowUident:false outExt.oext_type_name; |
869 | 809 | typeParams;
|
870 | 810 | Doc.text " += ";
|
871 | 811 | Doc.line;
|
@@ -904,7 +844,7 @@ and printOutTypeExtensionDoc (typeExtension : Outcometree.out_type_extension) =
|
904 | 844 | (Doc.concat
|
905 | 845 | [
|
906 | 846 | Doc.text "type ";
|
907 |
| - printIdentLike ~allowUident:false typeExtension.otyext_name; |
| 847 | + Printer.printIdentLike ~allowUident:false typeExtension.otyext_name; |
908 | 848 | typeParams;
|
909 | 849 | Doc.text " += ";
|
910 | 850 | (if typeExtension.otyext_private = Asttypes.Private then
|
|
0 commit comments