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

type Loc = (usize, usize);

#[derive(Debug, PartialEq, Clone)]
pub enum Token {
    Keyword(String, Loc),
    Section(String, String, Loc),
    Integer(usize, Loc),
    Float(f64, Loc),
    Symbol(char, String, Loc),
    Ident(String, Loc),
}

#[derive(Debug)]
enum Value {
    Start(String),
    End(String, String),
}

enum StartOrSection<'a> {
    Start(Vec<String>),
    Section(&'a Section),
}

#[derive(PartialEq, Eq)]
enum Mode {
    Section,
    Normal,
}

#[derive(Debug)]
pub struct Lexer {
    pub keywords: Vec<String>,
    pub sections: Vec<Section>,
    pub symbols: Vec<(char, String)>,
    pub buffer: Vec<u8>,
    pub allow_whitespace: bool,
}

#[derive(Debug, Clone)]
pub struct Section {
    pub name: String,
    pub start: String,
    pub end: String,
}


impl Token {
    pub fn as_string(&self) -> String {
        return match self {
            Token::Keyword(keyword, _) => keyword.clone(),
            Token::Section(_, value, _) => value.clone(),
            Token::Integer(integer, _) => integer.to_string(),
            Token::Float(float, _) => float.to_string(),
            Token::Symbol(value, _, _) => value.to_string(),
            Token::Ident(ident, _) => ident.clone(),
        };
    }

    pub fn loc(&self) -> Loc {
        return match self {
            Token::Keyword(_, loc) => *loc,
            Token::Section(_, _, loc) => *loc,
            Token::Integer(_, loc) => *loc,
            Token::Float(_, loc) => *loc,
            Token::Symbol(_, _, loc) => *loc,
            Token::Ident(_, loc) => *loc,
        };
    }

    pub fn is_keyword(&self, keyword: &str) -> Result<(), Box<dyn std::error::Error>> {
        if let Token::Keyword(value, _) = self {
            if value == keyword {
                return Ok(());
            }
        }
        return Err(format!("expected keyword: {:?}", self).into());
    }

    pub fn is_section(&self, name: &str) -> Result<String, Box<dyn std::error::Error>> {
        if let Token::Section(s_name, value, _) = self {
            if name == s_name {
                return Ok(value.clone());
            }
        }
        return Err(format!("expected section: {:?}", self).into());
    }

    pub fn is_ident(&self) -> Result<String, Box<dyn std::error::Error>> {
        if let Token::Ident(value, _) = self {
            return Ok(value.clone());
        }
        return Err(format!("expected ident: {:?}", self).into());
    }

    pub fn is_integer(&self) -> Result<usize, Box<dyn std::error::Error>> {
        if let Token::Integer(integer, _) = self {
            return Ok(*integer);
        }
        return Err(format!("expected integer: {:?}", self).into());
    }

    pub fn is_float(&self) -> Result<f64, Box<dyn std::error::Error>> {
        if let Token::Float(float, _) = self {
            return Ok(*float);
        }
        return Err(format!("expected float: {:?}", self).into());
    }

    pub fn is_symbol(&self, name: &str) -> Result<(), Box<dyn std::error::Error>> {
        if let Token::Symbol(_, s_name, _) = self {
            if s_name == name {
                return Ok(());
            }
        }
        return Err(format!("expected symbol: {:?}", self).into());
    }
}

impl Section {
    pub fn new(name: &str, start: &str, end: &str) -> Section {
        return Section {
            name: name.to_string(),
            start: start.to_string(),
            end: end.to_string(),
        };
    }

    pub fn from_end(end: String) -> Section {
        return Section {
            name: String::new(),
            start: String::new(),
            end,
        };
    }
}

impl Lexer {
    pub fn new(keywords: &[String], sections: &[Section], symbols: &[(char, String)], allow_whitespace: bool) -> Lexer {
        return Lexer {
            keywords: keywords.to_vec(),
            sections: sections.to_vec(),
            symbols: symbols.to_vec(),
            buffer: Vec::new(),
            allow_whitespace,
        };
    }

    pub fn load_str(&mut self, string: &str) {
        self.buffer = string.as_bytes().to_vec();
    }

    pub fn load_file(&mut self, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
        self.buffer = fs::read(filename)?;
        return Ok(());
    }

    fn symbols_contain(&self, value: &char) -> Option<&str> {
        for symbol in &self.symbols {
            if symbol.0 == *value {
                return Some(&symbol.1);
            }
        }
        return None;
    }

    fn section_exists(&self, start: &str, end: &str) -> Result<String, ()> {
        for section in &self.sections {
            if section.start == start && section.end == end {
                return Ok(section.name.to_string());
            }
        }
        return Err(());
    }

