Skip to content

Commit c8c1654

Browse files
committed
Clean up string processing
1 parent 2d589b7 commit c8c1654

21 files changed

+106
-84
lines changed

compiler/core/j.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ and for_ident = ident
7575
and for_direction = Js_op.direction_flag
7676
and property_map = (property_name * expression) list
7777
and length_object = Js_op.length_object
78-
and delim = External_arg_spec.delim = DNone | DStarJ | DNoQuotes | DBackQuotes
78+
and string_kind = String_kind.t = Standard | Verbatim | RawJs | Template
7979

8080
and expression_desc =
8181
| Length of expression * length_object
@@ -139,7 +139,7 @@ and expression_desc =
139139
async: bool;
140140
directive: string option;
141141
}
142-
| Str of {delim: delim; txt: string}
142+
| Str of {kind: string_kind; txt: string}
143143
(* A string is UTF-8 encoded, and may contain
144144
escape sequences.
145145
*)

compiler/core/js_analyzer.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ let rec eq_expression ({expression_desc = x0} : J.expression)
201201
| Bin (op1, a1, b1) ->
202202
op0 = op1 && eq_expression a0 a1 && eq_expression b0 b1
203203
| _ -> false)
204-
| Str {delim = a0; txt = b0} -> (
204+
| Str {kind = a0; txt = b0} -> (
205205
match y0 with
206-
| Str {delim = a1; txt = b1} -> a0 = a1 && b0 = b1
206+
| Str {kind = a1; txt = b1} -> a0 = a1 && b0 = b1
207207
| _ -> false)
208208
| Static_index (e0, p0, off0) -> (
209209
match y0 with

compiler/core/js_dump.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,16 +720,16 @@ and expression_desc cxt ~(level : int) f x : cxt =
720720
P.string f L.code_point_at;
721721
(* FIXME: use code_point_at *)
722722
P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b))
723-
| Str {delim; txt} ->
723+
| Str {kind; txt} ->
724724
(*TODO --
725725
when utf8-> it will not escape '\\' which is definitely not we want
726726
*)
727727
let () =
728-
match delim with
729-
| DStarJ -> P.string f ("\"" ^ txt ^ "\"")
730-
| DNoQuotes -> P.string f txt
731-
| DNone -> Js_dump_string.pp_string f txt
732-
| DBackQuotes -> P.string f ("`" ^ txt ^ "`")
728+
match kind with
729+
| Verbatim -> P.string f ("\"" ^ txt ^ "\"")
730+
| RawJs -> P.string f txt
731+
| Standard -> Js_dump_string.pp_string f txt
732+
| Template -> P.string f ("`" ^ txt ^ "`")
733733
in
734734
cxt
735735
| Raw_js_code {code = s; code_info = info} -> (

compiler/core/js_exp_make.ml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ let pure_runtime_call module_name fn_name args =
156156

157157
let runtime_ref module_name fn_name = runtime_var_dot module_name fn_name
158158

159-
let str ?(delim = J.DNone) ?comment txt : t =
160-
{expression_desc = Str {txt; delim}; comment}
159+
let str ?(kind = J.Standard) ?comment txt : t =
160+
{expression_desc = Str {txt; kind}; comment}
161161

162162
let raw_js_code ?comment info s : t =
163163
{
@@ -209,7 +209,7 @@ let instanceof ?comment (e0 : t) (e1 : t) : t =
209209
{expression_desc = Bin (InstanceOf, e0, e1); comment}
210210

211211
let is_array (e0 : t) : t =
212-
let f = str "Array.isArray" ~delim:DNoQuotes in
212+
let f = str "Array.isArray" ~kind:RawJs in
213213
{expression_desc = Call (f, [e0], Js_call_info.ml_full_call); comment = None}
214214

215215
let new_ ?comment e0 args : t = {expression_desc = New (e0, Some args); comment}
@@ -548,7 +548,8 @@ let array_length ?comment (e : t) : t =
548548

549549
let string_length ?comment (e : t) : t =
550550
match e.expression_desc with
551-
| Str {txt; delim = DNone} -> int ?comment (Int32.of_int (String.length txt))
551+
| Str {txt; kind = Standard} ->
552+
int ?comment (Int32.of_int (String.length txt))
552553
(* No optimization for {j||j}*)
553554
| _ -> {expression_desc = Length (e, String); comment}
554555

@@ -566,31 +567,31 @@ let function_length ?comment (e : t) : t =
566567
*)
567568

568569
let rec string_append ?comment (e : t) (el : t) : t =
569-
let concat a b ~delim = {e with expression_desc = Str {txt = a ^ b; delim}} in
570+
let concat a b ~kind = {e with expression_desc = Str {txt = a ^ b; kind}} in
570571
match (e.expression_desc, el.expression_desc) with
571572
| Str {txt = ""}, _ -> el
572573
| _, Str {txt = ""} -> e
573-
| ( Str {txt = a; delim},
574-
String_append ({expression_desc = Str {txt = b; delim = delim_}}, c) )
575-
when delim = delim_ ->
576-
string_append ?comment (concat a b ~delim) c
577-
| ( String_append (c, {expression_desc = Str {txt = b; delim}}),
578-
Str {txt = a; delim = delim_} )
579-
when delim = delim_ ->
580-
string_append ?comment c (concat b a ~delim)
581-
| ( String_append (a, {expression_desc = Str {txt = b; delim}}),
582-
String_append ({expression_desc = Str {txt = c; delim = delim_}}, d) )
583-
when delim = delim_ ->
584-
string_append ?comment (string_append a (concat b c ~delim)) d
585-
| Str {txt = a; delim}, Str {txt = b; delim = delim_} when delim = delim_ ->
586-
{(concat a b ~delim) with comment}
574+
| ( Str {txt = a; kind},
575+
String_append ({expression_desc = Str {txt = b; kind = kind_}}, c) )
576+
when kind = kind_ ->
577+
string_append ?comment (concat a b ~kind) c
578+
| ( String_append (c, {expression_desc = Str {txt = b; kind}}),
579+
Str {txt = a; kind = kind_} )
580+
when kind = kind_ ->
581+
string_append ?comment c (concat b a ~kind)
582+
| ( String_append (a, {expression_desc = Str {txt = b; kind}}),
583+
String_append ({expression_desc = Str {txt = c; kind = kind_}}, d) )
584+
when kind = kind_ ->
585+
string_append ?comment (string_append a (concat b c ~kind)) d
586+
| Str {txt = a; kind}, Str {txt = b; kind = kind_} when kind = kind_ ->
587+
{(concat a b ~kind) with comment}
587588
| _, _ -> {comment; expression_desc = String_append (e, el)}
588589

589590
let obj ?comment ?dup properties : t =
590591
{expression_desc = Object (dup, properties); comment}
591592

592-
let str_equal (txt0 : string) (delim0 : External_arg_spec.delim) txt1 delim1 =
593-
if delim0 = delim1 then
593+
let str_equal (txt0 : string) (kind0 : String_kind.t) txt1 kind1 =
594+
if kind0 = kind1 then
594595
if Ext_string.equal txt0 txt1 then Some true
595596
else if
596597
Ast_utf8_string.simple_comparison txt0
@@ -1242,7 +1243,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
12421243
let int_equal = float_equal
12431244

12441245
let tag_type = function
1245-
| Ast_untagged_variants.String s -> str s ~delim:DStarJ
1246+
| Ast_untagged_variants.String s -> str s ~kind:Verbatim
12461247
| Int i -> small_int i
12471248
| Float f -> float f
12481249
| BigInt i ->
@@ -1258,7 +1259,7 @@ let tag_type = function
12581259
| Untagged FunctionType -> str "function"
12591260
| Untagged StringType -> str "string"
12601261
| Untagged (InstanceType i) ->
1261-
str (Ast_untagged_variants.Instance.to_string i) ~delim:DNoQuotes
1262+
str (Ast_untagged_variants.Instance.to_string i) ~kind:RawJs
12621263
| Untagged ObjectType -> str "object"
12631264
| Untagged UnknownType ->
12641265
(* TODO: this should not happen *)
@@ -1280,7 +1281,7 @@ let rec emit_check (check : t Ast_untagged_variants.DynamicChecks.t) =
12801281
| IsInstanceOf (Array, x) -> is_array (emit_check x)
12811282
| IsInstanceOf (instance, x) ->
12821283
let instance_name = Ast_untagged_variants.Instance.to_string instance in
1283-
instanceof (emit_check x) (str instance_name ~delim:DNoQuotes)
1284+
instanceof (emit_check x) (str instance_name ~kind:RawJs)
12841285
| Not x -> not (emit_check x)
12851286
| Expr x -> x
12861287

@@ -1343,8 +1344,8 @@ let to_int32 ?comment (e : J.expression) : J.expression =
13431344

13441345
let string_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
13451346
match (e0.expression_desc, e1.expression_desc) with
1346-
| Str {txt = a0; delim = d0}, Str {txt = a1; delim = d1} -> (
1347-
match (cmp, str_equal a0 d0 a1 d1) with
1347+
| Str {txt = a0; kind = k0}, Str {txt = a1; kind = k1} -> (
1348+
match (cmp, str_equal a0 k0 a1 k1) with
13481349
| Ceq, Some b -> bool b
13491350
| Cneq, Some b -> bool (b = false)
13501351
| _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1)

compiler/core/js_exp_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ val pure_runtime_call :
9595

9696
val runtime_ref : string -> string -> t
9797

98-
val str : ?delim:J.delim -> ?comment:string -> string -> t
98+
val str : ?kind:J.string_kind -> ?comment:string -> string -> t
9999

100100
val ocaml_fun :
101101
?comment:string ->

compiler/core/lam.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ let switch lam (lam_switch : lambda_switch) : t =
430430

431431
let stringswitch (lam : t) cases default : t =
432432
match lam with
433-
| Lconst (Const_string {s; delim = None | Some DNoQuotes}) ->
433+
| Lconst (Const_string {s; kind = RawJs}) ->
434434
Ext_list.assoc_by_string cases s default
435435
| _ -> Lstringswitch (lam, cases, default)
436436

@@ -471,7 +471,7 @@ module Lift = struct
471471

472472
let bool b = if b then true_ else false_
473473

474-
let string s : t = Lconst (Const_string {s; delim = None})
474+
let string s : t = Lconst (Const_string {s; kind = Standard})
475475

476476
let char b : t = Lconst (Const_char b)
477477
end
@@ -488,7 +488,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
488488
Lift.int (Int32.of_float (float_of_string a))
489489
(* | Pnegfloat -> Lift.float (-. a) *)
490490
(* | Pabsfloat -> Lift.float (abs_float a) *)
491-
| Pstringlength, Const_string {s; delim = None} ->
491+
| Pstringlength, Const_string {s; kind = Standard} ->
492492
Lift.int (Int32.of_int (String.length s))
493493
(* | Pnegbint Pnativeint, ( (Const_nativeint i)) *)
494494
(* -> *)
@@ -537,11 +537,11 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
537537
| Psequor, Const_js_false, Const_js_true -> true_
538538
| Psequor, Const_js_false, Const_js_false -> false_
539539
| ( Pstringadd,
540-
Const_string {s = a; delim = None},
541-
Const_string {s = b; delim = None} ) ->
540+
Const_string {s = a; kind = Standard},
541+
Const_string {s = b; kind = Standard} ) ->
542542
Lift.string (a ^ b)
543543
| ( (Pstringrefs | Pstringrefu),
544-
Const_string {s = a; delim = None},
544+
Const_string {s = a; kind = Standard},
545545
Const_int {i = b} ) -> (
546546
try Lift.char (Char.code (String.get a (Int32.to_int b)))
547547
with _ -> default ())

compiler/core/lam_compile_const.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ and translate (x : Lam_constant.t) : J.expression =
6161
| Const_char i -> Js_of_lam_string.const_char i
6262
| Const_bigint (sign, i) -> E.bigint sign i
6363
| Const_float f -> E.float f (* TODO: preserve float *)
64-
| Const_string {s; delim = None | Some DNoQuotes} -> E.str s
65-
| Const_string {s; delim = Some delim} -> E.str ~delim s
64+
| Const_string {s; kind = RawJs} -> E.str s
65+
| Const_string {s; kind} -> E.str ~kind s
6666
| Const_pointer name -> E.str name
6767
| Const_block (tag, tag_info, xs) ->
6868
Js_of_lam_block.make_block NA tag_info (E.small_int tag)
@@ -79,4 +79,4 @@ and translate (x : Lam_constant.t) : J.expression =
7979
let translate_arg_cst (cst : External_arg_spec.cst) =
8080
match cst with
8181
| Arg_int_lit i -> E.int (Int32.of_int i)
82-
| Arg_string_lit (s, delim) -> E.str s ~delim
82+
| Arg_string_lit (s, kind) -> E.str s ~kind

compiler/core/lam_constant_convert.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
2727
| Const_base (Const_int i) -> Const_int {i = Int32.of_int i; comment = None}
2828
| Const_base (Const_char i) -> Const_char i
2929
| Const_base (Const_string (s, opt)) ->
30-
let delim = Ast_utf8_string_interp.parse_processed_delim opt in
31-
Const_string {s; delim}
30+
let kind = Ast_utf8_string_interp.parse_processed_delim opt in
31+
Const_string {s; kind = Option.value ~default:Standard kind}
3232
| Const_base (Const_float i) -> Const_float i
3333
| Const_base (Const_int32 i) -> Const_int {i; comment = None}
3434
| Const_base (Const_int64 _) -> assert false
@@ -59,7 +59,7 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
5959
if Ext_string.is_valid_hash_number name then
6060
Const_int {i = Ext_string.hash_number_as_i32_exn name; comment = None}
6161
else Const_pointer name)
62-
| Const_immstring s -> Const_string {s; delim = None}
62+
| Const_immstring s -> Const_string {s; kind = Standard}
6363
| Const_block (t, xs) -> (
6464
let tag = Lambda.tag_of_tag_info t in
6565
match t with
@@ -76,7 +76,7 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
7676
let tag_val : Lam_constant.t =
7777
if Ext_string.is_valid_hash_number s then
7878
Const_int {i = Ext_string.hash_number_as_i32_exn s; comment = None}
79-
else Const_string {s; delim = None}
79+
else Const_string {s; kind = Standard}
8080
in
8181
Const_block (tag, t, [tag_val; convert_constant value])
8282
| _ -> assert false))

