neure 0.10.1

A fast little combinational parsing library
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
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
474
475
476
477
478
479
480
use core::fmt::Debug;
use core::marker::PhantomData;
use core::ops::RangeBounds;

use crate::ctor::Ctor;

use crate::ctor::Handler;
use crate::ctx::Context;
use crate::ctx::Match;
use crate::ctx::new_span_inc;
use crate::err::Error;
use crate::neu::EmptyCond;
use crate::neu::calc_length;
use crate::regex::Regex;
use crate::regex::impl_not_for_regex;
use crate::span::Span;

use super::CRange;
use super::Condition;
use super::Neu;
use super::NeuCond;

///
/// Matches a sequence of elements with compile-time bounded repetition and context validation.
///
/// [`Between`] provides precise control over sequence length while maintaining per-element
/// context awareness, enforcing that every matched element satisfies both:
/// 1. **Base pattern match**: Core element validation ([`Neu`])
/// 2. **Context condition**: Runtime constraints ([`NeuCond`])
///
/// Matching occurs within the compile-time defined range `[M, N)`, meaning:
/// - **Minimum**: Exactly `M` elements must match
/// - **Maximum**: Matching stops after `N-1` elements (inclusive)
///
/// # Regex
///
/// Matches between `M` (inclusive) and `N` (exclusive) consecutive elements:
/// - **Success**: Returns span covering all matched elements
///   - Requires `M <= matched_count < N`
///   - **Every element** must satisfy BOTH conditions:
///     a. `unit.is_match(item)` returns true
///     b. `cond.check()` returns true for the context
///   - Stops early when:
///     - Maximum count (`N-1`) is reached
///     - An element fails either condition
/// - **Failure**: Returns error if:
///   - Total matched elements < `M`
///   - First element fails when `M > 0`
/// - **Special case**: When `M = 0`, empty matches succeed (returns zero-length span)
///
/// # Ctor
///
/// Uses identical matching logic as regex mode, then constructs a value from the result.
/// The specific constructed value depends on the active handler implementation.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
///     let hex = 'a'..'g';
///     let hex = hex.between::<1, 6>();
///     let mut ctx = CharsCtx::new("aabbccgg");
///
///     assert_eq!(ctx.try_mat(&hex)?, Span::new(0, 6));
/// #   Ok(())
/// # }
/// ```
///
/// # Example 1
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
///     let hex = 'a'..'g';
///     let hex = hex.many0();
///     let mut ctx = CharsCtx::new("aabbccgg");
///
///     assert_eq!(ctx.try_mat(&hex)?, Span::new(0, 6));
/// #   Ok(())
/// # }
/// ```
///
/// # Example 2
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
///     let hex = 'a'..'g';
///     let hex = hex.at_most::<6>();
///     let mut ctx = CharsCtx::new("aabbccgg");
///
///     assert_eq!(ctx.try_mat(&hex)?, Span::new(0, 6));
/// #   Ok(())
/// # }
/// ```
///
/// # Example 3
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
///     let hex = 'a'..'g';
///     let hex = hex.at_least::<2>();
///     let mut ctx = CharsCtx::new("aabbccgg");
///
///     assert_eq!(ctx.try_mat(&hex)?, Span::new(0, 6));
/// #   Ok(())
/// # }
/// ```
pub struct Between<const M: usize, const N: usize, C, U, I = EmptyCond> {
    unit: U,
    cond: I,
    marker: PhantomData<C>,
}

impl<const M: usize, const N: usize, C, U, I> core::ops::Not for Between<M, N, C, U, I> {
    type Output = crate::regex::Assert<Self>;

    fn not(self) -> Self::Output {
        crate::regex::not(self)
    }
}

impl<const M: usize, const N: usize, C, U, I> Debug for Between<M, N, C, U, I>
where
    I: Debug,
    U: Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Between")
            .field("unit", &self.unit)
            .field("cond", &self.cond)
            .finish()
    }
}

impl<const M: usize, const N: usize, C, U, I> Clone for Between<M, N, C, U, I>
where
    I: Clone,
    U: Clone,
{
    fn clone(&self) -> Self {
        Self {
            unit: self.unit.clone(),
            cond: self.cond.clone(),
            marker: self.marker,
        }
    }
}

impl<const M: usize, const N: usize, C, U, I> Copy for Between<M, N, C, U, I>
where
    I: Copy,
    U: Copy,
{
}

