2020-06-12 12:04:16 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, Position};
|
2020-03-16 05:41:19 +01:00
|
|
|
|
2021-04-02 06:34:39 +02:00
|
|
|
use std::{env, fs::File, io::Read, path::Path, process::exit};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2021-01-08 07:29:57 +01:00
|
|
|
fn eprint_error(input: &str, mut err: EvalAltResult) {
|
|
|
|
fn eprint_line(lines: &[&str], pos: Position, err_msg: &str) {
|
2020-06-12 12:04:16 +02:00
|
|
|
let line = pos.line().unwrap();
|
2022-08-11 13:01:23 +02:00
|
|
|
let line_no = format!("{line}: ");
|
2020-03-10 04:25:34 +01:00
|
|
|
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("{line_no}{}", lines[line - 1]);
|
2022-11-27 11:00:02 +01:00
|
|
|
|
|
|
|
for (i, err_line) in err_msg.to_string().split('\n').enumerate() {
|
|
|
|
// Display position marker
|
|
|
|
println!(
|
|
|
|
"{0:>1$}{err_line}",
|
2022-11-27 15:35:17 +01:00
|
|
|
if i > 0 { "| " } else { "^ " },
|
2022-11-27 11:00:02 +01:00
|
|
|
line_no.len() + pos.position().unwrap() + 1,
|
|
|
|
);
|
|
|
|
}
|
2022-07-27 10:04:24 +02:00
|
|
|
eprintln!();
|
2020-03-10 04:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 10:30:04 +01:00
|
|
|
let lines: Vec<_> = input.split('\n').collect();
|
2020-03-10 04:25:34 +01:00
|
|
|
|
|
|
|
// Print error
|
2021-02-28 07:38:34 +01:00
|
|
|
let pos = err.take_position();
|
2020-03-25 04:51:13 +01:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
if pos.is_none() {
|
|
|
|
// No position
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("{err}");
|
2020-06-12 12:04:16 +02:00
|
|
|
} else {
|
|
|
|
// Specific position
|
2020-10-20 12:09:26 +02:00
|
|
|
eprint_line(&lines, pos, &err.to_string())
|
2020-03-10 04:25:34 +01:00
|
|
|
}
|
2016-02-29 22:56:26 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
fn main() {
|
2020-12-29 15:04:31 +01:00
|
|
|
let mut contents = String::new();
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
for filename in env::args().skip(1) {
|
2021-04-02 06:34:39 +02:00
|
|
|
let filename = match Path::new(&filename).canonicalize() {
|
|
|
|
Err(err) => {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("Error script file path: {filename}\n{err}");
|
2021-04-02 06:34:39 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-07-14 12:32:22 +02:00
|
|
|
Ok(f) => match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap())
|
|
|
|
{
|
|
|
|
Ok(f) => f.into(),
|
|
|
|
_ => f,
|
|
|
|
},
|
2021-04-02 06:34:39 +02:00
|
|
|
};
|
|
|
|
|
2022-02-27 15:47:13 +01:00
|
|
|
// Initialize scripting engine
|
2023-01-30 06:03:47 +01:00
|
|
|
#[allow(unused_mut)]
|
2016-03-02 02:45:02 +01:00
|
|
|
let mut engine = Engine::new();
|
2016-03-02 03:36:46 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
2022-08-26 05:23:16 +02:00
|
|
|
engine.set_optimization_level(rhai::OptimizationLevel::Simple);
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
let mut f = match File::open(&filename) {
|
|
|
|
Err(err) => {
|
2021-04-02 06:34:39 +02:00
|
|
|
eprintln!(
|
|
|
|
"Error reading script file: {}\n{}",
|
|
|
|
filename.to_string_lossy(),
|
|
|
|
err
|
|
|
|
);
|
2020-03-10 04:25:34 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Ok(f) => f,
|
|
|
|
};
|
|
|
|
|
2020-12-29 15:04:31 +01:00
|
|
|
contents.clear();
|
2020-03-10 04:25:34 +01:00
|
|
|
|
2020-03-24 10:30:04 +01:00
|
|
|
if let Err(err) = f.read_to_string(&mut contents) {
|
2021-04-02 06:34:39 +02:00
|
|
|
eprintln!(
|
|
|
|
"Error reading script file: {}\n{}",
|
|
|
|
filename.to_string_lossy(),
|
|
|
|
err
|
|
|
|
);
|
2020-03-24 10:30:04 +01:00
|
|
|
exit(1);
|
2020-03-10 04:25:34 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 10:36:56 +02:00
|
|
|
let contents = if contents.starts_with("#!") {
|
|
|
|
// Skip shebang
|
|
|
|
&contents[contents.find('\n').unwrap_or(0)..]
|
|
|
|
} else {
|
|
|
|
&contents[..]
|
|
|
|
};
|
|
|
|
|
2021-04-02 06:34:39 +02:00
|
|
|
if let Err(err) = engine
|
|
|
|
.compile(contents)
|
2021-06-02 08:29:18 +02:00
|
|
|
.map_err(|err| err.into())
|
2021-04-02 06:34:39 +02:00
|
|
|
.and_then(|mut ast| {
|
2021-05-08 16:59:33 +02:00
|
|
|
ast.set_source(filename.to_string_lossy().to_string());
|
2021-08-06 08:46:27 +02:00
|
|
|
engine.run_ast(&ast)
|
2021-04-02 06:34:39 +02:00
|
|
|
})
|
|
|
|
{
|
|
|
|
let filename = filename.to_string_lossy();
|
|
|
|
|
2020-04-02 16:37:35 +02:00
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("{filename}");
|
2020-04-02 16:37:35 +02:00
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
2022-07-27 10:04:24 +02:00
|
|
|
eprintln!();
|
2020-03-10 04:25:34 +01:00
|
|
|
|
2021-03-28 10:36:56 +02:00
|
|
|
eprint_error(contents, *err);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|