whitehole 0.8.0

A simple, fast, intuitive parser combinator framework for Rust.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Overload `*` operator for [`Combinator`].
//!
//! `Combinator * Repeat` will create a new combinator to repeat the original combinator
//! with the given [`Repeat`] range.
//! The new combinator will return the output with the folded value
//! and the rest of the input text after the last repetition is executed,
//! or reject if the repetition is not satisfied.
//!
//! `0` is a valid repetition value.
//! # Basics
//! Use `*` to repeat a combinator:
//! ```
//! # use whitehole::{combinator::{eat, Combinator}, action::Action};
//! # fn t(_: Combinator<impl Action<Text = str>>) {}
//! // repeat the combinator for 2 times
//! # t(
//! eat("true") * 2
//! # );
//! // similar to
//! # t(
//! eat("true") + "true"
//! # );
//!
//! // repeat the combinator with a range, greedy
//! # t(
//! eat("true") * (1..=3)
//! # );
//! // similar to but faster than
//! # t(
//! (eat("true") + "true" + "true") | (eat("true") + "true") | eat("true")
//! # );
//!
//! // repeat for 0 or more times
//! # t(
//! eat("true") * (..)
//! # );
//! # t(
//! eat("true") * (..=3)
//! # );
//!
//! // repeating for 0 times will always accept with 0 bytes digested
//! # t(
//! eat("true") * 0
//! # );
//! # t(
//! eat("true") * (..1)
//! # );
//! # t(
//! eat("true") * (..=0)
//! # );
//! ```
//! # Accumulate Values
//! ## To an Array
//! If the repetition value is known at compile time and the `Value` type is `Clone`,
//! you can use `* [v; len]` to accumulate the values to an array.
//! ```
//! # use whitehole::{combinator::next, parser::Parser};
//! let entry = {
//!   // accept one ascii digit at a time
//!   next(|c| c.is_ascii_digit())
//!     // convert the char to a number
//!     .select(|accepted| accepted.content().as_bytes()[0] - b'0')
//!     // repeat for 3 times, accumulate the values to an array
//!     * [0; 3]
//! };
//!
//! // parse "123"
//! assert_eq!(
//!   Parser::builder().entry(entry).build("123").next().unwrap().value,
//!   [1, 2, 3]
//! )
//! ```
//! ## Ad-hoc Accumulator
//! You can use [`Combinator::fold`]
//! to specify an ad-hoc accumulator after performing `*`.
//! ```
//! # use whitehole::{combinator::next, parser::Parser};
//! let entry = {
//!   // accept one ascii digit at a time
//!   next(|c| c.is_ascii_digit())
//!     // convert the char to a number
//!     .select(|accepted| (accepted.content().as_bytes()[0] - b'0') as usize)
//!     // repeat for 1 or more times
//!     * (1..)
//! }
//! // init accumulator with 0, and fold values
//! .fold(|| 0 as usize, |acc, value| acc * 10 + value);
//!
//! // parse "123" to 123
//! assert_eq!(
//!   Parser::builder().entry(entry).build("123").next().unwrap().value,
//!   123
//! )
//! ```
//! ## To the Heap
//! If your accumulator requires heap allocation,
//! each time the combinator is executed, the accumulator will be re-allocated and dropped.
//! That's not efficient.
//!
//! To optimize the performance,
//! you can fold the values to [`Parser::heap`](crate::parser::Parser::heap) to prevent re-allocation.
//! ```
//! # use whitehole::{combinator::contextual, parser::Parser};
//!
//! // generate contextual combinators
//! contextual!((), Vec<i32>);
//!
//! let entry = {
//!   // eat one char, accumulate some value in the heap
//!   take(1).then(|accepted| accepted.heap.push(1))
//!     // repeat for 1 or more times
//!     * (1..)
//! }.prepare(|input| input.heap.clear()); // clear the vec before executing this combinator
//!
//! // create a re-usable heap
//! let mut parser = Parser::builder().heap(vec![]).entry(entry).build("123");
//! parser.next();
//! assert_eq!(parser.heap, vec![1, 1, 1]);
//! ```
//! # Separator
//! You can use [`Combinator::sep`]
//! to specify an other combinator as the separator after performing `*`.
//! ```
//! # use whitehole::{combinator::eat, parser::Parser};
//! let entry = (eat('a') * (1..)).sep(',');
//! assert_eq!(
//!   Parser::builder().entry(entry).build("a,a,a").next().unwrap().digested,
//!   5
//! )
//! ```
//! You can use [`Combinator::sep`] with [`Combinator::fold`]:
//! ```
//! # use whitehole::{combinator::eat, parser::Parser};
//! let entry = (eat('a').bind(1) * (1..)).sep(',').fold(|| 0, |v, acc| acc + v);
//! assert_eq!(
//!   Parser::builder().entry(entry).build("a,a,a").next().unwrap().value,
//!   3
//! );
//! ```
//! Or with array accumulator
//! ```
//! # use whitehole::{combinator::eat, parser::Parser};
//! let entry = (eat('a').bind(1) * [0; 3]).sep(',');
//! assert_eq!(
//!   Parser::builder().entry(entry).build("a,a,a").next().unwrap().value,
//!   [1, 1, 1]
//! );
//! ```
//! See [`Combinator::sep`] for more information.
mod fold;
mod repeat;
mod sep;

