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
//! Byte-oriented interpreter for A=B ordered rewrite programs.
//!
//! This page is the docs.rs API guide. The README carries the longer language
//! reference; this page focuses on the public Rust surface and the typed
//! boundaries a host program should use.
//!
//! `rsaeb` is a `no_std + alloc` library crate. It parses compact A=B source
//! into an immutable [`program::Program`], validates host bytes as
//! [`input::RuntimeInput`], admits that input into a one-run [`input::RunSeed`],
//! and executes only after [`limits::ExecutionLimits`] are attached. The
//! interpreter core does not read files, use process arguments, access
//! environment variables, write stdout/stderr, or perform lossy byte-to-text
//! display conversion.
//!
//! The public API is intentionally arranged as boundary types rather than root
//! re-exports. A host should move data through the domains in this order:
//! source bytes become a parsed program, raw input bytes become validated
//! runtime input, validated input becomes an admitted run seed, and only then
//! can execution or tracing start. That ordering keeps parse errors, input
//! validation errors, run-admission errors, runtime failures, and trace-sink
//! failures separate in both type signatures and diagnostics.
//!
//! # API map
//!
//! Use these public entry points according to the boundary being crossed:
//!
//! - [`source::ProgramSource::from_bytes`] and [`source::ProgramSource::from_text`] explicitly
//! label host bytes or strings as A=B source before parsing.
//! - [`program::Program::parse`] validates source syntax under [`limits::ParseLimits`] and
//! returns a reusable [`program::Program`].
//! - [`input::RuntimeInputSource::from_bytes`] labels host input bytes, and
//! [`input::RuntimeInput::validate`] validates and owns them in the runtime input
//! byte domain until execution consumes the value.
//! - [`limits::RuntimeInputLimits`] bounds raw input validation,
//! [`input::RunSeed`] admits validated input under [`limits::ExecutionLimits`],
//! and [`limits::TraceSnapshotByteLimit`] bounds trace snapshot materialization.
//! - [`program::Program::run`] runs to completion while borrowing the parsed
//! program, [`program::Program::start_run`] returns a borrowed typestate
//! execution, and [`program::Program::into_run`] returns the explicit owned
//! typestate execution.
//! - [`program::Program::run_with_borrowed_trace`] observes borrowed trace events without
//! per-event allocation; [`program::Program::run_with_trace_snapshots`] materializes
//! bounded owned trace events.
//! - [`inspect`] exposes borrowed structured rule views, and [`error`] exposes
//! structured parse, input, runtime, and trace errors.
//!
//! # Typed boundaries
//!
//! Program source and runtime input are different byte domains. Program payload
//! bytes are printable executable syntax bytes accepted by the parser. Runtime
//! input accepts any ASCII byte, including whitespace, control bytes, and bytes
//! that are reserved syntax in program source. Construct both explicitly before
//! execution so parsing, input validation, and runtime failures remain
//! distinguishable in the type system.
//!
//! [`input::RunSeed`] is the admission witness for one run. It consumes a
//! validated [`input::RuntimeInput`] together with
//! [`limits::ExecutionLimits`], checks the initial runtime-state budget, and
//! prevents a later execution API from receiving raw bytes or detached budget
//! values.
//!
//! # Basic execution
//!
//! Parse [`source::ProgramSource`], validate [`input::RuntimeInput`], then
//! admit an [`input::RunSeed`] before running:
//!
//! ```
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN, DEFAULT_MAX_STEPS,
//! };
//! use rsaeb::input::{RunSeed, RuntimeInput, RuntimeInputSource};
//! use rsaeb::limits::{ExecutionLimits, RuntimeInputLimits};
//! use rsaeb::program::{Program, RunOutcome};
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("a=b"), DEFAULT_PARSE_LIMITS)?;
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! DEFAULT_MAX_STEPS,
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//! let input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"a"), input_limits)?;
//! let seed = RunSeed::admit(input, execution_limits)?;
//! let result = program.run(seed)?;
//!
//! if !matches!(
//! result.outcome(),
//! RunOutcome::Stable(output) if output.as_slice() == b"b"
//! ) {
//! return Err("unexpected stable output".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! Parse [`program::Program`] once when the same rules should be reused. Per-run
//! `(once)` state is owned by each runtime invocation, not by the parsed
//! program:
//!
//! ```
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN,
//! ExecutionLimits, RuntimeInputLimits, StepLimit,
//! };
//! use rsaeb::input::{RunSeed, RuntimeInput, RuntimeInputSource};
//! use rsaeb::program::{Program, RunOutcome};
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("(once)a=b\na=c"), DEFAULT_PARSE_LIMITS)?;
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! StepLimit::new(10_000),
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//!
//! let first_input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"aa"), input_limits)?;
//! let second_input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"aa"), input_limits)?;
//!
//! let first = program.run(RunSeed::admit(first_input, execution_limits)?)?;
//! let second = program.run(RunSeed::admit(second_input, execution_limits)?)?;
//!
//! if !matches!(
//! first.outcome(),
//! RunOutcome::Stable(output) if output.as_slice() == b"bc"
//! ) {
//! return Err("unexpected first output".into());
//! }
//! if !matches!(
//! second.outcome(),
//! RunOutcome::Stable(output) if output.as_slice() == b"bc"
//! ) {
//! return Err("unexpected second output".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Stepwise execution
//!
//! Use [`program::Program::start_run`] when a host wants to wait after each
//! applied rule while keeping the parsed program reusable:
//!
//! ```
//! use rsaeb::execution::StepTransition;
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN, StepLimit,
//! };
//! use rsaeb::input::{RuntimeInput, RuntimeInputSource};
//! use rsaeb::input::RunSeed;
//! use rsaeb::limits::{ExecutionLimits, RuntimeInputLimits};
//! use rsaeb::program::Program;
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("a=b\nb=c"), DEFAULT_PARSE_LIMITS)?;
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! StepLimit::new(10),
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//! let input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"a"), input_limits)?;
//! let seed = RunSeed::admit(input, execution_limits)?;
//! let execution = program.start_run(seed)?;
//!
//! let execution = match execution.step() {
//! StepTransition::Applied(applied) => {
//! if applied.state().materialize()?.as_slice() != b"b" {
//! return Err("unexpected first applied state".into());
//! }
//! applied.into_session()
//! }
//! StepTransition::Stable(_) | StepTransition::Returned(_) | StepTransition::Failed(_) => {
//! return Err("expected first applied step".into());
//! }
//! };
//!
//! let execution = match execution.step() {
//! StepTransition::Applied(applied) => {
//! if applied.state().materialize()?.as_slice() != b"c" {
//! return Err("unexpected second applied state".into());
//! }
//! applied.into_session()
//! }
//! StepTransition::Stable(_) | StepTransition::Returned(_) | StepTransition::Failed(_) => {
//! return Err("expected second applied step".into());
//! }
//! };
//!
//! match execution.step() {
//! StepTransition::Stable(stable) => {
//! if stable.steps().get() != 2 {
//! return Err("unexpected stable step count".into());
//! }
//! if stable.state().materialize()?.as_slice() != b"c" {
//! return Err("unexpected stable state".into());
//! }
//! }
//! StepTransition::Applied(_) | StepTransition::Returned(_) | StepTransition::Failed(_) => {
//! return Err("expected stable completion".into());
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! A [`execution::StepTransition::Failed`] value is terminal. It exposes the
//! uncommitted state for diagnostics, then lets callers discard the failed run
//! into its [`error::RunError`]; it does not expose a retryable session.
//! [`execution::OwnedStepTransition::Failed`] carries the same error and
//! uncommitted-state diagnostics for owned sessions, and it can split back into
//! the runtime error plus the uncommitted owned session so hosts can recover the
//! parsed program when ownership matters.
//!
//! # Limits
//!
//! [`limits::RuntimeInputLimits`] carries input-byte validation policy.
//! [`limits::ExecutionLimits`] carries initial runtime-state admission, the step
//! budget, and byte budgets for rewrite states and `(return)` outputs. Trace
//! snapshot materialization uses an explicit
//! [`limits::TraceSnapshotByteLimit`]. Step limits are checked only when another
//! matching rule would apply after the configured number of completed steps:
//!
//! ```
//! use rsaeb::error::{LimitError, RunError};
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN, StepLimit,
//! };
//! use rsaeb::input::{RuntimeInput, RuntimeInputSource};
//! use rsaeb::input::RunSeed;
//! use rsaeb::limits::{ExecutionLimits, RuntimeInputLimits};
//! use rsaeb::program::Program;
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! StepLimit::new(0),
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//! let input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"a"), input_limits)?;
//! let seed = RunSeed::admit(input, execution_limits)?;
//! let result = Program::parse(ProgramSource::from_text("a=b"), DEFAULT_PARSE_LIMITS)?.run(seed);
//!
//! if !matches!(
//! result,
//! Err(RunError::Limit(LimitError::Step { completed_steps, .. }))
//! if completed_steps.get() == 0
//! ) {
//! return Err("unexpected step-limit error".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Rule inspection
//!
//! Parsed rules are exposed as borrowed structured views, not as stored source
//! strings:
//!
//! ```
//! use rsaeb::limits::DEFAULT_PARSE_LIMITS;
//! use rsaeb::inspect::{RuleActionView, RuleAnchor, RuleRepeat};
//! use rsaeb::program::Program;
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("( once ) ( start ) a = ( end ) b # comment"), DEFAULT_PARSE_LIMITS)?;
//! let rule = program.rules().next().ok_or("missing parsed rule")?;
//!
//! if rule.repeat() != RuleRepeat::Once {
//! return Err("unexpected repeat".into());
//! }
//! if rule.anchor() != RuleAnchor::Start {
//! return Err("unexpected anchor".into());
//! }
//! if rule.lhs().materialize()?.as_slice() != b"a" {
//! return Err("unexpected left side".into());
//! }
//! match rule.action() {
//! RuleActionView::MoveEnd(payload) => {
//! if payload.materialize()?.as_slice() != b"b" {
//! return Err("unexpected moved payload".into());
//! }
//! }
//! RuleActionView::Replace(_) | RuleActionView::MoveStart(_) | RuleActionView::Return(_) => {
//! return Err("expected move-end action".into());
//! }
//! }
//! if rule.canonical_source()?.as_slice() != b"(once)(start)a=(end)b" {
//! return Err("unexpected canonical source".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Tracing
//!
//! Borrowed trace events allocate no snapshots. Snapshot tracing is layered on
//! top when a caller needs owned event bytes:
//!
//! ```
//! use core::convert::Infallible;
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN, StepLimit,
//! };
//! use rsaeb::trace::BorrowedTraceEvent;
//! use rsaeb::input::{RuntimeInput, RuntimeInputSource};
//! use rsaeb::input::RunSeed;
//! use rsaeb::limits::{ExecutionLimits, RuntimeInputLimits};
//! use rsaeb::program::Program;
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("a=b\nb=(return)ok"), DEFAULT_PARSE_LIMITS)?;
//! let mut byte_counts = Vec::new();
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! StepLimit::new(10),
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//! let input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"a"), input_limits)?;
//! let seed = RunSeed::admit(input, execution_limits)?;
//!
//! program.run_with_borrowed_trace(
//! seed,
//! |event| {
//! byte_counts.push(event.byte_count().get());
//! if let BorrowedTraceEvent::Step { rule, .. } = event {
//! let _line = rule.line_number();
//! }
//! Ok::<(), Infallible>(())
//! },
//! )?;
//!
//! if byte_counts != [1, 1, 2] {
//! return Err("unexpected trace byte counts".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! Snapshot tracing materializes owned event bytes under an explicit snapshot
//! byte budget, which lets the caller retain events after each callback returns:
//!
//! ```
//! use rsaeb::limits::{
//! DEFAULT_MAX_INPUT_LEN, DEFAULT_PARSE_LIMITS, DEFAULT_MAX_RETURN_LEN, DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_TRACE_SNAPSHOT_LEN, StepLimit,
//! };
//! use rsaeb::trace::{TraceSnapshotEffect, TraceSnapshotEvent};
//! use rsaeb::input::{RuntimeInput, RuntimeInputSource};
//! use rsaeb::input::RunSeed;
//! use rsaeb::limits::{ExecutionLimits, RuntimeInputLimits};
//! use rsaeb::program::Program;
//! use rsaeb::source::ProgramSource;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let program = Program::parse(ProgramSource::from_text("a=b\nb=(return)ok"), DEFAULT_PARSE_LIMITS)?;
//! let input_limits = RuntimeInputLimits::new(DEFAULT_MAX_INPUT_LEN);
//! let execution_limits = ExecutionLimits::new(
//! StepLimit::new(10),
//! DEFAULT_MAX_STATE_LEN,
//! DEFAULT_MAX_RETURN_LEN,
//! );
//! let input = RuntimeInput::validate(RuntimeInputSource::from_bytes(b"a"), input_limits)?;
//! let seed = RunSeed::admit(input, execution_limits)?;
//! let mut states = Vec::new();
//! let mut returns = Vec::new();
//!
//! program.run_with_trace_snapshots(seed, DEFAULT_MAX_TRACE_SNAPSHOT_LEN, |event| {
//! match event {
//! TraceSnapshotEvent::Initial { state } => states.push(state.into_raw_bytes()),
//! TraceSnapshotEvent::Step {
//! effect: TraceSnapshotEffect::Continue { state },
//! ..
//! } => states.push(state.into_raw_bytes()),
//! TraceSnapshotEvent::Step {
//! effect: TraceSnapshotEffect::Return { output },
//! ..
//! } => returns.push(output.into_raw_bytes()),
//! }
//! Ok::<(), core::convert::Infallible>(())
//! })?;
//!
//! if states != [b"a".to_vec(), b"b".to_vec()] {
//! return Err("unexpected trace states".into());
//! }
//! if returns != [b"ok".to_vec()] {
//! return Err("unexpected trace returns".into());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Error model
//!
//! Source parsing, runtime input validation, runtime execution, trace snapshot
//! materialization, and user trace-sink failures are reported with structured
//! error types such as [`error::ParseError`], [`error::RuntimeInputError`],
//! [`error::RunError`], [`error::AllocationError`],
//! [`error::TraceSnapshotError`], [`error::TraceSnapshotRunError`], and
//! [`error::TracedRunError`].
//! Allocation reservation failures include a typed
//! [`error::RequestedCapacity`] instead of only a formatted string.
//! Representation and internal-invariant failures are distinct structured
//! domains, so parser/runtime witness contradictions do not become hidden
//! panics or allocation errors.
//!
//! ```
//! use rsaeb::error::RuntimeInputError;
//! use rsaeb::limits::{RuntimeInputByteLimit, RuntimeInputLimits};
//! use rsaeb::input::{RuntimeInput, RuntimeInputSource};
//!
//! fn validate_host_input(bytes: &[u8]) -> Result<RuntimeInput, RuntimeInputError> {
//! let limits = RuntimeInputLimits::new(RuntimeInputByteLimit::new(4));
//! RuntimeInput::validate(RuntimeInputSource::from_bytes(bytes), limits)
//! }
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let Err(error) = validate_host_input(&[0xff]) else {
//! return Err("expected non-ASCII input to fail".into());
//! };
//!
//! if !matches!(
//! error,
//! RuntimeInputError::NonAscii { column, byte }
//! if column.get() == 1 && byte.get() == 0xff
//! ) {
//! return Err("unexpected input error".into());
//! }
//! # Ok(())
//! # }
//! ```
extern crate alloc;
extern crate std;
/// Allocation boundary error model and fallible Vec helpers.
/// Byte-domain model shared by parser and runtime.
/// Domain-tagged owned byte buffers.
/// Program source parser.
/// Parsed rule domain model.
/// Runtime execution engine.
/// Reserved syntax token model.