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
use crate::err::{ECode, ParseError};
use crate::ptrait::ParseRes;
use std::str::{CharIndices, Chars};

#[derive(Clone, Debug)]
pub struct LCChars<'a> {
    iter: CharIndices<'a>,
    l: usize,
    c: usize,
}

impl<'a> LCChars<'a> {
    pub fn str(s: &'a str) -> Self {
        LCChars {
            iter: s.char_indices(),
            l: 0,
            c: 0,
        }
    }

    #[deprecated(since = "0.2.1", note = "use from_char_indices instead")]
    pub fn from_chars(iter: Chars<'a>) -> LCChars<'a> {
        LCChars::str(iter.as_str())
    }

    pub fn from_char_indices(iter: CharIndices<'a>) -> LCChars<'a> {
        LCChars { iter, l: 0, c: 0 }
    }

    pub fn as_str(&self) -> &'a str {
        self.iter.as_str()
    }

    pub fn err(&self, s: &'static str) -> ParseError {
        //println!("err {} {} ", self.l, self.c);
        ParseError::new(s, self.l, self.c)
    }
    pub fn err_r<V>(&self, s: &'static str) -> Result<V, ParseError> {
        //println!("err_r {} {} ", self.l, self.c);
        Err(self.err(s))
    }

    pub fn err_c(&self, c: ECode) -> ParseError {
        //println!("err_c {} {} ", self.l, self.c);
        ParseError::code(c, self.l, self.c)
    }
    pub fn err_cr<V>(&self, c: ECode) -> Result<V, ParseError> {
        //println!("err_cr {} {} ", self.l, self.c);
        Err(ParseError::code(c, self.l, self.c))
    }

    pub fn lc(&self) -> (usize, usize) {
        (self.l, self.c)
    }
    pub fn index(&self) -> Option<usize> {
        self.iter.clone().next().map(|(i, _)| i)
    }
}

impl<'a> Iterator for LCChars<'a> {
    type Item = char;
    fn next(&mut self) -> Option<char> {
        //println!("lc {} {} ", self.l, self.c);
        match self.iter.next() {
            Some((_, '\n')) => {
                self.l += 1;
                self.c = 0;
                Some('\n')
            }
            Some((_, v)) => {
                self.c += 1;
                Some(v)
            }
            None => None,
        }
    }
}

pub fn index<'a>(it: &LCChars<'a>) -> ParseRes<'a, Option<usize>> {
    return Ok((it.clone(), it.index()));
}

pub fn line_col<'a>(it: &LCChars<'a>) -> ParseRes<'a, (usize, usize)> {
    return Ok((it.clone(), (it.l, it.c)));
}