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
//! The spec of the library's `transduce-tree`, for routers that run it
//! natively.
//!
//! `transduce-tree spec input` (`stdlib/transduce.alc`) makes a tree's
//! JSON events of a grammar's rule passes as `spec` says: a record keyed
//! by rule name, each entry a record of what that rule's passes do. The
//! library's text is the specification. A host's routers may run it
//! natively ([`crate::shared::Routers::transduce_tree`]), and the lowering
//! asks them to only when the program's spec has the standard shape
//! [`TreeSpec`] holds: every entry a record whose `:container` is
//! `:object` or `:array`, whose `:key` and `:member` are each a string or
//! a token's index, whose `:scalar` is a token's index or `:value`, and
//! whose `:text` is a boolean, each of the five left out at will. A spec
//! of any other shape runs the library's text, which says what it does
//! with it.
//!
//! What a native must do is what the text does, pass for pass:
//!
//! - An open pass of a rule the spec names announces its `:key` first:
//! the string, or the `:val` of the pass's token at that index, nothing
//! when the pass consumed no such token.
//! - It then begins its `:container`, unless the rule it replaced carried
//! that container on (below), or yields its `:scalar` from the token at
//! that index, when the pass consumed one; `:value` yields nothing on an
//! open pass, and `:scalar` nothing for a container rule.
//! - It then announces its `:member`, read as `:key` is.
//! - A close pass of a rule whose `:text` is `true` first yields the text
//! the passes between its open pass and it consumed: the `:src` of each
//! of their tokens, in order, the tokens of any `:text` rule closed in
//! between included. Every pass writes its tokens' `:src` to the text
//! of the innermost `:text` rule open, if one is: an open pass before
//! its own rule's text opens, a close pass after its own rule's text
//! closes and joins the one around it.
//! - A close pass of a container rule then ends its container, unless the
//! pass's alternate replaces the rule with one the spec names a
//! container: then the container goes on, and the replacing rule's open
//! pass begins none. A close pass of any other rule yields its
//! `:scalar :value`, the pass's own value, when it has one.
//! - A pass of a rule the spec does not name yields nothing but writes
//! its tokens as above, and the end of the passes inside a container is
//! `PROTOCOL_ORDER_ERROR`; a `:text` rule's text still open there is
//! dropped.
//!
//! The library's failures are the native's, with their codes and texts: a
//! key or a member whose token's value is not a string, and a scalar whose
//! token's value is a container, are `DSL_TYPE_ERROR`; a close with no
//! container open is the library's `pop` of an empty stack, and a `:text`
//! rule's close with no text open its `top` of an empty one, each
//! `DSL_TYPE_ERROR` too. A text is held to [`TreeSpec::max_text_bytes`] as
//! it grows, as the library's `string-join` holds it: a pass's tokens
//! together, then the text they join, then a closed text joined to the one
//! around it, each past the bound refused with [`tree_text_refusal`]; a pass
//! that consumed no tokens joins nothing. (The library's scan also holds
//! its whole state to `max_metadata_bytes`, which a native has no state to
//! match: past that, the two refuse with the same code and different
//! limits.)
//!
//! A router built before `:member` and `:text` existed reads neither, so
//! the lowering hands a spec that uses either only to routers that say
//! they do ([`crate::shared::Routers::transduce_tree_reads_member_and_text`]).
use HashMap;
use crateFail;
/// The container a rule's passes begin and end.
/// The member name a rule's open pass announces.
/// The scalar a rule yields.
/// What one rule's passes do: an entry of the spec.
/// A `transduce-tree` spec of the standard shape: the rules it names.
/// The library's refusal of a text past [`TreeSpec::max_text_bytes`]: its
/// `string-join`'s, `len` the bytes the join would hold.