pub use repeat::*;
pub use sep::*;

use crate::{
  action::{Action, Input, Output},
  combinator::Combinator,
  digest::Digest,
  instant::Instant,
};
use std::{
  ops::{self, RangeFrom},
  slice::SliceIndex,
};

/// An [`Action`] created by the `*` operator.
/// See [`ops::mul`](crate::combinator::ops::mul) for more information.
#[derive(Debug, Clone, Copy)]
pub struct Mul<Lhs, Rhs, Sep = NoSep<Lhs>, Init = fn(), Fold = fn((), ())> {
  lhs: Lhs,
  rhs: Rhs,
  sep: Sep,
  init: Init,
  fold: Fold,
}

impl<Lhs, Rhs> Mul<Lhs, Rhs> {
  #[inline]
  const fn new(lhs: Lhs, rhs: Rhs) -> Self {
    Self {
      lhs,
      rhs,
      sep: NoSep::new(),
      init: || (),
      fold: |_, _| (),
    }
  }
}

impl<Lhs: Action, Rhs: Repeat> ops::Mul<Rhs> for Combinator<Lhs> {
  type Output = Combinator<Mul<Lhs, Rhs, NoSep<Lhs>>>;

  /// See [`ops::mul`](crate::combinator::ops::mul) for more information.
  #[inline]
  fn mul(self, rhs: Rhs) -> Self::Output {
    Self::Output::new(Mul::new(self.action, rhs))
  }
}

impl<Lhs: Action, const N: usize> ops::Mul<[Lhs::Value; N]> for Combinator<Lhs> {
  type Output = Combinator<Mul<Lhs, [Lhs::Value; N], NoSep<Lhs>>>;

  /// See [`ops::mul`](crate::combinator::ops::mul) for more information.
  #[inline]
  fn mul(self, rhs: [Lhs::Value; N]) -> Self::Output {
    Self::Output::new(Mul::new(self.action, rhs))
  }
}

unsafe impl<
    Lhs: Action<Text: Digest>,
    Rhs: Repeat,
    Sep: Action<Text = Lhs::Text, State = Lhs::State, Heap = Lhs::Heap>,
    Acc,
    Init: Fn() -> Acc,
    Fold: Fn(Acc, Lhs::Value) -> Acc,
  > Action for Mul<Lhs, Rhs, Sep, Init, Fold>
where
  RangeFrom<usize>: SliceIndex<Lhs::Text, Output = Lhs::Text>,
{
  type Text = Lhs::Text;
  type State = Lhs::State;
  type Heap = Lhs::Heap;
  type Value = Acc;

  #[inline]
  fn exec(
    &self,
    mut input: Input<&Instant<&Self::Text>, &mut Self::State, &mut Self::Heap>,
  ) -> Option<Output<Self::Value>> {
    let mut repeated = 0;
    let mut output = Output {
      value: (self.init)(),
      digested: 0,
    };

    let mut digested_with_sep = 0;
    while unsafe { self.rhs.validate(repeated) } {
      let Some(value_output) = self.lhs.exec(
        input.reborrow_with(&unsafe { input.instant.to_digested_unchecked(digested_with_sep) }),
      ) else {
        break;
      };
      repeated += 1;
      output.value = (self.fold)(output.value, value_output.value);
      // SAFETY: since `slice::len` is usize, so `output.digested` must be a valid usize
      debug_assert!(usize::MAX - digested_with_sep > value_output.digested);
      output.digested = unsafe { digested_with_sep.unchecked_add(value_output.digested) };

      let Some(sep_output) = self.sep.exec(
        input.reborrow_with(&unsafe { input.instant.to_digested_unchecked(output.digested) }),
      ) else {
        break;
      };
      // SAFETY: since `slice::len` is usize, so `output.digested` must be a valid usize
      debug_assert!(usize::MAX - output.digested > sep_output.digested);
      digested_with_sep = unsafe { output.digested.unchecked_add(sep_output.digested) };
    }

    self.rhs.accept(repeated).then_some(output)
  }
}

