; ZON's render: a tree's events written as one ZON (Zig Object Notation)
; document. This is a library of definitions, each named `zon-...`, which
; a host links with its own program (alchemy's `compile_sources`); its
; entry point is `zon-render`, and nothing else here is the host's to
; call.
;
; The profile: an object is a struct literal, `.{ .@"key" = value, ... }`,
; an array a tuple literal, `.{ value, ... }`, and an empty container of
; either kind `.{}`, which ZON reads as an empty tuple, so an empty object
; reads back as an empty array. Every field name is written in the quoted
; identifier form, `.@"name"`, whether or not it is a bare identifier. A
; string is double-quoted with Zig's escapes: `quoted` gives the JSON
; form, and the escapes Zig lacks are rewritten, `\u00XX` to the byte
; escape `\xXX`, or to the two bytes `\xc2\xXX` for U+0080 to U+009F,
; since `\x` writes a byte and the string is UTF-8; `\b` to `\x08`; `\f`
; to `\x0c`; `\ufffe` and `\uffff` to `\u{fffe}` and `\u{ffff}`. A number
; is its lexeme, or as the JSON renderer writes it when it has none; every
; JSON number is a Zig literal but `-0`, which ZON refuses as an ambiguous
; negative zero and is written `-0.0`; a non-finite one is `inf`, `-inf`
; or `nan`. The form is readable, as `zig fmt` writes it: one value per
; line, each ending in a comma, two spaces of indentation per level of
; nesting, and a container closed on its own line.
;
; A field name is written by a convention that keeps every name apart,
; since ZON has no identifier for some names, and the reader's own big
; integer takes the name `$big`. The empty name, which ZON has no
; identifier for (`.@""` is not one), is written as `$empty`. A name
; holding a character no ZON field name carries is written as `$json:`
; followed by the name's `quoted` form, which holds no such character:
; Zig's `@"..."` identifiers refuse a null byte and the empty name and
; nothing else, so that character is U+0000 (`chars-within` tells such a
; name). A name that begins with `$` is written with one more `$` in
; front: `$empty` as `$$empty`, `$big` as `$$big`, `$json:x` as
; `$$json:x`. Any other name is written as it is. The reverse, which
; reads each field name back: `$empty` is the empty name; `$json:` and a
; text is the name `unquoted` reads from the text; `$$` and a rest is
; `$` and the rest; any other name is as it is. No two names are written
; as one, so no struct the render writes holds a field twice.
;
; The reader has no big integer value: an integer literal no double holds
; exactly is built as the object `{"$big": "<digits>"}`, the digits after
; a minus sign when it is negative. So an object whose only member is
; `$big`, holding an integer as the reader spells one, is written as that
; integer: a string of the characters `0` to `9` and `-` alone
; (`chars-within`) that `number` reads (`is-number`), so a minus sign at
; most and first, and no leading zero, but not `-0`, which ZON refuses as
; an ambiguous negative zero. That is decided on the name as it is, before
; the convention above escapes it. The digits are held until the object
; ends, and an object with a second member is written as the struct it
; is, its first field `$big`, which the convention writes `$$big`; so is
; one whose `$big` holds anything else, such as the strings `1-2`, `01`,
; `-0` and `-`, which are written as strings.
;
; The events are a tree's: a struct holds each field once. A walked value
; is one by construction; a host that streams a parse must refuse a
; member the parse repeats, since the render writes what it is given and
; ZON forbids a repeated field.
;
; The state is the stack of open containers, one marker each, so it
; grows with a document's nesting and never with its width:
; `:object-open` and `:array-open` for a container whose `.{` is held
; until its first child or its end, so that an empty one is `.{}`;
; `:object` for a struct waiting for a field name, `:member` for one
; whose field name is written and waits for the value, and `:array` for
; a tuple waiting for an item; `:big-key` for an object whose first
; member is `$big` and waits for its value, and `[:big digits]` for one
; holding the digits, which its end writes as the integer. Once the root
; value is written the state is `[:root-done]`, and nothing may follow
; it. Events no tree has fail with PROTOCOL_ORDER_ERROR, saying so.
;
; Each value directly inside the root struct or tuple 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 opens the root, so a host's writer
; holds a record until its end, and a run that fails part way leaves
; whole records behind.
def zon-indent [s]
repeat (count s) " "
; The top marker, or `:none` before the root value.
def zon-top [s]
match (count s)
case 0 :none
case _ (top s)
; The top container under another marker.
def zon-mark [marker s]
push marker (pop s)
; A value completed: a struct waiting for its member's value waits for
; its next field name, and the root value, once written, is the document.
def zon-done [s]
match (count s)
case 0 [:root-done]
case _
match (zon-top s)
case :member (zon-mark :object s)
case :big-key (zon-mark :object s)
case _ s
def zon-key-due [s]
fail :protocol-order "the events hold a value where a field name is due, which a tree's never do"
; The state a value may begin in: anywhere but a struct waiting for a
; field name.
def zon-value [s]
match (zon-top s)
case :object-open (zon-key-due s)
case :object (zon-key-due s)
case [:big _] (zon-key-due s)
case _ s
; What comes before a value whose parent is the top of s: nothing at the
; root or after a field name's ` = `; the struct's opening and its `$big`
; field for the value of a `$big` that is not the reader's digits; the
; tuple's opening and the item's indentation for the first item of a held
; tuple; the indentation alone for a later item.
def zon-lead [s]
match (zon-top s)
case :none ""
case :member ""
case :big-key (concat ".{\n" (zon-indent s) (zon-key "$big") " = ")
case :array-open (concat ".{\n" (zon-indent s))
case :array (zon-indent s)
case _ (zon-key-due s)
; What comes after a value whose parent is the top of s: the line's end
; at the root, and a comma before it inside a container.
def zon-end [s]
match (count s)
case 0 "\n"
case _ ",\n"
; A held tuple is opened by its first item.
def zon-opened [s]
match (zon-top s)
case :array-open (zon-mark :array s)
case _ s
; A string in Zig's double-quoted form: `quoted`'s JSON form, protected,
; with its escapes rewritten.
def zon-string [text]
zon-escapes (zon-protect (quoted text))
; The JSON form with each `\\`, an escaped backslash, replaced by a
; sentinel no quoted string holds (`quoted` escapes U+0001), so that
; every `\` the rewrites meet begins an escape: an escaped backslash
; followed by the letters `u0085`, say, is never read as `\u0085`.
def zon-protect [json-form]
replace-text "\\\\" "\u0001" json-form
; The escapes Zig lacks, rewritten in a protected JSON form: the two C1
; ranges first, so that `\u008X` and `\u009X` become the two UTF-8 bytes
; `\xc2\x8X` and `\xc2\x9X`, then every other `\u00XX` to the byte `\xXX`,
; `\b` to `\x08`, `\f` to `\x0c`, the two noncharacters `\ufffe` and
; `\uffff` to Zig's `\u{fffe}` and `\u{ffff}`, and the sentinel back to
; `\\`.
def zon-escapes [protected]
replace-text "\u0001" "\\\\"
replace-text "\\uffff" "\\u{ffff}"
replace-text "\\ufffe" "\\u{fffe}"
replace-text "\\f" "\\x0c"
replace-text "\\b" "\\x08"
replace-text "\\u00" "\\x"
replace-text "\\u009" "\\xc2\\x9"
replace-text "\\u008" "\\xc2\\x8" protected
; A finite number by its text. ZON refuses `-0`, the one JSON lexeme that
; is not a Zig literal, as an ambiguous negative zero; `-0.0` is the
; float it means.
def zon-finite [text]
match text
case "-0" "-0.0"
case _ text
def zon-number [n]
match (number-class n)
case :finite (zon-finite (scalar-text csv-options n))
case :infinity "inf"
case :negative-infinity "-inf"
case :nan "nan"
def zon-scalar [value]
match (kind value)
case :null "null"
case :boolean
match value
case true "true"
case false "false"
case :number (zon-number value)
case _ (zon-string value)
; The characters a field name may hold in ZON: every one but U+0000.
def zon-name-chars [[1 1114111]]
; A field name, in the quoted identifier form.
def zon-key [name]
concat ".@" (zon-field name)
; The field a name is written as, in Zig's double-quoted form, by the
; convention above: the empty name as `$empty`; a name holding U+0000 as
; `$json:` and its quoted form; a name that begins with `$` with one more
; `$` in front; any other name as it is.
def zon-field [name]
match (length name)
case 0 (zon-string "$empty")
case _
match (chars-within zon-name-chars name)
case true (zon-escapes (zon-protect (zon-dollar (quoted name))))
case false (zon-string (string-join "" ["$json:" (quoted name)]))
; A name's JSON form with one more `$` after the opening quote when the
; name begins with `$`. The sentinel U+0002, which a quoted string never
; holds raw (`quoted` escapes it), marks where the form begins, so the
; `"$` that follows it is the opening quote and the name's first
; character, never an escaped quote and a `$` inside the name.
def zon-dollar [json-form]
replace-text "\u0002" ""
replace-text "\u0002\"$" "\"$$" (string-join "" ["\u0002" json-form])
; The characters of the reader's big integer digits: `-` and `0` to `9`.
def zon-digit-chars [[45 45] [48 57]]
; Whether a value is an integer as the reader writes a big integer's
; digits: a string, not empty, of the characters in `zon-digit-chars`
; alone, which `number` reads (`is-number`), so a minus sign at most and
; first, then `0` or digits that do not begin with `0`; and not `-0`,
; which ZON refuses as an ambiguous negative zero. Each is a ZON integer
; literal as it is.
def zon-big-digits [value]
match (kind value)
case :string
match (length value)
case 0 false
case _
match (chars-within zon-digit-chars value)
case false false
case true
match value
case "-0" false
case _ (is-number value)
case _ false
; A field name, written where the struct's fields go: the first opens the
; held struct, unless it is `$big`, whose value is waited for; a name
; after held digits opens the struct with `$big` first, its digits a
; string.
def zon-member [name s]
match (zon-top s)
case :object-open
match name
case "$big" (transition (zon-mark :big-key s) [])
case _ (transition (zon-mark :member s) [".{\n" (zon-indent s) (zon-key name) " = "])
case :object
transition (zon-mark :member s) [(zon-indent s) (zon-key name) " = "]
case [:big digits]
transition (zon-mark :member s) (zon-big-field digits name s)
case _ (fail :protocol-order "the events hold a field name where a value is due, which a tree's never do")
; The held digits as the struct's `$big` field, a string, then the field
; name after it. In the root struct the `$big` field is a value directly
; inside the root, so it ends a record before the next field begins.
def zon-big-field [digits name s]
match (count s)
case 1 [".{\n" (zon-indent s) (zon-key "$big") " = " (zon-string digits) ",\n" :record-end (zon-indent s) (zon-key name) " = "]
case _ [".{\n" (zon-indent s) (zon-key "$big") " = " (zon-string digits) ",\n" (zon-indent s) (zon-key name) " = "]
; The value of a `$big` that is an object's first member: the reader's
; digits are held until the object ends or another member begins; any
; other value is written as the struct's first field.
def zon-big [value s]
match (zon-big-digits value)
case true (transition (zon-mark [:big value] s) [])
case false (zon-to (zon-done s) [(zon-lead s) (zon-scalar value) (zon-end s)])
; The outputs of a step whose next state is s, with the end of a record
; after them when s is one level deep: the root opened, or a value
; directly inside it ended (or the root itself, where the mark is the
; document's end).
def zon-ended [s outputs]
match (count s)
case 1 (push :record-end outputs)
case _ outputs
; A step that moves to s, writing outputs, with the end of a record when
; s is one level deep.
def zon-to [s outputs]
transition s (zon-ended s outputs)
def zon-step [s event]
match (zon-top s)
case :root-done (fail :protocol-order "the events hold more after the root value, which a tree's never do")
case _ (zon-event s event)
def zon-event [s event]
match event
case object-start
zon-to (push :object-open (zon-opened (zon-value s))) [(zon-lead s)]
case array-start
zon-to (push :array-open (zon-opened (zon-value s))) [(zon-lead s)]
case (key name) (zon-member name s)
case (scalar value)
match (zon-top s)
case :big-key (zon-big value s)
case _
zon-to (zon-done (zon-opened (zon-value s))) [(zon-lead s) (zon-scalar value) (zon-end s)]
case object-end
match (zon-top s)
case :object-open
zon-to (zon-done (pop s)) [".{}" (zon-end (pop s))]
case :object
zon-to (zon-done (pop s)) [(zon-indent (pop s)) "}" (zon-end (pop s))]
case [:big digits]
zon-to (zon-done (pop s)) [digits (zon-end (pop s))]
case :member
fail :protocol-order "the events end a struct after a field name and before its value, which a tree's never do"
case :big-key
fail :protocol-order "the events end a struct after a field name and before its value, which a tree's never do"
case _
fail :protocol-order "the events end a struct that is not open, which a tree's never do"
case array-end
match (zon-top s)
case :array-open
zon-to (zon-done (pop s)) [".{}" (zon-end (pop s))]
case :array
zon-to (zon-done (pop s)) [(zon-indent (pop s)) "}" (zon-end (pop s))]
case _
fail :protocol-order "the events end a tuple that is not open, which a tree's never do"
def zon-finish [s]
match (zon-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 a container, which a tree's never do")
; The render: the tree's events in, the document's text out.
def zon-render [input]
join ""
scan-emit [] zon-step zon-finish (events input)