Formatter.Logical.LiteralFormat
Source-text formatting of primitive literals: strings, chars, and hex integers. Pure string-to-string conversions; no LPT awareness.
This module is exposed only so that the unit tests can reach it — tests/ is
a separate application that depends on this package, so it can import nothing
that is not in exposed-modules. An application using the formatter has no
reason to call these; it goes through Formatter.prettyPrint, which applies
them for you.
Escape content for embedding in a single-quoted "..." literal: tab,
newline, carriage return, " and \ become their two-character escapes, and
any other control character (C0, or DEL) becomes \u{HEX}. Everything else is
emitted verbatim.
Escape content for embedding in a triple-quoted """...""" literal.
These strings still process \ as an escape introducer, so literal
backslashes must be doubled or the parser will misinterpret the next
character (e.g. \. is rejected as "invalid escape sequence"). But
newlines and tabs are preserved as themselves — the whole point of """
is that real newlines stand for newlines.
A double quote only needs escaping when it is part of a run of three or
more (which would terminate the literal, or embed a stray """). An
isolated " (or a run of one or two mid-content) is left verbatim. This
mirrors elm-format's escapeMultiQuote: an isolated quote in an embedded
template or fixture (e.g. Debug.log "u") is preserved as-is rather than
backslash-escaped.
The source text of a character literal, quotes included: 'a', '\n',
'\u{00e9}'. Printable ASCII is emitted as itself; tab, newline, carriage
return, ' and \ take their two-character escapes; everything else —
including every non-ASCII character — becomes a \u{HEX} escape.
The source text of a hex integer literal, sign included: 0x1F, or -0x1.
A negative can reach here: the parser folds a leading - into the value it
stores (Src.PInt { value = -1, isHex = True }), so the sign is placed in front
of the 0x rather than left to the digits.
The hex digits of n, uppercase and unpadded. Callers building a literal
want hexLiteral, which places the 0x after the sign; a negative reaching
here still gets a - rather than a wrong digit.