unsafe impl<
    Lhs: Action<Text: Digest, Value: Clone>,
    const N: usize,
    Sep: Action<Text = Lhs::Text, State = Lhs::State, Heap = Lhs::Heap>,
  > Action for Mul<Lhs, [Lhs::Value; N], Sep>
where
  RangeFrom<usize>: SliceIndex<Lhs::Text, Output = Lhs::Text>,
{
  type Text = Lhs::Text;
  type State = Lhs::State;
  type Heap = Lhs::Heap;
  type Value = [Lhs::Value; N];

  #[inline]
  fn exec(
    &self,
    mut input: Input<&Instant<&Self::Text>, &mut Self::State, &mut Self::Heap>,
  ) -> Option<Output<Self::Value>> {
    let mut output: Output<[<Lhs as Action>::Value; N]> = Output {
      // don't use `mem::zeroed` to initialize the array
      // since the Lhs::Value may implement Drop and causing UB when the array is dropped but not fully filled
      value: self.rhs.clone(),
      digested: 0,
    };

    let mut digested_with_sep = 0;
    for i in 0..N {
      let value_output = self.lhs.exec(
        input.reborrow_with(&unsafe { input.instant.to_digested_unchecked(digested_with_sep) }),
      )?;
      // SAFETY: `i` must be in `0..N`
      debug_assert!(i < N);
      *unsafe { output.value.get_unchecked_mut(i) } = value_output.value;
      // SAFETY: since `slice::len` is usize, so `output.digested` must be a valid usize
      debug_assert!(usize::MAX - digested_with_sep > value_output.digested);
      output.digested = unsafe { digested_with_sep.unchecked_add(value_output.digested) };

      // SAFETY: `i` must be smaller than `N` and `N` is a valid usize
      if unsafe { i.unchecked_add(1) } == N {
        // skip the last separator if `N` is reached
        break;
      }

      let sep_output = self.sep.exec(
        input.reborrow_with(&unsafe { input.instant.to_digested_unchecked(output.digested) }),
      )?;
      // SAFETY: since `slice::len` is usize, so `output.digested` must be a valid usize
      debug_assert!(usize::MAX - output.digested > sep_output.digested);
      digested_with_sep = unsafe { output.digested.unchecked_add(sep_output.digested) };
    }

    Some(output)
  }
}

#[cfg(test)]
mod tests {
  use crate::{
    action::{Action, Input, Output},
    combinator::{bytes, take},
    digest::Digest,
    instant::Instant,
  };
  use std::{fmt::Debug, ops::RangeFrom, slice::SliceIndex};

  fn helper<Text: ?Sized + Digest>(
    action: impl Action<Text = Text, State = (), Heap = (), Value = ()>,
    input: &Text,
    expected: Option<usize>,
  ) where
    RangeFrom<usize>: SliceIndex<Text, Output = Text>,
  {
    assert_eq!(
      action.exec(Input {
        instant: &Instant::new(input),
        state: &mut (),
        heap: &mut ()
      }),
      expected.map(|digested| Output {
        value: (),
        digested,
      })
    )
  }

