aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/ptr.rs
blob: b380117e6317f4ecd131224cd31e4b8d242ef3f5 (plain)
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
use {TextUnit};

use std::str::Chars;

pub(crate) struct Ptr<'s> {
    text: &'s str,
    len: TextUnit,
}

impl<'s> Ptr<'s> {
    pub fn new(text: &'s str) -> Ptr<'s> {
        Ptr { text, len: TextUnit::new(0) }
    }

    pub fn into_len(self) -> TextUnit {
        self.len
    }

    pub fn next(&self) -> Option<char> {
        self.chars().next()
    }

    pub fn nnext(&self) -> Option<char> {
        let mut chars = self.chars();
        chars.next()?;
        chars.next()
    }

    pub fn next_is(&self, c: char) -> bool {
        self.next() == Some(c)
    }

    pub fn nnext_is(&self, c: char) -> bool {
        self.nnext() == Some(c)
    }

    pub fn nnext_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
        self.nnext().map(p) == Some(true)
    }

    pub fn bump(&mut self) -> Option<char> {
        let ch = self.chars().next()?;
        self.len += TextUnit::len_of_char(ch);
        Some(ch)
    }

    pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
        loop {
            match self.next() {
                Some(c) if pred(c) => {
                    self.bump();
                },
                _ => return,
            }
        }
    }

    fn chars(&self) -> Chars {
        let len: u32 = self.len.into();
        self.text[len as usize ..].chars()
    }
}