Skip to content

Commit 0c4eec1

Browse files
committed
dedupe snippets
1 parent c582cf4 commit 0c4eec1

File tree

3 files changed

+13
-71
lines changed

3 files changed

+13
-71
lines changed

jscomp/syntax/src/res_outcome_printer.ml

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,7 @@
88
* In general it represent messages to show results or errors to the user. *)
99

1010
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 printPolyVarIdent txt =
64-
(* numeric poly-vars don't need quotes: #644 *)
65-
if isValidNumericPolyvarNumber txt then Doc.text txt
66-
else
67-
match classifyIdentContent ~allowUident:true txt with
68-
| ExoticIdent -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
69-
| NormalIdent -> Doc.text txt
11+
module Printer = Res_printer
7012

7113
(* ReScript doesn't have parenthesized identifiers.
7214
* We don't support custom operators. *)
@@ -393,7 +335,7 @@ and printOutVariant variant =
393335
(Doc.concat
394336
[
395337
Doc.text "#";
396-
printPolyVarIdent name;
338+
Printer.printPolyVarIdent name;
397339
(match types with
398340
| [] -> Doc.nil
399341
| types ->

jscomp/syntax/src/res_printer.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,25 +376,23 @@ let printLongident = function
376376
| Longident.Lident txt -> Doc.text txt
377377
| lid -> Doc.join ~sep:Doc.dot (printLongidentAux [] lid)
378378

379-
type identifierStyle = ExoticIdent | NormalIdent
379+
type identifierStyle = ExoticLike | NormalIdent
380380

381-
let classifyIdentContent ?(allowUident = false) ?(allowHyphen = false) txt =
382-
if Token.isKeywordTxt txt then ExoticIdent
381+
let classifyIdentContent txt =
382+
if Ext_ident.is_exotic txt then ExoticLike
383+
else if Token.isKeywordTxt txt then ExoticLike
383384
else
384385
let len = String.length txt in
385386
let rec loop i =
386387
if i == len then NormalIdent
387388
else if i == 0 then
388389
match String.unsafe_get txt i with
389-
| 'A' .. 'Z' when allowUident -> loop (i + 1)
390-
| 'a' .. 'z' | '_' -> loop (i + 1)
391-
| '-' when allowHyphen -> loop (i + 1)
392-
| _ -> ExoticIdent
390+
| 'A' .. 'Z' | 'a' .. 'z' | '_' -> loop (i + 1)
391+
| _ -> ExoticLike
393392
else
394393
match String.unsafe_get txt i with
395394
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '\'' | '_' -> loop (i + 1)
396-
| '-' when allowHyphen -> loop (i + 1)
397-
| _ -> ExoticIdent
395+
| _ -> ExoticLike
398396
in
399397
loop 0
400398

@@ -427,8 +425,8 @@ let printPolyVarIdent txt =
427425
(* numeric poly-vars don't need quotes: #644 *)
428426
if isValidNumericPolyvarNumber txt then Doc.text txt
429427
else
430-
match classifyIdentContent ~allowUident:true txt with
431-
| ExoticIdent ->
428+
match classifyIdentContent txt with
429+
| ExoticLike ->
432430
Doc.concat
433431
[Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
434432
| NormalIdent -> (

jscomp/syntax/src/res_printer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ val printImplementation :
2222
val printInterface :
2323
width:int -> Parsetree.signature -> comments:Res_comment.t list -> string
2424

25+
val printPolyVarIdent : string -> Res_doc.t
26+
2527
val polyVarIdentToString : string -> string [@@live]

0 commit comments

Comments
 (0)