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
use std::iter::Fuse;

#[derive(Clone, Debug)]
pub struct ParserStream<I>
where
    I: Iterator,
{
    iter: Fuse<I>,
    pub buf: Vec<char>,
    peek_index: usize,
    index: usize,

    pub ch: Option<char>,

    iter_end: bool,
    peek_end: bool,
}

impl<I: Iterator<Item = char>> ParserStream<I> {
    pub fn new(iterable: I) -> ParserStream<I> {
        let mut iter = iterable.into_iter().fuse();
        let ch = iter.next();

        ParserStream {
            iter,
            buf: vec![],
            peek_index: 0,
            index: 0,
            ch,

            iter_end: false,
            peek_end: false,
        }
    }

    pub fn current(&mut self) -> Option<char> {
        self.ch
    }

    pub fn current_is(&mut self, ch: char) -> bool {
        self.ch == Some(ch)
    }

    pub fn current_peek(&self) -> Option<char> {
        if self.peek_end {
            return None;
        }

        let diff = self.peek_index - self.index;

        if diff == 0 {
            self.ch
        } else {
            Some(self.buf[diff - 1])
        }
    }

    pub fn current_peek_is(&mut self, ch: char) -> bool {
        self.current_peek() == Some(ch)
    }

    pub fn peek(&mut self) -> Option<char> {
        if self.peek_end {
            return None;
        }

        self.peek_index += 1;

        let diff = self.peek_index - self.index;

        if diff > self.buf.len() {
            match self.iter.next() {
                Some(c) => {
                    self.buf.push(c);
                }
                None => {
                    self.peek_end = true;
                    return None;
                }
            }
        }

        Some(self.buf[diff - 1])
    }

    pub fn get_index(&self) -> usize {
        self.index
    }

    pub fn get_peek_index(&self) -> usize {
        self.peek_index
    }

    pub fn peek_char_is(&mut self, ch: char) -> bool {
        if self.peek_end {
            return false;
        }

        let ret = self.peek() == Some(ch);

        self.peek_index -= 1;
        ret
    }

    pub fn reset_peek(&mut self, pos: Option<usize>) {
        match pos {
            Some(pos) => {
                if pos < self.peek_index {
                    self.peek_end = false
                }
                self.peek_index = pos
            }
            None => {
                self.peek_index = self.index;
                self.peek_end = self.iter_end;
            }
        }
    }

    pub fn skip_to_peek(&mut self) {
        let diff = self.peek_index - self.index;

        for _ in 0..diff {
            self.ch = Some(self.buf.remove(0));
        }

        self.index = self.peek_index;
    }
}

impl<I> Iterator for ParserStream<I>
where
    I: Iterator<Item = char>,
{
    type Item = char;

    fn next(&mut self) -> Option<char> {
        if self.iter_end {
            return None;
        }

        self.ch = if self.buf.is_empty() {
            self.iter.next()
        } else {
            Some(self.buf.remove(0))
        };

        self.index += 1;

        if self.ch.is_none() {
            self.iter_end = true;
            self.peek_end = true;
        }

        self.peek_index = self.index;

        self.ch
    }
}