impl<const M: usize, const N: usize, C, U, I> Between<M, N, C, U, I> {
    pub const fn new(unit: U, cond: I) -> Self {
        Self {
            unit,
            cond,
            marker: PhantomData,
        }
    }

    pub const fn unit(&self) -> &U {
        &self.unit
    }

    pub const fn unit_mut(&mut self) -> &mut U {
        &mut self.unit
    }

    pub fn set_unit(&mut self, unit: U) -> &mut Self {
        self.unit = unit;
        self
    }
}

impl<'a, const M: usize, const N: usize, C, U, I> Condition<'a, C> for Between<M, N, C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Context<'a>,
{
    type Out<F> = Between<M, N, C, U, F>;

    fn set_cond<F>(self, cond: F) -> Self::Out<F>
    where
        F: NeuCond<'a, C>,
    {
        Between::<M, N, C, U, F>::new(self.unit, cond)
    }
}

impl<'a, const M: usize, const N: usize, U, C, O, I, H> Ctor<C, O, H> for Between<M, N, C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Match<'a>,
    H: Handler<C, Out = O>,
{
    #[inline(always)]
    fn construct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
        let ret = ctx.try_mat(self);

        func.invoke(ctx, &ret?).map_err(Into::into)
    }
}

impl<'a, const M: usize, const N: usize, U, C, I> Regex<C> for Between<M, N, C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Context<'a>,
{
    #[inline(always)]
    fn try_parse(&self, ctx: &mut C) -> Result<Span, crate::err::Error> {
        let mut cnt = 0;
        let mut beg = None;
        let mut end = None;
        let mut ret = Err(Error::Between);
        let mut iter = ctx.peek()?;
        let remaining_len = ctx.len() - ctx.offset();
        let range = M..N;

        crate::debug_regex_beg!("Between", &range, ctx.offset());
        if remaining_len >= M * self.unit.min_length() {
            while cnt < N {
                if let Some(pair) = iter.next() {
                    if self.unit.is_match(&pair.1) && self.cond.check(ctx, &pair)? {
                        cnt += 1;
                        if beg.is_none() {
                            beg = Some(pair.0);
                        }
                        continue;
                    } else {
                        end = Some(pair);
                    }
                }
                break;
            }
            if cnt >= M {
                let end = end.or_else(|| iter.next());
                let len = calc_length(beg, end.map(|v| v.0), remaining_len);

                ret = Ok(new_span_inc(ctx, len));
            }
        }
        crate::debug_regex_reval!("Between", cnt, ret)
    }
}

///
/// Matches a sequence of elements with runtime-specified repetition bounds and context validation.
///
/// [`Times`] provides dynamic control over sequence length while maintaining per-element
/// context awareness, enforcing that every matched element satisfies both:
/// 1. **Base pattern match**: Core element validation ([`Neu`])
/// 2. **Context condition**: Runtime constraints ([`NeuCond`])
///
/// Matching occurs within the runtime-defined range specified by `range`, supporting all standard
/// range types (inclusive/exclusive bounds, unbounded ranges). This combinator bridges the gap between
/// strict compile-time patterns and dynamic runtime requirements.
///
/// # Regex
///
/// Matches elements according to the specified range bounds:
/// - **Success**: Returns span covering all matched elements
///   - Requires element count `cnt` where `range.contains(&cnt)` is true
///   - **Every element** must satisfy BOTH conditions:
///     a. `unit.is_match(item)` returns true
///     b. `cond.check()` returns true for the context
///   - Stops early when:
///     - Range upper bound is reached (based on bound type)
///     - An element fails either condition
/// - **Failure**: Returns error if:
///   - Total matched elements not in specified range
///   - First element fails when minimum bound > 0
/// - **Special cases**:
///   - Empty ranges (e.g., `5..3`) always fail
///   - Unbounded ranges (e.g., `3..`) match as many as possible
///   - Zero-length ranges (e.g., `0..0`) only match empty sequences
///
/// # Ctor
///
/// Uses identical matching logic as regex mode, then constructs a value from the result.
/// The specific constructed value depends on the active handler implementation.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
///     let hex = 'a'..'g';
///     let hex = hex.times(1..7);
///     let mut ctx = CharsCtx::new("aabbccgg");
///
///     assert_eq!(ctx.try_mat(&hex)?, Span::new(0, 6));
/// #   Ok(())
/// # }
/// ```
pub struct Times<C, U, I = EmptyCond> {
    unit: U,
    cond: I,
    range: CRange<usize>,
    marker: PhantomData<C>,
}

