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
mod guard;
mod policy;
#[allow(clippy::module_inception)]
mod regex;
mod span;

use std::marker::PhantomData;

use crate::err::Error;
use crate::re::Regex;
use crate::MayDebug;

pub use self::guard::CtxGuard;
pub use self::policy::PolicyCtx;
pub use self::regex::RegexCtx;
pub use self::span::Span;

pub type BytesCtx<'a> = RegexCtx<'a, [u8]>;
pub type CharsCtx<'a> = RegexCtx<'a, str>;

pub trait Context<'a> {
    type Orig: ?Sized;

    type Item;

    type Iter<'b>: Iterator<Item = (usize, Self::Item)>
    where
        Self: 'b;

    fn len(&self) -> usize;

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn offset(&self) -> usize;

    fn set_offset(&mut self, offset: usize) -> &mut Self;

    fn inc(&mut self, offset: usize) -> &mut Self;

    fn dec(&mut self, offset: usize) -> &mut Self;

    fn peek(&self) -> Result<Self::Iter<'a>, Error> {
        self.peek_at(self.offset())
    }

    fn peek_at(&self, offset: usize) -> Result<Self::Iter<'a>, Error>;

    fn orig(&self) -> Result<&'a Self::Orig, Error> {
        self.orig_at(self.offset())
    }

    fn orig_at(&self, offset: usize) -> Result<&'a Self::Orig, Error>;

    fn orig_sub(&self, offset: usize, len: usize) -> Result<&'a Self::Orig, Error>;

    fn clone_with(&self, orig: &'a Self::Orig) -> Self;
}

pub trait Ret: MayDebug
where
    Self: Sized,
{
    fn fst(&self) -> usize;

    fn snd(&self) -> usize;

    fn is_zero(&self) -> bool;

    fn add_assign(&mut self, other: Self) -> &mut Self;

    fn from_ctx<'a, C>(ctx: &mut C, info: (usize, usize)) -> Self
    where
        C: Context<'a>;
}

pub trait Match<C> {
    fn is_mat<Pat: Regex<C> + ?Sized>(&mut self, pat: &Pat) -> bool {
        self.try_mat_t(pat).is_ok()
    }

    fn try_mat_t<Pat: Regex<C> + ?Sized>(&mut self, pat: &Pat) -> Result<Pat::Ret, Error>;

    fn try_mat<Pat: Regex<C, Ret = Span> + ?Sized>(
        &mut self,
        pat: &Pat,
    ) -> Result<Pat::Ret, Error> {
        self.try_mat_t(pat)
    }
}

pub trait PolicyMatch<C, B> {
    fn try_mat_policy<Pat>(&mut self, pat: &Pat, b_policy: &B) -> Result<Pat::Ret, Error>
    where
        Pat: Regex<C> + ?Sized;
}

impl Ret for () {
    fn fst(&self) -> usize {
        0
    }

    fn snd(&self) -> usize {
        0
    }

    fn is_zero(&self) -> bool {
        true
    }

    fn add_assign(&mut self, _: Self) -> &mut Self {
        self
    }

    fn from_ctx<'a, C>(_: &mut C, _: (usize, usize)) -> Self
    where
        C: Context<'a>,
    {
    }
}

pub trait BPolicy<C> {
    fn invoke_policy(&self, ctx: &mut C) -> Result<(), Error>;
}

impl<C, F> BPolicy<C> for F
where
    F: Fn(&mut C) -> Result<(), Error>,
{
    fn invoke_policy(&self, ctx: &mut C) -> Result<(), Error> {
        (self)(ctx)
    }
}

impl<C, B> BPolicy<C> for Option<B>
where
    B: BPolicy<C>,
{
    fn invoke_policy(&self, ctx: &mut C) -> Result<(), Error> {
        match self {
            Some(ref_) => ref_.invoke_policy(ctx),
            None => unimplemented!(""),
        }
    }
}

#[derive(Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RePolicy<C, T> {
    regex: T,
    marker: PhantomData<C>,
}

impl<C, T> Clone for RePolicy<C, T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        Self {
            regex: self.regex.clone(),
            marker: self.marker,
        }
    }
}

impl<C, T> RePolicy<C, T> {
    pub fn new(regex: T) -> Self {
        Self {
            regex,
            marker: PhantomData,
        }
    }
}

impl<'a, C, T> BPolicy<C> for RePolicy<C, T>
where
    C::Orig: 'a,
    T: Regex<C>,
    C: Context<'a> + Match<C>,
{
    fn invoke_policy(&self, ctx: &mut C) -> Result<(), Error> {
        ctx.try_mat_t(&self.regex)?;
        Ok(())
    }
}

pub fn re_policy<C, T>(regex: T) -> RePolicy<C, T> {
    RePolicy::new(regex)
}