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
use std::{collections::HashMap, fs};
use log::*;

pub mod prelude {
    pub use super::PreprocessorOptions;
    pub use super::Preporcessor;
    pub use super::CodeSource;
}

#[derive(Debug, Clone)]
pub struct PreprocessorOptions {
    pub start_operator: String
}

impl Default for PreprocessorOptions {
    fn default() -> Self {
        Self {
            start_operator: "//!".to_string()
        }
    }
}

pub struct CodeSource {
    sources: HashMap<String, String>,
}

impl CodeSource {
    pub fn new(sources: HashMap<String, String>) -> Self {
        Self {
            sources,
        }
    }

    pub fn from_path(path: String) -> Self {
        let mut sources = HashMap::<String, String>::new();

        for entry in glob::glob(format!("{}/**/*", path).as_str()).unwrap() {
            let entry = &entry.unwrap();

            let name = entry.file_name().unwrap().to_str().unwrap().to_string().replace("/", "__");
            let source = fs::read_to_string(entry.as_path()).unwrap();

            sources.insert(name, source);
        }

        Self {
            sources
        }
    }

    pub fn get_source(&self, name: String) -> &String {
        self.sources.get(&name).unwrap()
    }
}

#[derive(Debug)]
pub enum Token {
    Command(CommandType),
    Literal(LiteralType),
    Separator(SeparatorType),

    OtherCode(String)
}

#[derive(Debug)]
pub enum SeparatorType {
    Colon
}

#[derive(Debug)]
pub enum CommandType {
    Include,
    Define,
    UnDefine,
    IfDef,
    IfNotDef,
    Endif,
    Error,
    Warn
}

#[derive(Debug)]
pub enum LiteralType {
    NameLiteral(String),
    StringLiteral(String),
}


pub struct Preporcessor {
    sources: HashMap<String, CodeSource>,
    preprocessor_options: PreprocessorOptions,

    defines: HashMap<String, String>,
    ifs: Vec<bool>
}

impl Preporcessor {
    pub fn new(preprocessor_options: PreprocessorOptions) -> Self {
        Self {
            preprocessor_options: preprocessor_options.clone(),
            sources: HashMap::new(),
            defines: HashMap::new(),
            ifs: Vec::new(),
        }
    }

    pub fn add_source(&mut self, name: String, code_source: CodeSource) {
        self.sources.insert(name, code_source);
    }

    pub fn tokenize_line(&mut self, line: String) -> Vec<Token> {
        let mut tokens = Vec::new();

        let trimmed_line = line.trim_start().trim_end();
        let line_words = trimmed_line.split(" ");

        let mut other_code_buffer = "".to_string();
        let mut other_code = true;

        let mut str_literal_buffer = "".to_string();
        let mut str_literal = false;

        for word in line_words {
            if word == self.preprocessor_options.start_operator {
                other_code = false;
                tokens.push(Token::OtherCode(other_code_buffer.clone()));

                continue;
            }

            if other_code {
                other_code_buffer += &(word.to_owned() + " ");
                continue;
            }

            if str_literal {
                str_literal_buffer += word;
            }

            if word.starts_with('"') {
                str_literal_buffer = "".to_string();
                str_literal = true;

                str_literal_buffer += word;
            }

            if word.ends_with('"') {
                str_literal = false;
                tokens.push(Token::Literal(LiteralType::StringLiteral(str_literal_buffer.replace("\"", "").clone())));

                continue;
            }

            if str_literal {
                str_literal_buffer += " ";
                continue;
            }

            match word {
                "include" => tokens.push(Token::Command(CommandType::Include)),
                "define" => tokens.push(Token::Command(CommandType::Define)),
                "undef" => tokens.push(Token::Command(CommandType::UnDefine)),
                "ifdef" => tokens.push(Token::Command(CommandType::IfDef)),
                "ifndef" => tokens.push(Token::Command(CommandType::IfNotDef)),
                "endif" => tokens.push(Token::Command(CommandType::Endif)),
                "error" => tokens.push(Token::Command(CommandType::Error)),
                "warn" => tokens.push(Token::Command(CommandType::Warn)),

                _ => tokens.push(Token::Literal(LiteralType::NameLiteral(word.to_string())))
            }
        }

        if other_code {
            tokens.push(Token::OtherCode(other_code_buffer.clone()));
        }

        tokens
    }

