summaryrefslogtreecommitdiff
path: root/src/parser.rs
blob: 13cd22bc9f399c14cb6e94fbb749b96de3fbc91d (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
pub struct Parser<'a> {
    buf: &'a str,
    offset: usize
}

impl<'a> Parser<'a> {
    pub fn new(buf: &'a str) -> Self {
        Parser{buf, offset: 0}
    }

    pub fn next_word(&mut self) -> &'a str {
        match self.buf[self.offset..].split_once(' ') {
            Some((a,_)) => {
                self.offset += a.len() + 1;
                a
            },
            None => {
                let res = &self.buf[self.offset..];
                self.offset += self.buf.len() - self.offset;
                res
            }
        }
    }

    pub fn remainder(&mut self) -> String {
        let res = self.buf[self.offset..].to_owned();
        self.offset = self.buf.len();
        res
    }

    pub fn assert_end(&self) {
        if self.buf.len() != self.offset {
            println!("ERROR: failed parsing");
            panic!()
        }
    }
}