oni_comb_parser_rs/extension/parsers/
repeat_parsers.rs

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
use crate::core::Parsers;
use std::fmt::Debug;

use crate::utils::RangeArgument;

pub trait RepeatParsers: Parsers {
  /// `rep(5)` repeat p exactly 5 times
  /// `rep(0..)` repeat p zero or more times
  /// `rep(1..)` repeat p one or more times
  /// `rep(1..4)` match p at least 1 and at most 3 times
  fn repeat<'a, I, A, R>(parser: Self::P<'a, I, A>, range: R) -> Self::P<'a, I, Vec<A>>
  where
    R: RangeArgument<usize> + Debug + 'a,
    A: Debug + 'a, {
    Self::repeat_sep::<'a, I, A, (), R>(parser, range, None)
  }

  fn many0<'a, I, A>(parser: Self::P<'a, I, A>) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a, {
    Self::repeat_sep(parser, 0.., None as Option<Self::P<'a, I, ()>>)
  }

  fn many1<'a, I, A>(parser: Self::P<'a, I, A>) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a, {
    Self::repeat_sep(parser, 1.., None as Option<Self::P<'a, I, ()>>)
  }

  fn many_n_m<'a, I, A>(parser: Self::P<'a, I, A>, n: usize, m: usize) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a, {
    Self::repeat_sep(parser, n..=m, None as Option<Self::P<'a, I, ()>>)
  }

  fn count<'a, I, A>(parser: Self::P<'a, I, A>, n: usize) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a, {
    Self::repeat_sep(parser, n, None as Option<Self::P<'a, I, ()>>)
  }

  fn repeat_sep<'a, I, A, B, R>(
    parser: Self::P<'a, I, A>,
    range: R,
    separator: Option<Self::P<'a, I, B>>,
  ) -> Self::P<'a, I, Vec<A>>
  where
    R: RangeArgument<usize> + Debug + 'a,
    A: Debug + 'a,
    B: Debug + 'a;

  fn many0_sep<'a, I, A, B>(parser: Self::P<'a, I, A>, separator: Self::P<'a, I, B>) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a,
    B: Debug + 'a, {
    Self::repeat_sep(parser, 0.., Some(separator))
  }

  fn many1_sep<'a, I, A, B>(parser: Self::P<'a, I, A>, separator: Self::P<'a, I, B>) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a,
    B: Debug + 'a, {
    Self::repeat_sep(parser, 1.., Some(separator))
  }

  fn many_n_m_sep<'a, I, A, B>(
    parser: Self::P<'a, I, A>,
    n: usize,
    m: usize,
    separator: Self::P<'a, I, B>,
  ) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a,
    B: Debug + 'a, {
    Self::repeat_sep(parser, n..=m, Some(separator))
  }

  fn count_sep<'a, I, A, B>(
    parser: Self::P<'a, I, A>,
    n: usize,
    separator: Self::P<'a, I, B>,
  ) -> Self::P<'a, I, Vec<A>>
  where
    A: Debug + 'a,
    B: Debug + 'a, {
    Self::repeat_sep(parser, n, Some(separator))
  }
}