compiler/core/lam_convert.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
184184
let tag_val : Lam_constant.t =
185185
if Ext_string.is_valid_hash_number s then
186186
Const_int {i = Ext_string.hash_number_as_i32_exn s; comment = None}
187-
else Const_string {s; delim = None}
187+
else Const_string {s; kind = Standard}
188188
in
189189
prim
190190
~primitive:(Pmakeblock (tag, info, mutable_flag))
@@ -465,7 +465,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
465465
| Lprim (Pgetglobal id, args, _) ->
466466
let args = Ext_list.map args convert_aux in
467467
if Ident.is_predef_exn id then
468-
Lam.const (Const_string {s = id.name; delim = None})
468+
Lam.const (Const_string {s = id.name; kind = Standard})
469469
else (
470470
may_depend may_depends (Lam_module_ident.of_ml ~dynamic_import id);
471471
assert (args = []);

compiler/core/lam_pass_lets_dce.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
6363
->
6464
Hash_ident.add subst v (simplif l1);
6565
simplif l2
66-
| _, Lconst (Const_string {s; delim = None}) ->
66+
| _, Lconst (Const_string {s; kind = Standard}) ->
6767
(* only "" added for later inlining *)
6868
Hash_ident.add string_table v s;
6969
Lam.let_ Alias v l1 (simplif l2)
@@ -112,7 +112,7 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
112112
| _ -> (
113113
let l1 = simplif l1 in
114114
match l1 with
115-
| Lconst (Const_string {s; delim = None}) ->
115+
| Lconst (Const_string {s; kind = Standard}) ->
116116
Hash_ident.add string_table v s;
117117
(* we need move [simplif lbody] later, since adding Hash does have side effect *)
118118
Lam.let_ Alias v l1 (simplif lbody)
@@ -127,7 +127,7 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
127127
let l1 = simplif l1 in
128128

129129
match (kind, l1) with
130-
| Strict, Lconst (Const_string {s; delim = None}) ->
130+
| Strict, Lconst (Const_string {s; kind = Standard}) ->
131131
Hash_ident.add string_table v s;
132132
Lam.let_ Alias v l1 (simplif l2)
133133
| _ -> Lam_util.refine_let ~kind v l1 (simplif l2))
@@ -157,7 +157,7 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
157157
let r' = simplif r in
158158
let opt_l =
159159
match l' with
160-
| Lconst (Const_string {s = ls; delim = None}) -> Some ls
160+
| Lconst (Const_string {s = ls; kind = Standard}) -> Some ls
161161
| Lvar i -> Hash_ident.find_opt string_table i
162162
| _ -> None
163163
in
@@ -166,13 +166,14 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
166166
| Some l_s -> (
167167
let opt_r =
168168
match r' with
169-
| Lconst (Const_string {s = rs; delim = None}) -> Some rs
169+
| Lconst (Const_string {s = rs; kind = Standard}) -> Some rs
170170
| Lvar i -> Hash_ident.find_opt string_table i
171171
| _ -> None
172172
in
173173
match opt_r with
174174
| None -> Lam.prim ~primitive:Pstringadd ~args:[l'; r'] loc
175-
| Some r_s -> Lam.const (Const_string {s = l_s ^ r_s; delim = None})))
175+
| Some r_s -> Lam.const (Const_string {s = l_s ^ r_s; kind = Standard}))
176+
)
176177
| Lglobal_module _ -> lam
177178
| Lprim {primitive; args; loc} ->
178179
Lam.prim ~primitive ~args:(Ext_list.map args simplif) loc

0 commit comments

Comments
 (0)