2020-03-10 04:25:34 +01:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
|
|
|
use std::{env, fs::File, io::Read, iter, process::exit};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-10 04:25:34 +01:00
|
|
|
fn padding(pad: &str, len: usize) -> String {
|
|
|
|
iter::repeat(pad).take(len).collect::<String>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eprint_error(input: &str, err: EvalAltResult) {
|
|
|
|
fn eprint_line(lines: &Vec<&str>, line: usize, pos: usize, err: &str) {
|
|
|
|
let line_no = format!("{}: ", line);
|
|
|
|
let pos_text = format!(" (line {}, position {})", line, pos);
|
|
|
|
|
|
|
|
eprintln!("{}{}", line_no, lines[line - 1]);
|
|
|
|
eprintln!(
|
|
|
|
"{}^ {}",
|
|
|
|
padding(" ", line_no.len() + pos - 1),
|
|
|
|
err.replace(&pos_text, "")
|
|
|
|
);
|
|
|
|
eprintln!("");
|
|
|
|
}
|
|
|
|
|
|
|
|
let lines: Vec<_> = input.split("\n").collect();
|
|
|
|
|
|
|
|
// Print error
|
|
|
|
match err.position() {
|
|
|
|
p if p.is_eof() => {
|
|
|
|
// EOF
|
|
|
|
let line = lines.len() - 1;
|
|
|
|
let pos = lines[line - 1].len();
|
|
|
|
eprint_line(&lines, line, pos, &err.to_string());
|
|
|
|
}
|
|
|
|
p if p.is_none() => {
|
|
|
|
// No position
|
|
|
|
eprintln!("{}", err);
|
|
|
|
}
|
|
|
|
p => {
|
|
|
|
// Specific position
|
|
|
|
eprint_line(
|
|
|
|
&lines,
|
|
|
|
p.line().unwrap(),
|
|
|
|
p.position().unwrap(),
|
|
|
|
&err.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
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-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();
|
|
|
|
|
|
|
|
match f.read_to_string(&mut contents) {
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Error reading script file: {}\n{}", filename, err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-03-12 07:54:14 +01:00
|
|
|
if let Err(err) = engine.consume(&contents, false) {
|
2020-03-10 04:25:34 +01:00
|
|
|
eprintln!("{}", padding("=", filename.len()));
|
|
|
|
eprintln!("{}", filename);
|
|
|
|
eprintln!("{}", padding("=", filename.len()));
|
|
|
|
eprintln!("");
|
|
|
|
|
|
|
|
eprint_error(&contents, err);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|