  #[test]
  fn combinator_mul_usize() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * 3, "1234", Some(3));
    helper(accepter_b() * 3, b"1234", Some(3));

    // reject if not enough repetitions
    helper(accepter() * 3, "12", None);
    helper(accepter_b() * 3, b"12", None);

    // reject with rejector
    helper(rejecter() * 3, "123", None);
    helper(rejecter_b() * 3, b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    helper(accepter() * 0, "123", Some(0));
    helper(accepter_b() * 0, b"123", Some(0));
    // even with rejecter
    helper(rejecter() * 0, "123", Some(0));
    helper(rejecter_b() * 0, b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (2..4), "1234", Some(3));
    helper(accepter_b() * (2..4), b"1234", Some(3));

    // reject if not enough repetitions
    helper(accepter() * (2..4), "1", None);
    helper(accepter_b() * (2..4), b"1", None);

    // reject with rejector
    helper(rejecter() * (2..4), "123", None);
    helper(rejecter_b() * (2..4), b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    helper(accepter() * (0..1), "123", Some(0));
    helper(accepter_b() * (0..1), b"123", Some(0));
    // even with rejecter
    helper(rejecter() * (0..1), "123", Some(0));
    helper(rejecter_b() * (0..1), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range_from() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (2..), "1234", Some(4));
    helper(accepter_b() * (2..), b"1234", Some(4));

    // reject if not enough repetitions
    helper(accepter() * (2..), "1", None);
    helper(accepter_b() * (2..), b"1", None);

    // reject with rejector
    helper(rejecter() * (2..), "123", None);
    helper(rejecter_b() * (2..), b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    // even with rejecter
    helper(rejecter() * (0..), "123", Some(0));
    helper(rejecter_b() * (0..), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range_full() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (..), "1234", Some(4));
    helper(accepter_b() * (..), b"1234", Some(4));

    // repeat for 0 times will always accept with 0 bytes digested
    // even with rejecter
    helper(rejecter() * (..), "123", Some(0));
    helper(rejecter_b() * (..), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range_inclusive() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (2..=3), "1234", Some(3));
    helper(accepter_b() * (2..=3), b"1234", Some(3));

    // reject if not enough repetitions
    helper(accepter() * (2..=3), "1", None);
    helper(accepter_b() * (2..=3), b"1", None);

    // reject with rejector
    helper(rejecter() * (2..=3), "123", None);
    helper(rejecter_b() * (2..=3), b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    helper(accepter() * (0..=0), "123", Some(0));
    helper(accepter_b() * (0..=0), b"123", Some(0));
    // even with rejecter
    helper(rejecter() * (0..=0), "123", Some(0));
    helper(rejecter_b() * (0..=0), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range_to() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (..4), "1234", Some(3));
    helper(accepter_b() * (..4), b"1234", Some(3));

    // repeat for 0 times will always accept with 0 bytes digested
    helper(accepter() * (..1), "123", Some(0));
    helper(accepter_b() * (..1), b"123", Some(0));
    // even with rejecter
    helper(rejecter() * (..1), "123", Some(0));
    helper(rejecter_b() * (..1), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_range_to_inclusive() {
    let accepter = || take(1);
    let accepter_b = || bytes::take(1);
    let rejecter = || take(0).reject(|_| true);
    let rejecter_b = || bytes::take(0).reject(|_| true);

    // normal
    helper(accepter() * (2..=3), "1234", Some(3));
    helper(accepter_b() * (2..=3), b"1234", Some(3));

    // reject if not enough repetitions
    helper(accepter() * (2..=3), "1", None);
    helper(accepter_b() * (2..=3), b"1", None);

    // reject with rejector
    helper(rejecter() * (2..=3), "123", None);
    helper(rejecter_b() * (2..=3), b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    helper(accepter() * (0..=0), "123", Some(0));
    helper(accepter_b() * (0..=0), b"123", Some(0));
    // even with rejecter
    helper(rejecter() * (0..=0), "123", Some(0));
    helper(rejecter_b() * (0..=0), b"123", Some(0));
  }

  #[test]
  fn combinator_mul_array() {
    fn helper<Text: ?Sized + Digest, Value: PartialEq + Debug>(
      action: impl Action<Text = Text, State = (), Heap = (), Value = Value>,
      input: &Text,
      expected: Option<Output<Value>>,
    ) where
      RangeFrom<usize>: SliceIndex<Text, Output = Text>,
    {
      assert_eq!(
        action.exec(Input {
          instant: &Instant::new(input),
          state: &mut (),
          heap: &mut ()
        }),
        expected
      )
    }

    let accepter = || take(1).select(|accepted| accepted.content().as_bytes()[0] - b'0');
    let accepter_b = || bytes::take(1).select(|accepted| accepted.content()[0] - b'0');
    let rejecter = || accepter().reject(|_| true);
    let rejecter_b = || accepter_b().reject(|_| true);

    // normal
    helper(
      accepter() * [0; 3],
      "123",
      Some(Output {
        value: [1, 2, 3],
        digested: 3,
      }),
    );
    helper(
      accepter_b() * [0; 3],
      b"123",
      Some(Output {
        value: [1, 2, 3],
        digested: 3,
      }),
    );

    // reject if not enough repetitions
    helper(accepter() * [0; 3], "12", None);
    helper(accepter_b() * [0; 3], b"12", None);

    // reject with rejector
    helper(rejecter() * [0; 3], "123", None);
    helper(rejecter_b() * [0; 3], b"123", None);

    // repeat for 0 times will always accept with 0 bytes digested
    helper(
      accepter() * [0; 0],
      "123",
      Some(Output {
        value: [],
        digested: 0,
      }),
    );
    helper(
      accepter_b() * [0; 0],
      b"123",
      Some(Output {
        value: [],
        digested: 0,
      }),
    );
    // even with rejecter
    helper(
      rejecter_b() * [0; 0],
      b"123",
      Some(Output {
        value: [],
        digested: 0,
      }),
    );
  }
}