; XML's render: a tree's events written as one XML 1.0 document. This is
; a library of definitions, each named `xml-...`, which a host links with
; its own program (alchemy's `compile_sources`); its entry point is
; `xml-render`, and nothing else here is the host's to call.
;
; The profile writes the reader's own tree and no other, as the
; translation design says a render of a format whose value has a shape
; of its own does: an element is an object whose members are `name` (a
; string, the tag), `localName` (read and not written), `attributes` (an
; object whose values are text) and `children` (an array in which a
; string is character data and an object is an element), in that order,
; which is the order the reader builds them in and a walked value keeps.
; The reader annotates an element it has resolved with up to four more
; members, `prefix`, `namespace`, `space` and `lang`, derived from the
; name and from the xmlns, xml:space and xml:lang attributes, which stay
; in `attributes`; the TypeScript reader places them before `children`
; and the Rust reader after it, so each is accepted anywhere after
; `name`, and none is written, since a reader derives them again. Any
; other member, a member out of its place, a root that is not an
; element, a name, localName, prefix, namespace, space or lang that is
; not a string, attributes that are not an object, children that are not
; an array, an attribute whose value is an object or an array, and a
; child that is an array fail with TARGET_VALUE_UNREPRESENTABLE, naming
; what was met.
;
; An element with no children is written `<name a="v"/>`, and otherwise
; `<name a="v">`, its children, `</name>`: the opening tag's `>` is held
; until the first child or the children's end decides between the two,
; as YAML's render holds a container's opening. Character data has `&`,
; `<` and `>` written as `&`, `<` and `>`, and an attribute's
; value, always double-quoted, has `&`, `<` and `"` written as `&`,
; `<` and `"`. A carriage return in either, and a tab or a line
; feed in an attribute's value, are written as the character references
; ` `, `	` and ` `, since a reader normalises a bare one
; away. A number or a boolean met where XML wants text, which a tree
; from another format can hold, is written as its text, and null as
; empty text. An empty text child is written as an empty CDATA section,
; `<![CDATA[]]>`, which the reader builds as the empty string child it
; was, where nothing at all would read back as no child. No XML
; declaration is written, and the document ends with one line feed.
;
; What XML cannot write at all fails with TARGET_VALUE_UNREPRESENTABLE,
; naming where it was met: a text child or an attribute's value holding
; a character outside XML 1.0's production Char (U+0000 to U+0008,
; U+000B, U+000C, U+000E to U+001F, U+FFFE and U+FFFF, which not even a
; character reference writes), and an element or attribute name that is
; empty or holds a character outside the production NameChar (fifth
; edition). A name's first character is not held to NameStartChar, the
; narrower set a name begins with, which leaves out `-`, `.`, the digits,
; U+00B7 and the combining marks: alchemy reads no single character of a
; string, so a name such as `1a` is written as it is. A plain tree
; reaches the render through the embedding (`embed.alc`), which writes
; what XML cannot carry by a convention instead.
;
; The state is a vector of markers that grows with the document's
; nesting and never with its width. Each open element holds two: its
; name, a string kept for the close tag, under a keyword for the phase
; the element is in, which says what is due next:
;
; :name-key its first member, `name`
; :name the name's value
; :local-key `localName`
; :local the local name's value
; :attributes-key `attributes`
; :attributes-start the start of the attributes object
; :attribute-key an attribute's name, or the attributes' end
; :attribute an attribute's value
; :children-key `children`
; :children-start the start of the children array
; :first-child the first child, or the children's end; the
; opening tag's `>` is held
; :child the next child, or the children's end
; :end the element's end
;
; Before the name is read the phase stands alone. While one of `prefix`,
; `namespace`, `space` and `lang` is being read, its keyword sits on top
; of the phase and is popped with its value. Nothing is kept per
; attribute or per child. Once the root element is written the state is
; `[:root-done]`, and nothing may follow it. Events no tree has fail
; with PROTOCOL_ORDER_ERROR, saying so.
;
; Each child of the root element, an element or a text, is a record of
; the text: the step that ends one answers alchemy's `:record-end` after
; its outputs, and so does the step that begins the root's children, so
; a host's writer holds a child until its end, and a run that fails part
; way leaves whole children behind.
; The top marker, or :none before the root element.
def xml-top [s]
match (count s)
case 0 :none
case _ (top s)
; The top marker replaced.
def xml-mark [marker s]
push marker (pop s)
; The characters XML 1.0 can carry, its production Char, as code-point
; ranges: a tab, a line feed, a carriage return, and every character from
; the space on but the surrogates, U+FFFE and U+FFFF.
def xml-char-ranges [[9 10] [13 13] [32 55295] [57344 65533] [65536 1114111]]
; The characters a name may hold, XML 1.0's production NameChar (fifth
; edition), as code-point ranges: the NameStartChar characters a name
; begins with, and `-`, `.`, the digits, U+00B7 and the combining marks
; U+0300 to U+036F, U+203F and U+2040.
def xml-name-ranges
[[45 46] [48 58] [65 90] [95 95] [97 122] [183 183] [192 214] [216 246] [248 893]
[895 8191] [8204 8205] [8255 8256] [8304 8591] [11264 12271] [12289 55295]
[63744 64975] [65008 65533] [65536 983039]]
; A scalar as text: a string as it is, a number by its lexeme, a boolean
; by its name, and null as nothing.
def xml-text [value]
match (kind value)
case :null ""
case _ (scalar-text csv-options value)
; Text XML can carry, as it is, or the failure naming where it was met.
def xml-carried [what text]
if (chars-within xml-char-ranges text)
text
fail :unrepresentable
string-join ""
["the tree holds " what " with a character XML cannot carry, one outside XML 1.0's "
"Char production (U+0000 to U+0008, U+000B, U+000C, U+000E to U+001F, U+FFFE or U+FFFF)"]
; A name XML can write, as it is, or the failure naming it: one or more
; characters, each one a name may hold. Whether the first is one a name
; may begin with is not tested, since alchemy reads no single character
; of a string.
def xml-name [what name]
match name
case "" (fail :unrepresentable (string-join "" ["the tree's " what " is empty, where XML has a name"]))
case _
if (chars-within xml-name-ranges name)
name
fail :unrepresentable
string-join "" ["the tree's " what " " (quoted name) " is not an XML name: it holds a character no name may hold"]
; Character data: `&`, `<` and `>` escaped, and a carriage return as a
; character reference, since a bare one is read as a line feed.
def xml-chars [text]
replace-text "\r" " "
replace-text ">" ">"
replace-text "<" "<"
replace-text "&" "&" text
; A text child: its character data, or an empty CDATA section when the
; text is empty, since the reader builds an empty string child from one
; and no child from nothing.
def xml-child [value]
let [text (xml-carried "a text child" (xml-text value))]
match text
case "" "<![CDATA[]]>"
case _ (xml-chars text)
; An attribute's value, between double quotes: `&`, `<` and `"` escaped,
; and a tab, a line feed and a carriage return as character references,
; since attribute-value normalisation reads a bare one as a space.
def xml-attr [value]
replace-text "\r" " "
replace-text "\n" " "
replace-text "\t" "	"
replace-text "\"" """
replace-text "<" "<"
replace-text "&" "&" (xml-carried "an attribute's value" (xml-text value))
; A member met out of its place, or one no element has.
def xml-wrong-member [name]
match name
case "name" (fail :unrepresentable "the tree is not an XML element: its member name is out of place, where an element's members are name, localName, attributes and children, in that order")
case "localName" (fail :unrepresentable "the tree is not an XML element: its member localName is out of place, where an element's members are name, localName, attributes and children, in that order")
case "attributes" (fail :unrepresentable "the tree is not an XML element: its member attributes is out of place, where an element's members are name, localName, attributes and children, in that order")
case "children" (fail :unrepresentable "the tree is not an XML element: its member children is out of place, where an element's members are name, localName, attributes and children, in that order")
case _ (fail :unrepresentable "the tree is not an XML element: it has a member that is not one of name, localName, attributes and children")
; A member the reader derives from the name and the attributes, read and
; not written: its keyword goes on top of the phase until its value is
; read. Any other member here is not an element's.
def xml-annotation [name s]
match name
case "prefix" (transition (push :prefix s) [])
case "namespace" (transition (push :namespace s) [])
case "space" (transition (push :space s) [])
case "lang" (transition (push :lang s) [])
case _ (xml-wrong-member name)
; A value met where the tree's shape has no place for it: one that is
; not a string where a string is due, a scalar where a container is due,
; a value where a key is due, or a root that is not an element.
def xml-misplaced [s]
match (xml-top s)
case :none (fail :unrepresentable "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
case :name (fail :unrepresentable "the tree is not an XML element: its name is not a string")
case :local (fail :unrepresentable "the tree is not an XML element: its localName is not a string")
case :prefix (fail :unrepresentable "the tree is not an XML element: its prefix is not a string")
case :namespace (fail :unrepresentable "the tree is not an XML element: its namespace is not a string")
case :space (fail :unrepresentable "the tree is not an XML element: its space is not a string")
case :lang (fail :unrepresentable "the tree is not an XML element: its lang is not a string")
case :attribute (fail :unrepresentable "the tree is not an XML element: an attribute's value is an object or an array, where XML has text")
case :attributes-start (fail :unrepresentable "the tree is not an XML element: its attributes are not an object")
case :children-start (fail :unrepresentable "the tree is not an XML element: its children are not an array")
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
; A member whose value is read and not written, which must still be a
; string: the state after it, or the failure naming it.
def xml-string [value next s]
match (kind value)
case :string (transition next [])
case _ (xml-misplaced s)
; A key, in the phase that awaits it: an element's next member, or an
; attribute's name, an XML name, written with its opening quote.
def xml-key [name s]
match (xml-top s)
case :name-key
match name
case "name" (transition (xml-mark :name s) [])
case _ (fail :unrepresentable "the tree is not an XML element: its first member is not name, where an element's members are name, localName, attributes and children")
case :local-key
match name
case "localName" (transition (xml-mark :local s) [])
case _ (xml-annotation name s)
case :attributes-key
match name
case "attributes" (transition (xml-mark :attributes-start s) [])
case _ (xml-annotation name s)
case :attribute-key (transition (xml-mark :attribute s) [" " (xml-name "attribute name" name) "=\""])
case :children-key
match name
case "children" (transition (xml-mark :children-start s) [])
case _ (xml-annotation name s)
case :end (xml-annotation name s)
case _ (fail :protocol-order "the events hold a key where a value is due, which a tree's never do")
; The outputs of a step that ends a child of the element whose markers
; are s, with the end of a record after them when the element is the
; root: its child is written whole.
def xml-ended [s outputs]
match (count s)
case 2 (push :record-end outputs)
case _ outputs
; A scalar, in the phase that awaits it: the name, an XML name, which
; opens the tag and goes under the phase for the close tag; an
; attribute's value, closed with its quote; a text child, after the held
; `>` when it is the first child; or a member that is read and not
; written.
def xml-scalar [value s]
match (xml-top s)
case :name
match (kind value)
case :string (transition (push :local-key (push value (pop s))) ["<" (xml-name "element name" value)])
case _ (xml-misplaced s)
case :local (xml-string value (xml-mark :attributes-key s) s)
case :attribute (transition (xml-mark :attribute-key s) [(xml-attr value) "\""])
case :first-child (transition (xml-mark :child s) (xml-ended s [">" (xml-child value)]))
case :child (transition s (xml-ended s [(xml-child value)]))
case :prefix (xml-string value (pop s) s)
case :namespace (xml-string value (pop s) s)
case :space (xml-string value (pop s) s)
case :lang (xml-string value (pop s) s)
case _ (xml-misplaced s)
; An object: the root element, the attributes, or a child element, which
; closes its parent's opening tag when it is the first child.
def xml-object [s]
match (xml-top s)
case :none (transition (push :name-key s) [])
case :attributes-start (transition (xml-mark :attribute-key s) [])
case :first-child (transition (push :name-key (xml-mark :child s)) [">"])
case :child (transition (push :name-key s) [])
case _ (xml-misplaced s)
; An array: the children, and nothing else an element has.
def xml-array [s]
match (xml-top s)
case :children-start (transition (xml-mark :first-child s) (xml-ended s []))
case :first-child (fail :unrepresentable "the tree is not an XML element: a child is an array, where XML has text or an element")
case :child (fail :unrepresentable "the tree is not an XML element: a child is an array, where XML has text or an element")
case _ (xml-misplaced s)
; An object's end: the attributes', after which the children are due,
; or an element's, whose two markers come off; the root's end is the
; document's.
def xml-object-end [s]
match (xml-top s)
case :attribute-key (transition (xml-mark :children-key s) [])
case :end (xml-close s)
case :name-key (fail :unrepresentable "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
case :local-key (fail :unrepresentable "the tree is not an XML element: it has no localName, where an element's members are name, localName, attributes and children")
case :attributes-key (fail :unrepresentable "the tree is not an XML element: it has no attributes, where an element's members are name, localName, attributes and children")
case :children-key (fail :unrepresentable "the tree is not an XML element: it has no children, where an element's members are name, localName, attributes and children")
case :first-child (fail :protocol-order "the events end an object inside an array, which a tree's never do")
case :child (fail :protocol-order "the events end an object inside an array, which a tree's never do")
case :none (fail :protocol-order "the events end an object that is not open, which a tree's never do")
case :attributes-start (fail :protocol-order "the events end an object that is not open, which a tree's never do")
case :children-start (fail :protocol-order "the events end an object that is not open, which a tree's never do")
case _ (fail :protocol-order "the events end an object after a key and before its value, which a tree's never do")
def xml-close [s]
let [s2 (pop (pop s))]
match (count s2)
case 0 (transition [:root-done] ["\n"])
case _ (transition s2 (xml-ended s2 []))
; The children's end: the close tag, or the self-closing tag's end when
; there was no child.
def xml-array-end [s]
match (xml-top s)
case :first-child (transition (xml-mark :end s) ["/>"])
case :child (transition (xml-mark :end s) ["</" (top (pop s)) ">"])
case _ (fail :protocol-order "the events end an array that is not open, which a tree's never do")
def xml-step [s event]
match (xml-top s)
case :root-done (fail :protocol-order "the events hold more after the root element, which a tree's never do")
case _ (xml-event s event)
def xml-event [s event]
match event
case object-start (xml-object s)
case array-start (xml-array s)
case (key name) (xml-key name s)
case (scalar value) (xml-scalar value s)
case object-end (xml-object-end s)
case array-end (xml-array-end s)
def xml-finish [s]
match (xml-top s)
case :root-done []
case :none (fail :protocol-order "the events hold no value, where a tree's hold one")
case _ (fail :protocol-order "the events ended inside an element")
; The render: the tree's events in, the document's text out.
def xml-render [input]
join ""
scan-emit [] xml-step xml-finish (events input)