rsaeb 0.10.1

A no_std + alloc interpreter for A=B ordered rewrite programs.
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
use core::fmt;

use crate::allocation::{AllocationContext, AllocationError, AllocationErrorKind};

use super::{
    InputColumn, LeftModifierKind, OwnedRuleAttemptStepError, OwnedRunStepError, ParseError,
    ParseErrorKind, ParseErrorLocation, ParseRepresentationError, PayloadKind,
    ReturnOutputLimitError, RewriteSizeError, RightActionKind, RuleAttemptCursorError,
    RuleAttemptLimitError, RuleAttemptStepError, RuleRuntimeStateError, RunAdmissionError,
    RunError, RunFinishError, RunStartError, RunStepError, RuntimeInputError,
    RuntimeStateLimitError, StepLimitError, TraceSnapshotError, TraceSnapshotRunError,
    TracedRunError,
};

impl fmt::Display for AllocationContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ProgramCodeLine => f.write_str("program code line"),
            Self::ProgramPayload => f.write_str("program payload"),
            Self::ProgramRuleTable => f.write_str("program rule table"),
            Self::CanonicalSource => f.write_str("canonical source bytes"),
            Self::RuntimeInputValidation => f.write_str("runtime input validation"),
            Self::RuntimeOnceRuleState => f.write_str("runtime once rule state"),
            Self::RuntimeRewriteState => f.write_str("runtime rewrite state"),
            Self::PayloadView => f.write_str("payload view"),
            Self::OwnedRuleWitness => f.write_str("owned execution rule witness"),
            Self::RuntimeStateView => f.write_str("runtime state view"),
            Self::FinalOutput => f.write_str("final output"),
            Self::ReturnOutput => f.write_str("return output"),
            Self::TraceSnapshot => f.write_str("trace snapshot"),
        }
    }
}

impl fmt::Display for AllocationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind() {
            AllocationErrorKind::CapacityOverflow => {
                write!(
                    f,
                    "allocation capacity overflow while building {}",
                    self.context(),
                )
            }
            AllocationErrorKind::ReservationFailed { requested_capacity } => {
                write!(
                    f,
                    "allocation reservation failure while building {}; requested capacity: {}",
                    self.context(),
                    requested_capacity,
                )
            }
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.location() {
            ParseErrorLocation::Line(line) => write!(f, "parse error at line {}", line.get())?,
            ParseErrorLocation::Position(position) => write!(
                f,
                "parse error at line {}, column {}",
                position.line().get(),
                position.column().get()
            )?,
        }

        write!(f, ": {}", self.kind())
    }
}

impl fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Allocation(error) => error.fmt(f),
            Self::Representation(error) => error.fmt(f),
            Self::Limit(error) => error.fmt(f),
            Self::NonAsciiInCode { byte } => {
                write!(f, "non-ASCII byte 0x{:02x} in code", byte.get())
            }
            Self::NonPrintableAsciiInCode { byte } => {
                write!(f, "non-printable ASCII byte 0x{:02x} in code", byte.get())
            }
            Self::MissingEquals => f.write_str("missing '='"),
            Self::MultipleEquals => f.write_str("multiple '=' characters are not allowed"),
            Self::ReservedSyntaxInPayload { byte, payload_kind } => write!(
                f,
                "reserved syntax byte '{}' in {payload_kind}",
                printable_ascii(byte.get()),
            ),
            Self::UnsupportedLeftModifierOrder { modifier } => write!(
                f,
                "duplicated or unsupported left-side modifier order at {modifier}"
            ),
            Self::UnsupportedRightActionSyntax { action } => {
                write!(
                    f,
                    "nested or unsupported right-side action syntax at {action}"
                )
            }
        }
    }
}

impl fmt::Display for ParseRepresentationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SourceLineNumber => f.write_str("source line number could not be represented"),
            Self::SourceColumn { line } => write!(
                f,
                "source column could not be represented at line {}",
                line.get(),
            ),
            Self::RulePosition => f.write_str("rule position could not be represented"),
            Self::RuleCount => f.write_str("rule count could not be represented"),
        }
    }
}

impl fmt::Display for super::ParseLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Source {
                limit,
                attempted_len,
            } => write!(
                f,
                "source length {attempted_len} exceeds the configured source limit {}",
                limit.get(),
            ),
            Self::CodeLine {
                limit,
                attempted_len,
            } => write!(
                f,
                "code line length {attempted_len} exceeds the configured code-line limit {}",
                limit.get(),
            ),
            Self::Payload {
                limit,
                attempted_len,
            } => write!(
                f,
                "payload length {attempted_len} exceeds the configured payload limit {}",
                limit.get(),
            ),
            Self::Rules {
                limit,
                attempted_count,
            } => write!(
                f,
                "rule count {} exceeds the configured rule limit {}",
                attempted_count.get(),
                limit.get(),
            ),
        }
    }
}

impl fmt::Display for LeftModifierKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Once => f.write_str("(once)"),
            Self::Start => f.write_str("(start)"),
            Self::End => f.write_str("(end)"),
        }
    }
}

impl fmt::Display for RightActionKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Start => f.write_str("(start)"),
            Self::End => f.write_str("(end)"),
            Self::Return => f.write_str("(return)"),
        }
    }
}