    fn is_section(&self, value: Value) -> Result<StartOrSection, ()> {
        let mut matches: Vec<String> = Vec::new();
        for section in &self.sections {
            if let Value::Start(start) = &value {
                if &section.start == start {
                    matches.push(section.end.clone());
                }
            } else if let Value::End(start, end) = &value {
                if &section.end == end && &section.start == start {
                    return Ok(StartOrSection::Section(section)); // matches is not really needed here
                }
            }
        }

        if matches.len() != 0 {
            return Ok(StartOrSection::Start(matches));
        }
        return Err(());
    }

    fn is_numeric(&self, token: &String, loc: Loc) -> Token {
        if let Ok(integer) = token.parse::<usize>() {
            return Token::Integer(integer, loc);
        } else if let Ok(integer) = token.parse::<f64>() {
            return Token::Float(integer, loc);
        } else {
            return Token::Ident(token.clone(), loc);
        }
    }

    fn lex_token(&self, token: &String, loc: Loc) -> Option<Token> {
        if token == "\n" {
            return None;
        } else if token == "" {
            if self.allow_whitespace {
                return Some(Token::Ident(" ".to_string(), loc));
            } else {
                return None;
            }
        } else if self.keywords.contains(&token) {
            return Some(Token::Keyword(token.clone(), loc));
        } else if token.len() == 1 {
            let character = token.chars().collect::<Vec<char>>()[0];
            if let Some(symbol_name) = self.symbols_contain(&character) {
                return Some(Token::Symbol(character, symbol_name.to_string(), loc));
            } else {
                return Some(self.is_numeric(token, loc));
            }
        } else if let Ok(name) = self.section_exists(&token[0..1], &token[token.len()-1..token.len()]) {
            return Some(Token::Section(name, token[1..token.len() - 1].to_string(), loc));
        } else {
            return Some(self.is_numeric(token, loc));
        }
    }

    pub fn tokenize(&mut self) -> Result<Vec<Token>, Box<dyn std::error::Error>> {
        if self.symbols_contain(&' ').is_none() {
            self.symbols.push((' ', "Space".to_string()));
        }

        let mut mode = Mode::Normal;
        let mut token = String::new();
        let mut tokens: Vec<Token> = Vec::new();
        let mut section: Vec<Section> = Vec::new();
        let mut loc = (1, 1);

        let mut index = 0;
        while index < self.buffer.len() {
            let byte = &self.buffer[index];
            let character = String::from_utf8(vec![byte.clone()])?;
            if (index + 1) < self.buffer.len() {
                if mode == Mode::Normal {
                    if let Ok(StartOrSection::Start(ends)) = self.is_section(Value::Start(character.clone())) {
                        token = token + &character;
                        for end in ends {
                            section.push(Section::from_end(end.clone()));
                            let idx = section.len() - 1;
                            section[idx].start = character.clone();
                        }
                        mode = Mode::Section;
                    } else if character.as_str() == "\n" {
                        self.lex_token(&token, loc).map(|t| tokens.push(t));
                        token = String::new();
                    } else if character.as_str() != " " {
                        token = token + &character;
                    }
                    if (self.symbols_contain(&char::from(byte.clone())).is_some() || self.symbols_contain(&char::from(self.buffer[index + 1])).is_some()) &&
                       section.len() == 0 { // making sure we arent lexing symbols when we're in a section
                        self.lex_token(&token, loc).map(|t| tokens.push(t));
                        token = String::new();
                    }
                } else if mode == Mode::Section {
                    if &character == "\\" {
                        if index + 1 >= self.buffer.len() {
                            return Ok(tokens);
                        } else {
                            index += 1;
                            token = token + &(self.buffer[index] as char).to_string();
                        }
                    } else if self.is_section(Value::End(section[0].start.to_string(), character.clone())).is_ok() || index + 2 >= self.buffer.len() { // index doesnt matter here because all indexes has the same start
                        println!("Closed");
                        token = token + &character;
                        self.lex_token(&token, loc).map(|t| tokens.push(t));
                        section = Vec::new();
                        token = String::new();
                        mode = Mode::Normal;
                    } else {
                        token = token + &character;
                    }
                }
            }

            if &character == "\n" {
                loc.0 += 1;
                loc.1 = 1;
            } else {
                loc.1 += 1;
            }
            index += 1;
        }

        return Ok(tokens);
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn load_test() -> Result<(), Box<dyn std::error::Error>> {
        let mut lexer = Lexer::new(
            &["def".to_string(), "if".to_string(), "return".to_string()],
            &[Section::new("string", "\"", "\"")],
            &[(':', "column".to_string()), ('(', "openbrace".to_string()), (')', "closebrace".to_string())],
            true,
        );

        lexer.load_str("def test(): \" return 0 ");

        println!("tokens: {:?}", lexer.tokenize()?);
        return Ok(());
    }
}