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
mod matchers;
mod result;

pub use self::matchers::*;
pub use self::result::{CaptureInfo, MatchInfo, MatchResult, MatchType};

use crate::common::*;

pub type AnyMatcher<'a> = Box<dyn DynMatcher + 'a>;

pub type AnyMatcherMut<'a> = Box<dyn DynMatcherMut + 'a>;

/// This trait is used for match patterns which are pure, i.e. they have
/// no effect on the current [MatchContext].
pub trait Matcher: MatcherMut {
    /// Search for a match in the given input buffer
    ///
    /// The first match found is returned.
    fn try_match<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized;
}
impl<'a, M> Matcher for &'a M
where
    M: ?Sized + Matcher,
{
    fn try_match<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        <M as Matcher>::try_match(self, input, context)
    }
}

pub trait DynMatcher: DynMatcherMut {
    fn try_match_dyn<'input>(
        &self,
        input: Input<'input>,
        context: &dyn Context<'input, '_>,
    ) -> DiagResult<MatchResult<'input>>;
}
impl<M> DynMatcher for M
where
    M: Matcher,
{
    fn try_match_dyn<'input>(
        &self,
        input: Input<'input>,
        context: &dyn Context<'input, '_>,
    ) -> DiagResult<MatchResult<'input>> {
        self.try_match(input, context)
    }
}

/// This trait is used for match patterns which have side effects on the
/// current [MatchContext] when successful.
///
/// These matchers may bind new variables, push operands or otherwise modify the match context.
/// Implementors should ensure that such effects are only applied if a match succeeds however,
/// to avoid polluting the match context.
pub trait MatcherMut: fmt::Debug + Spanned {
    /// Search for a match in the given input buffer
    ///
    /// The first match found is returned.
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized;
}
impl<'a, M> MatcherMut for &'a M
where
    M: ?Sized + MatcherMut,
{
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        <M as MatcherMut>::try_match_mut(self, input, context)
    }
}
impl<'a> MatcherMut for (dyn DynMatcher + 'a) {
    #[inline]
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        let passthrough = context.protect();
        self.try_match_dyn(input, &passthrough)
    }
}
impl<'a> MatcherMut for Box<dyn DynMatcher + 'a> {
    #[inline]
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        (**self).try_match_mut(input, context)
    }
}

pub trait DynMatcherMut: fmt::Debug + Spanned {
    fn try_match_mut_dyn<'input>(
        &self,
        input: Input<'input>,
        context: &mut dyn Context<'input, '_>,
    ) -> DiagResult<MatchResult<'input>>;
}
impl<M> DynMatcherMut for M
where
    M: MatcherMut,
{
    fn try_match_mut_dyn<'input>(
        &self,
        input: Input<'input>,
        context: &mut dyn Context<'input, '_>,
    ) -> DiagResult<MatchResult<'input>> {
        self.try_match_mut(input, context)
    }
}
impl<'a> MatcherMut for (dyn DynMatcherMut + 'a) {
    #[inline]
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        let mut passthrough = context.protect();
        let result = self.try_match_mut_dyn(input, &mut passthrough)?;
        if result.is_ok() {
            passthrough.save();
        }
        Ok(result)
    }
}
impl<'a> MatcherMut for Box<dyn DynMatcherMut + 'a> {
    #[inline]
    fn try_match_mut<'input, 'context, C>(
        &self,
        input: Input<'input>,
        context: &mut C,
    ) -> DiagResult<MatchResult<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        (**self).try_match_mut(input, context)
    }
}