impl fmt::Display for PayloadKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LeftSideData => f.write_str("left-side data"),
            Self::RightSideData => f.write_str("right-side data"),
            Self::RightSideMoveStartPayload => f.write_str("right-side move-to-start payload"),
            Self::RightSideMoveEndPayload => f.write_str("right-side move-to-end payload"),
            Self::RightSideReturnPayload => f.write_str("right-side return payload"),
        }
    }
}

impl fmt::Display for RunError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Start(error) => error.fmt(f),
            Self::Finish(error) => error.fmt(f),
        }
    }
}

impl fmt::Display for RunStartError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Allocation(error) => {
                write!(f, "run start failed: {error}")
            }
        }
    }
}

impl fmt::Display for RunStepError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Allocation(error) => error.fmt(f),
            Self::RewriteSize(error) => error.fmt(f),
            Self::RuntimeStateLimit(error) => error.fmt(f),
            Self::ReturnOutputLimit(error) => error.fmt(f),
            Self::StepLimit(error) => error.fmt(f),
            Self::RuleRuntimeState(error) => error.fmt(f),
        }
    }
}

impl fmt::Display for OwnedRunStepError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Step(error) => error.fmt(f),
            Self::RuleWitnessAllocation(error) => {
                write!(f, "owned rule witness materialization failed: {error}")
            }
        }
    }
}

impl fmt::Display for RuleAttemptStepError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Step(error) => error.fmt(f),
            Self::RuleAttemptLimit(error) => error.fmt(f),
            Self::RuleCursor(error) => error.fmt(f),
        }
    }
}

impl fmt::Display for OwnedRuleAttemptStepError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Attempt(error) => error.fmt(f),
            Self::RuleWitnessAllocation(error) => {
                write!(f, "owned rule witness materialization failed: {error}")
            }
        }
    }
}

impl fmt::Display for RunFinishError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Step(error) => error.fmt(f),
            Self::FinalOutput(error) => {
                write!(f, "final output materialization failed: {error}")
            }
        }
    }
}

impl<E> fmt::Display for TracedRunError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Run(error) => error.fmt(f),
            Self::Trace(error) => write!(f, "trace callback failed: {error}"),
        }
    }
}

impl fmt::Display for TraceSnapshotError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Limit {
                limit,
                attempted_len,
            } => write!(
                f,
                "trace snapshot limit exceeded; attempted length: {attempted_len}, limit: {}",
                limit.get(),
            ),
            Self::Allocation(error) => error.fmt(f),
        }
    }
}

impl<E> fmt::Display for TraceSnapshotRunError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Run(error) => error.fmt(f),
            Self::Snapshot(error) => error.fmt(f),
            Self::Trace(error) => write!(f, "trace callback failed: {error}"),
        }
    }
}

impl fmt::Display for RuntimeInputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NonAscii { column, byte } => write!(
                f,
                "input error: non-ASCII byte 0x{:02x} at column {column}",
                byte.get(),
            ),
            Self::ColumnOverflow => write!(f, "input error: column number overflow"),
            Self::InputLimit {
                limit,
                attempted_len,
            } => write!(
                f,
                "input error: runtime input length {} exceeds the configured input limit {}",
                attempted_len.get(),
                limit.get()
            ),
            Self::Allocation(error) => error.fmt(f),
        }
    }
}

impl fmt::Display for RunAdmissionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InitialStateTooLarge {
                limit,
                attempted_len,
            } => write!(
                f,
                "run admission error: initial runtime state length {} exceeds the configured state limit {}",
                attempted_len.get(),
                limit.get()
            ),
        }
    }
}

impl fmt::Display for RuleRuntimeStateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "missing runtime once-rule state for rule {}",
            self.rule().number().get()
        )
    }
}

impl fmt::Display for RuleAttemptCursorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rule-attempt cursor points at missing rule {}",
            self.rule().number().get()
        )
    }
}

impl fmt::Display for InputColumn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.get().fmt(f)
    }
}

impl fmt::Display for RewriteSizeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rewrite size failure: replacing {} bytes in a {} byte state with {} bytes",
            self.lhs_len(),
            self.state_len(),
            self.rhs_len(),
        )
    }
}

impl fmt::Display for RuntimeStateLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rewrite state limit exceeded; attempted length: {}, limit: {}",
            self.attempted_len(),
            self.limit().get(),
        )
    }
}

impl fmt::Display for ReturnOutputLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "return output limit exceeded; attempted length: {}, limit: {}",
            self.attempted_len(),
            self.limit().get(),
        )
    }
}

impl fmt::Display for StepLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "step limit exceeded after {} steps; max steps: {}, state length: {} bytes",
            self.completed_steps().get(),
            self.max_steps().get(),
            self.state_len(),
        )
    }
}

impl fmt::Display for RuleAttemptLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rule-attempt limit exceeded after {} attempts; max attempts: {}, state length: {} bytes",
            self.completed_attempts().get(),
            self.max_attempts().get(),
            self.state_len(),
        )
    }
}

/// Returns the printable ASCII character or a replacement marker.
fn printable_ascii(byte: u8) -> char {
    if byte.is_ascii() {
        char::from(byte)
    } else {
        '\u{fffd}'
    }
}