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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::io::Read;
use error::{Error, ErrorType};
use Result;
use std::str;
use std::ops::Index;
/// Collection of several helper methods that can be used when reading input.
pub struct InputReaderHelper {}
impl InputReaderHelper {
/// Returns `true` if the provided character is a whitespace.
pub fn whitespace(c: char) -> bool {
c == '\n' || c == '\r' || c == ' '
}
/// Returns `true` if the provided character is a line break.
pub fn line_break(c: char) -> bool {
c == '\n' || c == '\r'
}
/// Returns `true` if the provided character can be used to separate two nodes.
pub fn node_delimiter(c: char) -> bool {
c == '\n' || c == '\r' || c == ' ' || c == '.'
}
/// Returns `true` if the provided character is a digit.
pub fn digit(c: char) -> bool {
c >= '0' && c <= '9'
}
}
type InputChar = Option<char>;
#[derive(Debug, Clone)]
/// Represents a sequence of read input characters.
pub struct InputChars {
input_chars: Vec<InputChar>,
}
impl ToString for InputChars {
fn to_string(&self) -> String {
let s: String = self.input_chars
.clone()
.into_iter()
.flat_map(|c| c)
.collect();
s
}
}
impl Index<usize> for InputChars {
type Output = InputChar;
fn index(&self, i: usize) -> &InputChar {
&self.input_chars[i]
}
}
impl InputChars {
pub fn new(chars: Vec<InputChar>) -> InputChars {
InputChars { input_chars: chars }
}
pub fn to_vec(&self) -> Vec<InputChar> {
self.input_chars.clone()
}
pub fn len(&self) -> usize {
self.input_chars.len()
}
pub fn is_empty(&self) -> bool {
self.input_chars.len() == 0
}
pub fn push(&mut self, c: InputChar) {
self.input_chars.push(c);
}
pub fn insert(&mut self, i: usize, c: InputChar) {
self.input_chars.insert(i, c);
}
pub fn remove(&mut self, i: usize) -> InputChar {
self.input_chars.remove(i)
}
pub fn append(&mut self, other: &mut InputChars) {
self.input_chars.append(&mut other.to_vec());
}
}
/// Reads input and transforms it to `InputChars`.
pub struct InputReader<R: Read> {
input: R,
peeked_chars: InputChars,
}
impl<R: Read> InputReader<R> {
/// Constructor for `InputReader`.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let reader = InputReader::new("_:auto0".as_bytes());
/// ```
pub fn new(input: R) -> InputReader<R> {
InputReader {
input,
peeked_chars: InputChars::new(Vec::new()),
}
}
/// Returns the next `k` characters but does not consume them.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
/// use rdf::reader::input_reader::InputChars;
///
/// let mut reader = InputReader::new("_:auto0".as_bytes());
/// assert_eq!(reader.peek_next_k_chars(2).unwrap().to_vec(), vec![Some('_'), Some(':')]);
/// assert_eq!(reader.peek_next_k_chars(2).unwrap().to_vec(), vec![Some('_'), Some(':')]);
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn peek_next_k_chars(&mut self, k: usize) -> Result<InputChars> {
if self.peeked_chars.len() >= k {
Ok(InputChars::new(self.peeked_chars.to_vec()[0..k].to_vec()))
} else {
let next_k_chars = self.get_next_k_chars(k)?;
self.peeked_chars = next_k_chars.clone();
Ok(next_k_chars)
}
}
/// Returns the next character but does not consume it.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
/// use rdf::reader::input_reader::InputChars;
///
/// let mut reader = InputReader::new("_:auto0".as_bytes());
/// assert_eq!(reader.peek_next_char().unwrap(), Some('_'));
/// assert_eq!(reader.peek_next_char().unwrap(), Some('_'));
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn peek_next_char(&mut self) -> Result<InputChar> {
let peeked_char = try!(self.peek_next_k_chars(1));
Ok(peeked_char.to_vec()[0])
}
/// Returns the next character that is not a whitespace but does not consume it.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
/// use rdf::reader::input_reader::InputChars;
///
/// let mut reader = InputReader::new(" \n _:auto0".as_bytes());
/// assert_eq!(reader.peek_next_char_discard_leading_spaces().unwrap(), Some('_'));
/// assert_eq!(reader.peek_next_char_discard_leading_spaces().unwrap(), Some('_'));
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn peek_next_char_discard_leading_spaces(&mut self) -> Result<InputChar> {
match self.get_next_char_discard_leading_spaces() {
Ok(Some(next_char)) => {
if self.peeked_chars.is_empty() {
self.peeked_chars.push(Some(next_char));
}
Ok(Some(next_char))
}
Ok(None) => Ok(None),
Err(err) => Err(err),
}
}
/// Returns the next character of an input source.
///
/// # Examples
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = "Hello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!(Some('H'), input_reader.get_next_char().unwrap());
/// assert_eq!(Some('e'), input_reader.get_next_char().unwrap());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn get_next_char(&mut self) -> Result<InputChar> {
if !self.peeked_chars.is_empty() {
return Ok(self.peeked_chars.remove(0));
}
const MAX_BYTES: usize = 4;
let mut buf = [0u8; MAX_BYTES];
let input = &mut self.input;
let mut bytes = input.bytes();
for pos in 0..MAX_BYTES {
let byte = match bytes.next() {
Some(Ok(b)) => b,
None => return Ok(None),
Some(Err(_)) => {
return Err(Error::new(
ErrorType::InvalidReaderInput,
"Invalid input character.",
))
}
};
buf[pos] = byte;
match str::from_utf8(&buf[..(pos + 1)]) {
Ok(s) => return Ok(s.chars().next()),
Err(_) if pos < MAX_BYTES - 1 => {}
_ => {
return Err(Error::new(
ErrorType::InvalidByteEncoding,
"Invalid byte encoding of input.",
))
}
}
}
Err(Error::new(
ErrorType::InvalidReaderInput,
"Unexpected error while reading input.",
))
}
/// Returns the next `k` characters of an input source and consumes them.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
/// use rdf::reader::input_reader::InputChars;
///
/// let mut reader = InputReader::new("_:auto0".as_bytes());
/// assert_eq!(reader.get_next_k_chars(2).unwrap().to_vec(), vec![Some('_'), Some(':')]);
/// assert_eq!(reader.get_next_k_chars(2).unwrap().to_vec(), vec![Some('a'), Some('u')]);
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn get_next_k_chars(&mut self, k: usize) -> Result<InputChars> {
let mut next_k_chars = Vec::new();
for _ in 0..k {
let next_char = self.get_next_char()?;
next_k_chars.push(next_char);
}
Ok(InputChars::new(next_k_chars))
}
/// Returns the next character of an input source that is not a whitespace.
///
/// # Examples
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = "H ello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!(Some('H'), input_reader.get_next_char_discard_leading_spaces().unwrap());
/// assert_eq!(Some('e'), input_reader.get_next_char_discard_leading_spaces().unwrap());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn get_next_char_discard_leading_spaces(&mut self) -> Result<InputChar> {
loop {
match self.get_next_char() {
Ok(Some(' ')) => {}
Ok(Some('\n')) => {}
Ok(Some('\t')) => {}
Ok(Some('\r')) => {}
c => return c,
}
}
}
/// Returns all characters of a input source until a certain delimiter occurs but does not consume them.
///
/// The delimiter itself is skipped.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = "Hello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!("Hello".to_string(), input_reader.peek_until(|c| c == ' ').unwrap().to_string());
/// assert_eq!("Hello".to_string(), input_reader.peek_until(|c| c == ' ').unwrap().to_string());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn peek_until<F: Fn(char) -> bool>(&mut self, delimiter: F) -> Result<InputChars> {
let mut chars = self.get_until(delimiter)?;
let result = chars.clone();
chars.append(&mut self.peeked_chars);
self.peeked_chars = chars;
Ok(result)
}
/// Returns all characters without consuming them of a input source until a certain delimiter
/// occurs and removes leading whitespaces.
///
/// The delimiter itself is skipped.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = " Hello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!("Hello".to_string(), input_reader.peek_until_discard_leading_spaces(|c| c == ' ').unwrap().to_string());
/// assert_eq!("Hello".to_string(), input_reader.peek_until_discard_leading_spaces(|c| c == ' ').unwrap().to_string());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn peek_until_discard_leading_spaces<F: Fn(char) -> bool>(
&mut self,
delimiter: F,
) -> Result<InputChars> {
let mut chars = self.get_until_discard_leading_spaces(delimiter)?;
let result = chars.clone();
chars.append(&mut self.peeked_chars);
self.peeked_chars = chars;
Ok(result)
}
/// Returns all characters of a input source until a certain delimiter occurs.
///
/// The delimiter itself is skipped.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = "Hello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!("Hello".to_string(), input_reader.get_until(|c| c == ' ').unwrap().to_string());
/// assert_eq!(" World".to_string(), input_reader.get_until(|c| c == '!').unwrap().to_string());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn get_until<F: Fn(char) -> bool>(&mut self, delimiter: F) -> Result<InputChars> {
let mut buf = Vec::new();
loop {
match self.get_next_char()? {
Some(c) if delimiter(c) => {
self.peeked_chars.insert(0, Some(c));
return Ok(InputChars::new(buf.into_iter().collect()));
}
Some(c) if !delimiter(c) => buf.push(Some(c)),
_ => {
return Err(Error::new(
ErrorType::EndOfInput(InputChars::new(buf.into_iter().collect())),
"End of input.",
))
}
}
}
}
/// Returns all characters of a input source until a certain delimiter occurs and removes leading whitespaces.
///
/// The delimiter itself is skipped.
///
/// # Examples
///
/// ```
/// use rdf::reader::input_reader::InputReader;
///
/// let mut input = "Hello World!".as_bytes();
/// let mut input_reader = InputReader::new(input);
///
/// assert_eq!("Hello".to_string(), input_reader.get_until_discard_leading_spaces(|c| c == ' ').unwrap().to_string());
/// assert_eq!("World".to_string(), input_reader.get_until_discard_leading_spaces(|c| c == '!').unwrap().to_string());
/// ```
///
/// # Failures
///
/// - End of input reached.
///
pub fn get_until_discard_leading_spaces<F: Fn(char) -> bool>(
&mut self,
delimiter: F,
) -> Result<InputChars> {
let whitespaces = InputReaderHelper::whitespace;
// consume leading whitespaces
while whitespaces(self.peek_next_char()?.unwrap_or('x')) {
let _ = self.get_next_char();
}
self.get_until(delimiter)
}
}