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

Commit c6807e8

Browse files
Change char payload (#709)
* change Pconst_char payload(WIP) * tweak & tweak * remove Char.chr * tweak * bugfix: correct escapes printer * valid code point verification * tweak * take Res_utf8.repl back * remove wrong simplification * remove unneeded if-else * format * safe int to char to string * remove Obj.magic in scanner * remove string_of_int_as_char from stl to Pprintast * refactor * changelog
1 parent 180e252 commit c6807e8

14 files changed

+6278
-7462
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#### :nail_care Polish
6060

6161
- Change the internal representation of props for the lowercase components to record. https://github.com/rescript-lang/syntax/pull/665
62+
- Change the payload of Pconst_char for type safety. https://github.com/rescript-lang/rescript-compiler/pull/5759
6263

6364
## ReScript 10.0
6465

compiler-libs-406/ast_helper.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ val with_default_loc: loc -> (unit -> 'a) -> 'a
3636
(** {1 Constants} *)
3737

3838
module Const : sig
39-
val char : char -> constant
39+
val char : int -> constant
4040
val string : ?quotation_delimiter:string -> string -> constant
4141
val integer : ?suffix:char -> string -> constant
4242
val int : ?suffix:char -> int -> constant

compiler-libs-406/asttypes.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
type constant =
1919
Const_int of int
20-
| Const_char of char
20+
| Const_char of int
2121
| Const_string of string * string option
2222
| Const_float of string
2323
| Const_int32 of int32

compiler-libs-406/char.mli

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ val equal: t -> t -> bool
7070
(* The following is for system use only. Do not call directly. *)
7171

7272
external unsafe_chr : int -> char = "%identity"
73+

compiler-libs-406/parmatch.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ let is_cons = function
376376

377377
let pretty_const c = match c with
378378
| Const_int i -> Printf.sprintf "%d" i
379-
| Const_char c -> Printf.sprintf "%C" c
379+
| Const_char i -> Printf.sprintf "%s" (Pprintast.string_of_int_as_char i)
380380
| Const_string (s, _) -> Printf.sprintf "%S" s
381381
| Const_float f -> Printf.sprintf "%s" f
382382
| Const_int32 i -> Printf.sprintf "%ldl" i
@@ -1093,7 +1093,7 @@ let build_other ext env = match env with
10931093
let rec find_other i imax =
10941094
if i > imax then raise Not_found
10951095
else
1096-
let ci = Char.chr i in
1096+
let ci = i in
10971097
if List.mem ci all_chars then
10981098
find_other (i+1) imax
10991099
else

compiler-libs-406/parser.ml

+6,234-7,438
Large diffs are not rendered by default.

compiler-libs-406/parsetree.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type constant =
2424
Suffixes [g-z][G-Z] are accepted by the parser.
2525
Suffixes except 'l', 'L' and 'n' are rejected by the typechecker
2626
*)
27-
| Pconst_char of char
27+
| Pconst_char of int
2828
(* 'c' *)
2929
| Pconst_string of string * string option
3030
(* "constant"

compiler-libs-406/pprintast.ml

+17-1
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,24 @@ let rec longident f = function
191191

192192
let longident_loc f x = pp f "%a" longident x.txt
193193

194+
let string_of_int_as_char i =
195+
let str = match Char.unsafe_chr i with
196+
| '\'' -> "\\'"
197+
| '\\' -> "\\\\"
198+
| '\n' -> "\\n"
199+
| '\t' -> "\\t"
200+
| '\r' -> "\\r"
201+
| '\b' -> "\\b"
202+
| ' ' .. '~' as c ->
203+
let s = (Bytes.create [@doesNotRaise]) 1 in
204+
Bytes.unsafe_set s 0 c;
205+
Bytes.unsafe_to_string s
206+
| _ -> Printf.sprintf "\\%d" i
207+
in
208+
Printf.sprintf "\'%s\'" str
209+
194210
let constant f = function
195-
| Pconst_char i -> pp f "%C" i
211+
| Pconst_char i -> pp f "%s" (string_of_int_as_char i)
196212
| Pconst_string (i, None) -> pp f "%S" i
197213
| Pconst_string (i, Some delim) -> pp f "{%s|%s|%s}" delim i delim
198214
| Pconst_integer (i, None) -> paren (i.[0]='-') (fun f -> pp f "%s") f i

compiler-libs-406/pprintast.mli

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ val pattern: Format.formatter -> Parsetree.pattern -> unit
2424
val signature: Format.formatter -> Parsetree.signature -> unit
2525
val structure: Format.formatter -> Parsetree.structure -> unit
2626
val string_of_structure: Parsetree.structure -> string
27+
val string_of_int_as_char: int -> string

compiler-libs-406/printast.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ let fmt_char_option f = function
5959
let fmt_constant f x =
6060
match x with
6161
| Pconst_integer (i,m) -> fprintf f "PConst_int (%s,%a)" i fmt_char_option m;
62-
| Pconst_char (c) -> fprintf f "PConst_char %02x" (Char.code c);
62+
| Pconst_char (i) -> fprintf f "PConst_char %02x" i;
6363
| Pconst_string (s, None) -> fprintf f "PConst_string(%S,None)" s;
6464
| Pconst_string (s, Some delim) ->
6565
fprintf f "PConst_string (%S,Some %S)" s delim;

compiler-libs-406/typecore.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
10971097
else
10981098
or_ ~loc:gloc
10991099
(constant ~loc:gloc (Pconst_char c1))
1100-
(loop (Char.chr(Char.code c1 + 1)) c2)
1100+
(loop (c1 + 1) c2)
11011101
in
11021102
let p = if c1 <= c2 then loop c1 c2 else loop c2 c1 in
11031103
let p = {p with ppat_loc=loc} in
@@ -3804,7 +3804,7 @@ and type_format loc str env =
38043804
| Escaped_percent ->
38053805
mk_constr "Escaped_percent" []
38063806
| Scan_indic c ->
3807-
mk_constr "Scan_indic" [ mk_char c ]
3807+
mk_constr "Scan_indic" [ mk_char (Char.code c) ]
38083808
and mk_formatting_gen : type a b c d e f .
38093809
(a, b, c, d, e, f) formatting_gen -> Parsetree.expression =
38103810
fun fmting -> match fmting with
@@ -3954,7 +3954,7 @@ and type_format loc str env =
39543954
| String_literal (s, rest) ->
39553955
mk_constr "String_literal" [ mk_string s; mk_fmt rest ]
39563956
| Char_literal (c, rest) ->
3957-
mk_constr "Char_literal" [ mk_char c; mk_fmt rest ]
3957+
mk_constr "Char_literal" [ mk_char (Char.code c); mk_fmt rest ]
39583958
| Format_arg (pad_opt, fmtty, rest) ->
39593959
mk_constr "Format_arg" [
39603960
mk_int_opt pad_opt; mk_fmtty fmtty; mk_fmt rest ]

src/res_printer.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ let printConstant ?(templateLiteral = false) c =
553553
| Pconst_float (s, _) -> Doc.text s
554554
| Pconst_char c ->
555555
let str =
556-
match c with
556+
match Char.unsafe_chr c with
557557
| '\'' -> "\\'"
558558
| '\\' -> "\\\\"
559559
| '\n' -> "\\n"
@@ -564,7 +564,7 @@ let printConstant ?(templateLiteral = false) c =
564564
let s = (Bytes.create [@doesNotRaise]) 1 in
565565
Bytes.unsafe_set s 0 c;
566566
Bytes.unsafe_to_string s
567-
| c -> Res_utf8.encodeCodePoint (Obj.magic c)
567+
| _ -> Res_utf8.encodeCodePoint c
568568
in
569569
Doc.text ("'" ^ str ^ "'")
570570

src/res_scanner.ml

+12-11
Original file line numberDiff line numberDiff line change
@@ -464,24 +464,23 @@ let scanEscape scanner =
464464
next scanner
465465
done;
466466
let c = !x in
467-
if Res_utf8.isValidCodePoint c then Char.unsafe_chr c
468-
else Char.unsafe_chr Res_utf8.repl
467+
if Res_utf8.isValidCodePoint c then c else Res_utf8.repl
469468
in
470469
let codepoint =
471470
match scanner.ch with
472471
| '0' .. '9' -> convertNumber scanner ~n:3 ~base:10
473472
| 'b' ->
474473
next scanner;
475-
'\008'
474+
8
476475
| 'n' ->
477476
next scanner;
478-
'\010'
477+
10
479478
| 'r' ->
480479
next scanner;
481-
'\013'
480+
13
482481
| 't' ->
483482
next scanner;
484-
'\009'
483+
009
485484
| 'x' ->
486485
next scanner;
487486
convertNumber scanner ~n:2 ~base:16
@@ -508,14 +507,13 @@ let scanEscape scanner =
508507
| '}' -> next scanner
509508
| _ -> ());
510509
let c = !x in
511-
if Res_utf8.isValidCodePoint c then Char.unsafe_chr c
512-
else Char.unsafe_chr Res_utf8.repl
510+
if Res_utf8.isValidCodePoint c then c else Res_utf8.repl
513511
| _ ->
514512
(* unicode escape sequence: '\u007A', exactly 4 hex digits *)
515513
convertNumber scanner ~n:4 ~base:16)
516514
| ch ->
517515
next scanner;
518-
ch
516+
Char.code ch
519517
in
520518
let contents =
521519
(String.sub [@doesNotRaise]) scanner.src offset (scanner.offset - offset)
@@ -849,7 +847,10 @@ let rec scan scanner =
849847
let offset = scanner.offset + 1 in
850848
next3 scanner;
851849
Token.Codepoint
852-
{c = ch; original = (String.sub [@doesNotRaise]) scanner.src offset 1}
850+
{
851+
c = Char.code ch;
852+
original = (String.sub [@doesNotRaise]) scanner.src offset 1;
853+
}
853854
| ch, _ ->
854855
next scanner;
855856
let offset = scanner.offset in
@@ -865,7 +866,7 @@ let rec scan scanner =
865866
(String.sub [@doesNotRaise]) scanner.src offset length
866867
in
867868
next scanner;
868-
Token.Codepoint {c = Obj.magic codepoint; original = contents})
869+
Token.Codepoint {c = codepoint; original = contents})
869870
else (
870871
scanner.ch <- ch;
871872
scanner.offset <- offset;

src/res_token.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type t =
55
| Open
66
| True
77
| False
8-
| Codepoint of {c: char; original: string}
8+
| Codepoint of {c: int; original: string}
99
| Int of {i: string; suffix: char option}
1010
| Float of {f: string; suffix: char option}
1111
| String of string

0 commit comments

Comments
 (0)