Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit de70968

Browse files
author
Iwan
committed
Implement printer support for numeric poly var
1 parent fb8ebff commit de70968

File tree

8 files changed

+63
-21
lines changed

8 files changed

+63
-21
lines changed

src/res_printer.ml

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,42 @@ let printIdentLike ?allowUident txt =
425425
]
426426
| NormalIdent -> Doc.text txt
427427

428+
let rec unsafe_for_all_range s ~start ~finish p =
429+
start > finish ||
430+
p (String.unsafe_get s start) &&
431+
unsafe_for_all_range s ~start:(start + 1) ~finish p
432+
433+
let for_all_from s start p =
434+
let len = String.length s in
435+
if start < 0 then invalid_arg "Ext_string.for_all_from"
436+
else unsafe_for_all_range s ~start ~finish:(len - 1) p
437+
438+
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
439+
let isValidNumericPolyvarNumber (x : string) =
440+
let len = String.length x in
441+
len > 0 && (
442+
let a = Char.code (String.unsafe_get x 0) in
443+
a <= 57 &&
444+
(if len > 1 then
445+
a > 48 &&
446+
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
447+
else
448+
a >= 48 )
449+
)
450+
428451
(* Exotic identifiers in poly-vars have a "lighter" syntax: #"ease-in" *)
429452
let printPolyVarIdent txt =
430-
match classifyIdentContent ~allowUident:true txt with
431-
| ExoticIdent -> Doc.concat [
432-
Doc.text "\"";
433-
Doc.text txt;
434-
Doc.text"\""
435-
]
436-
| NormalIdent -> Doc.text txt
453+
(* numeric poly-vars don't need quotes: #644 *)
454+
if isValidNumericPolyvarNumber txt then
455+
Doc.text txt
456+
else
457+
match classifyIdentContent ~allowUident:true txt with
458+
| ExoticIdent -> Doc.concat [
459+
Doc.text "\"";
460+
Doc.text txt;
461+
Doc.text"\""
462+
]
463+
| NormalIdent -> Doc.text txt
437464

438465

439466
let printLident l = match l with

tests/printer/expr/__snapshots__/render.spec.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,12 +3456,12 @@ let r = #lident(a, b)
34563456
let r = #\\"exotic lident\\"
34573457
let r = #\\"exotic lident\\"(a, b)
34583458
3459-
let x = #\\"1\\"
3460-
let x = #\\"123\\"
3459+
let x = #1
3460+
let x = #123
34613461
let x = #\\"10space\\"
34623462
let x = #space10
34633463
3464-
let a = #\\"1\\"
3464+
let a = #1
34653465
let b = #\\"1a\\"
34663466
let c = #a2
34673467
let d = #abcd

tests/printer/expr/expected/polyvariant.res.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ let r = #lident(a, b)
108108
let r = #"exotic lident"
109109
let r = #"exotic lident"(a, b)
110110

111-
let x = #"1"
112-
let x = #"123"
111+
let x = #1
112+
let x = #123
113113
let x = #"10space"
114114
let x = #space10
115115

116-
let a = #"1"
116+
let a = #1
117117
let b = #"1a"
118118
let c = #a2
119119
let d = #abcd

tests/printer/pattern/__snapshots__/render.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ let #Shape = x
429429
let #\\"type\\" = x
430430
let #\\"test 🏚\\" = x
431431
let #\\"Shape✅\\" = x
432-
let #\\"1\\" = x
433-
let #\\"123\\" = x
432+
let #1 = x
433+
let #123 = x
434434
let #\\"10space\\" = x
435435
436436
let #space10 = x
@@ -453,8 +453,8 @@ let cmp = (selectedChoice, value) =>
453453
| (#...a: typ) => true
454454
| lazy #...a => true
455455
| exception #...a => true
456-
| #\\"1\\" => true
457-
| #\\"123\\" => true
456+
| #1 => true
457+
| #123 => true
458458
| #\\"10space\\" => x
459459
| #space10 => x
460460
| _ => false

tests/printer/pattern/expected/variant.res.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ let #Shape = x
33
let #"type" = x
44
let #"test 🏚" = x
55
let #"Shape✅" = x
6-
let #"1" = x
7-
let #"123" = x
6+
let #1 = x
7+
let #123 = x
88
let #"10space" = x
99

1010
let #space10 = x
@@ -27,8 +27,8 @@ let cmp = (selectedChoice, value) =>
2727
| (#...a: typ) => true
2828
| lazy #...a => true
2929
| exception #...a => true
30-
| #"1" => true
31-
| #"123" => true
30+
| #1 => true
31+
| #123 => true
3232
| #"10space" => x
3333
| #space10 => x
3434
| _ => false

tests/printer/typexpr/__snapshots__/render.spec.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,5 +756,10 @@ type currencyPoly = [
756756
757757
type t = [s]
758758
type t = [ListStyleType.t]
759+
760+
type permissions = [
761+
| #777
762+
| #644
763+
]
759764
"
760765
`;

tests/printer/typexpr/expected/variant.res.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,8 @@ type currencyPoly = [
102102

103103
type t = [s]
104104
type t = [ListStyleType.t]
105+
106+
type permissions = [
107+
| #777
108+
| #644
109+
]

tests/printer/typexpr/variant.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ type currencyPoly = [#UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSD | #CAAA
9696

9797
type t = [s]
9898
type t = [ListStyleType.t];
99+
100+
type permissions = [
101+
| #777
102+
| #644
103+
]

0 commit comments

Comments
 (0)