Skip to content

Commit 89e608e

Browse files
committed
Simplify string literal abstraction
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 55f58f3 commit 89e608e

4 files changed

Lines changed: 3 additions & 31 deletions

File tree

compiler/ml/ast_mapper_from0.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ let has_template_attr attrs =
8888
let remove_template_attr attrs =
8989
List.filter (fun ({Location.txt}, _) -> txt <> "res.template") attrs
9090

91-
let decode_js_string ~loc s =
92-
match String_literal.decode_js_escapes s with
93-
| Some s -> s
94-
| None -> Location.raise_errorf ~loc "Invalid string escape sequence"
95-
9691
let normalize_ppx_semantic_string semantic =
9792
let length = String.length semantic in
9893
let buffer = Buffer.create length in

compiler/ml/string_literal.ml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,6 @@ let template_from_source source : template_segment option =
240240
let template_from_semantic semantic : template_segment =
241241
Valid {source = encode_js_template semantic; semantic}
242242
243-
let string_as_template (literal : string_literal) : template_segment =
244-
match literal with
245-
| Valid {source; semantic} -> (
246-
match decode_js_template_escapes source with
247-
| Some decoded when decoded = semantic -> Valid {source; semantic}
248-
| _ -> template_from_semantic semantic)
249-
| Invalid_source _ -> template_from_semantic ""
250-
251243
let concat_template segments =
252244
let source = String.concat "" (List.map source segments) in
253245
let semantic = String.concat "" (List.map semantic segments) in

compiler/ml/string_literal.mli

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ type template_kind
33
type 'kind payload
44

55
type string_literal = string_kind payload
6-
(** A validated ordinary JavaScript string body and its semantic value. *)
6+
(** An ordinary JavaScript string body and its semantic value. Parser error
7+
recovery may retain an invalid source spelling with an empty placeholder
8+
semantic value. *)
79

810
type template_segment = template_kind payload
911
(** A validated JavaScript template segment and its semantic value. *)
@@ -52,10 +54,6 @@ val template_from_source : string -> template_segment option
5254
val template_from_semantic : string -> template_segment
5355
(** Construct a template segment with canonical source spelling. *)
5456

55-
val string_as_template : string_literal -> template_segment
56-
(** Convert an ordinary string payload to a template segment, preserving its
57-
source spelling when that spelling has the same template semantics. *)
58-
5957
val concat_template : template_segment list -> template_segment
6058
(** Concatenate template segments, preserving their combined source spelling
6159
when it still decodes to the combined semantic value and otherwise using a

tests/ounit_tests/ounit_string_literal_tests.ml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,6 @@ let suites =
407407
( "template segments reject interpolation openers" >:: fun _ ->
408408
assert_invalid_template "${value}";
409409
assert_template_decoded ~encoded:"\\${value}" ~expected:"${value}" );
410-
( "ordinary strings convert safely to template segments" >:: fun _ ->
411-
let preserved =
412-
String_literal.string_from_source {|\x61|}
413-
|> Option.get |> String_literal.string_as_template
414-
in
415-
OUnit.assert_equal ~printer:(Printf.sprintf "%S") {|\x61|}
416-
(String_literal.source preserved);
417-
let escaped_interpolation =
418-
String_literal.string_from_source "${value}"
419-
|> Option.get |> String_literal.string_as_template
420-
in
421-
OUnit.assert_equal ~printer:(Printf.sprintf "%S") "\\${value}"
422-
(String_literal.source escaped_interpolation) );
423410
( "ordinary literals become semantic strings" >:: fun _ ->
424411
assert_parsed_string ~source:{|\x61\n\uD83D\uDE00|}
425412
~expected_semantic:"a\n😀" );

0 commit comments

Comments
 (0)