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

File tree

14 files changed

+6278
-7462
lines changed

14 files changed

+6278
-7462
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6234 additions & 7438 deletions
Large diffs are not rendered by default.

compiler-libs-406/parsetree.mli

Lines changed: 1 addition & 1 deletion
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

Lines changed: 17 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)