@@ -62,20 +62,6 @@ type cxt = {
62
62
63
63
type exn + = Error of pos * pos * error
64
64
65
- let pp_error fmt err =
66
- Format. pp_print_string fmt
67
- @@
68
- match err with
69
- | Invalid_code_point -> " Invalid code point"
70
- | Unterminated_backslash -> " \\ ended unexpectedly"
71
- | Invalid_escape_code c -> " Invalid escape code: " ^ String. make 1 c
72
- | Invalid_hex_escape -> " Invalid \\ x escape"
73
- | Invalid_unicode_escape -> " Invalid \\ u escape"
74
- | Unterminated_variable -> " $ unterminated"
75
- | Unmatched_paren -> " Unmatched paren"
76
- | Invalid_syntax_of_var s ->
77
- " `" ^ s ^ " ' is not a valid syntax of interpolated identifer"
78
-
79
65
let valid_lead_identifier_char x =
80
66
match x with 'a' .. 'z' | '_' -> true | _ -> false
81
67
@@ -97,31 +83,6 @@ let valid_identifier s =
97
83
| ' ' | '\n' | '\t' -> true
98
84
| _ -> false *)
99
85
100
- (* *
101
- FIXME: multiple line offset
102
- if there is no line offset. Note {|{j||} border will never trigger a new line
103
- *)
104
- let update_position border ({ lnum; offset; byte_bol } : pos )
105
- (pos : Lexing.position ) =
106
- if lnum = 0 then { pos with pos_cnum = pos.pos_cnum + border + offset }
107
- (* When no newline, the column number is [border + offset] *)
108
- else
109
- {
110
- pos with
111
- pos_lnum = pos.pos_lnum + lnum;
112
- pos_bol = pos.pos_cnum + border + byte_bol;
113
- pos_cnum =
114
- pos.pos_cnum + border + byte_bol + offset
115
- (* when newline, the column number is [offset] *) ;
116
- }
117
-
118
- let update border (start : pos ) (finish : pos ) (loc : Location.t ) : Location.t =
119
- let start_pos = loc.loc_start in
120
- {
121
- loc with
122
- loc_start = update_position border start start_pos;
123
- loc_end = update_position border finish start_pos;
124
- }
125
86
126
87
(* * Note [Var] kind can not be mpty *)
127
88
let empty_segment { content } = Ext_string. is_empty content
@@ -308,21 +269,6 @@ let transform_test s =
308
269
check_and_transform 0 s 0 cxt;
309
270
List. rev cxt.segments
310
271
311
- (* * TODO: test empty var $() $ failure,
312
- Allow identifers x.A.y *)
313
-
314
- open Ast_helper
315
-
316
- (* * Longident.parse "Pervasives.^" *)
317
- let concat_ident : Longident.t = Ldot (Lident " Pervasives" , " ^" )
318
- (* FIXME: remove deps on `Pervasives` *)
319
-
320
- (* JS string concatMany *)
321
- (* Ldot (Ldot (Lident "Js", "String2"), "concat") *)
322
-
323
- (* Longident.parse "Js.String.make" *)
324
- let to_string_ident : Longident.t = Ldot (Ldot (Lident " Js" , " String2" ), " make" )
325
-
326
272
module Delim = struct
327
273
let parse_processed = function
328
274
| None -> Some External_arg_spec. DNone
@@ -332,102 +278,43 @@ module Delim = struct
332
278
333
279
type interpolation =
334
280
| Js (* string interpolation *)
335
- | J (* old unsafe interpolation *)
336
281
| Unrecognized (* no interpolation: delimiter not recognized *)
337
- let parse_unprocessed = function
282
+ let parse_unprocessed loc = function
338
283
| "js" -> Js
339
- | "j" -> J
284
+ | "j" ->
285
+ Location. raise_errorf ~loc
286
+ " The unsafe j`$(a)$(b)` interpolation was removed, use string template `${a}${b}` instead."
340
287
| _ -> Unrecognized
341
288
342
289
let escaped_j_delimiter = " *j" (* not user level syntax allowed *)
343
- let unescaped_j_delimiter = " j"
344
290
let unescaped_js_delimiter = " js"
345
291
let escaped = Some escaped_j_delimiter
346
292
end
347
293
348
- let border = String. length " {j|"
349
-
350
- let aux loc (segment : segment ) ~to_string_ident : Parsetree. expression =
351
- match segment with
352
- | { start; finish; kind; content } -> (
353
- match kind with
354
- | String ->
355
- let loc = update border start finish loc in
356
- Ast_compatible. const_exp_string content ?delimiter:Delim. escaped ~loc
357
- | Var (soffset , foffset ) ->
358
- let loc =
359
- {
360
- loc with
361
- loc_start = update_position (soffset + border) start loc.loc_start;
362
- loc_end = update_position (foffset + border) finish loc.loc_start;
363
- }
364
- in
365
- Ast_compatible. apply_simple ~loc
366
- (Exp. ident ~loc { loc; txt = to_string_ident })
367
- [ Exp. ident ~loc { loc; txt = Lident content } ])
368
-
369
- let concat_exp a_loc x ~(lhs : Parsetree.expression ) : Parsetree.expression =
370
- let loc = Bs_loc. merge a_loc lhs.pexp_loc in
371
- Ast_compatible. apply_simple ~loc
372
- (Exp. ident { txt = concat_ident; loc })
373
- [ lhs; aux loc x ~to_string_ident: (Longident. Lident " __unsafe_cast" ) ]
374
-
375
- (* Invariant: the [lhs] is always of type string *)
376
- let rec handle_segments loc (rev_segments : segment list ) =
377
- match rev_segments with
378
- | [] -> Ast_compatible. const_exp_string ~loc " " ?delimiter:Delim. escaped
379
- | [ segment ] -> aux loc segment ~to_string_ident (* string literal *)
380
- | { content = "" } :: rest -> handle_segments loc rest
381
- | a :: rest -> concat_exp loc a ~lhs: (handle_segments loc rest)
382
-
383
- let transform_interp loc s =
384
- let s_len = String. length s in
385
- let buf = Buffer. create (s_len * 2 ) in
386
- try
387
- let cxt : cxt =
388
- {
389
- segment_start = { lnum = 0 ; offset = 0 ; byte_bol = 0 };
390
- buf;
391
- s_len;
392
- segments = [] ;
393
- pos_lnum = 0 ;
394
- byte_bol = 0 ;
395
- pos_bol = 0 ;
396
- }
397
- in
398
-
399
- check_and_transform 0 s 0 cxt;
400
- handle_segments loc cxt.segments
401
- with Error (start , pos , error ) ->
402
- Location. raise_errorf ~loc: (update border start pos loc) " %a" pp_error error
403
-
404
294
let transform_exp (e : Parsetree.expression ) s delim : Parsetree.expression =
405
- match Delim. parse_unprocessed delim with
295
+ match Delim. parse_unprocessed e.pexp_loc delim with
406
296
| Js ->
407
297
let js_str = Ast_utf8_string. transform e.pexp_loc s in
408
298
{
409
299
e with
410
300
pexp_desc = Pexp_constant (Pconst_string (js_str, Delim. escaped));
411
301
}
412
- | J -> transform_interp e.pexp_loc s
413
302
| Unrecognized -> e
414
303
415
304
416
305
let transform_pat (p : Parsetree.pattern ) s delim : Parsetree.pattern =
417
- match Delim. parse_unprocessed delim with
306
+ match Delim. parse_unprocessed p.ppat_loc delim with
418
307
| Js ->
419
308
let js_str = Ast_utf8_string. transform p.ppat_loc s in
420
309
{
421
310
p with
422
311
ppat_desc = Ppat_constant (Pconst_string (js_str, Delim. escaped));
423
312
}
424
- | J (* No j interpolation on patterns *)
425
313
| Unrecognized -> p
426
314
427
315
let is_unicode_string opt = Ext_string. equal opt Delim. escaped_j_delimiter
428
316
429
317
let is_unescaped s =
430
- Ext_string. equal s Delim. unescaped_j_delimiter
431
- || Ext_string. equal s Delim. unescaped_js_delimiter
318
+ Ext_string. equal s Delim. unescaped_js_delimiter
432
319
433
320
let parse_processed_delim = Delim. parse_processed
0 commit comments