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
use crate::{error::RepetitionError, Span};
use super::Rule;
#[derive(Clone)]
pub struct Repetition<'i> {
pub rule: Rule<'i>,
pub kind: RepetitionKind,
pub quantifier: Quantifier,
pub span: Span,
}
impl<'i> Repetition<'i> {
pub(crate) fn new(
rule: Rule<'i>,
kind: RepetitionKind,
quantifier: Quantifier,
span: Span,
) -> Self {
Repetition { rule, kind, quantifier, span }
}
#[cfg(feature = "dbg")]
pub(super) fn pretty_print(&self, buf: &mut crate::PrettyPrinter) {
self.rule.pretty_print(buf, true);
match self.kind {
RepetitionKind { lower_bound, upper_bound: None } => {
buf.push('{');
buf.write_fmt(lower_bound);
buf.push(',');
buf.push('}');
}
RepetitionKind { lower_bound, upper_bound: Some(upper_bound) }
if lower_bound == upper_bound =>
{
buf.push('{');
buf.write_fmt(lower_bound);
buf.push('}');
}
RepetitionKind { lower_bound, upper_bound: Some(upper_bound) } => {
buf.push('{');
buf.write_fmt(lower_bound);
buf.push(',');
buf.write_fmt(upper_bound);
buf.push('}');
}
}
match self.quantifier {
Quantifier::Greedy => buf.push_str(" greedy"),
Quantifier::Lazy => buf.push_str(" lazy"),
Quantifier::Default => {}
}
}
}
#[derive(Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "dbg", derive(Debug))]
pub enum Quantifier {
Greedy,
Lazy,
Default,
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "dbg", derive(Debug))]
pub struct RepetitionKind {
pub lower_bound: u32,
pub upper_bound: Option<u32>,
}
impl RepetitionKind {
pub(crate) fn zero_inf() -> Self {
RepetitionKind { lower_bound: 0, upper_bound: None }
}
pub(crate) fn one_inf() -> Self {
RepetitionKind { lower_bound: 1, upper_bound: None }
}
pub(crate) fn zero_one() -> Self {
RepetitionKind { lower_bound: 0, upper_bound: Some(1) }
}
pub(crate) fn fixed(n: u32) -> Self {
RepetitionKind { lower_bound: n, upper_bound: Some(n) }
}
}
impl TryFrom<(u32, Option<u32>)> for RepetitionKind {
type Error = RepetitionError;
fn try_from((lower_bound, upper_bound): (u32, Option<u32>)) -> Result<Self, Self::Error> {
if lower_bound > upper_bound.unwrap_or(u32::MAX) {
return Err(RepetitionError::NotAscending);
}
Ok(RepetitionKind { lower_bound, upper_bound })
}
}