Improve repl and rhai_runner examples with error messages.
This commit is contained in:
@@ -1,25 +1,66 @@
|
||||
use rhai::{Engine, RegisterFn, Scope};
|
||||
use std::io::{stdin, stdout, Write};
|
||||
use std::process::exit;
|
||||
use rhai::{Engine, EvalAltResult, Scope};
|
||||
use std::{
|
||||
io::{stdin, stdout, Write},
|
||||
iter,
|
||||
};
|
||||
|
||||
pub fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
fn print_error(input: &str, err: EvalAltResult) {
|
||||
fn padding(pad: &str, len: usize) -> String {
|
||||
iter::repeat(pad).take(len).collect::<String>()
|
||||
}
|
||||
|
||||
engine.register_fn("exit", || exit(0));
|
||||
let lines: Vec<_> = input.split("\n").collect();
|
||||
|
||||
loop {
|
||||
print!("> ");
|
||||
|
||||
let mut input = String::new();
|
||||
stdout().flush().expect("couldn't flush stdout");
|
||||
|
||||
if let Err(e) = stdin().read_line(&mut input) {
|
||||
println!("input error: {}", e);
|
||||
// Print error
|
||||
match err.position() {
|
||||
p if p.is_eof() => {
|
||||
// EOF
|
||||
let last = lines[lines.len() - 2];
|
||||
println!("{}", last);
|
||||
println!("{}^ {}", padding(" ", last.len() - 1), err);
|
||||
}
|
||||
p if p.is_none() => {
|
||||
// No position
|
||||
println!("{}", err);
|
||||
}
|
||||
p => {
|
||||
// Specific position
|
||||
let pos_text = format!(
|
||||
" (line {}, position {})",
|
||||
p.line().unwrap(),
|
||||
p.position().unwrap()
|
||||
);
|
||||
|
||||
if let Err(e) = engine.consume_with_scope(&mut scope, &input) {
|
||||
println!("error: {}", e);
|
||||
println!("{}", lines[p.line().unwrap() - 1]);
|
||||
println!(
|
||||
"{}^ {}",
|
||||
padding(" ", p.position().unwrap() - 1),
|
||||
err.to_string().replace(&pos_text, "")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
let mut input = String::new();
|
||||
|
||||
loop {
|
||||
print!("rhai> ");
|
||||
stdout().flush().expect("couldn't flush stdout");
|
||||
|
||||
input.clear();
|
||||
|
||||
if let Err(err) = stdin().read_line(&mut input) {
|
||||
println!("input error: {}", err);
|
||||
}
|
||||
|
||||
if let Err(err) = engine.consume_with_scope(&mut scope, true, &input) {
|
||||
println!("");
|
||||
print_error(&input, err);
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,27 +1,79 @@
|
||||
use rhai::{Engine, RegisterFn};
|
||||
use std::env;
|
||||
use std::fmt::Display;
|
||||
use rhai::{Engine, EvalAltResult};
|
||||
use std::{env, fs::File, io::Read, iter, process::exit};
|
||||
|
||||
fn showit<T: Display>(x: &mut T) -> () {
|
||||
println!("{}", x)
|
||||
fn padding(pad: &str, len: usize) -> String {
|
||||
iter::repeat(pad).take(len).collect::<String>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for fname in env::args().skip(1) {
|
||||
let mut engine = Engine::new();
|
||||
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);
|
||||
|
||||
engine.register_fn("print", showit as fn(x: &mut i32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool) -> ());
|
||||
engine.register_fn("print", showit as fn(x: &mut String) -> ());
|
||||
eprintln!("{}{}", line_no, lines[line - 1]);
|
||||
eprintln!(
|
||||
"{}^ {}",
|
||||
padding(" ", line_no.len() + pos - 1),
|
||||
err.replace(&pos_text, "")
|
||||
);
|
||||
eprintln!("");
|
||||
}
|
||||
|
||||
match engine.eval_file::<()>(&fname) {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
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(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for filename in env::args().skip(1) {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
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);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if let Err(err) = engine.consume(&contents) {
|
||||
eprintln!("{}", padding("=", filename.len()));
|
||||
eprintln!("{}", filename);
|
||||
eprintln!("{}", padding("=", filename.len()));
|
||||
eprintln!("");
|
||||
|
||||
eprint_error(&contents, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user