    pub fn preprocess_line(&mut self, line: String) -> Vec<String> {
        let mut strings = Vec::new();
        let mut tokens = self.tokenize_line(line).into_iter().peekable();

        while tokens.peek().is_some() {
            let command = tokens.next().unwrap();

            match command {
                Token::Command(CommandType::IfDef) => {
                    let name = tokens.next().unwrap();

                    if let Token::Literal(LiteralType::NameLiteral(name)) = name {
                        self.ifs.push(self.defines.contains_key(&name));
                    }
                },
                Token::Command(CommandType::IfNotDef) => {
                    let name = tokens.next().unwrap();

                    if let Token::Literal(LiteralType::NameLiteral(name)) = name {
                        self.ifs.push(!self.defines.contains_key(&name));
                    }
                },
                Token::Command(CommandType::Endif) => {
                    self.ifs.pop();
                },

                _ => {}
            }

            if self.ifs.len() != 0 {
                if !self.ifs[self.ifs.len() - 1] {
                    continue;
                }
            }

            match command {
                Token::Command(command) => match command {
                    CommandType::Include => {
                        let lib = tokens.next().unwrap();
                        let name = tokens.next().unwrap();

                        if let Token::Literal(LiteralType::StringLiteral(lib)) = lib {
                            if let Token::Literal(LiteralType::StringLiteral(name)) = name {
                                let source = self.preprocess(lib, name);

                                strings.append(&mut source.lines().map(|x| x.to_string()).collect());
                            } else {
                                panic!("Unexpected token");
                            }
                        } else {
                            panic!("Unexpected token");
                        }
                    },
                    CommandType::Define => {
                        let name = tokens.next().unwrap();
                        let value = tokens.next().unwrap();

                        if let Token::Literal(LiteralType::NameLiteral(name)) = name {
                            if let Token::Literal(LiteralType::StringLiteral(value)) = value {
                                self.defines.insert(name, value);
                            } else {
                                panic!("Unexpected token");
                            }
                        } else {
                            panic!("Unexpected token");
                        }
                    },
                    CommandType::UnDefine => {
                        let name = tokens.next().unwrap();

                        if let Token::Literal(LiteralType::NameLiteral(name)) = name {
                            self.defines.remove(&name);
                        } else {
                            panic!("Unexpected token");
                        }
                    },
                    CommandType::Error => {
                        let messange = tokens.next().unwrap();

                        if let Token::Literal(LiteralType::StringLiteral(messange)) = messange {
                            error!("Error: {}", messange);
                            panic!("Error: {}", messange);
                        } else {
                            panic!("Unexpected token");
                        }
                    },
                    CommandType::Warn => {
                        let messange = tokens.next().unwrap();

                        if let Token::Literal(LiteralType::StringLiteral(messange)) = messange {
                            warn!("Warning: {}", messange);
                        } else {
                            panic!("Unexpected token");
                        }
                    },
                    _ => {},
                },
                Token::OtherCode(code) => strings.push(self.replace_defines(code)),

                _ => panic!("Exepted command found {:?}", command)
            }
        }

        strings
    }

    pub fn replace_defines(&self, string: String) -> String {
        let mut string = string;

        for define in self.defines.keys() {
            let define = self.defines.get_key_value(define).unwrap();
            string = string.replace(define.0, define.1);
        }

        string
    }

    pub fn preprocess(&mut self, lib: String, name: String) -> String {
        let mut out_sources = Vec::<String>::new();
        
        let main_namespace = self.sources.get(lib.as_str()).unwrap();
        let main_file = main_namespace.sources.get(name.as_str()).unwrap().clone();
        let main_file_lines = main_file.lines();

        for line in main_file_lines {
            for preprocessed_line in self.preprocess_line(line.to_string()) {
                if preprocessed_line.trim() == "" {
                    continue;
                }

                out_sources.push(preprocessed_line);
            }
        }

        out_sources.join("\n")
    }
}