2020-06-12 12:04:16 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, Position};
|
2020-03-16 05:41:19 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
|
|
|
use rhai::OptimizationLevel;
|
2020-03-16 05:41:19 +01:00
|
|
|
|
2020-05-13 13:39:34 +02:00
|
|
|
use std::{env, fs::File, io::Read, process::exit};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
fn eprint_error(input: &str, err: EvalAltResult) {
|
2020-06-12 12:04:16 +02:00
|
|
|
fn eprint_line(lines: &[&str], pos: Position, err: &str) {
|
|
|
|
let line = pos.line().unwrap();
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
let line_no = format!("{}: ", line);
|
2020-06-12 12:04:16 +02:00
|
|
|
let pos_text = format!(" ({})", pos);
|
2020-03-10 04:25:34 +01:00
|
|
|
|
|
|
|
eprintln!("{}{}", line_no, lines[line - 1]);
|
|
|
|
eprintln!(
|
2020-04-02 16:37:35 +02:00
|
|
|
"{:>1$} {2}",
|
|
|
|
"^",
|
2020-06-12 12:04:16 +02:00
|
|
|
line_no.len() + pos.position().unwrap(),
|
2020-03-10 04:25:34 +01:00
|
|
|
err.replace(&pos_text, "")
|
|
|
|
);
|
|
|
|
eprintln!("");
|
|
|
|
}
|
|
|
|
|
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
|
2020-04-17 14:01:41 +02:00
|
|
|
let pos = err.position();
|
2020-03-25 04:51:13 +01:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
if pos.is_none() {
|
|
|
|
// No position
|
|
|
|
eprintln!("{}", err);
|
|
|
|
} else {
|
|
|
|
// Specific position
|
|
|
|
let err_text = match err {
|
|
|
|
EvalAltResult::ErrorRuntime(err, _) if !err.is_empty() => {
|
|
|
|
format!("Runtime error: {}", err)
|
|
|
|
}
|
|
|
|
err => err.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
eprint_line(&lines, pos, &err_text)
|
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-03-10 04:25:34 +01:00
|
|
|
for filename in env::args().skip(1) {
|
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"))]
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
let mut f = match File::open(&filename) {
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Error reading script file: {}\n{}", filename, err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Ok(f) => f,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
2020-03-24 10:30:04 +01:00
|
|
|
if let Err(err) = f.read_to_string(&mut contents) {
|
|
|
|
eprintln!("Error reading script file: {}\n{}", filename, err);
|
|
|
|
exit(1);
|
2020-03-10 04:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
if let Err(err) = engine.consume(&contents) {
|
2020-04-02 16:37:35 +02:00
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
2020-03-10 04:25:34 +01:00
|
|
|
eprintln!("{}", filename);
|
2020-04-02 16:37:35 +02:00
|
|
|
eprintln!("{:=<1$}", "", filename.len());
|
2020-03-10 04:25:34 +01:00
|
|
|
eprintln!("");
|
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
eprint_error(&contents, *err);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|