-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcompiler.rs
340 lines (314 loc) Β· 12.3 KB
/
compiler.rs
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
extern crate chrono;
use crate::docs::module::DocumentationModule;
use crate::modules::block::Block;
use crate::translate::check_all_blocks;
use crate::translate::module::TranslateModule;
use crate::utils::{ParserMetadata, TranslateMetadata};
use crate::rules;
use postprocessor::PostProcessor;
use chrono::prelude::*;
use colored::Colorize;
use heraclitus_compiler::prelude::*;
use wildmatch::WildMatchPattern;
use std::env;
use std::fs;
use std::fs::File;
use std::io::{ErrorKind, Write};
use std::path::PathBuf;
use std::process::{Command, ExitStatus};
use std::time::Instant;
pub mod postprocessor;
const NO_CODE_PROVIDED: &str = "No code has been provided to the compiler";
const AMBER_DEBUG_PARSER: &str = "AMBER_DEBUG_PARSER";
const AMBER_DEBUG_TIME: &str = "AMBER_DEBUG_TIME";
pub struct CompilerOptions {
pub no_proc: Vec<String>,
pub minify: bool,
}
impl Default for CompilerOptions {
fn default() -> Self {
let no_proc = vec![String::from("*")];
Self { no_proc, minify: false }
}
}
impl CompilerOptions {
pub fn from_args(no_proc: &[String], minify: bool) -> Self {
let no_proc = no_proc.to_owned();
Self { no_proc, minify }
}
}
pub struct AmberCompiler {
pub cc: Compiler,
pub path: Option<String>,
pub options: CompilerOptions,
}
impl AmberCompiler {
pub fn new(code: String, path: Option<String>, options: CompilerOptions) -> AmberCompiler {
let cc = Compiler::new("Amber", rules::get_rules());
let compiler = AmberCompiler { cc, path, options };
compiler.load_code(AmberCompiler::comment_shebang(code))
}
fn comment_shebang(code: String) -> String {
if code.starts_with("#!") {
String::from("// ") + &code
} else {
code
}
}
fn env_flag_set(flag: &str) -> bool {
if let Ok(value) = env::var(flag) {
value == "1" || value == "true"
} else {
false
}
}
pub fn load_code(mut self, code: String) -> Self {
self.cc.load(code);
self
}
pub fn tokenize(&self) -> Result<Vec<Token>, Message> {
let time = Instant::now();
match self.cc.tokenize() {
Ok(tokens) => {
if Self::env_flag_set(AMBER_DEBUG_TIME) {
let pathname = self.path.clone().unwrap_or(String::from("unknown"));
println!(
"[{}]\tin\t{}ms\t{pathname}",
"Tokenize".cyan(),
time.elapsed().as_millis()
);
}
Ok(tokens)
}
Err((err_type, pos)) => {
let error_message = match err_type {
LexerErrorType::Singleline => {
format!("Singleline {} not closed", pos.data.as_ref().unwrap())
}
LexerErrorType::Unclosed => {
format!("Unclosed {}", pos.data.as_ref().unwrap())
}
};
let code = self.cc.code.as_ref().expect(NO_CODE_PROVIDED).clone();
let meta = ParserMetadata::new(vec![], self.path.clone(), Some(code));
Err(Message::new_err_at_position(&meta, pos).message(error_message))
}
}
}
pub fn parse(&self, tokens: Vec<Token>) -> Result<(Block, ParserMetadata), Message> {
let code = self.cc.code.as_ref().expect(NO_CODE_PROVIDED).clone();
let mut meta = ParserMetadata::new(tokens, self.path.clone(), Some(code));
if let Err(Failure::Loud(err)) = check_all_blocks(&meta) {
return Err(err);
}
let mut block = Block::new();
let time = Instant::now();
// Parse with debug or not
let result = if Self::env_flag_set(AMBER_DEBUG_PARSER) {
block.parse_debug(&mut meta)
} else {
block.parse(&mut meta)
};
if Self::env_flag_set(AMBER_DEBUG_TIME) {
let pathname = self.path.clone().unwrap_or(String::from("unknown"));
println!(
"[{}]\tin\t{}ms\t{pathname}",
"Parsed".blue(),
time.elapsed().as_millis()
);
}
// Return result
match result {
Ok(()) => Ok((block, meta)),
Err(failure) => Err(failure.unwrap_loud()),
}
}
pub fn get_sorted_ast_forest(
&self,
block: Block,
meta: &ParserMetadata,
) -> Vec<(String, Block)> {
let imports_sorted = meta.import_cache.topological_sort();
let imports_blocks = meta.import_cache.files.iter()
.map(|file| {
file.metadata
.as_ref()
.map(|meta| (file.path.clone(), meta.block.clone()))
})
.collect::<Vec<Option<(String, Block)>>>();
let mut result = vec![];
for index in imports_sorted.iter() {
if let Some((path, block)) = imports_blocks[*index].clone() {
result.push((path, block));
}
}
result.push((self.path.clone().unwrap_or(String::from("unknown")), block));
result
}
pub fn translate(&self, block: Block, meta: ParserMetadata) -> Result<String, Message> {
let ast_forest = self.get_sorted_ast_forest(block, &meta);
let mut meta_translate = TranslateMetadata::new(meta, &self.options);
let time = Instant::now();
let mut result = vec![];
for (_path, block) in ast_forest {
result.push(block.translate(&mut meta_translate));
}
if Self::env_flag_set(AMBER_DEBUG_TIME) {
let pathname = self.path.clone().unwrap_or(String::from("unknown"));
println!(
"[{}]\tin\t{}ms\t{pathname}",
"Translate".magenta(),
time.elapsed().as_millis()
);
}
let mut result = result.join("\n") + "\n";
let filters = self.options.no_proc.iter()
.map(|x| WildMatchPattern::new(x))
.collect();
let postprocessors = PostProcessor::filter_default(filters);
for postprocessor in postprocessors {
result = match postprocessor.execute(result) {
Ok(result) => result,
Err(error) => {
let error = format!(
"Postprocessor '{}' failed\n{}",
postprocessor.name,
error.to_string().trim_end(),
);
return Err(Message::new_err_msg(error));
},
};
}
let now = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let header = include_str!("header.sh")
.replace("{{ version }}", env!("CARGO_PKG_VERSION"))
.replace("{{ date }}", now.as_str());
Ok(format!("{}{}", header, result))
}
pub fn document(&self, block: Block, meta: ParserMetadata, output: String) {
let base_path = meta.get_path()
.map(PathBuf::from)
.expect("Input file must exist in docs generation");
let base_dir = fs::canonicalize(base_path).map(|val| {
val.parent()
.expect("Parent dir must exist in docs generation")
.to_owned()
.clone()
});
let base_dir = base_dir.unwrap_or_else(|err| {
Message::new_err_msg("Couldn't get the absolute path to the provided input file")
.comment(err.to_string())
.show();
std::process::exit(1);
});
let ast_forest = self.get_sorted_ast_forest(block, &meta);
let mut paths = vec![];
for (path, block) in ast_forest {
let dep_path = {
let dep_path = match fs::canonicalize(PathBuf::from(path)) {
Ok(path) => path,
Err(_) => continue,
};
if !dep_path.starts_with(&base_dir) {
continue;
}
dep_path
};
let document = block.document(&meta);
// Save to file; replace the base directory if the output
// path is absolute, otherwise append the output path.
let dir_path = {
let file_path = dep_path.strip_prefix(&base_dir).unwrap();
let file_dir = file_path.parent().unwrap();
base_dir.join(&output).join(file_dir)
};
if let Err(err) = fs::create_dir_all(dir_path.clone()) {
Message::new_err_msg(format!(
"Couldn't create directory `{}`. Do you have sufficient permissions?", dir_path.display()
))
.comment(err.to_string())
.show();
std::process::exit(1);
}
let filename = dep_path.file_stem().unwrap().to_string_lossy();
let path = dir_path.join(format!("{filename}.md"));
let mut file = File::create(path.clone()).unwrap();
file.write_all(document.as_bytes()).unwrap();
paths.push(String::from(path.to_string_lossy()));
}
let file_text = if paths.len() > 1 { "Files" } else { "File" };
Message::new_info_msg(format!("{file_text} generated at:\n{}", paths.join("\n")))
.show();
}
pub fn compile(&self) -> Result<(Vec<Message>, String), Message> {
let tokens = self.tokenize()?;
let (block, meta) = self.parse(tokens)?;
let messages = meta.messages.clone();
let code = self.translate(block, meta)?;
Ok((messages, code))
}
pub fn execute(mut code: String, args: Vec<String>) -> Result<ExitStatus, std::io::Error> {
if let Some(mut command) = Self::find_bash() {
if !args.is_empty() {
let args = args.into_iter()
.map(|arg| arg.replace("\"", "\\\""))
.map(|arg| format!("\"{arg}\""))
.collect::<Vec<String>>();
code = format!("set -- {}\n{}", args.join(" "), code);
}
command.arg("-c").arg(code).spawn()?.wait()
} else {
let error = std::io::Error::new(ErrorKind::NotFound, "Failed to find Bash");
Err(error)
}
}
pub fn generate_docs(&self, output: String, usage: bool) -> Result<(), Message> {
let tokens = self.tokenize()?;
let (block, mut meta) = self.parse(tokens)?;
meta.doc_usage = usage;
self.document(block, meta, output);
Ok(())
}
#[cfg(test)]
pub fn test_eval(&mut self) -> Result<String, Message> {
self.options.no_proc = vec!["*".into()];
self.compile().map_or_else(Err, |(_, code)| {
if let Some(mut command) = Self::find_bash() {
let child = command.arg("-c").arg::<&str>(code.as_ref()).output().unwrap();
let output = String::from_utf8_lossy(&child.stdout).to_string();
Ok(output)
} else {
let message = Message::new_err_msg("Failed to find Bash");
Err(message)
}
})
}
#[cfg(windows)]
fn find_bash() -> Option<Command> {
if let Some(paths) = env::var_os("PATH") {
for path in env::split_paths(&paths) {
let path = path.join("bash.exe");
if path.exists() {
let command = Command::new(path);
return Some(command);
}
}
}
return None;
}
/// Return bash command. In some situations, mainly for testing purposes, this can return a command, for example, containerized execution which is not bash but behaves like bash.
#[cfg(not(windows))]
fn find_bash() -> Option<Command> {
if env::var("AMBER_TEST_STRATEGY").is_ok_and(|value| value == "docker") {
let mut command = Command::new("docker");
let args_string = env::var("AMBER_TEST_ARGS").expect("Please pass docker arguments in AMBER_TEST_ARGS environment variable.");
let args: Vec<&str> = args_string.split_whitespace().collect();
command.args(args);
Some(command)
} else {
let mut command = Command::new("/usr/bin/env");
command.arg("bash");
Some(command)
}
}
}