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
// Original file: "InputStream.hs"
// File auto-generated using Corollary.

#[macro_use]
use corollary_support::*;

use std::str;
use std::rc::Rc;
use std::fs::File;
use std::io::Read;

#[derive(Clone, Debug)]
pub struct InputStream {
    src: Rc<Vec<u8>>,
    pos: usize,
}

impl InputStream {

    pub fn from_file(f: &FilePath) -> InputStream {
        let mut src = vec![];
        File::open(&f.path).unwrap().read_to_end(&mut src).unwrap();
        InputStream { src: Rc::new(src), pos: 0 }
    }

    pub fn from_string(src: String) -> InputStream {
        InputStream { src: Rc::new(src.into_bytes()), pos: 0 }
    }

    pub fn to_string(self) -> String {
        String::from_utf8_lossy(&self.src[self.pos..]).into_owned()
    }

    pub fn is_empty(&self) -> bool {
        self.pos == self.src.len()
    }

    pub fn take_byte(mut self) -> (u8, InputStream) {
        let pos = self.pos;
        let byte = self.src[pos];
        self.pos += 1;
        (byte, self)
    }

    // TODO correct unicode char handling

    pub fn take_char(mut self) -> (char, InputStream) {
        let pos = self.pos;
        let ch = self.src[pos] as char;
        self.pos += 1;
        (ch, self)
    }

    pub fn take_string(&self, n: isize) -> &str {
        str::from_utf8(&self.src[self.pos..self.pos + n as usize]).unwrap() // TODO UNICODE
    }

    pub fn count_lines(self) -> isize {
        // TODO
        self.to_string().lines().count() as isize
    }
}