summaryrefslogtreecommitdiff
path: root/axisc/src/reader.rs
blob: b75649a55b9d6d6c450d78bfe98678ac0ebf7b45 (plain)
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
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Peekable;
use std::path::PathBuf;
use std::process::exit;
use std::str::Chars;

pub fn read_file(source: &PathBuf) -> String {
    let file = File::open(&source).unwrap_or_else(|e| {
        eprintln!("axisc: Unable to open {:?} for reading: {}", &source, e);
        exit(1)
    });
    let reader = BufReader::new(file);
    let mut string = String::from("");

    for line in reader.lines() {
        let mut line = line.unwrap();

        if let Some(char_index) = line.find("--") {
            line.truncate(char_index);
        }

        if !line.is_empty() {
            string.push_str(line.trim());
            string.push('\n');
        }
    }

    string
}

pub struct TokenScanner<'a> {
    pub chars: Peekable<Chars<'a>>
}

impl <'a> TokenScanner<'a> {
    pub fn advance(&mut self, num: usize) -> Option<char> {
        self.chars.nth(num - 1)
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.chars.peek()
    }

    pub fn advance_word(&mut self) -> String {
        let mut word = String::new();

        while let Some(char) = self.chars.next() {
            if char.is_ascii_whitespace() {
                break;
            }

            word.push(char);
        }

        word
    }

    pub fn peek_word(&mut self) -> String {
        todo!(":(")
    }

    pub fn from_string(string: &'a str) -> Self {
        Self {
            chars: string.chars().peekable()
        }
    }
}