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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::cell::{RefCell};
use std::io::{Result, Error};

use crate::{Position, Span};

/// Lazy string buffer that fills up on demand.
///
/// The `lazy::Buffer` wraps aroung a `char` iterator. It can be itself used as a `char` iterator,
/// or as a `Buffer` to access an arbitrary fragment of the input source stream.
pub struct Buffer<I: Iterator<Item=Result<char>>> {
	p: RefCell<Inner<I>>
}

struct Inner<I: Iterator<Item=Result<char>>> {
    /// Input source `char` stream.
	input: I,

    /// Buffer error state.
	error: Option<Error>,

    /// The buffer data.
	data: Vec<char>,

    /// Lines index.
    ///
    /// Contains the index of the first character of each line.
    lines: Vec<usize>,

    /// The span of the buffer.
    span: Span
}

impl<I: Iterator<Item=Result<char>>> Inner<I> {
    /// Read the next line from the input stream and add it to the buffer.
    /// Returns `true` if a new line has been added. Returns `false` if the source stream is
    /// done.
	fn read_line(&mut self) -> bool {
        if self.error.is_none() {
            let line = self.span.end().line;
            while line == self.span.end().line {
                match self.input.next() {
                    Some(Ok(c)) => {
                        self.data.push(c);
                        self.span.push(c);
                    },
                    Some(Err(e)) => {
                        self.error = Some(e);
                        return false
                    },
                    None => {
                        return false
                    }
                }
            }

            // register the next line index.
            self.lines.push(self.data.len());

            true
        } else {
            false
        }
	}

	/// Get the index of the char at the given cursor position if it is in the buffer.
    /// If it is not in the buffer but after the buffered content, the input stream will be read
    /// until the buffer span includes the given position.
    ///
    /// Returns `None` if the given position if previous to the buffer start positions, if the
    /// source stream ends before the given position, or if the line at the given position is
    /// shorter than the given position column.
	fn index_at(&mut self, pos: Position) -> Option<Result<usize>> {
		if pos < self.span.start() {
			None
		} else {
			while pos >= self.span.end() && self.read_line() { }

			if pos >= self.span.end() {
                let mut error = None;
                std::mem::swap(&mut error, &mut self.error);
				match error {
                    Some(e) => Some(Err(e)),
                    None => None
                }
			} else {
                // line index relative to the first line of the buffer.
				let relative_line = pos.line - self.span.start().line;
                // get the index of the char of the begining of the line in the buffer.
                let mut i = self.lines[relative_line];
                // place a virtual cursor at the begining of the target line.
				let mut cursor = Position::new(pos.line, 0);

                while cursor < pos {
                    cursor = cursor.next(self.data[i]);
                    i += 1;
                }

                if cursor == pos {
                    // found it!
                    Some(Ok(i))
                } else {
                    // the position does not exist in the buffer.
                    None
                }
			}
		}
	}

	/// Get the character at the given index.
	///
	/// If it is not in the buffer but after the buffered content, the input stream will be read
    /// until the buffer span includes the given position.
	/// Returns `None` if the source stream ends before the given position.
	fn get(&mut self, i: usize) -> Option<Result<char>> {
		while i >= self.data.len() && self.read_line() { }

		if i >= self.data.len() {
			let mut error = None;
			std::mem::swap(&mut error, &mut self.error);
			match error {
				Some(e) => Some(Err(e)),
				None => None
			}
		} else {
			Some(Ok(self.data[i]))
		}
	}
}
//
impl<I: Iterator<Item=Result<char>>> Buffer<I> {
	/// Create a new empty buffer starting at the given position.
	pub fn new(input: I, position: Position) -> Buffer<I> {
		Buffer {
			p: RefCell::new(Inner {
				input: input,
				error: None,
				data: Vec::new(),
                lines: vec![0],
				span: position.into()
			})
		}
	}

	/// Get the span of the entire buffered data.
	pub fn span(&self) -> Span {
		self.p.borrow().span
	}

	/// Get the index of the char at the given cursor position if it is in the buffer.
    /// If it is not in the buffer but after the buffered content, the input stream will be read
    /// until the buffer span includes the given position.
    ///
    /// Returns `None` if the given position if previous to the buffer start positions, if the
    /// source stream ends before the given position, or if the line at the given position is
    /// shorter than the given position column.
	pub fn index_at(&self, pos: Position) -> Option<Result<usize>> {
		self.p.borrow_mut().index_at(pos)
	}

    /// Get the char at the given position if it is in the buffer.
    /// If it is not in the buffer but after the buffered content, the input stream will be read
    /// until the buffer span includes the given position.
    ///
    /// Returns `None` if the given position if previous to the buffer start positions, if the
    /// source stream ends before the given position, or if the line at the given position is
    /// shorter than the given position column.
	pub fn at(&self, pos: Position) -> Option<Result<char>> {
		match self.index_at(pos) {
			Some(Ok(i)) => self.p.borrow_mut().get(i),
			Some(Err(e)) => Some(Err(e)),
			None => None
		}
	}

	/// Get the character at the given index.
	///
	/// If it is not in the buffer but after the buffered content, the input stream will be read
    /// until the buffer span includes the given position.
	/// Returns `None` if the source stream ends before the given position.
	fn get(&self, i: usize) -> Option<Result<char>> {
		self.p.borrow_mut().get(i)
	}

    /// Returns an iterator through the characters of the buffer from the begining of it.
    ///
    /// When it reaches the end of the buffer, the buffer will start reading from the source
    /// stream.
	pub fn iter(&self) -> Iter<I> {
		Iter {
			buffer: &self,
			i: Some(Ok(0))
		}
	}

    /// Returns an iterator through the characters of the buffer from the given position.
    ///
    /// If the input position precedes the buffer start position, that it will start from the
    /// buffer start position.
    /// When it reaches the end of the buffer, the buffer will start reading from the source
    /// stream.
	pub fn iter_from(&self, pos: Position) -> Iter<I> {
		Iter {
			buffer: &self,
			i: self.index_at(std::cmp::max(self.p.borrow().span.start(), pos))
		}
	}
}

/// Iterator over the characters of a [`Buffer`].
///
/// This iterator is created using the [`Buffer::iter`] method or the [`Buffer::iter_from`] method.
/// When it reaches the end of the buffer, the buffer will start reading from the source
/// stream until the stream itself return `None`.
pub struct Iter<'b, I: 'b + Iterator<Item=Result<char>>> {
	buffer: &'b Buffer<I>,
    i: Option<Result<usize>>
}

impl<'b, I: 'b + Iterator<Item=Result<char>>> Iterator for Iter<'b, I> {
	type Item = Result<char>;

	fn next(&mut self) -> Option<Result<char>> {
		match &mut self.i {
			Some(Ok(ref mut i)) => {
				match self.buffer.get(*i) {
					Some(Ok(c)) => {
						*i = *i+1;
						Some(Ok(c))
					},
		            Some(Err(e)) => Some(Err(e)),
					None => None
				}
			},
			None => None,
			ref mut i => {
				let mut new_i = None;
				std::mem::swap(&mut new_i, i);
				if let Some(Err(e)) = new_i {
					Some(Err(e))
				} else {
					unreachable!()
				}
			},
		}
	}
}