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
//! Proc macro crate for the UOR Foundation.
//!
//! Provides the `uor!` macro that parses the EBNF term grammar at compile time,
//! producing typed `Term` ASTs in the `uor_foundation::enforcement` module.
//!
//! # Usage
//!
//! ```rust,ignore
//! use uor_foundation::uor;
//!
//! // Type declaration (named-argument constraint syntax)
//! let pixel = uor! { type Pixel { ResidueConstraint(modulus: 256, residue: 255); } };
//!
//! // Term expression
//! let sum = uor! { add(mul(3, 5), 7) };
//!
//! // Assertion (ground — checked at compile time)
//! uor! { assert add(1, 2) = 3; };
//! ```
use TokenStream;
/// The UOR term language DSL macro.
///
/// Parses EBNF surface syntax at compile time and produces typed `Term` ASTs
/// in the `uor_foundation::enforcement` module. The macro handles:
///
/// - **Term expressions**: `add(mul(3, 5), 7)` — operation applications
/// - **Type declarations**: `type Pixel { ResidueConstraint(modulus: 256, residue: 255); }` — constrained types
/// - **Bindings**: `let x : Pixel = add(0, 0);` — named term bindings
/// - **Assertions**: `assert lhs = rhs;` — ground assertions checked at compile time
/// - **Effect declarations**: `effect Name { target: {0,1}; delta: 0; commutes: true; }` — generic props
/// - **Boundary declarations**: `source name : Type via grounding;`
/// - **Quantum literals**: `42@Q7` — level-annotated integers
/// - **Lift/Project**: `lift(x, Q3)`, `project(y, Q0)` — level transitions
/// - **Match**: `match x { pred => expr; otherwise => expr; }`
/// - **Recursion**: `recurse f(n) measure n base is_zero => 1 step => mul(n, f(pred(n)))`
/// - **Streams**: `unfold nat : Successor from 0`
///
/// # Examples
///
/// ```rust,ignore
/// use uor_foundation::uor;
///
/// // Term expressions produce a TermArena with the expression tree.
/// let sum = uor! { add(mul(3, 5), 7) };
///
/// // Quantum-annotated literals tag a value at a specific ring width.
/// let wide = uor! { 144115188075855617@Q7 };
///
/// // Type declarations define constrained types.
/// let pixel = uor! {
/// type Pixel {
/// ResidueConstraint(modulus: 256, residue: 255);
/// HammingConstraint(hammingBound: 8);
/// DepthConstraint(minDepth: 0, maxDepth: 1);
/// }
/// };
///
/// // Bindings carry surface syntax and content addresses.
/// let origin = uor! { let origin : Pixel = add(0, 0); };
///
/// // Ground assertions are checked at COMPILE TIME.
/// uor! { assert add(1, 2) = 3; };
/// uor! { assert mul(3, 5) = 15; };
///
/// // Effect declarations register fiber-targeted effects.
/// let blit = uor! {
/// effect Blit {
/// target: {0, 1, 2, 3};
/// delta: 0;
/// commutes: true;
/// }
/// };
///
/// // Boundary declarations define data sources and sinks.
/// uor! { source pixel_in : Pixel via sRGB; };
/// uor! { sink pixel_out : Pixel via DisplayP3; };
///
/// // Lift/Project handle level transitions explicitly.
/// let widened = uor! { lift(x, Q3) };
/// let narrowed = uor! { project(y, Q0) };
///
/// // Match expressions with pattern arms and a required otherwise arm.
/// let clamped = uor! {
/// match x {
/// is_negative => 0;
/// exceeds_max => 255;
/// otherwise => x;
/// }
/// };
///
/// // Bounded recursion with a descent measure and base case.
/// let factorial = uor! {
/// recurse fact(n)
/// measure n
/// base is_zero => 1
/// step => mul(n, fact(pred(n)))
/// };
///
/// // Stream construction via unfold (coinductive).
/// let naturals = uor! { unfold nat : Successor from 0 };
/// ```
///
/// # Errors
///
/// Parse errors produce `compile_error!()` at the macro call site.
/// The error message includes the unexpected token:
///
/// ```text
/// uor! { add(1, ) }
/// // error: uor! parse error: Unexpected token in expression: RParen
/// ```
///
/// Ground assertions that fail ring evaluation also produce compile errors:
///
/// ```text
/// uor! { assert add(1, 2) = 4; }
/// // error: assertion failed at compile time
/// ```