@@ -47,7 +47,6 @@ type pos = {
4747(* * Note the position is about code point *)
4848
4949type segment = { start : pos ; finish : pos ; kind : kind ; content : string }
50-
5150type segments = segment list
5251
5352type cxt = {
@@ -324,13 +323,23 @@ let concat_ident : Longident.t = Ldot (Lident "Pervasives", "^")
324323(* Longident.parse "Js.String.make" *)
325324let to_string_ident : Longident.t = Ldot (Ldot (Lident " Js" , " String2" ), " make" )
326325
327- let escaped_j_delimiter = " *j" (* not user level syntax allowed *)
328-
329- let unescaped_j_delimiter = " j"
326+ module Delim = struct
327+ let parse_processed = function
328+ | None -> Some J. DNone
329+ | Some "json" -> Some DJson
330+ | Some "*j" -> Some DStarJ
331+ | _ -> None
330332
331- let unescaped_js_delimiter = " js"
333+ let parse_unprocessed = function
334+ | "js" -> `string_interpolation
335+ | "j" -> `old_unsafe_interpolation
336+ | _ -> `no_interpolation
332337
333- let escaped = Some escaped_j_delimiter
338+ let escaped_j_delimiter = " *j" (* not user level syntax allowed *)
339+ let unescaped_j_delimiter = " j"
340+ let unescaped_js_delimiter = " js"
341+ let escaped = Some escaped_j_delimiter
342+ end
334343
335344let border = String. length " {j|"
336345
@@ -340,7 +349,7 @@ let aux loc (segment : segment) ~to_string_ident : Parsetree.expression =
340349 match kind with
341350 | String ->
342351 let loc = update border start finish loc in
343- Ast_compatible. const_exp_string content ?delimiter:escaped ~loc
352+ Ast_compatible. const_exp_string content ?delimiter:Delim. escaped ~loc
344353 | Var (soffset , foffset ) ->
345354 let loc =
346355 {
@@ -362,7 +371,7 @@ let concat_exp a_loc x ~(lhs : Parsetree.expression) : Parsetree.expression =
362371(* Invariant: the [lhs] is always of type string *)
363372let rec handle_segments loc (rev_segments : segment list ) =
364373 match rev_segments with
365- | [] -> Ast_compatible. const_exp_string ~loc " " ?delimiter:escaped
374+ | [] -> Ast_compatible. const_exp_string ~loc " " ?delimiter:Delim. escaped
366375 | [ segment ] -> aux loc segment ~to_string_ident (* string literal *)
367376 | { content = "" } :: rest -> handle_segments loc rest
368377 | a :: rest -> concat_exp loc a ~lhs: (handle_segments loc rest)
@@ -389,15 +398,20 @@ let transform_interp loc s =
389398 Location. raise_errorf ~loc: (update border start pos loc) " %a" pp_error error
390399
391400let transform (e : Parsetree.expression ) s delim : Parsetree.expression =
392- if Ext_string. equal delim unescaped_js_delimiter then
393- let js_str = Ast_utf8_string. transform e.pexp_loc s in
394- { e with pexp_desc = Pexp_constant (Pconst_string (js_str, escaped)) }
395- else if Ext_string. equal delim unescaped_j_delimiter then
396- transform_interp e.pexp_loc s
397- else e
401+ match Delim. parse_unprocessed delim with
402+ | `string_interpolation ->
403+ let js_str = Ast_utf8_string. transform e.pexp_loc s in
404+ {
405+ e with
406+ pexp_desc = Pexp_constant (Pconst_string (js_str, Delim. escaped));
407+ }
408+ | `old_unsafe_interpolation -> transform_interp e.pexp_loc s
409+ | `no_interpolation -> e
398410
399- let is_unicode_string opt = Ext_string. equal opt escaped_j_delimiter
411+ let is_unicode_string opt = Ext_string. equal opt Delim. escaped_j_delimiter
400412
401413let is_unescaped s =
402- Ext_string. equal s unescaped_j_delimiter
403- || Ext_string. equal s unescaped_js_delimiter
414+ Ext_string. equal s Delim. unescaped_j_delimiter
415+ || Ext_string. equal s Delim. unescaped_js_delimiter
416+
417+ let parse_processed_delim = Delim. parse_processed
0 commit comments