Skip to content

Refactor delimiters: processed and unprocessed. #5645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ and for_ident = ident
and for_direction = Js_op.direction_flag
and property_map = (property_name * expression) list
and length_object = Js_op.length_object
and delim = | DNone | DJ | DJS | DStarJ | DJson
and delim = | DNone | DStarJ | DJson

and expression_desc =
| Length of expression * length_object
Expand Down
10 changes: 6 additions & 4 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,12 @@ and expression_desc cxt ~(level : int) f x : cxt =
(*TODO --
when utf8-> it will not escape '\\' which is definitely not we want
*)
if delim = DJ || delim = DStarJ then
P.string f ("\"" ^ txt ^ "\"")
else if delim = DJson then P.string f txt
else Js_dump_string.pp_string f txt;
let () =
match delim with
| DStarJ -> P.string f ("\"" ^ txt ^ "\"")
| DJson -> P.string f txt
| DNone -> Js_dump_string.pp_string f txt
in
cxt
| Raw_js_code { code = s; code_info = info } -> (
match info with
Expand Down
1 change: 0 additions & 1 deletion jscomp/core/js_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


open J

let[@inline] unknown _self _ = _self
Expand Down
1 change: 0 additions & 1 deletion jscomp/core/js_record_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


open J

let[@inline] unknown _ st _ = st
Expand Down
1 change: 0 additions & 1 deletion jscomp/core/js_record_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


open J

let unknown _ _ = ()
Expand Down
1 change: 0 additions & 1 deletion jscomp/core/js_record_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


open J

let[@inline] unknown _ x = x
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_compile_const.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ and translate (x : Lam_constant.t) : J.expression =
(* https://github.com/google/closure-library/blob/master/closure%2Fgoog%2Fmath%2Flong.js *)
| Const_float f -> E.float f (* TODO: preserve float *)
| Const_string { s; unicode = false } -> E.str s
| Const_string { s; unicode = true } -> E.str ~delim:DJ s
| Const_string { s; unicode = true } -> E.str ~delim:DStarJ s
| Const_pointer name -> E.str name
| Const_block (tag, tag_info, xs) ->
Js_of_lam_block.make_block NA tag_info (E.small_int tag)
Expand Down
10 changes: 2 additions & 8 deletions jscomp/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,6 @@ type as_const_payload = Int of int | Str of string * J.delim

let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
let st = ref None in
let process_delim = function
| None -> Some J.DNone
| Some "json" -> Some DJson
| Some "*j" -> Some DStarJ
| _ -> None
in
Ext_list.iter attrs (fun (({ txt; loc }, payload) as attr) ->
match txt with
| "bs.as" | "as" ->
Expand All @@ -311,9 +305,9 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
_;
};
]
when process_delim delim_ <> None -> (
when Ast_utf8_string_interp.parse_processed_delim delim_ <> None -> (
let delim =
match process_delim delim_ with
match Ast_utf8_string_interp.parse_processed_delim delim_ with
| None -> assert false
| Some delim -> delim
in
Expand Down
48 changes: 31 additions & 17 deletions jscomp/frontend/ast_utf8_string_interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type pos = {
(** Note the position is about code point *)

type segment = { start : pos; finish : pos; kind : kind; content : string }

type segments = segment list

type cxt = {
Expand Down Expand Up @@ -324,13 +323,23 @@ let concat_ident : Longident.t = Ldot (Lident "Pervasives", "^")
(* Longident.parse "Js.String.make" *)
let to_string_ident : Longident.t = Ldot (Ldot (Lident "Js", "String2"), "make")

let escaped_j_delimiter = "*j" (* not user level syntax allowed *)

let unescaped_j_delimiter = "j"
module Delim = struct
let parse_processed = function
| None -> Some J.DNone
| Some "json" -> Some DJson
| Some "*j" -> Some DStarJ
| _ -> None

let unescaped_js_delimiter = "js"
let parse_unprocessed = function
| "js" -> `string_interpolation
| "j" -> `old_unsafe_interpolation
| _ -> `no_interpolation

let escaped = Some escaped_j_delimiter
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
let unescaped_j_delimiter = "j"
let unescaped_js_delimiter = "js"
let escaped = Some escaped_j_delimiter
end

let border = String.length "{j|"

Expand All @@ -340,7 +349,7 @@ let aux loc (segment : segment) ~to_string_ident : Parsetree.expression =
match kind with
| String ->
let loc = update border start finish loc in
Ast_compatible.const_exp_string content ?delimiter:escaped ~loc
Ast_compatible.const_exp_string content ?delimiter:Delim.escaped ~loc
| Var (soffset, foffset) ->
let loc =
{
Expand All @@ -362,7 +371,7 @@ let concat_exp a_loc x ~(lhs : Parsetree.expression) : Parsetree.expression =
(* Invariant: the [lhs] is always of type string *)
let rec handle_segments loc (rev_segments : segment list) =
match rev_segments with
| [] -> Ast_compatible.const_exp_string ~loc "" ?delimiter:escaped
| [] -> Ast_compatible.const_exp_string ~loc "" ?delimiter:Delim.escaped
| [ segment ] -> aux loc segment ~to_string_ident (* string literal *)
| { content = "" } :: rest -> handle_segments loc rest
| a :: rest -> concat_exp loc a ~lhs:(handle_segments loc rest)
Expand All @@ -389,15 +398,20 @@ let transform_interp loc s =
Location.raise_errorf ~loc:(update border start pos loc) "%a" pp_error error

let transform (e : Parsetree.expression) s delim : Parsetree.expression =
if Ext_string.equal delim unescaped_js_delimiter then
let js_str = Ast_utf8_string.transform e.pexp_loc s in
{ e with pexp_desc = Pexp_constant (Pconst_string (js_str, escaped)) }
else if Ext_string.equal delim unescaped_j_delimiter then
transform_interp e.pexp_loc s
else e
match Delim.parse_unprocessed delim with
| `string_interpolation ->
let js_str = Ast_utf8_string.transform e.pexp_loc s in
{
e with
pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped));
}
| `old_unsafe_interpolation -> transform_interp e.pexp_loc s
| `no_interpolation -> e

let is_unicode_string opt = Ext_string.equal opt escaped_j_delimiter
let is_unicode_string opt = Ext_string.equal opt Delim.escaped_j_delimiter

let is_unescaped s =
Ext_string.equal s unescaped_j_delimiter
|| Ext_string.equal s unescaped_js_delimiter
Ext_string.equal s Delim.unescaped_j_delimiter
|| Ext_string.equal s Delim.unescaped_js_delimiter

let parse_processed_delim = Delim.parse_processed
6 changes: 1 addition & 5 deletions jscomp/frontend/ast_utf8_string_interp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type pos = { lnum : int; offset : int; byte_bol : int }
(** Note the position is about code point *)

type segment = { start : pos; finish : pos; kind : kind; content : string }

type segments = segment list

type cxt = {
Expand All @@ -55,11 +54,8 @@ type cxt = {
type exn += Error of pos * pos * error

val empty_segment : segment -> bool

val transform_test : string -> segment list

val transform : Parsetree.expression -> string -> string -> Parsetree.expression

val is_unicode_string : string -> bool

val is_unescaped : string -> bool
val parse_processed_delim : string option -> J.delim option
3 changes: 2 additions & 1 deletion jscomp/frontend/bs_ast_invariant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ let check_constant loc kind (const : Parsetree.constant) =
if Ast_utf8_string_interp.is_unescaped s then
Bs_warnings.error_unescaped_delimiter loc s
| `pat ->
if s = "j" then
if Ast_utf8_string_interp.parse_processed_delim (Some s) = Some DStarJ
then
Location.raise_errorf ~loc
"Unicode string is not allowed in pattern match")
| Pconst_integer (s, None) -> (
Expand Down
4 changes: 2 additions & 2 deletions jscomp/main/builtin_cmi_datasets.ml

Large diffs are not rendered by default.

Loading