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
use super::lexer::{
    Lexer,
};
use super::parser::{
    Parser,
    Statement,
    StatementValue
};
use super::util::check_error;
use std::fs;
use std::ops::Range;

const LIB_PATH: &str =  "/usr/share/gtk-ui/";

fn path_exists(path: &String) -> bool {
    fs::metadata(path).is_ok()
}

// TODO: Warn about a file being included multiple times.
// At the moment, regardless of where the library is included, if it is included twice, everything defined in the library is redefined
// This is of course very easily avoidable, but for user friendliness, a notification about this would be nice

pub struct Preprocessor {
    pub statements: Vec<Statement>,
}

impl Preprocessor { 

    // Pubs
    pub fn preprocess(&mut self, input: Vec<Statement>, included_files: Vec<String>) -> Result<(), (String, Range<usize>)> {
        for statement in input {
            match statement.value {
                StatementValue::Include(path) => {
                    let file_content;
                    let file_path: String;

                    let lib_file_path = format!("{LIB_PATH}/{path}.gui");
                    if path_exists(&lib_file_path) {
                        file_content = fs::read_to_string(&lib_file_path).expect("could not read included file");
                        file_path = lib_file_path;
                    } else if path_exists(&path) {
                        file_content = fs::read_to_string(&path).expect("could not read included file");
                        file_path = path;
                    } else {
                        return Err((format!("could not find file '{}' in lib directory or current working directory", path), (1..0)));
                    }

                    if included_files.iter().any(|n| n == &file_path) {
                        return Err((format!("recursive include of '{}'", file_path), (1..0)));
                    }
                
                    let mut lexer = Lexer::new(file_content.clone());
                    check_error(lexer.lex(false), &file_path, &file_content);
                    let mut parser = Parser::new(lexer.tokens, included_files.last().unwrap().clone());
                    check_error(parser.parse(), &file_path, &file_content);

                    let mut included_files = included_files.clone();
                    included_files.push(file_path);

                    if let Err(err) = self.preprocess(parser.statements, included_files) {
                        return Err(err);
                    }
                },
                _ => {
                    self.statements.push(statement);
                }
            }
        }
        Ok(())
    }

    pub fn new() -> Self {
        Self {
            statements: Vec::new(),
        }
    } 
}