tabnas-xml 0.7.20

XML 1.0 grammar plugin for the tabnas parsing engine, with namespaces and entities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
; XML's embedding: any plain tree as an element tree, the shape XML's
; reader builds and its render writes, and back again. This is a library
; of definitions, each named `xml-...`, which a host links with its own
; program and with the render (alchemy's `compile_sources`). It has two
; entry points: `xml-embed` takes a plain tree's events to the element
; tree's, and `xml-unembed`, its reverse, takes an element tree's events
; back to the plain tree's. Nothing else here is the host's to call.
;
; The embedding:
;
; - The root value is the element `document`.
; - An object is an element with the attribute `type="object"`. Each of
;   its members, in order, is a child element `member` whose attribute
;   `name` is the member's key and whose own content is the member's
;   value. Every member is a `member` element, whatever its key: a key
;   may be any string, an element's name may not, and one spelling for
;   every member keeps the reverse to one rule, so no member is an
;   element named by its key.
; - An array is an element with the attribute `type="array"`. Each of
;   its elements, in order, is a child element `item` whose own content
;   is the element's value.
; - A string is its element's one child, character data. An empty
;   string is an element with the attribute `type="string"` and no
;   child, so that an element with no `type` always holds text.
; - A string holding a character XML cannot carry, one 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), is
;   its double-quoted JSON form, the text `quoted` makes, whose escapes
;   leave nothing XML cannot carry, in an element with the attributes
;   `type="string"` and `encoding="json"`.
; - A key XML cannot carry is written the same way: the member element's
;   `name` holds the key's double-quoted JSON form, and its attribute
;   `name-encoding="json"` says so.
; - A number is its character data, in an element with the attribute
;   `type="number"`: its lexeme, or the text JSON writes for it when it
;   has none, and XML Schema's `INF`, `-INF` or `NaN` for one that is not
;   finite, which JSON has no text for.
; - A boolean is `true` or `false`, in an element with the attribute
;   `type="boolean"`.
; - Null is an element with the attribute `type="null"` and no child.
;
; Every element's members are the reader's, in the reader's order, which
; the render holds a tree to: `name`; `localName`, the same name, since
; no name here has a prefix; `attributes`, an object; and `children`, an
; array. The attributes come in one order: `type` where the value has
; one, `encoding` where its text is encoded, and then, on a member,
; `name`, and `name-encoding` where the key is encoded. So `{"item": 1}`
; is written
;
;   <document type="object"><member type="number" name="item">1</member></document>
;
; `[1]` is written
;
;   <document type="array"><item type="number">1</item></document>
;
; and `{"k\u0000": "v\u0001"}`, a key and a string XML cannot carry, is
; written
;
;   <document type="object"><member type="string" encoding="json" name="&quot;k\u0000&quot;" name-encoding="json">"v\u0001"</member></document>
;
; The render writes nothing between two elements, neither white space nor
; a line break, and a text child as one run of character data, so the
; document it writes reads back as the element tree the embedding built.
;
; The reverse reads an element by its attributes and by its parent's
; `type`. The root element is the root value. Under an element of
; `type="object"` each child is a `member` element, whose `name` is the
; key of the member it holds, read with `unquoted` where its
; `name-encoding` is `json`, and under one of `type="array"` each child
; is an `item` element. Of the others, an element with no `type` holds
; one text, its string; `type="string"` holds one text or none, the
; empty string, and with `encoding="json"` one text, the string's
; double-quoted form, read with `unquoted`; `type="boolean"` holds `true`
; or `false`; `type="null"` holds nothing; and `type="number"` holds the
; number's text, which reads back as the number it spells, its lexeme
; kept: `INF`, `-INF` and `NaN` as the numbers that are not finite, and
; any other text by `number`. The attributes may come in any order, since
; XML gives them none.
;
; An element tree that is not an embedding fails with
; TARGET_VALUE_UNREPRESENTABLE, naming what was met: a root element not
; named `document`; a child of an object's element not named `member`,
; or of an array's not named `item`; a `localName` that is not the name;
; a `type` the embedding never writes; an `encoding` or a
; `name-encoding` other than `json`; an `encoding` on an element whose
; `type` is not `string`; an attribute other than those four, `name` or
; `name-encoding` on an element that is not a member, and an attribute
; given twice; a member without a `name`; text under an object's or an
; array's element; an element under a scalar's; a second child under a
; scalar's; a scalar's element without the text its type needs; a
; number's text holding a character no JSON number has; and a member the
; reader adds only for what the embedding never writes (`prefix`,
; `namespace`, `space`, `lang`). A tree that is not an element tree at
; all fails the same way, in the words the render's refusal uses. Two
; texts fail as the operator that reads them fails, with INPUT_INVALID,
; since alchemy reads no single character of a string and the reverse
; can test which characters a text holds but not their order: a number's
; text of a JSON number's characters that still spells no number (`1-2`),
; which `number` refuses, and an encoded text or name that is no
; double-quoted form, which `unquoted` refuses. Events no tree has fail
; with PROTOCOL_ORDER_ERROR, in both directions.
;
; Each direction's state is a stack with one marker per open container,
; so it grows with a document's nesting and never with its width or its
; length. The embedding keeps `:object` or `:array` for each open
; container of the plain tree, and, between a member's key and its value,
; the key on top, for the member element's `name`. Once the root value
; is written the state is `[:done]`, and nothing may follow it. The
; reverse keeps one marker for each open element, `[phase role kind]`:
; the phase says what the element has due next, as the render's markers
; do; the role is `:document`, `:item` or `:member`; and the kind is
; what the element holds, the `type` attribute's as a keyword
; (`:encoded` for a string of `encoding="json"`), or `:none` without a
; type. While the attributes are read the kind is what they have said so
; far, `[type encoding name name-encoding]`, each `:none` until its
; attribute is read, and when they end a member's key goes out and the
; kind is settled. Once the root element is read the state is `[:done]`.

