; 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 INPUT_INVALID, 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. A name is
; written as it is: the render has no character
; test, so a name no XML parser accepts, or a control character no XML
; document may hold, passes through unchanged. No XML declaration is
; written, and the document ends with one line feed.
;
; 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 INPUT_INVALID, saying so.
; 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)
; 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)
; 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-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-text value)
; A member met out of its place, or one no element has.
def xml-wrong-member [name]
match name
case "name" (fail "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 "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 "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 "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 "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 "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
case :name (fail "the tree is not an XML element: its name is not a string")
case :local (fail "the tree is not an XML element: its localName is not a string")
case :prefix (fail "the tree is not an XML element: its prefix is not a string")
case :namespace (fail "the tree is not an XML element: its namespace is not a string")
case :space (fail "the tree is not an XML element: its space is not a string")
case :lang (fail "the tree is not an XML element: its lang is not a string")
case :attribute (fail "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 "the tree is not an XML element: its attributes are not an object")
case :children-start (fail "the tree is not an XML element: its children are not an array")
case _ (fail "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, 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 "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) [" " 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 "the events hold a key where a value is due, which a tree's never do")
; A scalar, in the phase that awaits it: the 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))) ["<" 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-child value)])
case :child (transition 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) [])
case :first-child (fail "the tree is not an XML element: a child is an array, where XML has text or an element")
case :child (fail "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 "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
case :local-key (fail "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 "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 "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 "the events end an object inside an array, which a tree's never do")
case :child (fail "the events end an object inside an array, which a tree's never do")
case :none (fail "the events end an object that is not open, which a tree's never do")
case :attributes-start (fail "the events end an object that is not open, which a tree's never do")
case :children-start (fail "the events end an object that is not open, which a tree's never do")
case _ (fail "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 [])
; 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 "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 "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 "the events hold no value, where a tree's hold one")
case _ (fail "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)