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

use unicode_segmentation::UnicodeSegmentation;
use std::ops::Deref;
use regex::Regex;
use files::FileID;
use location::Location;
use lazy_static::lazy_static;

#[derive(Debug,Clone)]
pub struct Input<'b> {
    name: FileID,
    text: &'b str,
    pos: usize,
    line: usize,
    column: usize
}

impl<'b> Input<'b> {
    pub fn new<'y>(name: FileID, text: &'y str) -> Input<'y> {
        Input {
            name: name,
            text: text,
            pos: 0,
            line: 1,
            column: 0
        }
    }

    pub fn start_of_line(&self) -> bool {
        self.pos == 0
            || (self.pos > 0 && self.text.as_bytes()[self.pos - 1] == b'\n')
    }

    pub fn peek(&self) -> Option<&str> {
        UnicodeSegmentation::graphemes(&self.text[self.pos..], true).next()
    }

    pub fn len(&self) -> usize {
        self.text.len()
    }

    pub fn eof(&self) -> bool {
        self.pos >= self.text.len()
    }

    pub fn location(&self) -> Location {
        Location::new(self.name, self.pos, self.line, self.column)
    }

    pub fn as_str(&self) -> &'b str {
        &self.text[self.pos..]
    }

    pub fn starts_with(&self, text: &str) -> bool {
        self.as_str().starts_with(text)
    }

    pub fn name(&self) -> FileID {
        self.name
    }

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

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

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

    pub fn skip_whitespace(&self) -> Input<'b> {
        lazy_static! {
            static ref RE: Regex = Regex::new(r"^\s+").unwrap();
        }
        let mut skipped: usize = 0;
        match RE.captures_iter(&self.text[self.pos..]).next() {
            Some(cap) => skipped += cap[0].len(),
            None => ()
        }
        self.update(skipped)
    }

    /// Update the position information by advancing the current
    /// position by `num_bytes` bytes.
    pub fn update(&self, num_bytes: usize) -> Input<'b> {
        let mut input = self.clone();
        let start = input.pos;
        let mut graphemes = UnicodeSegmentation::graphemes(&input.text[input.pos..], true);
        loop {
            match graphemes.next() {
                Some(text) => {
                    if (input.pos - start) + text.len() > num_bytes {
                        break;
                    }
                    input.pos += text.len();
                    if text == "\r\n" || text == "\n" {
                        input.line += 1;
                        input.column = 0;
                    } else {
                        input.column += 1;
                    }
                },
                None => break
            }
        }
        input
    }
}

impl<'b> Deref for Input<'b> {
    type Target = str;

    fn deref(&self) -> &str {
        &self.text[self.pos..]
    }
}

#[cfg(test)]
mod tests {
    use input::Input;
    use files::FileID;

    #[test]
    fn test_update() {
        let input = Input::new(FileID::new(43), "some cool\n input \r\n and \n stuff");
        let input = input.update(20);
        assert!(input.pos == 20);
        assert!(input.line == 3);
        assert!(input.column == 1);
        assert!("and \n stuff" == &*input);
    }

    #[test]
    fn test_update_past_end() {
        let input = Input::new(FileID::new(43), "some cool\n input \r\n and \n stuff");
        let input = input.update(33);
        assert!(input.pos == 31);
        assert!(input.line == 4);
        assert!(input.column == 6);
        assert!("" == &*input);
    }

    #[test]
    fn test_skip_whitespace() {
        let input = Input::new(FileID::new(43), "   \n \r\n \r  \t some stuff");
        let input = input.skip_whitespace();
        assert!(input.pos == 13);
        assert!(input.line == 3);
        assert!(input.column == 6);
        assert!("some stuff" == &*input);
    }
}