; ---------------------------------------------------------------------
; The embedding
; ---------------------------------------------------------------------

; 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-embed-chars [[9 10] [13 13] [32 55295] [57344 65533] [65536 1114111]]

; The top of the stack: `:none` before the root value, `:done` after it,
; `:object` or `:array` for the open container, or the key of the member
; whose value is due.
def xml-embed-top [s]
  match (count s)
    case 0 :none
    case _ (top s)

; A member element's name attributes, after the attributes before them:
; the key in `name`, or, where XML cannot carry it, its double-quoted
; form there and `name-encoding="json"`; `:none` stands for no member.
def xml-embed-name [name head]
  match name
    case :none head
    case _
      if (chars-within xml-embed-chars name)
        push (scalar name) (push (key "name") head)
        push (scalar "json")
          push (key "name-encoding")
            push (scalar (quoted name)) (push (key "name") head)

; An element's attributes, after its head's events: `type` where the
; value has one, `encoding` where its text is encoded, and then a
; member's name; `:none` stands for none.
def xml-embed-attributes [type encoding name head]
  xml-embed-name name
    match [type encoding]
      case [:none _] head
      case [_ :none] (push (scalar type) (push (key "type") head))
      case _ (push (scalar encoding) (push (key "encoding") (push (scalar type) (push (key "type") head))))

; The events that open an element: its name, its localName, its
; attributes, and the start of its children.
def xml-embed-head [element type encoding name]
  push array-start
    push (key "children")
      push object-end
        xml-embed-attributes type encoding name
          [object-start (key "name") (scalar element) (key "localName") (scalar element)
            (key "attributes") object-start]

; A scalar's whole element when it has no child: the empty string and
; null.
def xml-embed-leaf [element type name]
  push object-end
    push array-end (xml-embed-head element type :none name)

; A scalar's whole element when its one child is its text.
def xml-embed-text [element type encoding name text]
  push object-end
    push array-end
      push (scalar text) (xml-embed-head element type encoding name)

; A number's text: its lexeme, or the text JSON writes for it, and XML
; Schema's spelling of one that is not finite.
def xml-embed-number [value]
  match (number-class value)
    case :finite (scalar-text csv-options value)
    case :infinity "INF"
    case :negative-infinity "-INF"
    case :nan "NaN"

; A string's element: its text where XML can carry it, and otherwise its
; double-quoted form, typed `string` and encoded `json`; the empty
; string has no child.
def xml-embed-string [element name value]
  match value
    case "" (xml-embed-leaf element "string" name)
    case _
      if (chars-within xml-embed-chars value)
        xml-embed-text element :none :none name value
        xml-embed-text element "string" "json" name (quoted value)

