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
use super::*;

/// Character parsing methods.
impl<'i> ParseState<'i> {
    /// Match a single character.
    ///
    /// ```ygg
    /// 'c'
    /// ```
    #[inline]
    pub fn match_char(self, target: char) -> ParseResult<'i, char> {
        match self.get_character(0) {
            Some(c) if c.eq(&target) => self.advance(target).finish(target),
            _ => StopBecause::missing_character(target, self.start_offset)?,
        }
    }
    /// Match a character in given range.
    ///
    /// ```ygg
    /// [a-z]
    /// ```
    #[inline]
    pub fn match_char_range(self, start: char, end: char) -> ParseResult<'i, char> {
        match self.get_character(0) {
            Some(c) if c <= end && c >= start => self.advance(c).finish(c),
            _ => StopBecause::missing_character_range(start, end, self.start_offset)?,
        }
    }
    /// Assert end of file
    /// ```ygg
    /// p $
    /// ```
    #[inline]
    pub fn match_eof(self) -> ParseResult<'i, ()> {
        match self.get_character(0) {
            Some(_) => StopBecause::expect_eof(self.start_offset)?,
            None => self.finish(()),
        }
    }
    /// Match any character, except `EOF`.
    #[inline]
    pub fn match_char_any(self) -> ParseResult<'i, char> {
        self.match_char_if(|_| true, "ANY")
    }
    /// Match a character with given set.
    #[inline]
    #[cfg(feature = "ucd-trie")]
    pub fn match_char_set(self, set: TrieSetSlice, message: &'static str) -> ParseResult<'i, char> {
        self.match_char_if(|c| set.contains_char(c), message)
    }
    /// Parsing a character with given rule.
    #[inline]
    pub fn match_char_if<F>(self, mut predicate: F, message: &'static str) -> ParseResult<'i, char>
    where
        F: FnMut(char) -> bool,
    {
        match self.get_character(0) {
            Some(c) if predicate(c) => self.advance(c).finish(c),
            _ => StopBecause::must_be(message, self.start_offset)?,
        }
    }
}

impl<'i> ParseState<'i> {
    /// Match a static string pattern.
    #[inline]
    pub fn match_str_pattern<P>(self, target: P, message: &'static str) -> ParseResult<'i, &'i str>
    where
        P: Pattern<'i>,
    {
        let mut searcher = target.into_searcher(&self.residual);
        match searcher.next_match() {
            Some((0, end)) => self.advance_view(end),
            _ => StopBecause::missing_string(message, self.start_offset)?,
        }
    }
    /// Match a static string.
    #[inline]
    pub fn match_str(self, target: &'static str) -> ParseResult<'i, &'i str> {
        let s = match self.get_string(0..target.len()) {
            Some(s) if s.eq(target) => s.len(),
            _ => StopBecause::missing_string(target, self.start_offset)?,
        };
        self.advance_view(s)
    }

    /// Match a static string.
    #[inline]
    pub fn match_str_insensitive(self, target: &'static str) -> ParseResult<'i, &'i str> {
        let s = match self.get_string(0..target.len()) {
            Some(s) if s.eq_ignore_ascii_case(target) => s.len(),
            _ => StopBecause::missing_string(target, self.start_offset)?,
        };
        self.advance_view(s)
    }
    /// Match a string with given regex.
    #[cfg(feature = "regex-automata")]
    pub fn match_regex(&self, re: &Regex, message: &'static str) -> ParseResult<'i, MultiMatch> {
        match re.try_find_leftmost(self.residual.as_bytes()) {
            Ok(Some(m)) => {
                let new = self.advance(m.end());
                Pending(new, m)
            }
            Ok(None) => StopBecause::must_be(message, self.start_offset)?,
            Err(e) => match e {
                regex_automata::MatchError::Quit { byte: _, offset } => {
                    StopBecause::custom_error("regex match quit", offset, offset)?
                }
                regex_automata::MatchError::GaveUp { offset } => {
                    StopBecause::custom_error("regex match gave up", offset, offset)?
                }
            },
        }
    }

    /// Match a string with given conditional.
    #[inline]
    pub fn match_str_if<F>(self, mut predicate: F, message: &'static str) -> ParseResult<'i, &'i str>
    where
        F: FnMut(char) -> bool,
    {
        let mut offset = 0;
        for char in self.residual.chars() {
            match predicate(char) {
                false => offset += char.len_utf8(),
                true => break,
            }
        }
        if offset == 0 {
            StopBecause::missing_string(message, self.start_offset)?;
        }
        self.advance(offset).finish(&self.residual[..offset])
    }
    /// Match a string with given conditional.
    #[inline]
    pub fn match_str_until<F>(self, mut predicate: F, message: &'static str) -> ParseResult<'i, &'i str>
    where
        F: FnMut(char) -> bool,
    {
        self.match_str_if(|c| !predicate(c), message)
    }
}

impl<'i> ParseState<'i> {
    /// Simple suffix call form
    #[inline]
    pub fn match_fn<T, F>(self, mut parse: F) -> ParseResult<'i, T>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        parse(self)
    }
    /// Parses a sequence of 0 or more repetitions of the given parser.
    /// ```regex
    /// p*
    /// p+ <=> p p*
    /// ```
    #[inline]
    pub fn match_repeats<T, F>(self, mut parse: F) -> ParseResult<'i, Vec<T>>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        let mut result = Vec::new();
        let mut state = self;
        loop {
            match parse(state) {
                Pending(new, value) => {
                    state = new;
                    result.push(value);
                }
                Stop(_) => break,
            }
        }
        state.finish(result)
    }

    /// Parses a sequence of 0 or more repetitions of the given parser.
    /// ```regex
    /// p* <=> p{0, \inf}
    /// p+ <=> p{1, \inf}
    /// p{min, max}
    /// ```
    #[inline]
    pub fn match_repeat_m_n<T, F>(self, min: usize, max: usize, mut parse: F) -> ParseResult<'i, Vec<T>>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        let mut result = Vec::new();
        let mut count = 0;
        let position = self.start_offset;
        let mut state = self;
        loop {
            match parse(state.clone()) {
                Pending(new, value) => {
                    state = new;
                    result.push(value);
                    count += 1;
                    if count >= max {
                        break;
                    }
                }
                Stop(_) => break,
            };
        }
        if count < min {
            Err(StopBecause::ExpectRepeats { min, current: count, position })?
        }
        state.finish(result)
    }
    /// Parse an optional element
    /// ```regex
    /// p?
    /// ```
    #[inline]
    pub fn match_optional<T, F>(self, mut parse: F) -> ParseResult<'i, Option<T>>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        match parse(self.clone()) {
            Pending(state, value) => state.finish(Some(value)),
            Stop(_) => self.finish(None),
        }
    }
    /// Match but does not return the result
    #[inline]
    pub fn skip<F, T>(self, mut parse: F) -> ParseState<'i>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        match parse(self.clone()) {
            Pending(new, _) => new,
            Stop(_) => self,
        }
    }
    /// Zero-width positive match, does not consume input
    ///
    /// Used to be a external rule, which used as assert
    ///
    /// ```regex
    /// &ahead p
    /// p &after
    /// ```
    #[inline]
    pub fn match_positive<F, T>(self, mut parse: F, message: &'static str) -> ParseResult<'i, ()>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        match parse(self.clone()) {
            Pending(..) => self.finish(()),
            Stop(_) => Stop(StopBecause::MustBe { message, position: self.start_offset }),
        }
    }
    /// Zero-width negative match, does not consume input
    /// ```regex
    /// !ahead p
    /// p !after
    /// ```
    #[inline]
    pub fn match_negative<F, T>(self, mut parse: F, message: &'static str) -> ParseResult<'i, ()>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        match parse(self.clone()) {
            Pending(..) => Stop(StopBecause::ShouldNotBe { message, position: self.start_offset }),
            Stop(_) => self.finish(()),
        }
    }
}

impl<'i> ParseState<'i> {
    /// Parse a comment line
    /// ```regex
    /// # comment
    /// // comment
    /// ```
    #[inline]
    pub fn match_comment_line(self, head: &'static str) -> ParseResult<'i, &'i str> {
        if !self.residual.starts_with(head) {
            StopBecause::missing_string(head, self.start_offset)?;
        }
        let offset = match self.residual.find(|c| c == '\r' || c == '\n') {
            Some(s) => s,
            None => self.residual.len(),
        };
        self.advance(offset).finish(&self.residual[..offset])
    }
    /// Parse the comment block
    ///
    /// ```ygg
    /// /* */
    /// ```
    #[inline]
    pub fn match_comment_block<F, T>(self, head: &'static str, tail: &'static str) -> ParseResult<'i, ()>
    where
        F: FnMut(ParseState<'i>) -> ParseResult<T>,
    {
        if !self.residual.starts_with(head) {
            StopBecause::missing_string(head, self.start_offset)?;
        }
        let mut offset = head.len();
        let rest = &self.residual[offset..];
        match rest.find(tail) {
            Some(s) => offset += s + tail.len(),
            None => StopBecause::missing_string(tail, self.start_offset + offset)?,
        }
        self.advance(offset).finish(())
    }
}