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 super::lexer::{
Lexer,
};
use super::parser::{
Parser,
Statement,
StatementValue
};
use super::util::{check_error, get_include_path};
use std::ops::Range;
use std::fs;
use std::error::Error;
pub struct Preprocessor {
pub statements: Vec<Statement>,
}
impl Preprocessor {
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) => {
if let Some(path) = get_include_path(&path) {
match fs::read_to_string(&path) {
Ok(content) => {
if included_files.iter().any(|n| n == &path) {
return Err((format!("recursive include of '{}'", path), (1..0)));
}
let mut lexer = Lexer::new(content.clone());
check_error(lexer.lex(false), &path, &content);
let mut parser = Parser::new(lexer.tokens, included_files.last().unwrap().clone());
check_error(parser.parse(), &path, &content);
let mut included_files = included_files.clone();
included_files.push(path);
if let Err(err) = self.preprocess(parser.statements, included_files) {
return Err(err);
}
},
Err(err) => return Err((String::from(err.to_string()), 1..0))
}
} else {
return Err((format!("could not find file '{}' in lib directory or current working directory", path), (1..0)));
}
},
_ => {
self.statements.push(statement);
}
}
}
Ok(())
}
pub fn new() -> Self {
Self {
statements: Vec::new(),
}
}
}