; A scalar's element, typed as the embedding types it.
def xml-embed-scalar [element name value]
  match (kind value)
    case :string (xml-embed-string element name value)
    case :number (xml-embed-text element "number" :none name (xml-embed-number value))
    case :boolean (xml-embed-text element "boolean" :none name (scalar-text csv-options value))
    case :null (xml-embed-leaf element "null" name)

; A container opens: the root's element, an item's, or the element of
; the member whose key is on top, which the container's marker replaces.
def xml-embed-open [s type marker]
  match (xml-embed-top s)
    case :none (transition [marker] (xml-embed-head "document" type :none :none))
    case :array (transition (push marker s) (xml-embed-head "item" type :none :none))
    case :object (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
    case name (transition (push marker (pop s)) (xml-embed-head "member" type :none name))

; A scalar: the root's whole element, after which the tree is done; an
; item's; or the element of the member whose key is on top, which comes
; off.
def xml-embed-value [s value]
  match (xml-embed-top s)
    case :none (transition [:done] (xml-embed-scalar "document" :none value))
    case :array (transition s (xml-embed-scalar "item" :none value))
    case :object (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
    case name (transition (pop s) (xml-embed-scalar "member" name value))

; A key, held on top until its value opens the member's element.
def xml-embed-key [s name]
  match (xml-embed-top s)
    case :object (transition (push name s) [])
    case :array (fail :protocol-order "the events hold a key inside an array, which a tree's never do")
    case :none (fail :protocol-order "the events begin with an end or a key, which a tree's never do")
    case _ (fail :protocol-order "the events hold a key where a value is due, which a tree's never do")

; A container's element closes, its children and then itself, and its
; marker comes off; the root's is the tree's end.
def xml-embed-close [s]
  let [rest (pop s)]
    match (count rest)
      case 0 (transition [:done] [array-end object-end])
      case _ (transition rest [array-end object-end])

; A container's end, which must be the open container's.
def xml-embed-end [s marker]
  match [(xml-embed-top s) marker]
    case [:object :object] (xml-embed-close s)
    case [:array :array] (xml-embed-close s)
    case [:none _] (fail :protocol-order "the events begin with an end or a key, which a tree's never do")
    case [:object _] (fail :protocol-order "the events end an array inside an object, which a tree's never do")
    case [:array _] (fail :protocol-order "the events end an object inside an array, which a tree's never do")
    case _ (fail :protocol-order "the events end a container after a key and before its value, which a tree's never do")

def xml-embed-event [s event]
  match event
    case object-start (xml-embed-open s "object" :object)
    case array-start (xml-embed-open s "array" :array)
    case (key name) (xml-embed-key s name)
    case (scalar value) (xml-embed-value s value)
    case object-end (xml-embed-end s :object)
    case array-end (xml-embed-end s :array)

def xml-embed-step [s event]
  match (xml-embed-top s)
    case :done (fail :protocol-order "the events hold more after the root value, which a tree's never do")
    case _ (xml-embed-event s event)

def xml-embed-finish [s]
  match (xml-embed-top s)
    case :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 embedding: a plain tree's events in, the element tree's out.
def xml-embed [input]
  as-events (scan-emit [] xml-embed-step xml-embed-finish (events input))

; ---------------------------------------------------------------------
; The reverse
; ---------------------------------------------------------------------

; The characters a JSON number's text may hold: `+`, `-`, `.`, the
; digits, `E` and `e`.
def xml-unembed-number-chars [[43 43] [45 46] [48 57] [69 69] [101 101]]

; The top of the stack: `:none` before the root element, `:done` after
; it, and otherwise the open element's marker.
def xml-unembed-top [s]
  match (count s)
    case 0 :none
    case _ (top s)

; The top marker replaced.
def xml-unembed-mark [marker s]
  push marker (pop s)

; A member met where an element has none of its own: one of the four out
; of its place, one the reader adds only for what an embedding never
; writes, or one no element has.
def xml-unembed-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 "prefix" (fail :unrepresentable "the element tree is not an embedding: an element has a prefix, which no element of an embedding has")
    case "namespace" (fail :unrepresentable "the element tree is not an embedding: an element has a namespace, which no element of an embedding has")
    case "space" (fail :unrepresentable "the element tree is not an embedding: an element has an xml:space, which no element of an embedding has")
    case "lang" (fail :unrepresentable "the element tree is not an embedding: an element has an xml:lang, which no element of an embedding has")
    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's `name` or `name-encoding`, which no other element has: its
; value is read in the phase named for it.
def xml-unembed-member-attribute [s role a phase name]
  match role
    case :member (transition (xml-unembed-mark [phase role a] s) [])
    case _
      fail :unrepresentable
        string-join ""
          ["the element tree is not an embedding: an element that is not a member has a " name
            " attribute, which only a member's has"]

; An attribute's name: `type` and `encoding` on any element, and `name`
; and `name-encoding` on a member, each once. The kind holds what the
; attributes have said so far, `[type encoding name name-encoding]`.
def xml-unembed-attribute [s role a name]
  match [name a]
    case ["type" [:none e n ne]] (transition (xml-unembed-mark [:type role a] s) [])
    case ["type" _] (fail :unrepresentable "the element tree is not an embedding: an element has two types")
    case ["encoding" [t :none n ne]] (transition (xml-unembed-mark [:encoding role a] s) [])
    case ["encoding" _] (fail :unrepresentable "the element tree is not an embedding: an element has two encodings")
    case ["name" [t e :none ne]] (xml-unembed-member-attribute s role a :key name)
    case ["name" _] (fail :unrepresentable "the element tree is not an embedding: a member element has two names")
    case ["name-encoding" [t e n :none]] (xml-unembed-member-attribute s role a :key-encoding name)
    case ["name-encoding" _] (fail :unrepresentable "the element tree is not an embedding: a member element has two name-encodings")
    case _
      fail :unrepresentable
        string-join ""
          ["the element tree is not an embedding: an element has the attribute " (quoted name)
            ", where the embedding writes type, encoding and, on a member, name and name-encoding"]

; A key, in the phase that awaits one: an element's next member, or an
; attribute's name.
def xml-unembed-key [s m name]
  match m
    case [:name-key role t]
      match name
        case "name" (transition (xml-unembed-mark [:name role t] 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 role t]
      match name
        case "localName" (transition (xml-unembed-mark [:local role t] s) [])
        case _ (xml-unembed-member name)
    case [:attributes-key role t]
      match name
        case "attributes" (transition (xml-unembed-mark [:attributes-start role t] s) [])
        case _ (xml-unembed-member name)
    case [:attribute-key role a] (xml-unembed-attribute s role a name)
    case [:children-key role t]
      match name
        case "children" (transition (xml-unembed-mark [:children-start role t] s) [])
        case _ (xml-unembed-member name)
    case [:end role t] (xml-unembed-member name)
    case [:children role t] (fail :protocol-order "the events hold a key inside an array, which a tree's never do")
    case [:filled role t] (fail :protocol-order "the events hold a key inside an array, which a tree's never do")
    case _ (fail :protocol-order "the events hold a key where a value is due, which a tree's never do")

; An element's name that is not the one its role gives it.
def xml-unembed-misnamed [role value]
  match (kind value)
    case :string
      match role
        case :document (fail :unrepresentable (string-join "" ["the element tree is not an embedding: the root element is named " (quoted value) ", where an embedding's is document"]))
        case :member (fail :unrepresentable (string-join "" ["the element tree is not an embedding: a child of an object's element is named " (quoted value) ", where a member's is member"]))
        case :item (fail :unrepresentable (string-join "" ["the element tree is not an embedding: a child of an array's element is named " (quoted value) ", where an item's is item"]))
    case _ (fail :unrepresentable "the tree is not an XML element: its name is not a string")

; The element's name, which its role decides: `document` for the root,
; `member` under an object's element and `item` under an array's.
def xml-unembed-name [s role t value]
  match [role value]
    case [:document "document"] (transition (xml-unembed-mark [:local-key role t] s) [])
    case [:member "member"] (transition (xml-unembed-mark [:local-key role t] s) [])
    case [:item "item"] (transition (xml-unembed-mark [:local-key role t] s) [])
    case _ (xml-unembed-misnamed role value)

; Its localName, which is the name, since no name of an embedding's has
; a prefix.
def xml-unembed-local [s role t value]
  match [role value]
    case [:document "document"] (transition (xml-unembed-mark [:attributes-key role t] s) [])
    case [:member "member"] (transition (xml-unembed-mark [:attributes-key role t] s) [])
    case [:item "item"] (transition (xml-unembed-mark [:attributes-key role t] s) [])
    case _
      match (kind value)
        case :string (fail :unrepresentable (string-join "" ["the element tree is not an embedding: an element's localName is " (quoted value) ", where it is the element's name"]))
        case _ (fail :unrepresentable "the tree is not an XML element: its localName is not a string")

; The `type` attribute's value, one of the six the embedding writes, kept
; as its text until the attributes end.
def xml-unembed-type [value]
  match value
    case "object" value
    case "array" value
    case "string" value
    case "number" value
    case "boolean" value
    case "null" value
    case _
      match (kind value)
        case :string (fail :unrepresentable (string-join "" ["the element tree is not an embedding: an element's type is " (quoted value) ", where the embedding writes object, array, string, number, boolean or null"]))
        case _ (fail :unrepresentable "the element tree is not an embedding: an element's type is not text")

; The value of `encoding` or `name-encoding`, which the embedding writes
; `json`, as `:json`; `what` names the attribute for a failure.
def xml-unembed-encoding [what value]
  match value
    case "json" :json
    case _
      match (kind value)
        case :string (fail :unrepresentable (string-join "" ["the element tree is not an embedding: " what " is " (quoted value) ", where the embedding writes json"]))
        case _ (fail :unrepresentable (string-join "" ["the element tree is not an embedding: " what " is not text"]))

; A member's `name`, its key or the key's double-quoted form, kept until
; the attributes end.
def xml-unembed-name-text [value]
  match (kind value)
    case :string value
    case _ (fail :unrepresentable "the element tree is not an embedding: a member's name is not text")

; An element's kind, settled when its attributes end: its type's keyword,
; `:encoded` for a string of `encoding="json"`, or `:none` without a
; type. An encoding on an element of any other type is refused.
def xml-unembed-kind [t e]
  match [t e]
    case ["string" :json] :encoded
    case [:none :json] (fail :unrepresentable "the element tree is not an embedding: an element has an encoding and no type, where an encoded string's element has the type string")
    case [_ :json] (fail :unrepresentable (string-join "" ["the element tree is not an embedding: an element of type " t " has an encoding, which only a string's element has"]))
    case ["object" _] :object
    case ["array" _] :array
    case ["string" _] :string
    case ["number" _] :number
    case ["boolean" _] :boolean
    case ["null" _] :null
    case _ :none

; The attributes' end: a member must have had its name, which goes out
; now, read with `unquoted` where its `name-encoding` is `json`, and the
; element's kind is settled.
def xml-unembed-attributes-end [s role a]
  match [role a]
    case [:member [t e :none ne]] (fail :unrepresentable "the element tree is not an embedding: a member element has no name attribute, where it holds its member's key")
    case [:member [t e n :none]] (transition (xml-unembed-mark [:children-key role (xml-unembed-kind t e)] s) [(key n)])
    case [:member [t e n ne]] (transition (xml-unembed-mark [:children-key role (xml-unembed-kind t e)] s) [(key (unquoted n))])
    case [_ [t e n ne]] (transition (xml-unembed-mark [:children-key role (xml-unembed-kind t e)] s) [])

; A number's text, read back as the number it spells, its lexeme kept:
; XML Schema's `INF`, `-INF` and `NaN` as the numbers that are not
; finite, and any other text by `number`, once it is known to hold only
; the characters a JSON number has.
def xml-unembed-number [value]
  match value
    case "INF" (number "Infinity")
    case "-INF" (number "-Infinity")
    case "NaN" (number "NaN")
    case "" (fail :unrepresentable "the element tree is not an embedding: a number's element holds no text, where it holds the number's")
    case _
      if (chars-within xml-unembed-number-chars value)
        number value
        fail :unrepresentable (string-join "" ["the element tree is not an embedding: a number's element holds " (quoted value) ", which is no number"])

; A scalar's text: its string where the element has no type or
; `type="string"`, the string its double-quoted form spells where it is
; encoded, `true` or `false` where it is a boolean, and the number it
; spells where it is a number.
def xml-unembed-scalar-text [s role t value]
  match t
    case :boolean
      match value
        case "true" (transition (xml-unembed-mark [:filled role t] s) [(scalar true)])
        case "false" (transition (xml-unembed-mark [:filled role t] s) [(scalar false)])
        case _ (fail :unrepresentable (string-join "" ["the element tree is not an embedding: a boolean's element holds " (quoted value) ", where it holds true or false"]))
    case :number (transition (xml-unembed-mark [:filled role t] s) [(scalar (xml-unembed-number value))])
    case :encoded (transition (xml-unembed-mark [:filled role t] s) [(scalar (unquoted value))])
    case _ (transition (xml-unembed-mark [:filled role t] s) [(scalar value)])

; A child that is text, which only a scalar's element holds.
def xml-unembed-text [s role t value]
  match t
    case :object (fail :unrepresentable "the element tree is not an embedding: an object's element holds text, where it holds member elements")
    case :array (fail :unrepresentable "the element tree is not an embedding: an array's element holds text, where it holds item elements")
    case :null (fail :unrepresentable "the element tree is not an embedding: a null's element holds text, where it holds nothing")
    case _
      match (kind value)
        case :string (xml-unembed-scalar-text s role t value)
        case _ (fail :unrepresentable "the element tree is not an embedding: a child is a number, a boolean or null, where text is a string")

; A scalar, in the phase that awaits it.
def xml-unembed-scalar [s m value]
  match m
    case [:name role t] (xml-unembed-name s role t value)
    case [:local role t] (xml-unembed-local s role t value)
    case [:type role [t e n ne]]
      transition (xml-unembed-mark [:attribute-key role [(xml-unembed-type value) e n ne]] s) []
    case [:encoding role [t e n ne]]
      transition (xml-unembed-mark [:attribute-key role [t (xml-unembed-encoding "an element's encoding" value) n ne]] s) []
    case [:key role [t e n ne]]
      transition (xml-unembed-mark [:attribute-key role [t e (xml-unembed-name-text value) ne]] s) []
    case [:key-encoding role [t e n ne]]
      transition (xml-unembed-mark [:attribute-key role [t e n (xml-unembed-encoding "a member's name-encoding" value)]] s) []
    case [:children role t] (xml-unembed-text s role t value)
    case [:filled role t] (fail :unrepresentable "the element tree is not an embedding: a scalar's element has a second child, where it holds one text")
    case [:attributes-start role t] (fail :unrepresentable "the tree is not an XML element: its attributes are not an object")
    case [:children-start role t] (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 child element, whose role its parent's type decides.
def xml-unembed-child [s t]
  match t
    case :object (transition (push [:name-key :member :none] s) [])
    case :array (transition (push [:name-key :item :none] s) [])
    case _ (fail :unrepresentable "the element tree is not an embedding: a scalar's element holds an element, where it holds its text")

; An attribute's value met as a container, where XML has text.
def xml-unembed-attribute-container []
  fail :unrepresentable "the tree is not an XML element: an attribute's value is an object or an array, where XML has text"

; An object: the attributes, whose reading starts with none said, or a
; child element.
def xml-unembed-object [s m]
  match m
    case [:attributes-start role t] (transition (xml-unembed-mark [:attribute-key role [:none :none :none :none]] s) [])
    case [:children role t] (xml-unembed-child s t)
    case [:filled role t] (fail :unrepresentable "the element tree is not an embedding: a scalar's element has a second child, where it holds one text")
    case [:name role t] (fail :unrepresentable "the tree is not an XML element: its name is not a string")
    case [:local role t] (fail :unrepresentable "the tree is not an XML element: its localName is not a string")
    case [:type role t] (xml-unembed-attribute-container)
    case [:encoding role t] (xml-unembed-attribute-container)
    case [:key role t] (xml-unembed-attribute-container)
    case [:key-encoding role t] (xml-unembed-attribute-container)
    case [:children-start role t] (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")

; The children's start, which opens the value an object's or an array's
; element holds.
def xml-unembed-open [s role t]
  match t
    case :object (transition (xml-unembed-mark [:children role t] s) [object-start])
    case :array (transition (xml-unembed-mark [:children role t] s) [array-start])
    case _ (transition (xml-unembed-mark [:children role t] s) [])

; An array: the children, and nothing else an element has.
def xml-unembed-array [s m]
  match m
    case [:children-start role t] (xml-unembed-open s role t)
    case [:children role t] (fail :unrepresentable "the tree is not an XML element: a child is an array, where XML has text or an element")
    case [:filled role t] (fail :unrepresentable "the tree is not an XML element: a child is an array, where XML has text or an element")
    case [:name role t] (fail :unrepresentable "the tree is not an XML element: its name is not a string")
    case [:local role t] (fail :unrepresentable "the tree is not an XML element: its localName is not a string")
    case [:type role t] (xml-unembed-attribute-container)
    case [:encoding role t] (xml-unembed-attribute-container)
    case [:key role t] (xml-unembed-attribute-container)
    case [:key-encoding role t] (xml-unembed-attribute-container)
    case [:attributes-start role t] (fail :unrepresentable "the tree is not an XML element: its attributes are not an object")
    case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")

; An element's end: its marker comes off, and the root element's is the
; tree's end.
def xml-unembed-close [s]
  let [rest (pop s)]
    match (count rest)
      case 0 (transition [:done] [])
      case _ (transition rest [])

; An object's end: the attributes', or the element's.
def xml-unembed-object-end [s m]
  match m
    case [:end role t] (xml-unembed-close s)
    case [:attribute-key role a] (xml-unembed-attributes-end s role a)
    case [:name-key role t] (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 role t] (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 role t] (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 role t] (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 [:children role t] (fail :protocol-order "the events end an object inside an array, which a tree's never do")
    case [:filled role t] (fail :protocol-order "the events end an object inside an array, 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")

; The children's end: the value an object's or an array's element holds
; closes, and a scalar's element with no child is the empty string or
; null, as its type says.
def xml-unembed-children-end [s role t]
  match t
    case :object (transition (xml-unembed-mark [:end role t] s) [object-end])
    case :array (transition (xml-unembed-mark [:end role t] s) [array-end])
    case :string (transition (xml-unembed-mark [:end role t] s) [(scalar "")])
    case :null (transition (xml-unembed-mark [:end role t] s) [(scalar null)])
    case :number (fail :unrepresentable "the element tree is not an embedding: a number's element holds no text, where it holds the number's")
    case :boolean (fail :unrepresentable "the element tree is not an embedding: a boolean's element holds no text, where it holds true or false")
    case :encoded (fail :unrepresentable "the element tree is not an embedding: an encoded string's element holds no text, where it holds the string's double-quoted form")
    case _ (fail :unrepresentable "the element tree is not an embedding: an element with no type holds no text, where it holds its string, and an empty string's has the type string")

; An array's end: the children's.
def xml-unembed-array-end [s m]
  match m
    case [:children role t] (xml-unembed-children-end s role t)
    case [:filled role t] (transition (xml-unembed-mark [:end role t] s) [])
    case _ (fail :protocol-order "the events end an array that is not open, which a tree's never do")

; The root element opens: the tree's root value.
def xml-unembed-root [event]
  match event
    case object-start (transition [[:name-key :document :none]] [])
    case array-start (fail :unrepresentable "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
    case (scalar value) (fail :unrepresentable "the tree is not an XML element: an element is an object whose members are name, localName, attributes and children")
    case _ (fail :protocol-order "the events begin with an end or a key, which a tree's never do")

def xml-unembed-event [s m event]
  match event
    case object-start (xml-unembed-object s m)
    case array-start (xml-unembed-array s m)
    case (key name) (xml-unembed-key s m name)
    case (scalar value) (xml-unembed-scalar s m value)
    case object-end (xml-unembed-object-end s m)
    case array-end (xml-unembed-array-end s m)

def xml-unembed-step [s event]
  match (xml-unembed-top s)
    case :done (fail :protocol-order "the events hold more after the root element, which a tree's never do")
    case :none (xml-unembed-root event)
    case m (xml-unembed-event s m event)

def xml-unembed-finish [s]
  match (xml-unembed-top s)
    case :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, which a tree's never do")

; The reverse: an element tree's events in, the plain tree's out.
def xml-unembed [input]
  as-events (scan-emit [] xml-unembed-step xml-unembed-finish (events input))