# Bounded writer
`LexPat.Writer` writes one XML 1.0 document as UTF-8 bytes from an element
tree. It is deliberately narrow: element names, ordered attribute pairs, ordered
child content and a caller-supplied output-byte limit. This page documents the
accepted input, the exact escapes, the byte budget and the typed refusals. Like
the rest of the Lean library API, the writer is not a supported integration
route; the bounded C ABI is the only supported integration route.
## API
```lean
namespace LexPat.Writer
structure Element where
name : String
attributes : Array (String × String) := #[]
children : Array Node := #[]
inductive Node where
| element (element : Element)
| text (data : String)
inductive Error where
| invalidName (name : String)
| invalidChar (scalar : Nat)
| duplicateAttribute (name : String)
| limitExceeded (limit : Nat)
def serialize (maxBytes : Nat) (root : Element) : Except Error ByteArray
```
`serialize` returns `.ok bytes` with the complete document or `.error e` with
one of the four typed refusals. A refusal returns no partial document.
## Document shape
The argument is always an `Element`, so a document has exactly one element root.
Text and nested elements occur only as child content: a text run cannot be a root
and cannot stand alone as a document.
* `Element.attributes` is an ordered array of `(name, value)` pairs, emitted in
that order. `Element.children` is an ordered array of `Node`, also emitted in
that order; nesting is preserved.
* A child is either a nested `Element` or a text run (`Node.text`).
* An element with no children is emitted self-closing, as ``; an element
with children uses a start tag, its children and an end tag.
* The output is UTF-8 XML 1.0. No XML declaration and no BOM are emitted.
For example, a root `root` with attributes `k="v"` and `j="w"` and children
text `t`, element `a` containing text `x`, and childless element `b` with
attribute `c="d"` is emitted as:
```xml
tx
```
## Text escaping
A text run is character data, not markup. Each source character is escaped at
most once: the text value `&"'` is emitted as `&"'`, because the
`&` of a reference-looking value is escaped like any other `&`, not resolved.
| input in text | emitted bytes |
| --- | --- |
| `&` (U+0026) | `&` |
| `<` (U+003C) | `<` |
| `>` (U+003E) | `>` |
| CR (U+000D) | `
` |
| TAB (U+0009) | literal TAB (`0x09`) |
| LF (U+000A) | literal LF (`0x0A`) |
| `"` (U+0022) | literal `"` (not escaped) |
| `'` (U+0027) | literal `'` (not escaped) |
A CR is always emitted as `
`, so a CRLF pair becomes `
` followed by a
literal LF. Tab and LF are not escaped in text.
For the text value `a&bd`, CR, LF, `e`, TAB, `f`:
```text
a&b<c>d
e f
```
The line break after `
` is the literal LF byte and the gap before `f` is
the literal TAB byte.
## Attribute escaping
Attribute values escape `&`, `<`, `"` and the three whitespace characters tab,
line feed and carriage return. Emitting those whitespace characters as
references is what keeps attribute-value normalization and line-end
normalization from changing the submitted value.
| input in an attribute value | emitted bytes |
| --- | --- |
| `&` (U+0026) | `&` |
| `<` (U+003C) | `<` |
| `"` (U+0022) | `"` |
| TAB (U+0009) | ` ` |
| LF (U+000A) | `
` |
| CR (U+000D) | `
` |
| `>` (U+003E) | literal `>` (not escaped) |
| `'` (U+0027) | literal `'` (not escaped) |
For example, the attribute value `1&2<3"4` then TAB, `5`, LF, `6`, CR, `7` on
attribute `a`, with `b="x>y"`, is emitted as:
```xml
```
`>` stays literal in an attribute value, as the `b` value shows.
## Byte budget
`maxBytes` bounds the whole serialized document in UTF-8 bytes, counting markup,
escapes and multi-byte scalars, not characters. The check `out.size +
chunk.size ≤ limit` runs before each append, and an escape or a multi-byte
scalar is appended as one chunk, so the limit is never satisfied by a prefix of
an escape sequence or a UTF-8 scalar.
An output exactly at the limit is accepted; one byte over is refused with
`.limitExceeded limit`, where `limit` is the caller-supplied `maxBytes`. A
refused call returns the error only, never a truncated document.
* `é` is 8 characters but 9 bytes (`é` is two UTF-8 bytes): accepted at
`maxBytes = 9`, refused at `8`.
* `&` is 12 bytes because the escaped `&` is five bytes: accepted at
`12`, refused at `11`.
* `serialize 0` on the empty element `` refuses with `.limitExceeded 0`.
## Refused inputs
| condition | error |
| --- | --- |
| an element or attribute name is empty or does not match the XML `Name` production | `.invalidName name` |
| a scalar in text or an attribute value is outside the XML 1.0 `Char` production | `.invalidChar scalar` |
| one element repeats an attribute name | `.duplicateAttribute name` |
| the next append would make the output exceed `maxBytes` | `.limitExceeded limit` |
An XML `Name` is a nonempty `NameStartChar` followed by zero or more
`NameChar`s. A name beginning with a digit, a space, a hyphen or a dot is
refused; a colon is a valid `NameStartChar` and `xmlns` is an ordinary
attribute name. Element and attribute names are both checked. A non-ASCII or
supplementary-plane name that satisfies `Name` is emitted as its UTF-8 bytes;
`é` and U+10000 are both valid names.
The XML 1.0 `Char` production is U+0009, U+000A, U+000D, U+0020–U+D7FF,
U+E000–U+FFFD and U+10000–U+10FFFF. NUL, the C0 controls other than tab, LF
and CR, U+FFFE and U+FFFF are refused wherever a text or attribute-value
character is checked; tab, LF and CR are legal characters and are escaped
according to the tables above.
A repeated attribute name is refused on the same element even when the values
differ; the same name may be used on different elements. Each error carries the
offending name, scalar or limit, and any refusal abandons the whole document.
## Unmodeled constructs
The tree can represent only elements and character data, so it cannot express
an XML declaration, BOM, comment, processing instruction, CDATA section,
DOCTYPE or DTD, entity declaration, entity reference, or multiple root
elements. Text that looks like markup or a reference is emitted as character
data, escaped once; the only references in the output are the writer's own
escapes.
A colon is an ordinary name character and `xmlns` is an ordinary attribute
name: no namespace resolution, prefix binding check or namespace-aware output
happens, and a name such as `:` alone is accepted as an XML `Name`. Whitespace
and indentation are emitted only as submitted text; the writer adds no
formatting.
## Evidence
`LexPat/Writer.lean` defines the tree, the error type, the name and character
predicates and the per-append limit checks. `tests/WriterCases.lean` asserts the
exact byte strings for text and attribute escaping, order and nesting, the
empty-element form, valid and refused names, valid and refused characters,
duplicate attributes, and the exact, plus-one and zero budget boundaries quoted
on this page.