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
use super::{Parse, ParseElem, ParseLiteral, ParseSlice, RuleResult};

impl<T> Parse for [T] {
    type PositionRepr = usize;
    fn start(&self) -> usize {
        0
    }

    fn position_repr(&self, pos: usize) -> usize {
        pos
    }
}

impl<T: Clone> ParseElem for [T] {
    type Element = T;

    fn parse_elem(&self, pos: usize) -> RuleResult<T> {
        match self[pos..].first() {
            Some(c) => RuleResult::Matched(pos + 1, c.clone()),
            None => RuleResult::Failed,
        }
    }
}

impl ParseLiteral for [u8] {
    fn parse_string_literal(&self, pos: usize, literal: &str) -> RuleResult<()> {
        let l = literal.len();
        if self.len() >= pos + l && &self[pos..pos + l] == literal.as_bytes() {
            RuleResult::Matched(pos + l, ())
        } else {
            RuleResult::Failed
        }
    }
}

impl<'input, T: 'input> ParseSlice<'input> for [T] {
    type Slice = &'input [T];
    fn parse_slice(&'input self, p1: usize, p2: usize) -> &'input [T] {
        &self[p1..p2]
    }
}