(** JavaScript string-literal representation and conversion utilities. A literal has two related values: [source] is the text between its source delimiters, including escape spelling, while [semantic] is the UTF-8 text observed at runtime after JavaScript escape decoding. Keeping them together prevents compiler passes from accidentally treating source spelling as a runtime value, or from emitting a semantic value without escaping it. Ordinary quoted strings and template segments have different grammars, so they are distinct abstract types. Values should be created through the constructors below so the source/semantic invariant holds. *) type string_literal (** An ordinary JavaScript string body and its semantic value. Parser error recovery may retain an invalid source spelling with an empty placeholder semantic value. *) type template_segment (** A validated JavaScript template segment and its semantic value. *) val string_source : string_literal -> string (** Return the source text between the literal's delimiters. *) val string_semantic : string_literal -> string (** Return the decoded runtime value. Invalid ordinary strings retained solely for parser recovery return the empty placeholder value. *) val template_source : template_segment -> string (** Return the source text between the template delimiters or interpolations. *) val template_semantic : template_segment -> string (** Return the decoded runtime value of a template segment. *) val is_valid_utf8 : string -> bool (** Whether a string consists entirely of valid UTF-8 scalar sequences. *) val replace_invalid_utf8 : string -> string (** Replace each malformed UTF-8 sequence with the Unicode replacement character. Use this for recovering source text intended for diagnostics. *) val normalize_semantic : string -> string (** Convert each malformed byte to the corresponding Unicode code point while preserving valid UTF-8 sequences. This retains the runtime value produced by the JavaScript string dumper for legacy and compiler-generated bytes. Valid UTF-8 is returned unchanged. *) val decode_js_escapes : string -> string option (** Decode the escape sequences in a JavaScript string-literal body into its semantic UTF-8 value. Returns [None] for malformed input or unpaired UTF-16 surrogates. *) val decode_js_template_escapes : string -> string option (** Decode the escape sequences in a JavaScript template segment into its semantic UTF-8 value. Literal CR and CRLF line endings are normalized to LF as required by JavaScript template-literal semantics. Returns [None] for malformed escapes, malformed UTF-8, or an unescaped interpolation opener. *) val decode_raw_template_source : string -> string (** Remove the escapes that protect the surrounding ReScript template syntax from JavaScript source embedded in a [raw], [ffi], or [re] extension. Backslashes belonging to the embedded JavaScript, such as [\\n], are preserved. *) val encode_raw_template_source : string -> string (** The inverse of [decode_raw_template_source]: the template source for the given JavaScript source. *) val encode_js_string : string -> string (** Encode a semantic UTF-8 string as a canonical JavaScript string-literal body. *) val string_from_source : string -> string_literal option (** Validate and decode an ordinary string-literal body. *) val string_from_semantic : string -> string_literal (** Construct an ordinary string payload with canonical source spelling. Malformed bytes are normalized with [normalize_semantic] first. *) val invalid_string_for_recovery : string -> string_literal (** Preserve the source of an invalid parser input after its diagnostic has been recorded. Its placeholder semantic value is the empty string. *) val template_from_source : string -> template_segment option (** Validate and decode one template segment. *) val template_from_semantic : string -> template_segment (** Construct a template segment with canonical source spelling. Malformed bytes are normalized with [normalize_semantic] first. *) val concat_template : template_segment list -> template_segment (** Concatenate template segments, preserving their combined source spelling when it still decodes to the combined semantic value and otherwise using a canonical spelling. Re-decoding is necessary because joining two valid segments can create syntax such as an interpolation opener at the boundary. *) val encode_char_source : int -> string (** Encode an integer as a canonical character-literal body. Non-scalar values use braced Unicode escape spelling so compiler-generated ghost patterns and legacy PPX output remain printable. *) val utf16_length : string -> int (** Return the number of UTF-16 code units in a semantic UTF-8 string, matching JavaScript's [String.length]. *) val code_point_at_utf16_index : string -> int -> int option (** Return the result of JavaScript's [String.codePointAt] for a UTF-16 code unit index into a semantic UTF-8 string. *)