impl_not_for_regex!(Times<C, U, I>);

impl<C, U, I> Debug for Times<C, U, I>
where
    U: Debug,
    I: Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Times")
            .field("unit", &self.unit)
            .field("cond", &self.cond)
            .field("range", &self.range)
            .finish()
    }
}

impl<C, U, I> Clone for Times<C, U, I>
where
    U: Clone,
    I: Clone,
{
    fn clone(&self) -> Self {
        Self {
            unit: self.unit.clone(),
            cond: self.cond.clone(),
            range: self.range,
            marker: self.marker,
        }
    }
}

impl<C, U, I> Copy for Times<C, U, I>
where
    U: Copy,
    I: Copy,
{
}

impl<C, U, I> Times<C, U, I> {
    pub const fn new(unit: U, range: CRange<usize>, cond: I) -> Self {
        Self {
            unit,
            range,
            cond,
            marker: PhantomData,
        }
    }

    pub const fn unit(&self) -> &U {
        &self.unit
    }

    pub const fn range(&self) -> &CRange<usize> {
        &self.range
    }

    pub const fn unit_mut(&mut self) -> &mut U {
        &mut self.unit
    }

    pub const fn range_mut(&mut self) -> &mut CRange<usize> {
        &mut self.range
    }

    pub fn set_unit(&mut self, unit: U) -> &mut Self {
        self.unit = unit;
        self
    }

    pub fn set_range(&mut self, range: impl Into<CRange<usize>>) -> &mut Self {
        self.range = range.into();
        self
    }
}

impl<'a, C, U, I> Condition<'a, C> for Times<C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Context<'a>,
{
    type Out<F> = Times<C, U, F>;

    fn set_cond<F>(self, cond: F) -> Self::Out<F>
    where
        F: NeuCond<'a, C>,
    {
        Times::<C, U, F>::new(self.unit, self.range, cond)
    }
}

impl<'a, U, C, O, I, H> Ctor<C, O, H> for Times<C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Match<'a>,
    H: Handler<C, Out = O>,
{
    #[inline(always)]
    fn construct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
        let ret = ctx.try_mat(self);

        func.invoke(ctx, &ret?).map_err(Into::into)
    }
}

impl<'a, U, C, I> Regex<C> for Times<C, U, I>
where
    U: Neu<C::Item>,
    I: NeuCond<'a, C>,
    C: Context<'a>,
{
    #[inline(always)]
    fn try_parse(&self, ctx: &mut C) -> Result<Span, crate::err::Error> {
        let mut cnt = 0;
        let mut beg = None;
        let mut end = None;
        let mut ret = Err(Error::Times);
        let mut iter = ctx.peek()?;
        let remaining_len = ctx.len() - ctx.offset();

        fn bound_checker(max: Option<usize>) -> impl Fn(usize) -> bool {
            move |val| max.map(|max| val < max).unwrap_or(true)
        }

        // only check end bound
        let cond = bound_checker(match self.range.end_bound() {
            core::ops::Bound::Included(max) => Some(*max),
            core::ops::Bound::Excluded(max) => Some(max.saturating_sub(1)),
            core::ops::Bound::Unbounded => None,
        });

        let min_length = || match self.range.start_bound() {
            core::ops::Bound::Included(max) => max * self.unit.min_length(),
            core::ops::Bound::Excluded(max) => max.saturating_sub(1) * self.unit.min_length(),
            core::ops::Bound::Unbounded => 0,
        };

        crate::debug_regex_beg!("Times", self.range, ctx.offset());
        if remaining_len >= min_length() {
            while cond(cnt) {
                if let Some(pair) = iter.next() {
                    if self.unit.is_match(&pair.1) && self.cond.check(ctx, &pair)? {
                        cnt += 1;
                        if beg.is_none() {
                            beg = Some(pair.0);
                        }
                        continue;
                    } else {
                        end = Some(pair);
                    }
                }
                break;
            }
            if self.range.contains(&cnt) {
                let end = end.or_else(|| iter.next());
                let len = calc_length(beg, end.map(|v| v.0), remaining_len);

                ret = Ok(new_span_inc(ctx, len));
            }
        }
        crate::debug_regex_reval!("Times", cnt, ret)
    }
}