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
//! Expression parser building blocks for creating operator precedence parsers.
//!
//! This module provides generic types for building expression parsers with proper operator
//! precedence and associativity. These are the fundamental building blocks that can be
//! composed to create complete expression grammars.
//!
//! # Expression Parser Types
//!
//! - [`InfixExpr`] (or [`NonAssocExpr`]) - Non-associative binary operators (e.g., comparison operators)
//! - [`PrefixExpr`] - Unary prefix operators (e.g., `-`, `!`, `*`, `&`)
//! - [`PostfixExpr`] - Unary postfix operators (e.g., `?`, `!`)
//! - [`LeftAssocExpr`] - Left-associative binary operators (e.g., `+`, `-`, `*`, `/`)
//! - [`RightAssocExpr`] - Right-associative binary operators (e.g., `=`, `+=`)
//!
//! For complete examples of building expression parsers with proper precedence hierarchies,
//! see the [Expression Parsing](../COOKBOOK.md#expression-parsing) section in the cookbook.
//!
//! Left/Right associativity is not handled by the parser, we parse flat sequences. We only
//! distinguish it here at parsing level for handing the AST structure to the later consumer.
//!
//!
//! # No Nesting Required
//!
//! All expression types use a vector internally to store multiple operators or operands.
//! This means **you don't need to nest expression types** - the vec handles multiplicity:
//!
//! - ✅ `PrefixExpr<Op, Operand>` - Handles 0..MAX operators via `DelimitedVec` (++x)
//! - ✅ `PostfixExpr<Operand, Op>` - Handles 0..MAX operators via `DelimitedVec` (x--)
//! - ✅ `LeftAssocExpr<Operand, Op>` and `RightAssocExpr<Operand, Op>` - Handle 1..MAX operands via `DelimitedVec` (1+2+3)
//! - ❌ `PrefixExpr<Op, PrefixExpr<Op, Operand>>` - Redundant! Don't nest these types.
//!
//! Since we use vecs internally, `Box` is only needed for recursive expression grammars
//! (e.g., parenthesized expressions), not for handling multiple operators.
//!
//! # Handling Parenthesized Expressions
//!
//! Parenthesized expressions are **not** part of these building blocks. They need special
//! handling in your grammar because they allow overriding operator precedence.
//!
//! The user defines how to integrate parentheses by including them in their primary expression type.
//!
//! # Examples
//!
//! ```rust
//! # use unsynn::*;
//!
//! unsynn! {
//! // For simple binary operations (non-associative):
//! type SimpleBinary = NonAssocExpr<LiteralInteger, Plus>;
//!
//! // For left-associative chains (unlimited by default):
//! type Addition = LeftAssocExpr<LiteralInteger, Plus>;
//! }
//!
//! // Parse a simple addition
//! let mut tokens = "1 + 2".to_token_iter();
//! let expr: Addition = tokens.parse().unwrap();
//! assert_eq!(expr.len(), 2); // 2 operands
//! ```
use crate::;
use ;
/// Generic infix operator expression.
///
/// Parses: `Operand (Op Operand)?` (minimum 1 operand, maximum 2 operands by default)
///
/// This is implemented as a `DelimitedVec` with:
/// - `MIN = 1`: At least one operand must be present (the left operand)
/// - `MAX = 2`: At most two operands (left and optional right), can be overridden
/// - `TrailingDelimiter::Forbidden`: No trailing operator allowed
///
/// # Type Parameters
///
/// - `Operand`: The type of expressions that can appear on either side of the operator
/// - `Operator`: The operator type (typically an enum of different operators at this precedence level)
/// - `MAX`: Maximum number of operands (defaults to 2 for binary, can be set to limit chaining)
pub type InfixExpr<Operand, Operator, const MAX: usize = 2> =
;
/// Type alias for non-associative binary operators.
///
/// Non-associative operators cannot be chained without parentheses. For example,
/// comparison operators in Rust: `a == b == c` is a syntax error, must be
/// `(a == b) == c` or `a == (b == c)`.
///
/// This is a convenience alias for [`InfixExpr`] with default MAX=2, intended for
/// documenting non-associative operators in expression grammars.
///
/// # Examples
///
/// ```rust
/// # use unsynn::*;
/// # use unsynn::operator::names::Equal;
///
/// // Comparison operators are typically non-associative
/// type ComparisonExpr = NonAssocExpr<LiteralInteger, Equal>;
///
/// let mut tokens = "1 == 2".to_token_iter();
/// let expr: ComparisonExpr = tokens.parse().unwrap();
/// assert_eq!(expr.len(), 2);
/// ```
pub type NonAssocExpr<Operand, Operator> = ;
/// Prefix unary operator expression.
///
/// Parses: `Op* Operand` (0 to MAX operators, then operand)
///
/// Allows zero or more prefix operators before an operand. Multiple prefix operators
/// are applied right-to-left (innermost first). For example, `--x` is parsed as
/// `-(-x)` (two negations).
///
/// # Type Parameters
///
/// - `Operator`: The prefix operator type (typically an enum of different unary operators)
/// - `Operand`: The type of expression that the operators apply to
/// - `MAX`: Maximum number of prefix operators (defaults to `usize::MAX` for unlimited)
///
/// # Examples
///
/// ```rust
/// # use unsynn::*;
/// # use unsynn::operator::names::Minus;
///
/// unsynn! {
/// type Number = LiteralInteger;
/// }
///
/// // Parse "- - 5"
/// let mut tokens = "--5".to_token_iter();
/// let expr: PrefixExpr<Minus, Number> = tokens.parse().unwrap();
/// assert_eq!(expr.operators.len(), 2); // 2 Minus operators
///
/// // Limit prefix depth
/// type LimitedPrefix = PrefixExpr<Minus, Number, 3>; // Max 3 prefix operators
/// ```
/// Postfix unary operator expression.
///
/// Parses: `Operand Op*` (operand, then 0 to MAX operators)
///
/// Allows zero or more postfix operators after an operand. Multiple postfix operators
/// are applied left-to-right (leftmost first). For example, `x??` is parsed as
/// `(x?)?` (try operators chain left-to-right).
///
/// # Type Parameters
///
/// - `Operand`: The type of expression that the operators apply to
/// - `Operator`: The postfix operator type (typically an enum of different postfix operations)
/// - `MAX`: Maximum number of postfix operators (defaults to `usize::MAX` for unlimited)
///
/// # Examples
///
/// ```rust
/// # use unsynn::*;
/// # use unsynn::operator::names::Bang;
///
/// unsynn! {
/// type Number = LiteralInteger;
/// }
///
/// // Parse "5 ! !"
/// let mut tokens = "5 ! !".to_token_iter();
/// let expr: PostfixExpr<Number, Bang> = tokens.parse().unwrap();
/// assert_eq!(expr.operators.len(), 2); // 2 Bang operators
///
/// // Limit postfix depth
/// type LimitedPostfix = PostfixExpr<Number, Bang, 3>; // Max 3 postfix operators
/// ```
/// Left-associative infix operator expression.
///
/// Parses: `Operand (Op Operand)*` (minimum 1 operand, unlimited by default)
///
/// Left-associative operators group left-to-right when chained. For example,
/// `a + b + c` should be interpreted as `((a + b) + c)`.
///
/// This is a newtype wrapper around `InfixExpr` that allows unlimited chaining by default.
/// During parsing, associativity doesn't matter - we parse a flat sequence. The
/// associativity only affects evaluation order, which is handled elsewhere.
///
/// # Type Parameters
///
/// - `Operand`: The type of expressions that can appear as operands
/// - `Operator`: The operator type (typically an enum of different operators at this precedence level)
/// - `MAX`: Maximum number of operands (defaults to `usize::MAX` for unlimited chaining)
///
/// # Examples
///
/// ```
/// # use unsynn::*;
/// # use unsynn::operator::names::Plus;
///
/// unsynn! {
/// type Number = LiteralInteger;
/// }
///
/// // Parse "1 + 2 + 3 + 4"
/// let mut tokens = "1 + 2 + 3 + 4".to_token_iter();
/// let expr: LeftAssocExpr<Number, Plus> = tokens.parse().unwrap();
/// assert_eq!(expr.len(), 4); // 4 operands
///
/// // With depth limit
/// type LimitedChain = LeftAssocExpr<Number, Plus, 10>; // Max 10 operands
/// ```
;
/// Right-associative infix operator expression.
///
/// Parses: `Operand (Op Operand)*` (minimum 1 operand, unlimited by default)
///
/// Right-associative operators group right-to-left when chained. For example,
/// `a = b = c` should be interpreted as `a = (b = c)`.
///
/// # Type Parameters
///
/// - `Operand`: The type of expressions that can appear as operands
/// - `Operator`: The operator type (typically an enum of different operators at this precedence level)
/// - `MAX`: Maximum number of operands (defaults to `usize::MAX` for unlimited chaining)
///
/// # Examples
///
/// ```
/// # use unsynn::*;
///
/// unsynn! {
/// type Number = LiteralInteger;
///
/// operator Caret = "^";
/// }
///
/// // Parse "2 ^ 3 ^ 4"
/// let mut tokens = "2 ^ 3 ^ 4".to_token_iter();
/// let expr: RightAssocExpr<Number, Caret> = tokens.parse().unwrap();
/// assert_eq!(expr.len(), 3); // 3 operands
///
/// // With depth limit
/// type LimitedChain = RightAssocExpr<Number, Caret, 10>; // Max 10 operands
/// ```
;